From a243487ee9f58c877cf393d2d0b5f490771955b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 2 Aug 2016 19:00:04 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20fixing:=20(please=20cherry-pi?= =?utf8?q?ck)=20-=20introduced=20uniqueAddContact()=20which=20uniquely=20a?= =?utf8?q?dds=20contact=20to=20controller's=20list(s)=20which=20prevents?= =?utf8?q?=20double-listing=20-=20also=20added=20missing=20call=20of=20abo?= =?utf8?q?ve=20method=20after=20user=20registration=20-=20introduced=20uni?= =?utf8?q?queAddUser()=20which=20uniquely=20adds=20user=20to=20controller?= =?utf8?q?=C3=A4s=20list(s)=20-=20used=20above=20method=20heavily?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../contact/JobsContactWebSessionBean.java | 93 ++++++++++--------- .../beans/user/JobsUserWebSessionBean.java | 65 ++++++++----- 2 files changed, 93 insertions(+), 65 deletions(-) diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java index 840af12e..34282bfd 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java @@ -243,7 +243,10 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job this.clear(); // Call other method - this.contactList.add(event.getAddedContact()); + this.uniqueAddContact(event.getAddedContact()); + + // Add to selectable contacts + this.selectableContacts.add(event.getAddedContact()); } @Override @@ -293,27 +296,8 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job 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()); @@ -348,6 +332,9 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job // 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); @@ -378,27 +365,8 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job 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.contactList.remove(next); - break; - } - } - - // Add contact to list - this.contactList.add(contact); + // Add contact instance only once + this.uniqueAddContact(event.getConfirmedUser().getUserContact()); } @Override @@ -1030,4 +998,43 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job 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/jjobs/beans/user/JobsUserWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java index ee602c26..124b9ea2 100644 --- a/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java @@ -224,8 +224,8 @@ public class JobsUserWebSessionBean extends BaseJobsController implements JobsUs 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(); @@ -299,8 +299,8 @@ public class JobsUserWebSessionBean extends BaseJobsController implements JobsUs // 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); @@ -338,24 +338,8 @@ public class JobsUserWebSessionBean extends BaseJobsController implements JobsUs 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 @@ -382,6 +366,7 @@ public class JobsUserWebSessionBean extends BaseJobsController implements JobsUs 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 @@ -904,6 +889,42 @@ public class JobsUserWebSessionBean extends BaseJobsController implements JobsUs 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 *

-- 2.39.5