]> git.mxchange.org Git - addressbook-ejb.git/blobdiff - src/java/org/mxchange/juserlogincore/model/user/register/AddressbookUserRegistrationSessionBean.java
Updated copyright year
[addressbook-ejb.git] / src / java / org / mxchange / juserlogincore / model / user / register / AddressbookUserRegistrationSessionBean.java
index 3d96a742407ed3a2b204482102fe877927814ba6..0d833ba59a52d55ed74add5e3a24f9500754353c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 - 2020 Free Software Foundation
+ * Copyright (C) 2016 - 2024 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
 package org.mxchange.juserlogincore.model.user.register;
 
 import java.text.MessageFormat;
+import java.util.List;
 import java.util.Objects;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
-import javax.persistence.NoResultException;
-import javax.persistence.Query;
-import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
-import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
 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.juserlogincore.login.UserLoginUtils;
+import org.mxchange.juserlogincore.utils.UserLoginUtils;
 
 /**
  * A session-scoped bean for user registration
@@ -38,7 +35,7 @@ import org.mxchange.juserlogincore.login.UserLoginUtils;
  * @author Roland Häder<roland@mxchange.org>
  */
 @Stateless (name = "userRegistration", description = "A bean handling the user registration")
-public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
+public class AddressbookUserRegistrationSessionBean extends BaseAddressbookEnterpriseBean implements UserRegistrationSessionBeanRemote {
 
        /**
         * Serial number
@@ -48,13 +45,13 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
        /**
         * Administrative user bean
         */
-       @EJB
+       @EJB (lookup = "java:global/addressbook-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
        private AdminUserSessionBeanRemote adminUserBean;
 
        /**
         * Regular user EJB
         */
-       @EJB
+       @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
        private UserSessionBeanRemote userBean;
 
        /**
@@ -76,8 +73,8 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
                        throw new NullPointerException("user is null"); //NOI18N
                }
 
-               // Create named instance
-               final Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
+               // Fetch whole list
+               final List<User> users = this.userBean.fetchAllUsers();
 
                // Init confirmation key
                String confirmationKey = null;
@@ -85,22 +82,16 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
                // Find a free one
                while (confirmationKey == null) {
                        // Create new one
-                       final String key = UserLoginUtils.generatedConfirmationKey(user);
-
-                       // Set key as parameter
-                       query.setParameter("confirmKey", key); //NOI18N
-
-                       // Try it
-                       try {
-                               // Get contact instance
-                               final 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;
+                       confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
+
+                       // Check all entries
+                       for (final User currentUser : users) {
+                               // Does the key match?
+                               if (Objects.equals(currentUser.getUserConfirmKey(), confirmationKey)) {
+                                       // Set key to null and exit loop
+                                       confirmationKey = null;
+                                       break;
+                               }
                        }
                }
 
@@ -111,42 +102,6 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
                return confirmationKey;
        }
 
-       @Override
-       public boolean isEmailAddressRegistered (final User user) {
-               // Trace message
-               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
-
-               // user should not be null
-               if (null == user) {
-                       // Abort here
-                       throw new NullPointerException("user is null"); //NOI18N
-               }
-
-               // Call other bean
-               return this.userBean.isEmailAddressRegistered(user);
-       }
-
-       @Override
-       public boolean isUserNameRegistered (final User user) {
-               // Trace message
-               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
-
-               // user should not be null
-               if (null == user) {
-                       // Abort here
-                       throw new NullPointerException("user is null"); //NOI18N
-               }
-
-               // Call other bean
-               return this.userBean.isUserNameRegistered(user);
-       }
-
        @Override
        public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
                // Trace message
@@ -165,13 +120,19 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
                } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
                        // Is empty
                        throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
+               } else if (null == baseUrl) {
+                       // Throw NPE again
+                       throw new NullPointerException("baseUrl is null"); //NOI18N
+               } else if (baseUrl.isEmpty()) {
+                       // Throw IAE
+                       throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
                }
 
                // Check if user is registered
-               if (this.isUserNameRegistered(user)) {
+               if (this.userBean.isUserNameRegistered(user)) {
                        // Abort here
                        throw new UserNameAlreadyRegisteredException(user);
-               } else if (this.isEmailAddressRegistered(user)) {
+               } else if (this.userBean.isEmailAddressRegistered(user)) {
                        // Abort here
                        throw new EmailAddressAlreadyRegisteredException(user);
                }
@@ -184,7 +145,7 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
 
                // Is password set?
                if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
-                       // Switch to other template
+                       // Switch to template with random password exposed
                        templateName = "user_registration_random"; //NOI18N
                }