From 75ddd2fbba47b501359c8e0cf67b3be664c94dd8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 20 May 2020 01:28:45 +0200 Subject: [PATCH] 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. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../model/user/PizzaAdminUserSessionBean.java | 4 +- .../model/user/PizzaUserSessionBean.java | 15 +- .../PizzaUserRegistrationSessionBean.java | 37 ++-- .../enterprise/BasePizzaEnterpriseBean.java | 196 +++++++++++++++++- 4 files changed, 215 insertions(+), 37 deletions(-) diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java index 366e174..3e80bb0 100644 --- a/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java @@ -82,7 +82,7 @@ public class PizzaAdminUserSessionBean extends BasePizzaEnterpriseBean implement } // Set created timestamp - user.setUserCreated(new Date()); + user.setUserEntryCreated(new Date()); user.getUserContact().setContactEntryCreated(new Date()); // Update mobile, land-line and fax instance @@ -179,7 +179,7 @@ public class PizzaAdminUserSessionBean extends BasePizzaEnterpriseBean implement 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/PizzaUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java index 58b8c54..4a1980d 100644 --- a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java @@ -70,7 +70,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use } @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 @@ -107,7 +107,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use // 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 @@ -262,7 +262,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use } @Override - public User updateUserData (final User detachedUser) { + public User updateUserData (final User detachedUser) throws UserNotFoundException { // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N @@ -281,7 +281,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use throw new NullPointerException("detachedUser.userAccountStatus is null"); //NOI18N } else if (!this.ifUserExists(detachedUser)) { // User does not exist - throw new EJBException(MessageFormat.format("User with id {0} does not exist.", detachedUser.getUserId())); //NOI18N + throw new UserNotFoundException(detachedUser.getUserId()); } // Find the instance @@ -303,7 +303,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use Users.copyAll(detachedUser, managedUser); // 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 @@ -363,7 +363,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use 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); @@ -421,8 +421,7 @@ public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements Use Users.copyUserData(user, managedUser); // Set as updated - managedUser.setUserUpdated(new Date()); - managedUser.getUserContact().setContactUpdated(new Date()); + managedUser.setUserEntryUpdated(new Date()); // Get contact from it and find it final Contact foundContact = this.getEntityManager().find(managedUser.getUserContact().getClass(), managedUser.getUserContact().getContactId()); diff --git a/src/java/org/mxchange/juserlogincore/model/user/register/PizzaUserRegistrationSessionBean.java b/src/java/org/mxchange/juserlogincore/model/user/register/PizzaUserRegistrationSessionBean.java index 35d3439..b175335 100644 --- a/src/java/org/mxchange/juserlogincore/model/user/register/PizzaUserRegistrationSessionBean.java +++ b/src/java/org/mxchange/juserlogincore/model/user/register/PizzaUserRegistrationSessionBean.java @@ -17,16 +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.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 +73,8 @@ public class PizzaUserRegistrationSessionBean extends BasePizzaEnterpriseBean im 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 +82,16 @@ public class PizzaUserRegistrationSessionBean extends BasePizzaEnterpriseBean im // 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 +139,7 @@ public class PizzaUserRegistrationSessionBean extends BasePizzaEnterpriseBean im // 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 } diff --git a/src/java/org/mxchange/pizzaapplication/enterprise/BasePizzaEnterpriseBean.java b/src/java/org/mxchange/pizzaapplication/enterprise/BasePizzaEnterpriseBean.java index 4f8907c..429a3e2 100644 --- a/src/java/org/mxchange/pizzaapplication/enterprise/BasePizzaEnterpriseBean.java +++ b/src/java/org/mxchange/pizzaapplication/enterprise/BasePizzaEnterpriseBean.java @@ -1155,10 +1155,7 @@ public abstract class BasePizzaEnterpriseBean 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("{0}.updateUserPersonalData: foundContact.contactId={1}", this.getClass().getSimpleName(), foundContact.getContactId())); //NOI18N @@ -1290,6 +1287,197 @@ public abstract class BasePizzaEnterpriseBean 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 -- 2.39.5