[myblog] r447 committed - adding jpa services.

0 views
Skip to first unread message

myb...@googlecode.com

unread,
Jun 16, 2011, 8:55:15 AM6/16/11
to mybl...@googlegroups.com
Revision: 447
Author: Ror...@gmail.com
Date: Thu Jun 16 05:52:43 2011
Log: adding jpa services.
http://code.google.com/p/myblog/source/detail?r=447

Added:
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/exception

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/exception/DuplicateException.java

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/repository/UserDao.java

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/repository/UserDaoImpl.java

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserService.java
Deleted:
/branches/myblog-springmvc/src/main/java/com/myblog
Modified:
/branches/myblog-springmvc/pom.xml
/branches/myblog-springmvc/src/main/resources/spring/global-context.xml

=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/exception/DuplicateException.java
Thu Jun 16 05:52:43 2011
@@ -0,0 +1,79 @@
+/**
+ * Copyright (c) 2005-2011, Rory Ye
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * * Redistributions of source code must retain the above copyright
notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
notice,
+ * this list of conditions and the following disclaimer in the
documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the Jdkcn.com nor the names of its
contributors may
+ * be used to endorse or promote products derived from this software
without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+/**
+ *
+ */
+package com.jdkcn.myblog.exception;
+
+import org.apache.commons.lang.exception.NestableRuntimeException;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+public class DuplicateException extends NestableRuntimeException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 7923812903904409142L;
+
+ public DuplicateException(String type, String name, Object value) {
+ super("The [" + type + "] has duplicate with [" + name + ":" + value
+ "]");
+ }
+
+ /**
+ *
+ */
+ public DuplicateException() {
+ super();
+ }
+
+ /**
+ * @param msg
+ * @param cause
+ */
+ public DuplicateException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+
+ /**
+ * @param msg
+ */
+ public DuplicateException(String msg) {
+ super(msg);
+ }
+
+ /**
+ * @param cause
+ */
+ public DuplicateException(Throwable cause) {
+ super(cause);
+ }
+
+}
=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/repository/UserDao.java
Thu Jun 16 05:52:43 2011
@@ -0,0 +1,42 @@
+package com.jdkcn.myblog.repository;
+
+import com.jdkcn.myblog.domain.User;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+public interface UserDao {
+
+ /**
+ * Save or update the user.
+ *
+ * @param user
+ * user to save or update
+ * @return The saved user
+ */
+ User saveOrUpdate(User user);
+
+ /**
+ * Get user by id
+ *
+ * @param id
+ * user's id
+ * @return user with this id or null if not exist
+ */
+ User get(String id);
+
+ /**
+ * Get user by username
+ * @param username the user's username
+ * @return user with this username or null if not exist
+ */
+ User getByUsername(String username);
+
+ /**
+ * Get user by email
+ * @param email the user's email
+ * @return user with this email or null if not exist
+ */
+ User getByEmail(String email);
+}
=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/repository/UserDaoImpl.java
Thu Jun 16 05:52:43 2011
@@ -0,0 +1,75 @@
+package com.jdkcn.myblog.repository;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+
+import org.apache.commons.lang.StringUtils;
+import org.springframework.stereotype.Repository;
+
+import com.jdkcn.myblog.domain.User;
+import com.jdkcn.myblog.exception.DuplicateException;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+@Repository
+public class UserDaoImpl implements UserDao {
+
+ @PersistenceContext
+ private EntityManager em;
+
+ /**
+ * {@inheritDoc}
+ */
+ public User saveOrUpdate(User user) {
+ User exist = getByUsername(user.getUsername());
+ if (exist != null && !StringUtils.equals(user.getId(), exist.getId())) {
+ throw new DuplicateException(User.class.getName(), "username",
user.getUsername());
+ }
+ exist = getByEmail(user.getEmail());
+ if (exist != null && !StringUtils.equals(user.getId(), exist.getId())) {
+ throw new DuplicateException(User.class.getName(), "email",
user.getEmail());
+ }
+ return em.merge(user);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public User get(String id) {
+ return em.find(User.class, id);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public User getByUsername(String username) {
+ Query query = em.createQuery("from User u where u.username = :username");
+ query.setParameter("username", username);
+ @SuppressWarnings("unchecked")
+ List<User> users = query.getResultList();
+ if (users.isEmpty()) {
+ return null;
+ }
+ return users.get(0);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public User getByEmail(String email) {
+ Query query = em.createQuery("from User u where u.email = :email");
+ query.setParameter("email", email);
+ @SuppressWarnings("unchecked")
+ List<User> users = query.getResultList();
+ if (users.isEmpty()) {
+ return null;
+ }
+ return users.get(0);
+ }
+
+}
=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserService.java
Thu Jun 16 05:52:43 2011
@@ -0,0 +1,9 @@
+package com.jdkcn.myblog.service;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+public interface UserService {
+
+}
=======================================
--- /branches/myblog-springmvc/pom.xml Tue Jun 14 10:33:56 2011
+++ /branches/myblog-springmvc/pom.xml Thu Jun 16 05:52:43 2011
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jdkcn.myblog</groupId>
<artifactId>myblog</artifactId>
@@ -80,13 +81,13 @@
</configuration>
</plugin>
<plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-eclipse-plugin</artifactId>
- <version>2.8</version>
- <configuration>
- <downloadSources>true</downloadSources>
- </configuration>
- </plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-eclipse-plugin</artifactId>
+ <version>2.8</version>
+ <configuration>
+ <downloadSources>true</downloadSources>
+ </configuration>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@@ -98,7 +99,8 @@
</configuration>
</plugin>
<plugin>
- <!-- Download UMLGraph from the website and set PATH to the bin and
CLASSPATH to the Lib -->
+ <!-- Download UMLGraph from the website and set PATH to the bin and
CLASSPATH
+ to the Lib -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
@@ -281,5 +283,13 @@
<artifactId>doclet</artifactId>
<version>5.1</version>
</dependency>
+
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.6</version>
+ <scope>compile</scope>
+ </dependency>
+
</dependencies>
</project>
=======================================
--- /branches/myblog-springmvc/src/main/resources/spring/global-context.xml
Mon Jun 13 11:36:49 2011
+++ /branches/myblog-springmvc/src/main/resources/spring/global-context.xml
Thu Jun 16 05:52:43 2011
@@ -5,8 +5,10 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
+ http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
+ http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
@@ -18,6 +20,8 @@
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
+
+ <context:annotation-config/>

<tx:annotation-driven transaction-manager="transactionManager" />

Reply all
Reply to author
Forward
0 new messages