X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjusercore%2Fmodel%2Fuser%2FAddressbookUserSessionBean.java;h=06ab9ad5ba29a66b1615aea5d3f3d1f39874d7af;hb=e31c1fb22e8aaee4b6368cb6a361d32a6b8a2165;hp=af3fa0220f9bb98a710f271fcdb5890cd7a1f553;hpb=f71c7f4f8558a62921d44716c56139c4a371b4b2;p=pizzaservice-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 af3fa02..06ab9ad 100644 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java @@ -1,17 +1,17 @@ /* - * Copyright (C) 2016 Roland Haeder + * Copyright (C) 2016 Roland Häder * * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package org.mxchange.jusercore.model.user; @@ -19,32 +19,49 @@ package org.mxchange.jusercore.model.user; import java.text.MessageFormat; import java.util.GregorianCalendar; 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; +import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; +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; /** - * A user bean + * A user EJB *

- * @author Roland Haeder + * @author Roland Häder */ -@Stateless (name = "user", mappedName = "ejb/stateless-addressbook-user", description = "A bean handling the user data") -public class AddressbookUserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote { +@Stateless (name = "user", description = "A bean handling the user data") +public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean implements UserSessionBeanRemote { /** * Serial number */ private static final long serialVersionUID = 542_145_347_916L; + /** + * Registration EJB + */ + @EJB + private UserRegistrationSessionBeanRemote registerBean; + /** * Default constructor */ @@ -55,10 +72,10 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User @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 @@ -69,7 +86,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User 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; @@ -79,10 +96,10 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User @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 @@ -92,16 +109,90 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User 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; } + @Override + @SuppressWarnings ("unchecked") + public List allUsers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N + + // Get result + List users = query.getResultList(); + + // Trace message + 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) { @@ -110,10 +201,10 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // Try to locate it - Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.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; @@ -128,21 +219,84 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // 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; } + @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 + throw new NullPointerException("userId is null"); //NOI18N + } else if (userId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N + } else if (!this.ifUserIdExists(userId)) { + // Does not exist + throw new UserNotFoundException(userId); + } + + // Create query instance + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N + + // Set user id + query.setParameter("id", userId); //NOI18N + + // Fetch the result, it should be there by now + User user = (User) query.getSingleResult(); + + // 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; } @@ -150,12 +304,18 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User @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; } @@ -163,7 +323,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User @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) { @@ -178,7 +338,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N // Set parameter query.setParameter("id", user.getUserId()); //NOI18N @@ -204,7 +364,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // 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; @@ -213,7 +373,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User @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) { @@ -225,7 +385,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N // Set parameter query.setParameter("id", userId); //NOI18N @@ -235,10 +395,10 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User 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; @@ -251,16 +411,57 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // 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; } @Override - public boolean isEmailAddressReqistered (final User user) { + public boolean ifUserNameExists (final String userName) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N + + // userId should not be null + if (null == userName) { + // Abort here + throw new NullPointerException("userName is null"); //NOI18N + } else if (userName.isEmpty()) { + // Abort here + throw new NullPointerException("userName is empty"); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("userName", userName); //NOI18N + + // Try this + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + 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("{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("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N + + // Found it + return true; + } + + @Override + public boolean isEmailAddressRegistered (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -269,20 +470,20 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.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("isEmailAddressReqistered: 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("isEmailAddressReqistered: 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; @@ -294,14 +495,17 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User throw ex; } + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N + // Found it return true; } @Override - public boolean isUserNameReqistered (final User user) { + public boolean isUserNameRegistered (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: 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) { @@ -310,20 +514,20 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.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("isUserNameReqistered: 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("isUserNameReqistered: 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; @@ -335,14 +539,119 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User throw ex; } + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N + // Found it return true; } @Override - public void updateUserPersonalData (final User user) { + public User updateUserData (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + 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.ifUserExists(user)) { + // User does not exist + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N + } + + // Remove contact instance as this is not updated + user.setUserContact(null); + + // Find the instance + User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); + + // Should be found! + assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + + // Merge user + User detachedUser = this.getEntityManager().merge(foundUser); + + // Should be found! + assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N + + // Copy all data + detachedUser.copyAll(user); + + // Set as updated + detachedUser.setUserUpdated(new GregorianCalendar()); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N + + // Return updated instance + return detachedUser; + } + + @Override + public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + 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.ifUserExists(user)) { + // User does not exist + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N + } else if (null == baseUrl) { + // Abort here + throw new NullPointerException("password is null"); //NOI18N + } else if (baseUrl.isEmpty()) { + // Abort here + throw new IllegalArgumentException("password is empty"); //NOI18N + } + + // Call other method + User updatedUser = this.updateUserData(user); + + // Create history entry + PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser); + + // Set created timestamp + entry.setUserPasswordHistoryCreated(new GregorianCalendar()); + + // Persist it + this.getEntityManager().persist(entry); + + // Flush it to get id number back + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N + + // Return it + return entry; + } + + @Override + public User updateUserPersonalData (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N // user should not be null if (null == user) { @@ -388,7 +697,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N // Merge contact instance Contact detachedContact = this.getEntityManager().merge(foundContact); @@ -402,52 +711,52 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User // Should be found! assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N - // Get cellphone instance - DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber(); + // Get mobile instance + DialableMobileNumber mobileNumber = detachedContact.getContactMobileNumber(); - // Is there a cellphone instance set? - if (cellphone instanceof DialableCellphoneNumber) { + // Is there a mobile instance set? + if (mobileNumber instanceof DialableMobileNumber) { // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N // Then find it, too - DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId()); + DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId()); // Should be there - assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); + assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N // Then merge it, too - DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone); + DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile); // Should be there - assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); + assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N // Copy all - detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber()); + detachedMobile.copyAll(user.getUserContact().getContactMobileNumber()); // Set it back - detachedContact.setContactCellphoneNumber(detachedCellphone); + detachedContact.setContactMobileNumber(detachedMobile); } - // Get cellphone instance + // Get mobile instance DialableFaxNumber fax = detachedContact.getContactFaxNumber(); // Is there a fax instance set? if (fax instanceof DialableFaxNumber) { // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N // Then find it, too DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); // Should be there - assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); + assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N // Then merge it, too DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax); // Should be there - assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); + assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N // Copy all detachedFax.copyAll(user.getUserContact().getContactFaxNumber()); @@ -456,25 +765,25 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User detachedContact.setContactFaxNumber(detachedFax); } - // Get cellphone instance + // Get mobile instance DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber(); // Is there a fax instance set? if (landLine instanceof DialableLandLineNumber) { // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N // Then find it, too DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId()); // Should be there - assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); + assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N // Then merge it, too DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine); // Should be there - assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); + assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N // Copy all detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber()); @@ -482,6 +791,12 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User // Set it back 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; } }