Repository: archiva-redback-core
Updated Branches:
  refs/heads/jpa 8d9f47436 -> 8fd711276


Changing transaction handling


Project: http://git-wip-us.apache.org/repos/asf/archiva-redback-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/archiva-redback-core/commit/8fd71127
Tree: http://git-wip-us.apache.org/repos/asf/archiva-redback-core/tree/8fd71127
Diff: http://git-wip-us.apache.org/repos/asf/archiva-redback-core/diff/8fd71127

Branch: refs/heads/jpa
Commit: 8fd7112762fabffa0e00a493d2afaceb079c3fec
Parents: 8d9f474
Author: Martin Stockhammer <marti...@apache.org>
Authored: Fri Oct 14 21:39:36 2016 +0200
Committer: Martin Stockhammer <marti...@apache.org>
Committed: Fri Oct 14 21:39:36 2016 +0200

----------------------------------------------------------------------
 .../src/main/resources/META-INF/persistence.xml |  2 +-
 .../redback-users-jpa/pom.xml                   |  5 +++
 .../redback/users/jpa/JpaUserManager.java       | 33 ++++++++++++--------
 .../redback/users/jpa/model/JpaUser.java        |  4 +--
 4 files changed, 28 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/archiva-redback-core/blob/8fd71127/redback-common/redback-common-jpa/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git 
a/redback-common/redback-common-jpa/src/main/resources/META-INF/persistence.xml 
b/redback-common/redback-common-jpa/src/main/resources/META-INF/persistence.xml
index 817eb20..5c1fa80 100644
--- 
a/redback-common/redback-common-jpa/src/main/resources/META-INF/persistence.xml
+++ 
b/redback-common/redback-common-jpa/src/main/resources/META-INF/persistence.xml
@@ -32,7 +32,7 @@
           <property name="openjpa.jdbc.SchemaFactory" 
value="native(ForeignKeys=true)" />
             <property name="openjpa.jdbc.MappingDefaults"
                       
value="ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict"/>
-            <property name="openjpa.Log" value="DefaultLevel=TRACE, 
Runtime=TRACE, Tool=INFO"/>
+            <property name="openjpa.Log" value="DefaultLevel=TRACE, 
Runtime=TRACE, Tool=INFO, SQL=TRACE"/>
         </properties>
     </persistence-unit>
 </persistence>

