*/
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;
/**
* @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
/**
* 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
public PizzaCompanyEmployeeWebRequestBean () {
// Call super constructor
super();
-
- // Init list instance
- this.companyEmployees = new LinkedList<>();
}
/**
*/
@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;
}
/**
*/
@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);
+ }
+ }
}
}