]> git.mxchange.org Git - addressbook-mailer-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
Continued:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / register / AddressbookUserRegistrationSessionBean.java
index e0c33dd6a7fba2a4a60f436a0c94c85c4a234e33..1427119289b7a20df0025dff728642ebb576cd28 100644 (file)
@@ -19,18 +19,23 @@ package org.mxchange.jusercore.model.register;
 import java.text.MessageFormat;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
+import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 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;
 
 /**
  * A session bean for user registration
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
-@Stateless (name = "register", mappedName = "ejb/stateless-addressbook-register", description = "A bean handling the user registration")
+@Stateless (name = "register", description = "A bean handling the user registration")
 public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
 
        /**
@@ -39,18 +44,64 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
        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
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
 
                // Check bean
-               assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
+               assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
 
                // user should not be null
                if (null == user) {
@@ -59,7 +110,7 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                }
 
                // Call other bean
-               return this.userBean.isEmailAddressReqistered(user);
+               return this.userBean.isEmailAddressRegistered(user);
        }
 
        @Override
@@ -68,7 +119,7 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
 
                // Check bean
-               assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
+               assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
 
                // user should not be null
                if (null == user) {
@@ -77,7 +128,7 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                }
 
                // Call other bean
-               return this.userBean.isUserNameReqistered(user);
+               return this.userBean.isUserNameRegistered(user);
        }
 
        @Override
@@ -100,16 +151,14 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                        throw new EmailAddressAlreadyRegisteredException(user);
                }
 
-               // Persist it
-               this.getEntityManager().persist(user);
-
-               // Flush to get id back
-               this.getEntityManager().flush();
+               // Call other EJB
+               User addedUser = this.userBean.addUser(user);
 
                // 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.userIdd={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N
 
                // Return it
-               return user;
+               return addedUser;
        }
+
 }