From: Roland Häder Date: Tue, 19 May 2020 23:28:45 +0000 (+0200) Subject: Please cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=1cd63354fe08e99108ffc3e5967248963ebc59ee;p=jjobs-ejb.git Please cherry-pick: - 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 --- diff --git a/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java b/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java index 35cb77c..74ea8a0 100644 --- a/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java +++ b/src/java/org/mxchange/jjobs/enterprise/BaseJobsEnterpriseBean.java @@ -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 + *

+ * @param detachedDepartment Department instance to merge + *

+ * @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. + *

+ * @param faxNumber Fax instance + * @param fetchedNumber Found fax number in database + *

+ * @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. + *

+ * @param landLineNumber Land-line instance + * @param fetchedNumber Found land-line number in database + *

+ * @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. + *

+ * @param mobileNumber Mobile instance + * @param fetchedNumber Found mobile number in database + *

+ * @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 diff --git a/src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java index 430a191..9934467 100644 --- a/src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java @@ -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); diff --git a/src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java index 9ff0e68..3780f4d 100644 --- a/src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java @@ -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()); diff --git a/src/java/org/mxchange/juserlogincore/model/user/register/JobsUserRegistrationSessionBean.java b/src/java/org/mxchange/juserlogincore/model/user/register/JobsUserRegistrationSessionBean.java index 743bab6..3795b66 100644 --- a/src/java/org/mxchange/juserlogincore/model/user/register/JobsUserRegistrationSessionBean.java +++ b/src/java/org/mxchange/juserlogincore/model/user/register/JobsUserRegistrationSessionBean.java @@ -17,17 +17,13 @@ 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 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 }