http://git-wip-us.apache.org/repos/asf/archiva-redback-core/blob/8fd71127/redback-users/redback-users-providers/redback-users-jpa/pom.xml
----------------------------------------------------------------------
diff --git a/redback-users/redback-users-providers/redback-users-jpa/pom.xml 
b/redback-users/redback-users-providers/redback-users-jpa/pom.xml
index 4d23718..29c24f4 100644
--- a/redback-users/redback-users-providers/redback-users-jpa/pom.xml
+++ b/redback-users/redback-users-providers/redback-users-jpa/pom.xml
@@ -53,6 +53,11 @@
       <artifactId>openjpa</artifactId>
       <version>${openjpa.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-tx</artifactId>
+      <version>${springVersion}</version>
+    </dependency>
 
     <dependency>
       <groupId>org.hsqldb</groupId>

http://git-wip-us.apache.org/repos/asf/archiva-redback-core/blob/8fd71127/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/JpaUserManager.java
----------------------------------------------------------------------
diff --git 
a/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/JpaUserManager.java
 
b/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/JpaUserManager.java
index 33fa7b6..4429ed8 100644
--- 
a/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/JpaUserManager.java
+++ 
b/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/JpaUserManager.java
@@ -23,12 +23,14 @@ import org.apache.archiva.redback.policy.UserSecurityPolicy;
 import org.apache.archiva.redback.users.*;
 import org.apache.archiva.redback.users.jpa.model.JpaUser;
 import org.apache.commons.lang.StringUtils;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.persistence.*;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import java.util.Date;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -102,6 +104,7 @@ public class JpaUserManager extends AbstractUserManager {
         return q.getResultList();
     }
 
+    @Transactional
     @Override
     public User addUser(User user) throws UserManagerException {
         EntityManager em = getEm();
@@ -133,12 +136,14 @@ public class JpaUserManager extends AbstractUserManager {
         {
             user.setPasswordChangeRequired( true );
         }
-        em.getTransaction().begin();
+        if (user.getLastPasswordChange()==null) {
+            user.setLastPasswordChange(new Date());
+        }
         em.persist((JpaUser)user);
-        em.getTransaction().commit();
         return user;
     }
 
+    @Transactional
     @Override
     public User updateUser(User user) throws UserNotFoundException, 
UserManagerException {
         return updateUser(user, false);
@@ -254,6 +259,7 @@ public class JpaUserManager extends AbstractUserManager {
 
 
 
+    @Transactional
     @Override
     public void deleteUser(String username) throws UserNotFoundException, 
UserManagerException {
         final EntityManager em = getEm();
@@ -261,12 +267,11 @@ public class JpaUserManager extends AbstractUserManager {
         if (u.isPermanent()) {
             throw new PermanentUserException("User "+username+" cannot be 
deleted");
         }
-        em.getTransaction().begin();
         em.remove(u);
-        em.getTransaction().commit();
         fireUserManagerUserRemoved(u);
     }
 
+    @Transactional
     @Override
     public void addUserUnchecked(User user) throws UserManagerException {
         log.info("addUserUnchecked "+user.getUsername());
@@ -282,21 +287,19 @@ public class JpaUserManager extends AbstractUserManager {
                     Messages.getString( 
"user.manager.cannot.add.user.without.username" ) ); //$NON-NLS-1$
         }
 
-        em.getTransaction().begin();
         TypedQuery<JpaUser> q = em.createQuery("SELECT u FROM JpaUser u", 
JpaUser.class);
         for (JpaUser u : q.getResultList()) {
             log.info("USER FOUND: "+u.getUsername());
         }
         log.info("NEW USER "+user.getUsername());
         em.persist((JpaUser)user);
-        em.getTransaction().commit();
 
     }
 
+    @Transactional
     @Override
     public void eraseDatabase() {
         EntityManager em = getEm();
-        em.getTransaction().begin();
         TypedQuery<JpaUser> q = em.createQuery("SELECT u FROM JpaUser u", 
JpaUser.class);
         for (JpaUser u : q.getResultList()) {
             u.getPreviousEncodedPasswords().clear();
@@ -304,23 +307,27 @@ public class JpaUserManager extends AbstractUserManager {
         em.flush();
         Query qd = em.createQuery("DELETE FROM JpaUser u");
         qd.executeUpdate();
-        em.getTransaction().commit();
         em.clear();
 
     }
 
+    @Transactional
     @Override
     public User updateUser(User user, boolean passwordChangeRequired) throws 
UserNotFoundException, UserManagerException {
+        if ( !( user instanceof JpaUser ) )
+        {
+            throw new UserManagerException( "Unable to update user. User 
object " + user.getClass().getName() +
+                    " is not an instance of " + JpaUser.class.getName() );
+        }
         if ( StringUtils.isNotEmpty( user.getPassword() ) )
         {
             userSecurityPolicy.extensionChangePassword( user, 
passwordChangeRequired );
         }
+        JpaUser jpaUser = (JpaUser) user;
         final EntityManager em = getEm();
-        em.getTransaction().begin();
-        em.persist((JpaUser)user);
-        em.getTransaction().commit();
-        fireUserManagerUserUpdated(user);
-        return user;
+        jpaUser = em.merge(jpaUser);
+        fireUserManagerUserUpdated(jpaUser);
+        return jpaUser;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/archiva-redback-core/blob/8fd71127/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/model/JpaUser.java
----------------------------------------------------------------------
diff --git 
a/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/model/JpaUser.java
 
b/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/model/JpaUser.java
index cfdaa4e..9b00c8e 100644
--- 
a/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/model/JpaUser.java
+++ 
b/redback-users/redback-users-providers/redback-users-jpa/src/main/java/org/apache/archiva/redback/users/jpa/model/JpaUser.java
@@ -131,7 +131,7 @@ public class JpaUser implements 
org.apache.archiva.redback.users.User {
 
     @Override
     public void setLastPasswordChange(Date passwordChangeDate) {
-        this.lastPasswordChange = lastPasswordChange;
+        this.lastPasswordChange = passwordChangeDate;
     }
 
     @Override
@@ -222,6 +222,6 @@ public class JpaUser implements 
org.apache.archiva.redback.users.User {
 
     @Override
     public String getUserManagerId() {
-        return null;
+        return "jpa";
     }
 }

Reply via email to