From: Roland Häder Date: Thu, 21 Apr 2016 10:52:44 +0000 (+0200) Subject: Implemented updateUserData() and let the detached user instance return X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0714497d6ebc0002fc02743644b735b8b2864d90;p=pizzaservice-mailer-ejb.git Implemented updateUserData() and let the detached user instance return --- diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java index b46e6d3..a4b903b 100644 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java @@ -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; } }