import fish.payara.cdi.jsr107.impl.NamedCache;
import java.text.MessageFormat;
-import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
/**
* A list of all selectable contacts
*/
- private List<Contact> selectableContacts;
+ @Inject
+ @NamedCache (cacheName = "selectableContactsCache")
+ private Cache<Long, Contact> selectableContactsCache;
/**
* Street
this.uniqueAddContact(event.getAddedContact());
// Add to selectable contacts
- this.selectableContacts.add(event.getAddedContact());
+ this.selectableContactsCache.put(event.getAddedContact().getContactId(), event.getAddedContact());
}
/**
}
// Remove contact from list available contacts list
- this.selectableContacts.remove(event.getLinkedUser().getUserContact());
+ this.selectableContactsCache.remove(event.getLinkedUser().getUserContact().getContactId());
// Clear all data
this.clear();
*/
@PostConstruct
public void init () {
- // Get all contacts
- final List<Contact> selectable = new LinkedList<>();
-
// Is cache there?
if (!this.contactsCache.iterator().hasNext()) {
// Get whole list
- List<Contact> list = this.contactBean.allContacts();
+ final List<Contact> contacts = this.contactBean.allContacts();
// Add all
- for (final Iterator<Contact> iterator = list.iterator(); iterator.hasNext();) {
- // Get next element
- final Contact next = iterator.next();
-
+ for (final Contact contact : contacts) {
// Add it to cache
- this.contactsCache.put(next.getContactId(), next);
- this.emailAddressCache.put(next.getContactId(), next.getContactEmailAddress());
- selectable.add(next);
+ this.contactsCache.put(contact.getContactId(), contact);
+ this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
}
+ } else if (this.selectableContactsCache.iterator().hasNext()) {
+ // Has already entries, avoid executing below code
+ return;
}
// Get all users
- List<User> allUsers = this.userController.allUsers();
+ final List<User> allUsers = this.userController.allUsers();
- // Get iterator
- Iterator<Contact> iterator = selectable.iterator();
+ // Get iterator from contacts cache
+ final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
- // Loop through it
+ // Loop through all contacts
while (iterator.hasNext()) {
// Get next element
- Contact next = iterator.next();
-
- // Get iterator
- Iterator<User> userIterator = allUsers.iterator();
+ final Cache.Entry<Long, Contact> next = iterator.next();
- // Loop through all users
- while (userIterator.hasNext()) {
- // Get user instance
- User nextUser = userIterator.next();
+ // Default is not found
+ boolean isFound = false;
- // Is contact same?
- if (Objects.equals(next, nextUser.getUserContact())) {
- // Found same
- iterator.remove();
+ // User list is not empty, check each entry, if contact is found
+ for (final User user : allUsers) {
+ // Is the contact the same?
+ if (Objects.equals(user.getUserContact(), next.getValue())) {
+ // Found one
+ isFound = true;
break;
}
}
- }
- // Set contact list
- this.selectableContacts = selectable;
+ // Is contact not found?
+ if (!isFound) {
+ // Add it as selectable
+ this.selectableContactsCache.put(next.getKey(), next.getValue());
+ }
+ }
}
@Override
* @return A list of all selectable contacts
*/
public List<Contact> selectableContacts () {
- return Collections.unmodifiableList(this.selectableContacts);
+ // Init list
+ final List<Contact> selectableContacts = new LinkedList<>();
+
+ // Get iterator from cache
+ final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
+
+ // Loop through all contacts
+ while (iterator.hasNext()) {
+ // Get next element
+ final Cache.Entry<Long, Contact> next = iterator.next();
+
+ // Add entry's value to list
+ selectableContacts.add(next.getValue());
+ }
+
+ // Return list
+ return selectableContacts;
}
@Override