]> git.mxchange.org Git - addressbook-mailer-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
updated jar(s)
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / register / AddressbookUserRegistrationSessionBean.java
index de011f986092922f7f6f7d8f9a3d4c85e771acf3..ff98ffc5a6773ca4ad0744573b23a1dc12044482 100644 (file)
@@ -1,37 +1,47 @@
 /*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016, 2017 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 <http://www.gnu.org/licenses/>.
  */
 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.addressbook.database.BaseAddressbookDatabaseBean;
+import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
+import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
+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
+ * A session-scoped bean for user registration
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
  */
-@Stateless (name = "register", mappedName = "ejb/stateless-addressbook-register", description = "A bean handling the user registration")
-public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
+@Stateless (name = "register", description = "A bean handling the user registration")
+public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
 
        /**
         * Serial number
@@ -39,18 +49,78 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
        private static final long serialVersionUID = 12_348_958_986_818_627L;
 
        /**
-        * User bean
+        * Administrative user bean
+        */
+       @EJB
+       private AdminUserSessionBeanRemote adminUserBean;
+
+       /**
+        * Regular user EJB
         */
        @EJB
        private UserSessionBeanRemote userBean;
 
+       /**
+        * Default constructor
+        */
+       public AddressbookUserRegistrationSessionBean () {
+               // Call super constructor
+               super();
+       }
+
+       @Override
+       public String generateConfirmationKey (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: 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
+               }
+
+               // 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("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
+                       } catch (final NoResultException ex) {
+                               // Not found, normal case
+                               confirmationKey = key;
+                               break;
+                       }
+               }
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), 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
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), 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,16 +129,16 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                }
 
                // Call other bean
-               return this.userBean.isEmailAddressReqistered(user);
+               return this.userBean.isEmailAddressRegistered(user);
        }
 
        @Override
        public boolean isUserNameRegistered (final User user) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), 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,18 +147,27 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                }
 
                // Call other bean
-               return this.userBean.isUserNameReqistered(user);
+               return this.userBean.isUserNameRegistered(user);
        }
 
        @Override
-       public User registerUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
+       public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0} - CALLED!", user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: 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.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,16 +179,29 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                        throw new EmailAddressAlreadyRegisteredException(user);
                }
 
-               // Persist it
-               this.getEntityManager().persist(user);
+               // Call other EJB
+               User addedUser = this.adminUserBean.addUser(user);
 
-               // Flush to get id back
-               this.getEntityManager().flush();
+               // 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);
+               }
+
+               // Send email
+               // @TODO: Internationlize the subject line somehow
+               this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
 
                // Return it
-               return user;
+               return addedUser;
        }
+
 }