]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/business/employee/PizzaCompanyEmployeeWebRequestBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / business / employee / PizzaCompanyEmployeeWebRequestBean.java
index a980cb6bad42865d7bdc1e3a58d47862fb7cc497..f129bbdd042ee69df9a60863d119a75ff2a6c9e2 100644 (file)
  */
 package org.mxchange.pizzaapplication.beans.business.employee;
 
+import fish.payara.cdi.jsr107.impl.NamedCache;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import javax.annotation.PostConstruct;
+import javax.cache.Cache;
 import javax.ejb.EJB;
-import javax.enterprise.context.SessionScoped;
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
 import javax.inject.Named;
-import org.mxchange.jcontactsbusiness.employee.CompanyEmployeeSessionBeanRemote;
-import org.mxchange.jcontactsbusiness.employee.Employee;
+import org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote;
+import org.mxchange.jcontactsbusiness.model.employee.Employee;
 import org.mxchange.pizzaapplication.beans.BasePizzaController;
 
 /**
@@ -32,8 +36,8 @@ import org.mxchange.pizzaapplication.beans.BasePizzaController;
  * @author Roland Häder<roland@mxchange.org>
  */
 @Named ("companyEmployeeController")
-@SessionScoped
-public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController implements PizzaCompanyEmployeeWebSessionController {
+@RequestScoped
+public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController implements PizzaCompanyEmployeeWebRequestController {
 
        /**
         * Serial number
@@ -43,13 +47,15 @@ public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController impl
        /**
         * EJB for general company employee purposes
         */
-       @EJB (lookup = "java:global/pizzaservice-ejb/companyEmployee!org.mxchange.jcontactsbusiness.employee.CompanyEmployeeSessionBeanRemote")
+       @EJB (lookup = "java:global/pizzaservice-ejb/companyEmployee!org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote")
        private CompanyEmployeeSessionBeanRemote companyEmployeeBean;
 
        /**
         * List of all company employees
         */
-       private final List<Employee> companyEmployees;
+       @Inject
+       @NamedCache (cacheName = "companyEmployeeCache")
+       private Cache<Long, Employee> companyEmployeeCache;
 
        /**
         * Default constructor
@@ -57,9 +63,6 @@ public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController impl
        public PizzaCompanyEmployeeWebRequestBean () {
                // Call super constructor
                super();
-
-               // Init list instance
-               this.companyEmployees = new LinkedList<>();
        }
 
        /**
@@ -69,7 +72,23 @@ public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController impl
         */
        @SuppressWarnings ("ReturnOfCollectionOrArrayField")
        public List<Employee> allCompanyEmployees () {
-               return this.companyEmployees;
+               // Init list
+               List<Employee> list = new LinkedList<>();
+
+               // Get iterator
+               Iterator<Cache.Entry<Long, Employee>> iterator = this.companyEmployeeCache.iterator();
+
+               // Loop over all
+               while (iterator.hasNext()) {
+                       // Get next entry
+                       final Cache.Entry<Long, Employee> next = iterator.next();
+
+                       // Add value to list
+                       list.add(next.getValue());
+               }
+
+               // Return it
+               return list;
        }
 
        /**
@@ -77,11 +96,20 @@ public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController impl
         */
        @PostConstruct
        public void init () {
-               // Get all entries from remote bean
-               List<Employee> employees = this.companyEmployeeBean.allCompanyEmployees();
+               // Is cache there?
+               if (!this.companyEmployeeCache.iterator().hasNext()) {
+                       // Get whole list
+                       List<Employee> list = this.companyEmployeeBean.allCompanyEmployees();
+
+                       // Add all
+                       for (final Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
+                               // Get next element
+                               final Employee next = iterator.next();
 
-               // Copy it to main list
-               this.companyEmployees.addAll(employees);
+                               // Add it to cache
+                               this.companyEmployeeCache.put(next.getEmployeeId(), next);
+                       }
+               }
        }
 
 }