]> git.mxchange.org Git - jjobs-war.git/blobdiff - src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java
No, putting these methods into admin (request-scoped) controller is not good as no...
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / JobsAdminUserWebRequestBean.java
index 4f6c46438e4e40cfcf977f0774f689e064f57855..fbbb33743dd95c70cc19b7659f423102483e2e03 100644 (file)
 package org.mxchange.jjobs.beans.user;
 
 import java.text.MessageFormat;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Objects;
 import javax.annotation.PostConstruct;
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.event.Event;
+import javax.enterprise.event.Observes;
 import javax.enterprise.inject.Any;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Inject;
@@ -33,11 +31,16 @@ import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
-import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
-import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
+import org.mxchange.jjobs.beans.helper.JobsAdminWebRequestController;
+import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
+import org.mxchange.jusercore.container.login.UserLoginContainer;
+import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
+import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
+import org.mxchange.jusercore.events.user.add.AdminUserAddedEvent;
+import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
+import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
-import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserPasswordRepeatMismatchException;
 import org.mxchange.jusercore.model.user.LoginUser;
 import org.mxchange.jusercore.model.user.User;
@@ -67,12 +70,25 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr
        @Any
        private Event<AdminAddedUserEvent> addedUserEvent;
 
+       /**
+        * Admin helper instance
+        */
+       @Inject
+       private JobsAdminWebRequestController adminHelper;
+
        /**
         * Regular contact controller
         */
        @Inject
        private JobsContactWebSessionController contactController;
 
+       /**
+        * An event fired when the administrator has updated a new user
+        */
+       @Inject
+       @Any
+       private Event<AdminUpdatedUserDataEvent> updatedUserDataEvent;
+
        /**
         * Remote user bean
         */
@@ -85,9 +101,10 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr
        private JobsUserWebSessionController userController;
 
        /**
-        * A list of all user profiles
+        * Login bean (controller)
         */
