From a4451dceaaac685a4f92779b037a513c70dde1a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 6 Jun 2016 16:02:42 +0200 Subject: [PATCH] Continued with rewrites: - moved selectableContacts to contact controller, makes more sense - fixed template for above - introduced isGenderRequired() to make difference between general and administrative contact controller - added new context parameter for above method - fired event when a new user account has been linked with existing contact data - the other event (added user) is being fired now on else path - No more need for afterAdminAddedContact() in general user controller MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- ...AddressbookAdminContactWebRequestBean.java | 13 ++++ ...sbookAdminContactWebRequestController.java | 8 +++ .../AddressbookContactWebSessionBean.java | 4 ++ .../AddressbookAdminUserWebRequestBean.java | 29 +++++--- .../user/AddressbookUserWebSessionBean.java | 67 +------------------ .../AddressbookUserWebSessionController.java | 8 --- web/admin/user/admin_user_list.xhtml | 2 +- 7 files changed, 47 insertions(+), 84 deletions(-) diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java index 1151723f..6ff9817f 100644 --- a/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java @@ -23,6 +23,7 @@ import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.inject.Any; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -712,6 +713,18 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookContro public void init () { } + @Override + public boolean isGenderRequired () { + // Get context parameter + String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N + + // Is it set? + boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N + + // Return value + return isRequired; + } + /** * Clears this bean */ diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestController.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestController.java index 8d5d0a19..e32ca68b 100644 --- a/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestController.java +++ b/src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestController.java @@ -416,4 +416,12 @@ public interface AddressbookAdminContactWebRequestController extends Serializabl */ String generateCellphoneNumber (final DialableCellphoneNumber cellphoneNumber); + /** + * Checks/returns whether the gender/salutation is required for this + * controller. + *

+ * @return Whether gender is required + */ + boolean isGenderRequired (); + } diff --git a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java index adbda5c4..9dd53aad 100644 --- a/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java @@ -224,6 +224,9 @@ public class AddressbookContactWebSessionBean extends BaseAddressbookController @Override public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) { + // Trace message + /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("contactController.afterAdminAddedContact(): event={0} - CALLED!", event)); //NOI18N + // The event must be valid if (null == event) { // Throw NPE @@ -469,6 +472,7 @@ public class AddressbookContactWebSessionBean extends BaseAddressbookController // Debug message //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress())); + // Don't set null or wrong references if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) { // Now the number must be given diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java index d93dc8a6..13ec9882 100644 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebRequestBean.java @@ -37,6 +37,8 @@ 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.linked.AdminLinkedUserEvent; +import org.mxchange.jusercore.events.user.linked.AdminUserLinkedEvent; import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent; import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent; import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; @@ -100,6 +102,14 @@ public class AddressbookAdminUserWebRequestBean extends BaseAddressbookControlle @Inject private AddressbookUserWebSessionController userController; + /** + * An event fired when the administrator has linked a user with existing + * contact data. + */ + @Inject + @Any + private Event userLinkedEvent; + /** * User name */ @@ -228,32 +238,29 @@ public class AddressbookAdminUserWebRequestBean extends BaseAddressbookControlle // Encrypt password and set it user.setUserEncryptedPassword(UserUtils.encryptPassword(password)); - // Init updated user instance - User updatedUser = null; - try { // Now, that all is set, call EJB if (this.beanHelper.getContact() instanceof Contact) { // Link contact with this user - updatedUser = this.userBean.linkUser(user); + User updatedUser = this.userBean.linkUser(user); + + // Fire event + this.userLinkedEvent.fire(new AdminUserLinkedEvent(updatedUser)); // Remove contact instance this.beanHelper.setContact(null); } else { // Add new contact - updatedUser = this.userBean.addUser(user); + User updatedUser = this.userBean.addUser(user); + + // Fire event + this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser)); } } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { // Throw again throw new FaceletException(ex); } - // Clear helper - this.beanHelper.setContact(null); - - // Fire event - this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser)); - // Clear this bean this.clear(); diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java index 25535651..7d624c3c 100644 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java @@ -40,7 +40,6 @@ import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionContro import org.mxchange.addressbook.beans.register.AddressbookUserRegisterWebSessionController; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; -import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent; import org.mxchange.jusercore.events.login.UserLoggedInEvent; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; @@ -183,27 +182,6 @@ public class AddressbookUserWebSessionBean extends BaseAddressbookController imp } } - @Override - public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) { - // The event must be valid - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getAddedContact() == null) { - // Throw again ... - throw new NullPointerException("event.addedContact is null"); //NOI18N - } else if (event.getAddedContact().getContactId() == null) { - // ... and again - throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N - } else if (event.getAddedContact().getContactId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N - } - - // Call other method - this.selectableContacts.add(event.getAddedContact()); - } - @Override public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) { // Trace message @@ -502,19 +480,19 @@ public class AddressbookUserWebSessionBean extends BaseAddressbookController imp @Override public String doChangePersonalData () { // This method shall only be called if the user is logged-in - if (!this.loginController.isUserLoggedIn()) { + if (!this.userLoginController.isUserLoggedIn()) { // Not logged-in throw new IllegalStateException("User is not logged-in"); //NOI18N } else if (!this.isRequiredChangePersonalDataSet()) { // Not all required fields are set throw new FaceletException("Not all required fields are set."); //NOI18N - } else if (!this.loginController.ifCurrentPasswordMatches()) { + } else if (!this.userLoginController.ifCurrentPasswordMatches()) { // Password not matching throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser())); } // Get user instance - User user = this.loginController.getLoggedInUser(); + User user = this.userLoginController.getLoggedInUser(); // Copy contact data to contact instance this.contactController.updateContactDataFromController(user.getUserContact()); @@ -614,40 +592,6 @@ public class AddressbookUserWebSessionBean extends BaseAddressbookController imp // Initialize user list this.visibleUserList = this.userBean.allPublicUsers(); } - - // Get all users - List allUsers = this.allUsers(); - - // Get all contacts - List allContacts = this.contactBean.getAllContacts(); - - // Get iterator - Iterator iterator = allContacts.iterator(); - - // Loop through it - while (iterator.hasNext()) { - // Get next element - Contact next = iterator.next(); - - // Get iterator - Iterator userIterator = allUsers.iterator(); - - // Loop through all users - while (userIterator.hasNext()) { - // Get user instance - User nextUser = userIterator.next(); - - // Is contact same? - if (Objects.equals(next, nextUser.getUserContact())) { - // Found same - iterator.remove(); - break; - } - } - } - - // Set contact list - this.selectableContacts = allContacts; } @Override @@ -842,11 +786,6 @@ public class AddressbookUserWebSessionBean extends BaseAddressbookController imp return user; } - @Override - public List selectableContacts () { - return Collections.unmodifiableList(this.selectableContacts); - } - /** * Adds user's name to bean's internal list. It also updates the public user * list if the user has decided to have a public account, diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java index 59ca6338..e7813b3c 100644 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionController.java @@ -155,14 +155,6 @@ public interface AddressbookUserWebSessionController extends Serializable { */ User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException; - /** - * Returns a list of all selectable contacts for user creation. Contacts - * from already existing users are excluded in this list. - *

- * @return A list of all selectable contacts - */ - List selectableContacts (); - /** * Creates an instance from all properties *

diff --git a/web/admin/user/admin_user_list.xhtml b/web/admin/user/admin_user_list.xhtml index 745b1fda..fbe3d66f 100644 --- a/web/admin/user/admin_user_list.xhtml +++ b/web/admin/user/admin_user_list.xhtml @@ -105,7 +105,7 @@

- +
-- 2.39.5