/*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2022 Free Software Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
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;
import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
-import org.mxchange.pizzaapplication.beans.BasePizzaController;
+import org.mxchange.pizzaapplication.beans.BasePizzaBean;
import org.mxchange.pizzaapplication.beans.user.PizzaUserWebRequestController;
import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionController;
*/
@Named ("contactController")
@RequestScoped
-public class PizzaContactWebRequestBean extends BasePizzaController implements PizzaContactWebRequestController {
+public class PizzaContactWebRequestBean extends BasePizzaBean implements PizzaContactWebRequestController {
/**
* Serial number
/**
* EJB for general contact purposes
*/
- @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote")
+ @EJB (lookup = "java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
private ContactSessionBeanRemote contactBean;
/**
* Contact list
*/
@Inject
- @NamedCache (cacheName = "contactsCache", managementEnabled = true)
- private transient Cache<Long, Contact> contactsCache;
+ @NamedCache (cacheName = "contactsCache")
+ private Cache<Long, Contact> contactsCache;
/**
* Country instance
* Email address list
*/
@Inject
- @NamedCache (cacheName = "emailAddressCache", managementEnabled = true)
- private transient Cache<Long, String> emailAddressCache;
+ @NamedCache (cacheName = "emailAddressCache")
+ private Cache<Long, String> emailAddressCache;
/**
* Email address repeated
/**
* 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();
}
// Get user instance
- Contact registeredContact = event.getRegisteredUser().getUserContact();
+ final Contact registeredContact = event.getRegisteredUser().getUserContact();
// Copy all data from registered->user
this.copyContact(registeredContact);
@SuppressWarnings ("ReturnOfCollectionOrArrayField")
public List<Contact> allContacts () {
// Init list
- List<Contact> list = new LinkedList<>();
+ final List<Contact> list = new LinkedList<>();
// Get iterator
- Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
+ final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
// Loop over all
while (iterator.hasNext()) {
*/
@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
}
// Get iterator from list
- Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
+ final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
// "Walk" through all entries
while (iterator.hasNext()) {
// Get next element
- Cache.Entry<Long, Contact> next = iterator.next();
+ final Cache.Entry<Long, Contact> next = iterator.next();
// Is id number the same?
if (Objects.equals(contact.getContactId(), next.getKey())) {