]> 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 7d374b892c6ba9bc741dca530a7e365bcf680dd2..79f52d47b02d206e88dcc172ed91f6f643ae3624 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -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;
@@ -54,7 +53,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,7 +64,7 @@ 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
@@ -95,15 +94,15 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
        /**
         * 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
@@ -119,8 +118,8 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
         * 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
@@ -210,7 +209,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 +272,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 +326,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();
@@ -431,7 +432,7 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
                }
 
                // Get user instance
-               Contact registeredContact = event.getRegisteredUser().getUserContact();
+               final Contact registeredContact = event.getRegisteredUser().getUserContact();
 
                // Copy all data from registered->user
                this.copyContact(registeredContact);
@@ -450,10 +451,10 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
        @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()) {
@@ -1025,56 +1026,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 +1128,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
@@ -1307,12 +1320,12 @@ public class PizzaContactWebRequestBean extends BasePizzaController implements P
                }
 
                // 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())) {