]> git.mxchange.org Git - pizzaservice-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
Please cherry-pick:
[pizzaservice-ejb.git] / src / java / org / mxchange / jusercore / model / user / PizzaUserSessionBean.java
index 728f10bee21c141798ee930260fb3144ebbf847f..7c452d6185e225e171e6405743b99302c51ce34b 100644 (file)
@@ -19,11 +19,10 @@ package org.mxchange.jusercore.model.user;
 import java.text.MessageFormat;
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 import javax.ejb.EJB;
 import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.persistence.NoResultException;
-import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 import org.mxchange.jcontacts.model.contact.Contact;
 import org.mxchange.jcontacts.model.contact.Contacts;
@@ -70,45 +69,6 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                super("jms/pizzaservice-queue-factory", "jms/pizzaservice-email-queue"); //NOI18N
        }
 
-       @Override
-       @SuppressWarnings ("unchecked")
-       @Deprecated
-       public List<String> allUserNames () {
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
-
-               // Get query
-               final Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
-
-               // Get result list
-               final List<String> userNameList = query.getResultList();
-
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
-
-               // Return it
-               return userNameList;
-       }
-
-       @Override
-       @SuppressWarnings ("unchecked")
-       public List<User> allUsers () {
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
-
-               // Get named query
-               final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
-
-               // Get result
-               final List<User> users = query.getResultList();
-
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
-
-               // Return full list
-               return users;
-       }
-
        @Override
        public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
                // Trace message
@@ -160,79 +120,25 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
        }
 
        @Override
-       @Deprecated
-       public User fillUserData (final User user) throws UserNotFoundException {
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
-               // user should not be null
-               if (null == user) {
-                       // Abort here
-                       throw new NullPointerException("user is null"); //NOI18N
-               } else if (user.getUserName() == null) {
-                       // Throw NPE
-                       throw new NullPointerException("user.userName is null");
-               } else if (user.getUserName().isEmpty()) {
-                       // Throw IAE
-                       throw new IllegalArgumentException("user.userName is empty");
-               } else if (!this.ifUserExists(user)) {
-                       // User does not exist
-                       throw new UserNotFoundException(user);
-               }
-
-               // Try to locate it
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
-
-               // Set parameter
-               query.setParameter("userName", user.getUserName()); //NOI18N
-
-               // Initialize variable
-               final User foundUser;
-
-               // Try it
-               try {
-                       // Try to get single result
-                       foundUser = (User) query.getSingleResult();
-               } catch (final NoResultException ex) {
-                       // Log it
-                       this.getLoggerBeanLocal().logException(ex);
-                       return null;
-               }
-
-               // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
-
-               // Return prepared instance
-               return foundUser;
-       }
-
-       @Override
-       @Deprecated
-       public String generateRandomUserName () {
+       @SuppressWarnings ("unchecked")
+       public List<User> fetchAllUsers () {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
-
-               // Get full list
-               final List<String> userList = this.allUserNames();
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
 
-               // Init variable
-               String userName = null;
+               // Get named query
+               final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
 
-               // Loop until a user name is found
-               while ((userName == null) || (userList.contains(userName))) {
-                       // Generate random name
-                       userName = Users.generateRandomUserName();
-               }
+               // Get result
+               final List<User> users = query.getResultList();
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
 
-               // Found one, so return it
-               return userName;
+               // Return full list
+               return users;
        }
 
        @Override
-       @Deprecated
        public boolean ifUserExists (final User user) {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
@@ -249,38 +155,24 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                        throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
                }
 
-               // Generate query
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
-
-               // Set parameter
-               query.setParameter("id", user.getUserId()); //NOI18N
-
-               // Try this
-               try {
-                       // Try to get single result
-                       final User dummy = (User) query.getSingleResult();
-
-                       // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
-               } catch (final NoResultException ex) {
-                       // Log it
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
-
-                       // User name does not exist
-                       return false;
-               } catch (final PersistenceException ex) {
-                       // Something bad happened
-                       this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
-
-                       // Throw again
-                       throw ex;
+               // Default is not found
+               boolean isFound = false;
+
+               // Fetch whole list
+               for (final User currentUser : this.fetchAllUsers()) {
+                       // Is found?
+                       if (Objects.equals(user, currentUser)) {
+                               // Yes, then set flag and exit iteration
+                               isFound = true;
+                               break;
+                       }
                }
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
 
-               // Found it
-               return true;
+               // Return flag
+               return isFound;
        }
 
        @Override
@@ -297,29 +189,21 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                        throw new NullPointerException("userName is empty"); //NOI18N
                }
 
