From: Roland Häder Date: Tue, 2 Aug 2016 17:00:04 +0000 (+0200) Subject: Continued with fixing: (please cherry-pick) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5971b7cb4bf4949a61fe0d1565fca071720d5ddb;p=pizzaservice-war.git Continued with fixing: (please cherry-pick) - introduced uniqueAddContact() which uniquely adds contact to controller's list(s) which prevents double-listing - also added missing call of above method after user registration - introduced uniqueAddUser() which uniquely adds user to controlleräs list(s) - used above method heavily Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java index 0660eb0b..de484d88 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java @@ -265,9 +265,9 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P this.clear(); // Call other method - this.contactList.add(event.getAddedContact()); + this.uniqueAddContact(event.getAddedContact()); - // Call other method + // Add to selectable contacts this.selectableContacts.add(event.getAddedContact()); } @@ -336,27 +336,8 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N } - // Get iterator from list - Iterator iterator = this.contactList.iterator(); - - // "Walk" through all entries - while (iterator.hasNext()) { - // Get next element - Contact next = iterator.next(); - - // Is id number the same? - if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) { - // Found entry, so remove it and abort - this.contactList.remove(next); - - // Remove also email from list - this.emailAddressList.remove(next.getContactEmailAddress()); - break; - } - } - - // Add contact to list - this.contactList.add(event.getUpdatedContact()); + // Add contact instance only once + this.uniqueAddContact(event.getUpdatedContact()); // Add email address to list this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress()); @@ -385,6 +366,9 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P // Copy all data from registered->user this.copyContact(registeredContact); + // Add contact instance only once + this.uniqueAddContact(registeredContact); + // Add user name and email address this.addUserNameEmailAddress(registeredContact); @@ -409,27 +393,8 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N } - // "Cache" contact instance - Contact contact = event.getConfirmedUser().getUserContact(); - - // Get iterator from list - Iterator iterator = this.contactList.iterator(); - - // "Walk" through all entries - while (iterator.hasNext()) { - // Get next element - Contact next = iterator.next(); - - // Is id number the same? - if (Objects.equals(contact.getContactId(), next.getContactId())) { - // Found entry, so remove it and abort - this.removeContact(next); - break; - } - } - - // Add contact to list - this.contactList.add(contact); + // Add contact instance only once + this.uniqueAddContact(event.getConfirmedUser().getUserContact()); } @Override @@ -1120,4 +1085,43 @@ public class PizzaContactWebSessionBean extends BasePizzaController implements P this.emailAddressList.remove(contact.getContactEmailAddress()); } + /** + * Adds unique instance to contact list. First any existing instance is + * being removed, then the new instance is added. + *

