]> git.mxchange.org Git - jjobs-ejb.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Tue, 19 May 2020 23:28:45 +0000 (01:28 +0200)
committerRoland Häder <roland@mxchange.org>
Wed, 10 Jun 2020 17:26:13 +0000 (19:26 +0200)
- let's use createManaged() where possible to avoid some duplicate code. It may
  look a bit to much encapsulation or to fine-granulated, the createManaged()
  method contains some validation on the entity instance and that is a good
  reason to have it encapsulated away.

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java
src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java
src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java
src/java/org/mxchange/juserlogincore/model/user/register/JobsUserRegistrationSessionBean.java

index 35cb77c9dd6ecf3845190e4be3c6a4d205769ea5..74ea8a0947c80f5dd565c29a0335ff1638612d4e 100644 (file)
@@ -857,10 +857,7 @@ public abstract class BaseJobsEnterpriseBean extends BaseEnterpriseBean {
                detachedContact.setContactEntryUpdated(new Date());
 
                // Get contact from it and find it
-               final Contact foundContact = this.getEntityManager().find(detachedContact.getClass(), detachedContact.getContactId());
-
-               // Should be found
-               assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
+               final Contact foundContact = this.createManaged(detachedContact);
 
                // Debug message
                this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeContactData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
@@ -980,6 +977,197 @@ public abstract class BaseJobsEnterpriseBean extends BaseEnterpriseBean {
                this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
        }
 
+       /**
+        * Merges given department's data
+        * <p>
+        * @param detachedDepartment Department instance to merge
+        * <p>
+        * @return Detached contact instance
+        */
+       protected Department mergeDepartmentData (final Department detachedDepartment) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeDepartmentData: detachedDepartment={0} - CALLED!", detachedDepartment)); //NOI18N
+
+               // The contact instance must be valid
+               if (null == detachedDepartment) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedDepartment is null"); //NOI18N
+               } else if (detachedDepartment.getDepartmentId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedDepartment.departmentId is null"); //NOI18N
+               } else if (detachedDepartment.getDepartmentId() < 1) {
+                       // Not valid
+                       throw new IllegalStateException(MessageFormat.format("detachedDepartment.departmentId ={0} is not valid.", detachedDepartment.getDepartmentId())); //NOI18N
+               }
+
+               // Set updated timestamp
+               detachedDepartment.setDepartmentEntryUpdated(new Date());
+
+               // Get contact from it and find it
+               final Department foundDepartment = this.createManaged(detachedDepartment);
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeDepartmentData: foundContact.contactId={0}", foundDepartment.getDepartmentId())); //NOI18N
+
+               // Copy all
+               Departments.copyDepartmentData(detachedDepartment, foundDepartment);
+
+               // Merge contact instance
+               final Department managedDepartment = this.getEntityManager().merge(foundDepartment);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeDepartmentData: managedDepartment={0} - EXIT!", managedDepartment)); //NOI18N
+
+               // Return detached contact
+               return managedDepartment;
+       }
+
+       /**
+        * Returns a detached instance from given fax instance merged into current.
+        * <p>
+        * @param faxNumber     Fax instance
+        * @param fetchedNumber Found fax number in database
+        * <p>
+        * @return Detached instance
+        */
+       protected DialableFaxNumber mergeFaxNumberData (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
+
+               // Should be valid
+               if (null == faxNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("faxNumber is null"); //NOI18N
+               } else if (fetchedNumber.getPhoneId() == null) {
+                       // ..and again
+                       throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+               // Init query instance
+               final DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+               // Default is null
+               DialableFaxNumber detachedNumber = null;
+
+               // Is there a difference?
+               if (!FaxNumbers.isSameFaxNumber(faxNumber, fetchedNumber)) {
+                       // @TODO Copy all to foundNumber, then merge
+
+                       // Merge this entry
+                       detachedNumber = this.getEntityManager().merge(foundNumber);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+               // Return it
+               return detachedNumber;
+       }
+
+       /**
+        * Returns a detached instance from given land-line instance merged with
+        * current.
+        * <p>
+        * @param landLineNumber Land-line instance
+        * @param fetchedNumber  Found land-line number in database
+        * <p>
+        * @return Detached instance
+        */
+       protected DialableLandLineNumber mergeLandLineNumberData (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
+
+               // Should be valid
+               if (null == landLineNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("landLineNumber is null"); //NOI18N
+               } else if (fetchedNumber.getPhoneId() == null) {
+                       // ..and again
+                       throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+               // Init query instance
+               final DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+               // Default is null
+               DialableLandLineNumber detachedNumber = null;
+
+               // Is there a difference?
+               if (!LandLineNumbers.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
+                       // @TODO Copy all to foundNumber, then merge
+
+                       // Merge this entry
+                       detachedNumber = this.getEntityManager().merge(foundNumber);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+               // Return it
+               return detachedNumber;
+       }
+
+       /**
+        * Returns a detached instance from given mobile instance merged with
+        * current.
+        * <p>
+        * @param mobileNumber  Mobile instance
+        * @param fetchedNumber Found mobile number in database
+        * <p>
+        * @return Detached instance
+        */
+       protected DialableMobileNumber mergeMobileNumberData (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: mobileNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
+
+               // Should be valid
+               if (null == mobileNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("mobileNumber is null"); //NOI18N
+               } else if (fetchedNumber.getMobileId() == null) {
+                       // ..and again
+                       throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getMobileId())); //NOI18N
+
+               // Init query instance
+               final DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getMobileId());
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+               // Default is null
+               DialableMobileNumber detachedNumber = null;
+
+               // Is there a difference?
+               if (!MobileNumbers.isSameMobileNumber(mobileNumber, fetchedNumber)) {
+                       // @TODO Copy all to foundNumber, then merge
+
+                       // Merge this entry
+                       detachedNumber = this.getEntityManager().merge(foundNumber);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+               // Return it
+               return detachedNumber;
+       }
+
        /**
         * Sends an email with given subject line, template name to given recipient
         * and user data
index 430a1910a3a298a65e117202a10edda4f1726e0a..99344677dc94e08875e1af8061a6d9687d521243 100644 (file)
@@ -81,7 +81,7 @@ public class JobsAdminUserSessionBean extends BaseJobsEnterpriseBean implements
                }
 
                // Set created timestamp
-               user.setUserCreated(new Date());
+               user.setUserEntryCreated(new Date());
                user.getUserContact().setContactEntryCreated(new Date());
 
                // Update mobile, land-line and fax instance
@@ -178,7 +178,7 @@ public class JobsAdminUserSessionBean extends BaseJobsEnterpriseBean implements
                user.setUserContact(foundContact);
 
                // Set timestamp
-               user.setUserCreated(new Date());
+               user.setUserEntryCreated(new Date());
 
                // Perist it
                this.getEntityManager().persist(user);
index 9ff0e68003899b0ed8f9c045678b07e74417f99b..3780f4dd045edf8501007457bed164bfd0738222 100644 (file)
@@ -69,7 +69,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
        }
 
        @Override
-       public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
+       public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException, UserNotFoundException {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
 
@@ -106,7 +106,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
                // Update user status and remove confirmation key
                managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
                managedUser.setUserConfirmKey(null);
-               managedUser.setUserUpdated(new Date());
+               managedUser.setUserEntryUpdated(new Date());
 
                // Send out email
                this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
@@ -261,7 +261,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
        }
 
        @Override
-       public User updateUserData (final User user) {
+       public User updateUserData (final User detachedUser) throws UserNotFoundException {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
 
@@ -280,7 +280,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
                        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
+                       throw new UserNotFoundException(detachedUser.getUserId());
                }
 
                // Find the instance
@@ -299,7 +299,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
                assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
 
                // Set as updated
-               managedUser.setUserUpdated(new Date());
+               managedUser.setUserEntryUpdated(new Date());
 
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
@@ -359,7 +359,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
                final User managedUser = this.updateUserData(user);
 
                // Update user account
-               managedUser.setUserUpdated(new Date());
+               managedUser.setUserEntryUpdated(new Date());
 
                // Create history entry
                PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
@@ -417,7 +417,7 @@ public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserS
                Users.copyUserData(user, managedUser);
 
                // Set as updated
-               managedUser.setUserUpdated(new Date());
+               managedUser.setUserEntryUpdated(new Date());
 
                // Update user data
                final Contact managedContact = this.mergeContactData(managedUser.getUserContact());
index 743bab681663c5b644b6f7f838fa7f41de2c98b8..3795b66945aedc524ed19e0af68b09632a681f50 100644 (file)
 package org.mxchange.juserlogincore.model.user.register;
 
 import java.text.MessageFormat;
+import java.util.List;
 import java.util.Objects;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
-import javax.persistence.NoResultException;
-import javax.persistence.Query;
-import org.mxchange.jcontacts.model.contact.Contact;
-import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
-import org.mxchange.jusercore.model.user.LoginUser;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.juserlogincore.login.UserLoginUtils;
@@ -76,8 +72,8 @@ public class JobsUserRegistrationSessionBean extends BaseJobsEnterpriseBean impl
                        throw new NullPointerException("user is null"); //NOI18N
                }
 
-               // Create named instance
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
+               // Fetch whole list
+               final List<User> users = this.userBean.fetchAllUsers();
 
                // Init confirmation key
                String confirmationKey = null;
@@ -85,22 +81,16 @@ public class JobsUserRegistrationSessionBean extends BaseJobsEnterpriseBean impl
                // Find a free one
                while (confirmationKey == null) {
                        // Create new one
-                       final String key = UserLoginUtils.generatedConfirmationKey(user);
-
-                       // Set key as parameter
-                       query.setParameter("confirmKey", key); //NOI18N
-
-                       // Try it
-                       try {
-                               // Get contact instance
-                               final Contact contact = (Contact) query.getSingleResult();
-
-                               // Warning message
-                               this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
-                       } catch (final NoResultException ex) {
-                               // Not found, normal case
-                               confirmationKey = key;
-                               break;
+                       confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
+
+                       // Check all entries
+                       for (final User currentUser : users) {
+                               // Does the key match?
+                               if (Objects.equals(currentUser.getUserConfirmKey(), confirmationKey)) {
+                                       // Set key to null and exit loop
+                                       confirmationKey = null;
+                                       break;
+                               }
                        }
                }
 
@@ -148,7 +138,7 @@ public class JobsUserRegistrationSessionBean extends BaseJobsEnterpriseBean impl
 
                // Is password set?
                if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
-                       // Switch to other template
+                       // Switch to template with random password exposed
                        templateName = "user_registration_random"; //NOI18N
                }