]> git.mxchange.org Git - pizzaservice-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/register/PizzaUserRegistrationSessionBean.java
opps, use addedUser for having the id number available
[pizzaservice-ejb.git] / src / java / org / mxchange / jusercore / model / register / PizzaUserRegistrationSessionBean.java
index 63c01d0aed0f6ae58d98a46f2972dab90a78d9b6..bad0d8ece8bb0836befb4d80a29503f206259e35 100644 (file)
@@ -18,20 +18,29 @@ package org.mxchange.jusercore.model.register;
 
 import java.text.MessageFormat;
 import javax.ejb.EJB;
+import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import javax.mail.Address;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
+import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
+import org.mxchange.jusercore.model.user.LoginUser;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
+import org.mxchange.jusercore.model.user.UserUtils;
+import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
 
 /**
  * A session bean for user registration
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
-@Stateless (name = "register", mappedName = "ejb/stateless-pizza-register", description = "A bean handling the user registration")
-public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
+@Stateless (name = "register", description = "A bean handling the user registration")
+public class PizzaUserRegistrationSessionBean extends BasePizzaDatabaseBean implements UserRegistrationSessionBeanRemote {
 
        /**
         * Serial number
@@ -39,11 +48,57 @@ public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implement
        private static final long serialVersionUID = 12_348_958_986_818_627L;
 
        /**
-        * User bean
+        * User EJB
         */
        @EJB
        private UserSessionBeanRemote userBean;
 
+       @Override
+       public String generateConfirmationKey (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: user={0} - CALLED!", user)); //NOI18N
+
+               // user should not be null
+               if (null == user) {
+                       // Abort here
+                       throw new NullPointerException("user is null"); //NOI18N
+               }
+
+               // Create named instance
+               Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
+
+               // Init confirmation key
+               String confirmationKey = null;
+
+               // Find a free one
+               while (confirmationKey == null) {
+                       // Create new one
+                       String key = UserUtils.generatedConfirmationKey(user);
+
+                       // Set key as parameter
+                       query.setParameter("confirmKey", key); //NOI18N
+
+                       // Try it
+                       try {
+                               // Get contact instance
+                               Contact contact = (Contact) query.getSingleResult();
+
+                               // Warning message
+                               this.getLoggerBeanLocal().logWarning(MessageFormat.format("generateConfirmationKey: key {0} already found: contact.contactId={1}", key, contact.getContactId())); //NOI18N
+                       } catch (final NoResultException ex) {
+                               // Not found, normal case
+                               confirmationKey = key;
+                               break;
+                       }
+               }
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: confirmationKey={0} - EXIT!", confirmationKey)); //NOI18N
+
+               // Return it
+               return confirmationKey;
+       }
+
        @Override
        public boolean isEmailAddressRegistered (final User user) {
                // Trace message
@@ -59,7 +114,7 @@ public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implement
                }
 
                // Call other bean
-               return this.userBean.isEmailAddressReqistered(user);
+               return this.userBean.isEmailAddressRegistered(user);
        }
 
        @Override
@@ -77,7 +132,7 @@ public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implement
                }
 
                // Call other bean
-               return this.userBean.isUserNameReqistered(user);
+               return this.userBean.isUserNameRegistered(user);
        }
 
        @Override
@@ -89,6 +144,15 @@ public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implement
                if (null == user) {
                        // Abort here
                        throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserContact() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userContact is null"); //NOI18N
+               } else if (user.getUserContact().getContactEmailAddress() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
+               } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
                }
 
                // Check if user is registered
@@ -100,17 +164,29 @@ public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implement
                        throw new EmailAddressAlreadyRegisteredException(user);
                }
 
-               // Persist it
-               this.getEntityManager().persist(user);
+               // Call other EJB
+               User addedUser = this.userBean.addUser(user);
+
+               // Init variable
+               Address emailAddress;
+
+               try {
+                       // Create email address and set
+                       emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
+               } catch (final AddressException ex) {
+                       // Throw again
+                       throw new EJBException(ex);
+               }
 
-               // Flush to get id back
-               this.getEntityManager().flush();
+               // Send email
+               // TODO: Internationlize the subject line somehow
+               this.sendEmail("Registration", "registration", emailAddress, addedUser); //NOI18N
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userId={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N
 
                // Return it
-               return user;
+               return addedUser;
        }
 
 }