]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebRequestBean.java
Added redirect outcome + updated jar(s)
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / PizzaAdminUserWebRequestBean.java
index 7f11e8b866590f422a67f69e2b777faba6a09edd..d72e1887fb2a52eb364f77af2d6cce67b1325b66 100644 (file)
@@ -24,6 +24,7 @@ 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;
@@ -32,9 +33,12 @@ import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
+import org.mxchange.jusercore.container.login.UserLoginContainer;
 import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
 import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
+import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
+import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
+import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNotFoundException;
@@ -45,6 +49,8 @@ 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;
+import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
+import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
 
 /**
  * A user bean (controller)
@@ -67,12 +73,25 @@ public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestCon
        @Any
        private Event<AdminAddedUserEvent> addedUserEvent;
 
+       /**
+        * Admin helper instance
+        */
+       @Inject
+       private PizzaAdminWebRequestController adminHelper;
+
        /**
         * Regular contact controller
         */
        @Inject
        private PizzaContactWebSessionController contactController;
 
+       /**
+        * An event fired when the administrator has updated a new user
+        */
+       @Inject
+       @Any
+       private Event<AdminUpdatedUserDataEvent> updatedUserDataEvent;
+
        /**
         * Remote user bean
         */
@@ -122,7 +141,7 @@ public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestCon
        }
 
        @Override
-       public void addUser () {
+       public String addUser () {
                // Create new user instance
                User localUser = new LoginUser();
 
@@ -183,11 +202,32 @@ public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestCon
                // Add user to local list
                this.userList.add(updatedUser);
 
-               // Clear all
-               this.clear();
-
                // Clear contact instance
                this.contactController.clear();
+
+               // Return to user list (for now)
+               return "admin_list_user"; //NOI18N
+       }
+
+       @Override
+       public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
+               // Check parameter
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getUpdatedUser() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.updatedUser is null"); //NOI18N
+               } else if (event.getUpdatedUser().getUserId() == null) {
+                       // ... and again
+                       throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
+               } else if (event.getUpdatedUser().getUserId() < 1) {
+                       // Invalid value
+                       throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
+               }
+
+               // All fine, so update list
+               this.updateList(event.getUpdatedUser());
        }
 
        @Override
@@ -196,6 +236,70 @@ public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestCon
                return Collections.unmodifiableList(this.userList);
        }
 
+       @Override
+       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()));
+               } 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.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);
+
+               // Update list
+               this.updateList(updatedUser);
+
+               // Fire event
+               this.updatedUserDataEvent.fire(new AdminUserDataUpdatedEvent(updatedUser));
+
+               // Return to user list (for now)
+               return "admin_list_user"; //NOI18N
+       }
+
        @Override
        public String getUserName () {
                return this.userName;
@@ -277,16 +381,6 @@ public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestCon
                return user;
        }
 
-       /**
-        * Clears this bean
-        */
-       private void clear () {
-               // Clear all
-               this.setUserName(null);
-               this.setUserPassword(null);
-               this.setUserPasswordRepeat(null);
-       }
-
        /**
         * Checks if same password is entered and that they are not empty.
         * <p>
@@ -296,4 +390,42 @@ public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestCon
                return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
        }
 
+       /**
+        * Updates list with given user instance
+        * <p>
+        * @param user User instance
+        */
+       private void updateList (final User user) {
+               // The user should be valid
+               if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // ... again NPE
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Invalid id
+                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
+               }
+
+               // Get iterator
+               Iterator<User> iterator = this.userList.iterator();
+
+               // Look whole list
+               while (iterator.hasNext()) {
+                       // Get next element
+                       User next = iterator.next();
+
+                       // Is the same user id?
+                       if (Objects.equals(user.getUserId(), next.getUserId())) {
+                               // Found it, so remove it
+                               this.userList.remove(next);
+                               break;
+                       }
+               }
+
+               // Re-add item
+               this.userList.add(user);
+       }
+
 }