]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
Continued a bit:
authorRoland Häder <roland@mxchange.org>
Tue, 26 Apr 2016 14:58:39 +0000 (16:58 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 27 Apr 2016 19:07:37 +0000 (21:07 +0200)
- implemented linkUser() method, please test this
- some cleanups from old code

src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java

index 7cfa90f98bfcca00491b11710fdde58b624e2f3d..299e9e5eb5b4578f68b59714ff824a94a009e896 100644 (file)
@@ -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