]> 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 a713d211855c7717686f998c4d29e76e16f28dfd..059d530b2676f8db7d828542728318409ab58c3b 100644 (file)
@@ -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;
@@ -33,6 +36,8 @@ 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;
@@ -112,7 +117,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                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
@@ -136,7 +141,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                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
@@ -159,7 +164,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                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();
@@ -171,6 +176,61 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                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
@@ -183,10 +243,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;
@@ -225,7 +285,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
@@ -233,11 +293,8 @@ 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("findUserById: user={0} - EXIT!!", user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: user={0} - EXIT!", user)); //NOI18N
 
                // Return found user
                return user;
@@ -311,7 +368,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
@@ -358,7 +415,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
@@ -405,10 +462,10 @@ 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 {
@@ -443,10 +500,10 @@ 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 {
@@ -484,10 +541,10 @@ 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 {
@@ -519,7 +576,6 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", 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
@@ -549,11 +605,8 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
                // Try to find the contact
                Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
 
-               // Merge the contact to get a detached object back
-               Contact detachedContact = this.getEntityManager().merge(foundContact);
-
                // Set detached object in rexcruiter instance
-               user.setUserContact(detachedContact);
+               user.setUserContact(foundContact);
 
                // Set timestamp
                user.setUserCreated(new GregorianCalendar());