]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebSessionBean.java
Added check for parameter, to make sure only valid can pass
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / PizzaAdminUserWebSessionBean.java
index 4f33d8446654935f795ed28fe8ca1be026c34230..e82dd71603ac1fa469e555423129650ec7c438c7 100644 (file)
@@ -23,6 +23,8 @@ import java.util.List;
 import java.util.Objects;
 import javax.annotation.PostConstruct;
 import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Any;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -40,6 +42,8 @@ import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
+import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
+import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNotFoundException;
@@ -49,6 +53,7 @@ import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserUtils;
 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
+import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
 /**
  * A user bean (controller)
@@ -64,7 +69,13 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
         */
        private static final long serialVersionUID = 542_145_347_916L;
 
-       /////////////////////// Properties /////////////////////
+       /**
+        * An event fired when the administrator has added a new user
+        */
+       @Inject
+       @Any
+       private Event<AdminAddedUserEvent> addedUserEvent;
+
        /**
         * Birth day
         */
@@ -161,9 +172,10 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
        private final UserSessionBeanRemote userBean;
 
        /**
-        * User id
+        * Regular user controller
         */
-       private Long userId;
+       @Inject
+       private PizzaUserWebSessionController userController;
 
        /**
         * A list of all user profiles
@@ -185,22 +197,11 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
         */
        private String userPasswordRepeat;
 
-       /**
-        * Whether the user wants a public profile
-        */
-       private ProfileMode userProfileMode;
-
        /**
         * ZIP code
         */
        private Integer zipCode;
 
-       /**
-        * Regular user controller
-        */
-       @Inject
-       private PizzaUserWebSessionController userController;
-
        /**
         * Default constructor
         */
@@ -224,9 +225,12 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
        @Override
        public void addUser () {
                // Create new user instance
-               User user = new LoginUser();
-               user.setUserName(this.getUserName());
-               user.setUserProfileMode(this.getUserProfileMode());
+               User localUser = new LoginUser();
+
+               // Set user name, CONFIRMED and INVISIBLE
+               localUser.setUserName(this.getUserName());
+               localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
+               localUser.setUserProfileMode(ProfileMode.INVISIBLE);
 
                // Generate phone number
                DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
@@ -303,44 +307,56 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
                contact.setContactComment(this.getComment());
 
                // Set contact in user
-               user.setUserContact(contact);
+               localUser.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(user)) {
+               if (this.userController.isUserNameRegistered(localUser)) {
                        // User name is already used
-                       throw new FaceletException(new UserNameAlreadyRegisteredException(user));
-               } else if (this.userController.isEmailAddressRegistered(user)) {
+                       throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
+               } else if (this.userController.isEmailAddressRegistered(localUser)) {
                        // Email address is already used
-                       throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
-               } else if ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty())) {
+                       throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
+               } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
                        // Empty password entered, then generate one
                        password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
                } else if (!this.isSamePasswordEntered()) {
                        // Both passwords don't match
-                       throw new FaceletException(new UserPasswordMismatchException(user));
+                       throw new FaceletException(new UserPasswordMismatchException(localUser));
                } else {
                        // Both match, so get it from this bean
                        password = this.getUserPassword();
                }
 
                // The password should not be null and at least 5 characters long
-               assert (password != null) : "password is null";
-               assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough.";
+               assert (password != null) : "password is null"; //NOI18N
+               assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
 
                // Encrypt password and set it
-               user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
+               localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
+
+               // Init updated user instance
+               User updatedUser = null;
 
                try {
                        // Now, that all is set, call EJB
-                       this.userBean.addUser(user);
+                       updatedUser = this.userBean.addUser(localUser);
                } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
                        // Throw again
                        throw new FaceletException(ex);
                }
+
+               // Fire event
+               this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
+
+               // Add user to local list
+               this.userList.add(updatedUser);
+
+               // Clear all
+               this.clear();
        }
 
        @Override
@@ -529,16 +545,6 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
                this.street = street;
        }
 
-       @Override
-       public Long getUserId () {
-               return this.userId;
-       }
-
-       @Override
-       public void setUserId (final Long userId) {
-               this.userId = userId;
-       }
-
        @Override
        public String getUserName () {
                return this.userName;
@@ -569,16 +575,6 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
                this.userPasswordRepeat = userPasswordRepeat;
        }
 
-       @Override
-       public ProfileMode getUserProfileMode () {
-               return this.userProfileMode;
-       }
-
-       @Override
-       public void setUserProfileMode (final ProfileMode userProfileMode) {
-               this.userProfileMode = userProfileMode;
-       }
-
        @Override
        public Integer getZipCode () {
                return this.zipCode;
@@ -603,17 +599,16 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
                this.userList = this.userBean.allUsers();
        }
 
-       /**
-        * Checks if same password is entered and that they are not empty.
-        * <p>
-        * @return Whether the same password was entered
-        */
-       private boolean isSamePasswordEntered () {
-               return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
-       }
-
        @Override
        public User lookupUserById (final Long userId) throws UserNotFoundException {
+               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;
 
@@ -640,4 +635,42 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon
                return user;
        }
 
+       /**
+        * Clears this bean
+        */
+       private void clear () {
+               // Clear all
+               this.setBirthday(null);
+               this.setCellphoneCarrier(null);
+               this.setCellphoneNumber(null);
+               this.setCity(null);
+               this.setComment(null);
+               this.setCountry(null);
+               this.setEmailAddress(null);
+               this.setFamilyName(null);
+               this.setFaxAreaCode(null);
+               this.setFaxCountry(null);
+               this.setFaxNumber(null);
+               this.setFirstName(null);
+               this.setGender(null);
+               this.setHouseNumber(null);
+               this.setPhoneAreaCode(null);
+               this.setPhoneCountry(null);
+               this.setPhoneNumber(null);
+               this.setStreet(null);
+               this.setUserName(null);
+               this.setUserPassword(null);
+               this.setUserPasswordRepeat(null);
+               this.setZipCode(null);
+       }
+
+       /**
+        * Checks if same password is entered and that they are not empty.
+        * <p>
+        * @return Whether the same password was entered
+        */
+       private boolean isSamePasswordEntered () {
+               return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
+       }
+
 }