/*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2024 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.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
import org.mxchange.jcontacts.model.contact.Contact;
import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
-import org.mxchange.jcontacts.model.contact.ContactUtils;
import org.mxchange.jcontacts.model.contact.UserContact;
import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
+import org.mxchange.jcontacts.model.utils.ContactUtils;
import org.mxchange.jcountry.model.data.Country;
import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
-import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
-import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
-import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
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
*/
- private static final long serialVersionUID = 542_145_347_916L;
+ private static final long serialVersionUID = 542_145_352_001L;
/**
* Academic academicTitle
/**
* 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;
/**
/**
* 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()) {
throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
}
- // Required personal data must be set
- assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
-
- // Generate phone number
- DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
- DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
- DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
-
// Create new contact
- Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
- contact.setContactStreet(this.getStreet());
- contact.setContactHouseNumber(this.getHouseNumber());
- contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
- contact.setContactZipCode(this.getZipCode());
- contact.setContactCity(this.getCity());
- contact.setContactCountry(this.getCountry());
- contact.setContactEmailAddress(this.getEmailAddress());
- contact.setContactBirthday(this.getBirthday());
- contact.setContactComment(this.getComment());
-
- // Don't set null or wrong references
- if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
- // Now the number must be given
- if (phone.getPhoneAreaCode() == null) {
- // Is null
- throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
- } else if (phone.getPhoneAreaCode() < 1) {
- // Abort here
- throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
- } else if (phone.getPhoneNumber() == null) {
- // Is null
- throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
- } else if (phone.getPhoneNumber() < 1) {
- // Abort here
- throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
- }
-
- // Set phone number
- contact.setContactLandLineNumber(phone);
- }
+ final Contact contact = new UserContact(
+ this.getPersonalTitle(),
+ this.getFirstName(),
+ this.getFamilyName(),
+ this.getCountry(),
+ Boolean.FALSE
+ );
// Don't set null or wrong references
if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
throw new FaceletException("Not all required fields are set."); //NOI18N
} else if (!this.userLoginController.ifCurrentPasswordMatches()) {
// Password not matching
- this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
+ this.showFacesException("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()), FacesMessage.SEVERITY_ERROR); //NOI18N
return ""; //NOI18N
}
*/
@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())) {