X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjusercore%2Fmodel%2Fuser%2FAddressbookUserSessionBean.java;h=4b1163375afef8cc1432a6a03cf1e6aa9cfcc50c;hb=71f544e35953214914e5dfce601adb82cd43ae20;hp=52df1727ecc7fb6d35dcd9fd299de9910206e58b;hpb=33954176a77fcf701b0348e425d4c8ef3568ff6a;p=addressbook-mailer-ejb.git diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java index 52df172..4b11633 100644 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java @@ -22,6 +22,9 @@ import java.util.List; import javax.ejb.EJB; import javax.ejb.EJBException; import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; import javax.persistence.NoResultException; import javax.persistence.PersistenceException; import javax.persistence.Query; @@ -30,10 +33,13 @@ import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; -import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.password_history.PasswordHistory; +import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; import org.mxchange.jusercore.model.user.status.UserAccountStatus; @@ -62,57 +68,14 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl public AddressbookUserSessionBean () { } - @Override - public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() != null) { - // Not allowed here - throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N - } - - // Check if user is registered - if (this.registerBean.isUserNameRegistered(user)) { - // Abort here - throw new UserNameAlreadyRegisteredException(user); - } else if (this.registerBean.isEmailAddressRegistered(user)) { - // Abort here - throw new EmailAddressAlreadyRegisteredException(user); - } - - // Set created timestamp - user.setUserCreated(new GregorianCalendar()); - user.getUserContact().setContactCreated(new GregorianCalendar()); - - // Update cellphone, land-line and fax instance - this.setAllContactPhoneEntriesCreated(user.getUserContact()); - - // Persist it - this.getEntityManager().persist(user); - - // Flush to get id back - this.getEntityManager().flush(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N - - // Return it - return user; - } - @Override @SuppressWarnings ("unchecked") public List allMemberPublicVisibleUsers () { // Trace message - this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N // Get named query - Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N // Set parameters query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N @@ -123,7 +86,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl List users = query.getResultList(); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N // Return full list return users; @@ -133,10 +96,10 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @SuppressWarnings ("unchecked") public List allPublicUsers () { // Trace message - this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N // Get named query - Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N // Set parameters query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N @@ -146,7 +109,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl List users = query.getResultList(); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N // Return full list return users; @@ -156,25 +119,80 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @SuppressWarnings ("unchecked") public List allUsers () { // Trace message - this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N // Get named query - Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N // Get result List users = query.getResultList(); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N + 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 + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // Parameter must be valid + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Abort here + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Account is already confirmed + throw new UserStatusConfirmedException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Account is already confirmed + throw new UserStatusLockedException(user); + } else if (user.getUserConfirmKey() == null) { + // Throw NPE + throw new NullPointerException("user.userConfirmKey is null"); //NOI18N + } + + // Update user status and remove confirmation key + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + user.setUserConfirmKey(null); + user.setUserUpdated(new GregorianCalendar()); + + // Update user account + User updatedUser = this.updateUserData(user); + + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send out email + this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return updated instance + return updatedUser; + } + @Override public User fillUserData (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -183,10 +201,10 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Try to locate it - Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N // Set parameter - query.setParameter("param", user.getUserName()); //NOI18N + query.setParameter("userName", user.getUserName()); //NOI18N // Initialize variable User foundUser = null; @@ -201,7 +219,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N // Return prepared instance return foundUser; @@ -209,6 +227,9 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override public User findUserById (final Long userId) throws UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N + // Is the parameter valid? if (null == userId) { // Throw NPE @@ -222,7 +243,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Create query instance - Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N // Set user id query.setParameter("id", userId); //NOI18N @@ -230,22 +251,52 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl // Fetch the result, it should be there by now User user = (User) query.getSingleResult(); - // Should be there - assert(user instanceof User) : "user is null"; //NOI18N + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N // Return found user return user; } + @Override + public String generateRandomUserName () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get full list + List userList = this.getUserNameList(); + + // Init variable + String userName = null; + + // Loop until a user name is found + while ((userName == null) || (userList.contains(userName))) { + // Generate random name + userName = UserUtils.generateRandomUserName(); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N + + // Found one, so return it + return userName; + } + @Override @SuppressWarnings ("unchecked") public List getEmailAddressList () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N + // Get query Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N // Get result list List emailAddressList = query.getResultList(); + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N + // Return it return emailAddressList; } @@ -253,12 +304,18 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override @SuppressWarnings ("unchecked") public List getUserNameList () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N + // Get query Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N // Get result list List 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; } @@ -266,7 +323,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override public boolean ifUserExists (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // userId should not be null if (null == user) { @@ -281,7 +338,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N // Set parameter query.setParameter("id", user.getUserId()); //NOI18N @@ -307,7 +364,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N // Found it return true; @@ -316,7 +373,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override public boolean ifUserIdExists (final Long userId) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N // userId should not be null if (null == userId) { @@ -328,7 +385,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N // Set parameter query.setParameter("id", userId); //NOI18N @@ -338,10 +395,10 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl User dummy = (User) query.getSingleResult(); // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N } catch (final NoResultException ex) { // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N // User name does not exist return false; @@ -354,7 +411,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N // Found it return true; @@ -363,7 +420,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override public boolean ifUserNameExists (final String userName) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N // userId should not be null if (null == userName) { @@ -375,27 +432,27 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N // Set parameter - query.setParameter("param", userName); //NOI18N + query.setParameter("userName", userName); //NOI18N // Try this try { User dummy = (User) query.getSingleResult(); // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N + 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("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N // User name does not exist return false; } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N // Found it return true; @@ -404,7 +461,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override public boolean isEmailAddressRegistered (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -413,20 +470,20 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N // Set parameter - query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N + query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N // Search for it try { User dummy = (User) query.getSingleResult(); // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N + 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("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N // Email address does not exist return false; @@ -438,6 +495,9 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl throw ex; } + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N + // Found it return true; } @@ -445,7 +505,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl @Override public boolean isUserNameRegistered (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -454,20 +514,20 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N // Set parameter - query.setParameter("param", user.getUserName()); //NOI18N + query.setParameter("userName", user.getUserName()); //NOI18N // Try this try { User dummy = (User) query.getSingleResult(); // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N + 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("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N // User name does not exist return false; @@ -479,72 +539,68 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl throw ex; } + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N + // Found it return true; } @Override - public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + public User updateUserData (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null - // @TODO Add check for email address if (null == user) { // Abort here throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() instanceof Long) { - // Id is set - throw new IllegalArgumentException("user.userId is not null"); //NOI18N - } else if (user.getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { + } else if (user.getUserId() == null) { // Throw NPE again - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N } else if (user.getUserAccountStatus() == null) { // Throw NPE again throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (this.ifUserNameExists(user.getUserName())) { - // Name already found - throw new UserNameAlreadyRegisteredException(user.getUserName()); - } else if (this.ifUserExists(user)) { + } else if (!this.ifUserExists(user)) { // User does not exist - throw new IllegalStateException("User does already exist."); //NOI18N + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N } - // Try to find the contact - Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); + // Remove contact instance as this is not updated + user.setUserContact(null); - // Merge the contact to get a detached object back - Contact detachedContact = this.getEntityManager().merge(foundContact); + // Find the instance + User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); - // Set detached object in rexcruiter instance - user.setUserContact(detachedContact); + // Should be found! + assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N - // Set timestamp - user.setUserCreated(new GregorianCalendar()); + // Merge user + User detachedUser = this.getEntityManager().merge(foundUser); - // Perist it - this.getEntityManager().persist(user); + // Should be found! + assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N - // Flush it to get updated instance back - this.getEntityManager().flush(); + // Copy all data + detachedUser.copyAll(user); - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - EXIT!", user)); //NOI18N + // Set as updated + detachedUser.setUserUpdated(new GregorianCalendar()); - // Return updated instanc - return user; + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N + + // Return updated instance + return detachedUser; } @Override - public User updateUserData (final User user) { + public PasswordHistory updateUserPassword (final User user) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -555,7 +611,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl throw new NullPointerException("user.userId is null"); //NOI18N } else if (user.getUserId() < 1) { // Not valid - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N } else if (user.getUserAccountStatus() == null) { // Throw NPE again throw new NullPointerException("user.userAccountStatus is null"); //NOI18N @@ -564,35 +620,32 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N } - // Remove contact instance as this is not updatedf - user.setUserContact(null); - - // Find the instance - User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); + // Call other method + User updatedUser = this.updateUserData(user); - // Should be found! - assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + // Create history entry + PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser); - // Merge user - User detachedUser = this.getEntityManager().merge(foundUser); + // Set created timestamp + entry.setUserPasswordHistoryCreated(new GregorianCalendar()); - // Should be found! - assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N + // Persist it + this.getEntityManager().persist(entry); - // Copy all data - detachedUser.copyAll(user); + // Flush it to get id number back + this.getEntityManager().flush(); - // Set as updated - detachedUser.setUserUpdated(new GregorianCalendar()); + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N - // Return updated instance - return detachedUser; + // Return it + return entry; } @Override public User updateUserPersonalData (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -733,6 +786,9 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl detachedContact.setContactLandLineNumber(detachedLandLine); } + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N + // Return updated user instance return detachedUser; }