]> git.mxchange.org Git - jfinancials-war.git/commitdiff
Contiuned with editing user data/rewrites:
authorRoland Häder <roland@mxchange.org>
Thu, 21 Apr 2016 11:55:57 +0000 (13:55 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 21 Apr 2016 18:30:15 +0000 (20:30 +0200)
- implemented editUserData() for when admin updates user data
- implemented listener for when a user updates personal data
- added private method updateList() for updating admin's user list
- updateUserPersonalData() now returns updated user instance (detached!)
- fired event when user updates personal data

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java
src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestController.java
src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java
web/WEB-INF/templates/admin/user/admin_form_user_data.tpl

index 07612f5bd99e25ff46ea24b00afc25d383739bf0..b6e537ecd3ecdbef80a40d502608b266ece7dbae 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;
@@ -67,12 +68,25 @@ public class AddressbookAdminUserWebRequestBean implements AddressbookAdminUserW
        @Any
        private Event<AdminAddedUserEvent> addedUserEvent;
 
+       /**
+        * Admin helper instance
+        */
+       @Inject
+       private RateCalcAdminWebRequestController adminHelper;
+
        /**
         * Regular contact controller
         */
        @Inject
        private AddressbookContactWebSessionController contactController;
 
+       /**
+        * An event fired when the administrator has updated a new user
+        */
+       @Inject
+       @Any
+       private Event<AdminUpdatedUserDataEvent> updatedUserDataEvent;
+
        /**
         * Remote user bean
         */
@@ -187,6 +201,27 @@ public class AddressbookAdminUserWebRequestBean implements AddressbookAdminUserW
                this.contactController.clear();
        }
 
+       @Override
+       public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
+               // Check parameter
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null");
+               } else if (event.getUpdatedUser() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.updatedUser is null");
+               } else if (event.getUpdatedUser().getUserId() == null) {
+                       // ... and again
+                       throw new NullPointerException("event.updatedUser.userId is null");
+               } else if (event.getUpdatedUser().getUserId() < 1) {
+                       // Invalid value
+                       throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId()));
+               }
+
+               // All fine, so update list
+               this.updateList(event.getUpdatedUser());
+       }
+
        @Override
        public List<User> allUsers () {
                // Return it
@@ -195,7 +230,63 @@ public class AddressbookAdminUserWebRequestBean implements AddressbookAdminUserW
 
        @Override
        public void editUserData () {
-               throw new UnsupportedOperationException("Not supported yet."); //NOI18N
+               // 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");
+               } else if (user.getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("adminHelper.user.userId is null");
+               } 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");
+               } else if (this.getUserName().isEmpty()) {
+                       // Not all required fields are set
+                       throw new IllegalArgumentException("this.userName is empty");
+               } 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");
+               } 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.");
+                       }
+
+                       // 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));
        }
 
        @Override
@@ -288,4 +379,42 @@ public class AddressbookAdminUserWebRequestBean implements AddressbookAdminUserW
                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");
+               } else if (user.getUserId() == null) {
+                       // ... again NPE
+                       throw new NullPointerException("user.userId is null");
+               } else if (user.getUserId() < 1) {
+                       // Invalid id
+                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId()));
+               }
+
+               // 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);
+       }
+
 }
index c697530c0be233d9291a8b5cdb3564d24ec3310f..5564dd69d190247c948d3105689f06e514a96ec6 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.addressbook.beans.user;
 
 import java.io.Serializable;
 import java.util.List;
+import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
 import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.model.user.User;
 
@@ -28,6 +29,13 @@ import org.mxchange.jusercore.model.user.User;
  */
 public interface AddressbookAdminUserWebRequestController extends Serializable {
 
+       /**
+        * Listens to fired event when user updated personal data
+        * <p>
+        * @param event Event being fired
+        */
+       void afterUserUpdatedPersonalData (final UpdatedUserPersonalDataEvent event);
+
        /**
         * Tries to lookup user by given id number. If the user is not found or the
         * account status is not CONFIRMED proper exceptions are thrown.
index 402c21e4ec89c223f4a191c2a5fd9265ff70a769..a51c7ab83a5d5d37b3cddc4ef90c7363e6e75213 100644 (file)
@@ -23,7 +23,9 @@ 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.event.Observes;
+import javax.enterprise.inject.Any;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -38,6 +40,8 @@ import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
+import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
+import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
 import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
 import org.mxchange.jusercore.model.user.LoginUser;
@@ -71,6 +75,13 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC
        @Inject
        private AddressbookUserLoginWebSessionController loginController;
 
+       /**
+        * Event being fired when user updated personal data
+        */
+       @Inject
+       @Any
+       private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
+
        /**
         * Remote user bean
         */
@@ -271,7 +282,10 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC
                user.setUserProfileMode(this.getUserProfileMode());
 
                // Send it to the EJB
-               this.userBean.updateUserPersonalData(user);
+               User updatedUser = this.userBean.updateUserPersonalData(user);
+
+               // Fire event
+               this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
 
                // All fine
                return "user_data_saved"; //NOI18N
@@ -348,6 +362,7 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC
        @Override
        public boolean isRequiredChangePersonalDataSet () {
                return ((this.getUserProfileMode() != null) &&
+                               (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
                                (this.contactController.isRequiredChangePersonalDataSet()));
        }
 
index ad6c8db7a12af1b630e9221e9381fd7393812bf2..1b0c1cb82d78017aacbd84f4feb551798515dce7 100644 (file)
                                                        <h:outputText value="#{msg.ADMIN_USER_DATA_PASSWORD_ADD_NOTICE}" rendered="#{mode == 'add'}" />
                                                </li>
 
-                                               <li>
-                                                       <h:outputText value="#{msg.ADMIN_USER_DATA_EMAIL_ADDRESS_NOTICE}" rendered="#{mode == 'add'}" />
-                                               </li>
+                                               <ui:fragment rendered="#{mode == 'add'}">
+                                                       <li>
+                                                               <h:outputText value="#{msg.ADMIN_USER_DATA_EMAIL_ADDRESS_NOTICE}" />
+                                                       </li>
+                                               </ui:fragment>
                                        </ul>
                                </div>
                        </fieldset>