]> git.mxchange.org Git - addressbook-mailer-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:26:30 +0000 (20:26 +0200)
src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java

index b46e6d3582299ab68364f002ffc77dd086dda610..a4b903b28f0b8ac5f7acc9b9f668ddc150404351 100644 (file)
@@ -71,6 +71,9 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                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 AddressbookUserSessionBean extends BaseDatabaseBean implements User
        }
 
        @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 AddressbookUserSessionBean extends BaseDatabaseBean implements User
                        // Set it back
                        detachedContact.setContactLandLineNumber(detachedLandLine);
                }
+
+               // Return updated user instance
+               return detachedUser;
        }
 
 }