]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
Implemented updateUserData() and let the detached user instance return
authorRoland Häder <roland@mxchange.org>
Thu, 21 Apr 2016 10:52:44 +0000 (12:52 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 21 Apr 2016 18:04:26 +0000 (20:04 +0200)
src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java

index 0d49b4c31bb4725550a490c620182070faa75beb..6c08e4516fd85860c1ed2e97e9b33e5041aa5fc1 100644 (file)
@@ -71,6 +71,9 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio
                if (null == user) {
                        // Abort here
                        throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() != null) {
+                       // Not allowed here
+                       throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
                }
 
                // Check if user is registered
@@ -460,7 +463,55 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio
        }
 
        @Override
-       public void updateUserPersonalData (final User user) {
+       public User updateUserData (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
+
+               // user should not be null
+               if (null == user) {
+                       // Abort here
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
+               } else if (user.getUserAccountStatus() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
+               } else if (!this.ifUserExists(user)) {
+                       // User does not exist
+                       throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
+               }
+
+               // Remove contact instance as this is not updatedf
+               user.setUserContact(null);
+
+               // Find the instance
+               User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
+
+               // Should be found!
+               assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
+
+               // Merge user
+               User detachedUser = this.getEntityManager().merge(foundUser);
+
+               // Should be found!
+               assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
+
+               // Copy all data
+               detachedUser.copyAll(user);
+
+               // Set as updated
+               detachedUser.setUserUpdated(new GregorianCalendar());
+
+               // Return updated instance
+               return detachedUser;
+       }
+
+       @Override
+       public User updateUserPersonalData (final User user) {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
 
@@ -602,6 +653,9 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio
                        // Set it back
                        detachedContact.setContactLandLineNumber(detachedLandLine);
                }
+
+               // Return updated user instance
+               return detachedUser;
        }
 
 }