-       private List<User> userList;
+       @Inject
+       private JobsUserLoginWebSessionController userLoginController;
 
        /**
         * User name
@@ -122,38 +139,80 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr
        }
 
        @Override
-       public void addUser () {
+       public String addUser () {
+               // As the form cannot validate the data (required="true"), check it here
+               if (this.getUserName() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("userName is null"); //NOI18N
+               } else if (this.getUserName().isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("userName is null"); //NOI18N
+               } else if (this.adminHelper.getContact() == null) {
+                       // No contact instance set, so test required fields: gender, first name and family name
+                       if (this.contactController.getGender() == null) {
+                               // Throw NPE again
+                               throw new NullPointerException("contactController.gender is null"); //NOI18N
+                       } else if (this.contactController.getFirstName() == null) {
+                               // ... and again
+                               throw new NullPointerException("contactController.firstName is null"); //NOI18N //NOI18N
+                       } else if (this.contactController.getFirstName().isEmpty()) {
+                               // ... and again
+                               throw new IllegalArgumentException("contactController.firstName is empty");
+                       } else if (this.contactController.getFamilyName() == null) {
+                               // ... and again
+                               throw new NullPointerException("contactController.familyName is null"); //NOI18N
+                       } else if (this.contactController.getFamilyName().isEmpty()) {
+                               // ... and again
+                               throw new IllegalArgumentException("contactController.familyName is empty"); //NOI18N //NOI18N
+                       } else if (this.contactController.getEmailAddress() == null) {
+                               // ... and again
+                               throw new NullPointerException("contactController.emailAddress is null");
+                       } else if (this.contactController.getEmailAddress().isEmpty()) {
+                               // ... and again
+                               throw new IllegalArgumentException("contactController.emailAddress is empty"); //NOI18N //NOI18N
+                       } else if (this.contactController.getEmailAddressRepeat() == null) {
+                               // ... and again
+                               throw new NullPointerException("contactController.emailAddressRepeat is null");
+                       } else if (this.contactController.getEmailAddressRepeat().isEmpty()) {
+                               // ... and again
+                               throw new IllegalArgumentException("contactController.emailAddressRepeat is empty"); //NOI18N //NOI18N
+                       } else if (!Objects.equals(this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat())) {
+                               // Is not same email address
+                               throw new IllegalArgumentException("Both entered email addresses don't match.");
+                       }
+               }
+
                // Create new user instance
-               User localUser = new LoginUser();
+               User user = new LoginUser();
 
                // Set user name, CONFIRMED and INVISIBLE
-               localUser.setUserName(this.getUserName());
-               localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
-               localUser.setUserProfileMode(ProfileMode.INVISIBLE);
+               user.setUserName(this.getUserName());
+               user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
+               user.setUserProfileMode(ProfileMode.INVISIBLE);
 
                // Create contact instance
                Contact contact = this.contactController.createContactInstance();
 
                // Set contact in user
-               localUser.setUserContact(contact);
+               user.setUserContact(contact);
 
                // Init variable for password
                String password = null;
 
                // Is the user name or email address used already?
                // @TODO Add password length check
-               if (this.userController.isUserNameRegistered(localUser)) {
+               if (this.userController.isUserNameRegistered(user)) {
                        // User name is already used
-                       throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
-               } else if (this.contactController.isEmailAddressRegistered(localUser.getUserContact())) {
+                       throw new FaceletException(new UserNameAlreadyRegisteredException(user));
+               } else if ((this.adminHelper.getContact() == null) && (this.contactController.isEmailAddressRegistered(user.getUserContact()))) {
                        // Email address is already used
-                       throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
+                       throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
                } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
                        // Empty password entered, then generate one
                        password = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
                } else if (!this.isSamePasswordEntered()) {
                        // Both passwords don't match
-                       throw new FaceletException(new UserPasswordRepeatMismatchException(localUser));
+                       throw new FaceletException(new UserPasswordRepeatMismatchException(user));
                } else {
                        // Both match, so get it from this bean
                        password = this.getUserPassword();
@@ -164,14 +223,20 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr
                assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
 
                // Encrypt password and set it
-               localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
+               user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
 
                // Init updated user instance
                User updatedUser = null;
 
                try {
                        // Now, that all is set, call EJB
-                       updatedUser = this.userBean.addUser(localUser);
+                       if (this.adminHelper.getContact() instanceof Contact) {
+                               // Link contact with this user
+                               updatedUser = this.userBean.linkUser(user);
+                       } else {
+                               // Add new contact
+                               updatedUser = this.userBean.addUser(user);
+                       }
                } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
                        // Throw again
                        throw new FaceletException(ex);
@@ -180,22 +245,105 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr
                // Fire event
                this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
 
-               // Add user to local list
-               this.userList.add(updatedUser);
-
-               // Clear contact instance
-               this.contactController.clear();
+               // Return to user list (for now)
+               return "admin_list_user"; //NOI18N
        }
 
        @Override
-       public List<User> allUsers () {
-               // Return it
-               return Collections.unmodifiableList(this.userList);
+       public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
+               // Trace message
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
+
+               // event should not be null
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getRegisteredUser() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.user is null"); //NOI18N
+               } else if (event.getRegisteredUser().getUserId() == null) {
+                       // userId is null
+                       throw new NullPointerException("event.user.userId is null"); //NOI18N
+               } else if (event.getRegisteredUser().getUserId() < 1) {
+                       // Not avalid id
+                       throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
+               }
+
+               // Get user instance
+               User registeredUser = event.getRegisteredUser();
+
+               // Debug message
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
+
+               // Clear all data
+               this.clear();
+
+               // Trace message
+               //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
        }
 
        @Override
-       public void editUserData () {
-               throw new UnsupportedOperationException("Not supported yet."); //NOI18N
+       public String editUserData () {
+               // Get user instance
+               User user = this.adminHelper.getUser();
+
+               // Null password means not setting it
+               String encryptedPassword = null;
+
+               // Check if user instance is in helper and valid
+               if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("adminHelper.user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("adminHelper.user.userId is null"); //NOI18N //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Invalid id
+                       throw new IllegalStateException(MessageFormat.format("adminHelper.user.userId={0} is invalid", user.getUserId())); //NOI18N
+               } else if (this.getUserName() == null) {
+                       // Not all required fields are set
+                       throw new NullPointerException("this.userName is null"); //NOI18N
+               } else if (this.getUserName().isEmpty()) {
+                       // Not all required fields are set
+                       throw new IllegalArgumentException("this.userName is empty"); //NOI18N
+               } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
+                       // Not same password entered
+                       this.setUserPassword(null);
+                       this.setUserPasswordRepeat(null);
+
+                       // Throw exception
+                       throw new FaceletException("Not same password entered"); //NOI18N
+               } else if (this.userBean.ifUserNameExists(this.getUserName())) {
+                       // User name already exists
+                       throw new FaceletException(new UserNameAlreadyRegisteredException(this.getUserName()));
+               } else if (this.isSamePasswordEntered()) {
+                       // Same password entered, create container
+                       if (UserUtils.ifPasswordMatches(new UserLoginContainer(user, this.getUserPassword()))) {
+                               // Same password entered
+                               throw new FaceletException("Same password as stored entered."); //NOI18N
+                       }
+
+                       // Encrypt password
+                       encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
+               }
+
+               // Set user name
+               user.setUserName(this.getUserName());
+
+               // Is a password set?
+               if (encryptedPassword != null) {
+                       // Set it as well
+                       user.setUserEncryptedPassword(encryptedPassword);
+               }
+
+               // Call EJB for updating user data
+               User updatedUser = this.userBean.updateUserData(user);
+
+               // Fire event
+               this.updatedUserDataEvent.fire(new AdminUserDataUpdatedEvent(updatedUser));
+
+               // Return to user list (for now)
+               return "admin_list_user"; //NOI18N
        }
 
        @Override
@@ -228,55 +376,22 @@ public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestContr
                this.userPasswordRepeat = userPasswordRepeat;
        }
 
-       @Override
-       public boolean hasUsers () {
-               return (!this.allUsers().isEmpty());
-       }
-
        /**
         * Post-initialization of this class
         */
        @PostConstruct
        public void init () {
-               // Initialize user list
-               this.userList = this.userBean.allUsers();
        }
 
-       @Override
-       public User lookupUserById (final Long userId) throws UserNotFoundException {
-               // Parameter must be valid
-               if (null == userId) {
-                       // Throw NPE
-                       throw new NullPointerException("userId is null"); //NOI18N
-               } else if (userId < 1) {
-                       // Not valid
-                       throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
-               }
-
-               // Init variable
-               User user = null;
-
-               // Try to lookup it in visible user list
-               for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
-                       // Get next user
-                       User next = iterator.next();
-
-                       // Is the user id found?
-                       if (Objects.equals(next.getUserId(), userId)) {
-                               // Copy to other variable
-                               user = next;
-                               break;
-                       }
-               }
-
-               // Is it still null?
-               if (null == user) {
-                       // Not visible for the current user
-                       throw new UserNotFoundException(userId);
-               }
-
-               // Return it
-               return user;
+       /**
+        * Clears this bean
+        */
+       private void clear () {
+               // Clear all data
+               // - other data
+               this.setUserName(null);
+               this.setUserPassword(null);
+               this.setUserPasswordRepeat(null);
        }
 
        /**