From 7b707f86eec7658e15f9e98abd6526ba68be6f22 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 --- .../BaseFinancialsEnterpriseBean.java | 10 +---- .../user/FinancialsAdminUserSessionBean.java | 4 +- .../model/user/FinancialsUserSessionBean.java | 14 +++---- ...FinancialsUserRegistrationSessionBean.java | 37 +++++++------------ 4 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/java/org/mxchange/jfinancials/enterprise/BaseFinancialsEnterpriseBean.java b/src/java/org/mxchange/jfinancials/enterprise/BaseFinancialsEnterpriseBean.java index 1178783..2a19807 100644 --- a/src/java/org/mxchange/jfinancials/enterprise/BaseFinancialsEnterpriseBean.java +++ b/src/java/org/mxchange/jfinancials/enterprise/BaseFinancialsEnterpriseBean.java @@ -809,10 +809,7 @@ public abstract class BaseFinancialsEnterpriseBean 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 @@ -959,10 +956,7 @@ public abstract class BaseFinancialsEnterpriseBean extends BaseEnterpriseBean { detachedDepartment.setDepartmentEntryUpdated(new Date()); // Get contact from it and find it - final Department foundDepartment = this.getEntityManager().find(detachedDepartment.getClass(), detachedDepartment.getDepartmentId()); - - // Should be found - assert (foundDepartment instanceof Department) : MessageFormat.format("Department with id {0} not found, but should be.", detachedDepartment.getDepartmentId()); //NOI18N + final Department foundDepartment = this.createManaged(detachedDepartment); // Debug message this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeDepartmentData: foundContact.contactId={0}", foundDepartment.getDepartmentId())); //NOI18N diff --git a/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java index 431b19e..19b6eb3 100644 --- a/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java @@ -81,7 +81,7 @@ public class FinancialsAdminUserSessionBean extends BaseFinancialsEnterpriseBean } // 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 FinancialsAdminUserSessionBean extends BaseFinancialsEnterpriseBean 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/FinancialsUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/FinancialsUserSessionBean.java index 755000f..231ea0f 100644 --- a/src/java/org/mxchange/jusercore/model/user/FinancialsUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/FinancialsUserSessionBean.java @@ -69,7 +69,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl } @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 @@ -100,7 +100,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl // 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 @@ -255,7 +255,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl } @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 @@ -274,7 +274,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl 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()); } // Remove contact instance as this is not updated @@ -296,7 +296,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", managedUser.getUserId()); //NOI18N // Set as updated - managedUser.setUserUpdated(new Date()); + managedUser.setUserEntryUpdated(new Date()); // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - CALLED!", this.getClass().getSimpleName(), managedUser)); //NOI18N @@ -356,7 +356,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl 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); @@ -414,7 +414,7 @@ public class FinancialsUserSessionBean extends BaseFinancialsEnterpriseBean impl 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/FinancialsUserRegistrationSessionBean.java b/src/java/org/mxchange/juserlogincore/model/user/register/FinancialsUserRegistrationSessionBean.java index 65d1a12..477ba3a 100644 --- a/src/java/org/mxchange/juserlogincore/model/user/register/FinancialsUserRegistrationSessionBean.java +++ b/src/java/org/mxchange/juserlogincore/model/user/register/FinancialsUserRegistrationSessionBean.java @@ -17,17 +17,14 @@ 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.jfinancials.enterprise.BaseFinancialsEnterpriseBean; 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 FinancialsUserRegistrationSessionBean extends BaseFinancialsEnterpr 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 FinancialsUserRegistrationSessionBean extends BaseFinancialsEnterpr // 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 FinancialsUserRegistrationSessionBean extends BaseFinancialsEnterpr // 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 } -- 2.39.5