-               // Generate query
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
-
-               // Set parameter
-               query.setParameter("userName", userName); //NOI18N
-
-               // Try this
-               try {
-                       // Try to get single result
-                       final User dummy = (User) query.getSingleResult();
-
-                       // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
-               } catch (final NoResultException ex) {
-                       // Log it
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
-
-                       // User name does not exist
-                       return false;
+               // Default is not registered
+               boolean isRegistered = false;
+
+               // Iterate over all records
+               for (final User currentUser : this.fetchAllUsers()) {
+                       // Does the username match?
+                       if (userName.equals(currentUser.getUserName())) {
+                               // Yes, then set flag and exit iteration
+                               isRegistered = true;
+                               break;
+                       }
                }
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
 
                // Found it
                return true;
@@ -336,38 +220,24 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                        throw new NullPointerException("user is null"); //NOI18N
                }
 
-               // Generate query
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
-
-               // Set parameter
-               query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
-
-               // Search for it
-               try {
-                       // Try to get single result
-                       final User dummy = (User) query.getSingleResult();
-
-                       // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
-               } catch (final NoResultException ex) {
-                       // Log it
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
-
-                       // Email address does not exist
-                       return false;
-               } catch (final PersistenceException ex) {
-                       // Something bad happened
-                       this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
-
-                       // Throw again
-                       throw ex;
+               // Default is not registered
+               boolean isRegistered = false;
+
+               // Iterate over all records
+               for (final User currentUser : this.fetchAllUsers()) {
+                       // Does the email address match?
+                       if (user.getUserContact().getContactEmailAddress().equals(currentUser.getUserContact().getContactEmailAddress())) {
+                               // Yes, then set flag and exit iteration
+                               isRegistered = true;
+                               break;
+                       }
                }
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
 
                // Found it
-               return true;
+               return isRegistered;
        }
 
        @Override
@@ -381,38 +251,14 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                        throw new NullPointerException("user is null"); //NOI18N
                }
 
-               // Generate query
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
-
-               // Set parameter
-               query.setParameter("userName", user.getUserName()); //NOI18N
-
-               // Try this
-               try {
-                       // Try to get single result
-                       final User dummy = (User) query.getSingleResult();
-
-                       // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
-               } catch (final NoResultException ex) {
-                       // Log it
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
-
-                       // User name does not exist
-                       return false;
-               } catch (final PersistenceException ex) {
-                       // Something bad happened
-                       this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
-
-                       // Throw again
-                       throw ex;
-               }
+               // Ask other method
+               final boolean isRegistered = this.ifUserNameExists(user.getUserName());
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
 
-               // Found it
-               return true;
+               // Return flag
+               return isRegistered;
        }
 
        @Override
@@ -602,19 +448,22 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                // Is there a  mobile instance set?
                if (mobileNumber instanceof DialableMobileNumber) {
                        // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
 
                        // Then find it, too
-                       final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId());
+                       final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
 
                        // Should be there
-                       assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
+                       assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
+
+                       // Copy all
+                       MobileNumbers.copyMobileNumber(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
 
                        // Then merge it, too
                        final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
 
                        // Should be there
-                       assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getPhoneId()); //NOI18N
+                       assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
 
                        // Copy all
                        MobileNumbers.copyAll(managedUser.getUserContact().getContactMobileNumber(), managedMobile);
@@ -637,6 +486,9 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                        // Should be there
                        assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
 
+                       // Copy all
+                       FaxNumbers.copyFaxNumber(managedUser.getUserContact().getContactFaxNumber(), foundFax);
+
                        // Then merge it, too
                        final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
 
@@ -664,6 +516,9 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use
                        // Should be there
                        assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
 
+                       // Copy all
+                       LandLineNumbers.copyLandLineNumber(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
+
                        // Then merge it, too
                        final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);