+ * @param contact Contact instance to add uniquely + */ + private void uniqueAddContact (final Contact contact) { + // Is the instance valid? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Get iterator from list + Iterator iterator = this.contactList.iterator(); + + // "Walk" through all entries + while (iterator.hasNext()) { + // Get next element + Contact next = iterator.next(); + + // Is id number the same? + if (Objects.equals(contact.getContactId(), next.getContactId())) { + // Found entry, so remove it and abort + this.removeContact(next); + break; + } + } + + // Add contact to list + this.contactList.add(contact); + } + } diff --git a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java index 097f02e7..c3c2503c 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaUserWebSessionBean.java @@ -191,8 +191,8 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N } - // Add user to local list - this.userList.add(event.getAddedUser()); + // Add user uniquely + this.uniqueAddUser(event.getAddedUser()); // Clear all data this.clear(); @@ -266,8 +266,8 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz // Clear all data this.clear(); - // Add user to local list - this.userList.add(registeredUser); + // Add user uniquely + this.uniqueAddUser(registeredUser); // Add user name this.addUserName(registeredUser); @@ -305,24 +305,8 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N } - // Get iterator from list - Iterator iterator = this.userList.iterator(); - - // "Walk" through all entries - while (iterator.hasNext()) { - // Get next element - User next = iterator.next(); - - // Is id number the same? - if (Objects.equals(event.getConfirmedUser().getUserId(), next.getUserId())) { - // Found entry, so remove it and abort - this.userList.remove(next); - break; - } - } - - // Add contact to list - this.userList.add(event.getConfirmedUser()); + // Add user uniquely + this.uniqueAddUser(event.getConfirmedUser()); } @Override @@ -349,6 +333,7 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz this.copyUser(event.getLoggedInUser()); // Re-initialize list + // @TODO This calls the EJB again, after a user logs in which can cause lots of calls on it. this.visibleUserList = this.userBean.allMemberPublicVisibleUsers(); // Trace message @@ -697,14 +682,14 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz } @Override - public User lookupUserById (final Long userId) throws UserNotFoundException { + public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException { // Parameter must be valid - if (null == userId) { + if (null == emailAddress) { // Throw NPE - throw new NullPointerException("userId is null"); //NOI18N - } else if (userId < 1) { + throw new NullPointerException("emailAddress is null"); //NOI18N + } else if (emailAddress.isEmpty()) { // Not valid - throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N + throw new IllegalArgumentException("emailAddress is empty"); //NOI18N } // Init variable @@ -715,8 +700,17 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz // Get next user User next = iterator.next(); - // Is the user id found? - if (Objects.equals(next.getUserId(), userId)) { + // Contact should be set + if (next.getUserContact() == null) { + // Contact is null + throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N + } else if (next.getUserContact().getContactEmailAddress() == null) { + // Email address should be set + throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N //NOI18N + } + + // Is the email address found? + if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) { // Copy to other variable user = next; break; @@ -726,7 +720,7 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz // Is it still null? if (null == user) { // Not visible for the current user - throw new UserNotFoundException(userId); + throw new UserEmailAddressNotFoundException(emailAddress); } // Return it @@ -734,14 +728,14 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz } @Override - public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException { + public User lookupUserById (final Long userId) throws UserNotFoundException { // Parameter must be valid - if (null == emailAddress) { + if (null == userId) { // Throw NPE - throw new NullPointerException("emailAddress is null"); //NOI18N - } else if (emailAddress.isEmpty()) { + throw new NullPointerException("userId is null"); //NOI18N + } else if (userId < 1) { // Not valid - throw new IllegalArgumentException("emailAddress is empty"); //NOI18N + throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N } // Init variable @@ -752,17 +746,8 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz // Get next user User next = iterator.next(); - // Contact should be set - if (next.getUserContact() == null) { - // Contact is null - throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N - } else if (next.getUserContact().getContactEmailAddress() == null) { - // Email address should be set - throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N //NOI18N - } - - // Is the email address found? - if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) { + // Is the user id found? + if (Objects.equals(next.getUserId(), userId)) { // Copy to other variable user = next; break; @@ -772,7 +757,7 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz // Is it still null? if (null == user) { // Not visible for the current user - throw new UserEmailAddressNotFoundException(emailAddress); + throw new UserNotFoundException(userId); } // Return it @@ -835,6 +820,42 @@ public class PizzaUserWebSessionBean extends BasePizzaController implements Pizz this.setUserProfileMode(user.getUserProfileMode()); } + /** + * Uniquely adds given user instance to user list. First an existing + * instance will be removed, then the given instance is being added. + *

+ * @param user User instance to add + */ + private void uniqueAddUser (final User user) { + // Make sure the instance is valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw again ... + throw new NullPointerException("user.userContact is null"); //NOI18N + } + + // Get iterator from list + Iterator iterator = this.userList.iterator(); + + // "Walk" through all entries + while (iterator.hasNext()) { + // Get next element + User next = iterator.next(); + + // Is id number the same? + if (Objects.equals(user.getUserId(), next.getUserId())) { + // Found entry, so remove it and abort + this.userList.remove(next); + break; + } + } + + // Add contact to list + this.userList.add(user); + } + /** * Updates list with given user instance *