From: Roland Häder Date: Tue, 26 Apr 2016 14:58:39 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6968c952aa2ba2ee8219c5e2c7233978052fe6c3;p=pizzaservice-ejb.git Continued a bit: - implemented linkUser() method, please test this - some cleanups from old code --- diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java index 7cfa90f..299e9e5 100644 --- a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java @@ -26,9 +26,6 @@ import javax.persistence.NoResultException; import javax.persistence.PersistenceException; import javax.persistence.Query; import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; import org.mxchange.jusercore.exceptions.UserNotFoundException; @@ -67,13 +64,16 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N - // user should not be null + // userId should not be null 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 + } else if (user.getUserId() == null) { + // Abort here + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N } // Check if user is registered @@ -89,28 +89,8 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS user.setUserCreated(new GregorianCalendar()); user.getUserContact().setContactCreated(new GregorianCalendar()); - // Get all phone instances - DialableLandLineNumber landLineNumber = user.getUserContact().getContactLandLineNumber(); - DialableFaxNumber faxNumber = user.getUserContact().getContactFaxNumber(); - DialableCellphoneNumber cellphoneNumber = user.getUserContact().getContactCellphoneNumber(); - - // Is a phone number instance set? - if (landLineNumber instanceof DialableLandLineNumber) { - // Set created timestamp - landLineNumber.setPhoneEntryCreated(new GregorianCalendar()); - } - - // Is a fax number instance set? - if (faxNumber instanceof DialableFaxNumber) { - // Set created timestamp - faxNumber.setPhoneEntryCreated(new GregorianCalendar()); - } - - // Is a mobile number instance set? - if (cellphoneNumber instanceof DialableCellphoneNumber) { - // Set created timestamp - cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar()); - } + // Set all created timestamps + this.setAllContactPhoneEntriesCreated(user.getUserContact()); // Persist it this.getEntityManager().persist(user); @@ -503,6 +483,54 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS return true; } + @Override + public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + // userId should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } + + // Check if user is registered + if (this.registerBean.isUserNameRegistered(user)) { + // Abort here + throw new UserNameAlreadyRegisteredException(user); + } else if (this.registerBean.isEmailAddressRegistered(user)) { + // Abort here + throw new EmailAddressAlreadyRegisteredException(user); + } + + // Set created timestamp + user.setUserCreated(new GregorianCalendar()); + + // Try to find the contact instance + Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); + + // Try to merge it + Contact detachedContact = this.getEntityManager().merge(foundContact); + + // Set it in user + user.setUserContact(detachedContact); + + // Persist user + this.getEntityManager().persist(user); + + // Flush it to get id + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N + + // Return updated instance + return user; + } + @Override public User updateUserData (final User user) { // Trace message