]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/business/employee/PizzaCompanyEmployeeWebRequestBean.java
Please rename/cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / business / employee / PizzaCompanyEmployeeWebRequestBean.java
1 /*
2  * Copyright (C) 2017 RRoland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaapplication.beans.business.employee;
18
19 import java.util.Iterator;
20 import java.util.LinkedList;
21 import java.util.List;
22 import javax.annotation.PostConstruct;
23 import javax.cache.Cache;
24 import javax.ejb.EJB;
25 import javax.enterprise.context.RequestScoped;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote;
29 import org.mxchange.jcontactsbusiness.model.employee.Employee;
30 import org.mxchange.pizzaapplication.beans.BasePizzaController;
31
32 /**
33  * A request-scoped bean for general purposes for company employees.
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @Named ("companyEmployeeController")
38 @RequestScoped
39 public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController implements PizzaCompanyEmployeeWebRequestController {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 12_886_968_547_361L;
45
46         /**
47          * EJB for general company employee purposes
48          */
49         @EJB (lookup = "java:global/pizzaservice-ejb/companyEmployee!org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote")
50         private CompanyEmployeeSessionBeanRemote companyEmployeeBean;
51
52         /**
53          * List of all company employees
54          */
55         @Inject
56         @Cached(cacheName = "companyEmployeeCache")
57         private transient Cache<Long, Employee> companyEmployeeCache;
58
59         /**
60          * Default constructor
61          */
62         public PizzaCompanyEmployeeWebRequestBean () {
63                 // Call super constructor
64                 super();
65         }
66
67         /**
68          * Returns a list of all company employees
69          * <p>
70          * @return List of all company employees
71          */
72         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
73         public List<Employee> allCompanyEmployees () {
74                 // Init list
75                 List<Employee> list = new LinkedList<>();
76
77                 // Get iterator
78                 Iterator<Cache.Entry<Long, Employee>> iterator = this.companyEmployeeCache.iterator();
79
80                 // Loop over all
81                 while (iterator.hasNext()) {
82                         // Get next entry
83                         final Cache.Entry<Long, Employee> next = iterator.next();
84
85                         // Add value to list
86                         list.add(next.getValue());
87                 }
88
89                 // Return it
90                 return list;
91         }
92
93         /**
94          * Initialization method
95          */
96         @PostConstruct
97         public void init () {
98                 // Is cache there?
99                 if (!this.companyEmployeeCache.iterator().hasNext()) {
100                         // Get whole list
101                         List<Employee> list = this.companyEmployeeBean.allCompanyEmployees();
102
103                         // Add all
104                         for (final Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
105                                 // Get next element
106                                 final Employee next = iterator.next();
107
108                                 // Add it to cache
109                                 this.companyEmployeeCache.put(next.getEmployeeId(), next);
110                         }
111                 }
112         }
113
114 }