]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebRequestBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / PizzaContactWebRequestBean.java
index 9be93736e86212c3d9be30dcbd4edcb055313390..8cb9a521160d019c75142f86a2dc1952aaa0e7c6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -18,7 +18,6 @@ package org.mxchange.pizzaapplication.beans.contact;
 
 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;
@@ -36,16 +35,13 @@ import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEven
 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;
@@ -54,7 +50,7 @@ import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAc
 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;
 
@@ -65,12 +61,12 @@ import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionCo
  */
 @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
@@ -95,7 +91,7 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
        /**
         * EJB for general contact purposes
         */
-       @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
+       @EJB (lookup = "java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
        private ContactSessionBeanRemote contactBean;
 
        /**
@@ -210,7 +206,9 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
        /**
         * A list of all selectable contacts
         */
-       private List<Contact> selectableContacts;
+       @Inject
+       @NamedCache (cacheName = "selectableContactsCache")
+       private Cache<Long, Contact> selectableContactsCache;
 
        /**
         * Street
@@ -271,7 +269,7 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
                this.uniqueAddContact(event.getAddedContact());
 
                // Add to selectable contacts
-               this.selectableContacts.add(event.getAddedContact());
+               this.selectableContactsCache.put(event.getAddedContact().getContactId(), event.getAddedContact());
        }
 
        /**
@@ -325,7 +323,7 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
                }
 
                // 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();
@@ -483,46 +481,14 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
                        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)) {
@@ -575,7 +541,7 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
                        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
                }
 
@@ -1025,56 +991,52 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
         */
        @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
@@ -1131,7 +1093,23 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
         * @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