]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
Please cherry-pick / fix Copyright:
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookUserSessionBean.java
index cb012dc9c3db42b61150844e34f536796c2cb80e..059d530b2676f8db7d828542728318409ab58c3b 100644 (file)
@@ -22,17 +22,22 @@ 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.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.model.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
@@ -42,8 +47,8 @@ import org.mxchange.jusercore.model.user.status.UserAccountStatus;
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
-@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
@@ -89,30 +94,10 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                user.setUserCreated(new GregorianCalendar());
                user.getUserContact().setContactCreated(new GregorianCalendar());
 
-               // Get all phone instances
-               DialableLandLineNumber landLineNumber = user.getUserContact().getContactLandLineNumber();
-               DialableFaxNumber faxNumber = user.getUserContact().getContactFaxNumber();
-               DialableCellphoneNumber cellphoneNumber = user.getUserContact().getContactCellphoneNumber();
+               // Update cellphone, land-line and fax instance
+               this.setAllContactPhoneEntriesCreated(user.getUserContact());
 
-               // Is a phone number instance set?
-               if (landLineNumber instanceof DialableLandLineNumber) {
-                       // Set created timestamp
-                       landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
-               }
-
-               // Is a fax number instance set?
-               if (faxNumber instanceof DialableFaxNumber) {
-                       // Set created timestamp
-                       faxNumber.setPhoneEntryCreated(new GregorianCalendar());
-               }
-
-               // Is a mobile number instance set?
-               if (cellphoneNumber instanceof DialableCellphoneNumber) {
-                       // Set created timestamp
-                       cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar());
-               }
-
-                       // Persist it
+               // Persist it
                this.getEntityManager().persist(user);
 
                // Flush to get id back
@@ -132,7 +117,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //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
@@ -156,7 +141,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //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
@@ -179,7 +164,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //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<User> users = query.getResultList();
@@ -191,6 +176,61 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                return users;
        }
 
+       @Override
+       public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("confirmAccount: user={0},baseUrl={1} - CALLED!", 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("confirmAccount: updatedUser={0} - EXIT!", updatedUser)); //NOI18N
+
+               // Return updated instance
+               return updatedUser;
+       }
+
        @Override
        public User fillUserData (final User user) {
                // Trace message
@@ -203,10 +243,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;
@@ -229,6 +269,9 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
 
        @Override
        public User findUserById (final Long userId) throws UserNotFoundException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: userId={0} - CALLED!", userId)); //NOI18N
+
                // Is the parameter valid?
                if (null == userId) {
                        // Throw NPE
@@ -242,7 +285,7 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                }
 
                // Create query instance
-               Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
 
                // Set user id
                query.setParameter("id", userId); //NOI18N
@@ -250,13 +293,37 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                // 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("findUserById: user={0} - EXIT!", user)); //NOI18N
 
                // Return found user
                return user;
        }
 
+       @Override
+       public String generateRandomUserName () {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace("generateRandomUserName - CALLED!"); //NOI18N
+
+               // Get full list
+               List<String> 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("generateRandomUserName: userName={0} - EXIT!", userName)); //NOI18N
+
+               // Found one, so return it
+               return userName;
+       }
+
        @Override
        @SuppressWarnings ("unchecked")
        public List<String> getEmailAddressList () {
@@ -301,7 +368,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
@@ -348,7 +415,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
@@ -395,10 +462,10 @@ 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", userName); //NOI18N
+               query.setParameter("userName", userName); //NOI18N
 
                // Try this
                try {
@@ -422,9 +489,9 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
        }
 
        @Override
-       public boolean isEmailAddressReqistered (final User user) {
+       public boolean isEmailAddressRegistered (final User user) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
 
                // user should not be null
                if (null == user) {
@@ -433,20 +500,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("isEmailAddressRegistered: dummy.id={0} found.", 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("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
 
                        // Email address does not exist
                        return false;
@@ -463,9 +530,9 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
        }
 
        @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("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
 
                // user should not be null
                if (null == user) {
@@ -474,20 +541,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("isUserNameRegistered: dummy.id={0} found.", 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("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
 
                        // User name does not exist
                        return false;
@@ -503,6 +570,60 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
                return true;
        }
 
+       @Override
+       public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: 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() 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) {
+                       // 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
+               } 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)) {
+                       // User does not exist
+                       throw new IllegalStateException("User does already exist."); //NOI18N
+               }
+
+               // Try to find the contact
+               Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
+
+               // Set detached object in rexcruiter instance
+               user.setUserContact(foundContact);
+
+               // Set timestamp
+               user.setUserCreated(new GregorianCalendar());
+
+               // Perist it
+               this.getEntityManager().persist(user);
+
+               // Flush it to get updated instance back
+               this.getEntityManager().flush();
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - EXIT!", user)); //NOI18N
+
+               // Return updated instanc
+               return user;
+       }
+
        @Override
        public User updateUserData (final User user) {
                // Trace message