]> git.mxchange.org Git - pizzaservice-war.git/blob
f7ec105acffb9367742883b1522795f53fddaa2f
[pizzaservice-war.git] /
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 fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
23 import javax.annotation.PostConstruct;
24 import javax.cache.Cache;
25 import javax.ejb.EJB;
26 import javax.enterprise.context.RequestScoped;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote;
30 import org.mxchange.jcontactsbusiness.model.employee.Employee;
31 import org.mxchange.pizzaapplication.beans.BasePizzaController;
32
33 /**
34  * A request-scoped bean for general purposes for company employees.
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Named ("companyEmployeeController")
39 @RequestScoped
40 public class PizzaCompanyEmployeeWebRequestBean extends BasePizzaController implements PizzaCompanyEmployeeWebRequestController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 12_886_968_547_361L;
46
47         /**
48          * EJB for general company employee purposes
49          */
50         @EJB (lookup = "java:global/pizzaservice-ejb/companyEmployee!org.mxchange.jcontactsbusiness.model.employee.CompanyEmployeeSessionBeanRemote")
51         private CompanyEmployeeSessionBeanRemote companyEmployeeBean;
52
53         /**
54          * List of all company employees
55          */
56         @Inject
57         @NamedCache (cacheName = "companyEmployeeCache", managementEnabled = true)
58         private transient Cache<Long, Employee> companyEmployeeCache;
59
60         /**
61          * Default constructor
62          */
63         public PizzaCompanyEmployeeWebRequestBean () {
64                 // Call super constructor
65                 super();
66         }
67
68         /**
69          * Returns a list of all company employees
70          * <p>
71          * @return List of all company employees
72          */
73         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
74         public List<Employee> allCompanyEmployees () {
75                 // Init list
76                 List<Employee> list = new LinkedList<>();
77
78                 // Get iterator
79                 Iterator<Cache.Entry<Long, Employee>> iterator = this.companyEmployeeCache.iterator();
80
81                 // Loop over all
82                 while (iterator.hasNext()) {
83                         // Get next entry
84                         final Cache.Entry<Long, Employee> next = iterator.next();
85
86                         // Add value to list
87                         list.add(next.getValue());
88                 }
89
90                 // Return it
91                 return list;
92         }
93
94         /**
95          * Initialization method
96          */
97         @PostConstruct
98         public void init () {
99                 // Is cache there?
100                 if (!this.companyEmployeeCache.iterator().hasNext()) {
101                         // Get whole list
102                         List<Employee> list = this.companyEmployeeBean.allCompanyEmployees();
103
104                         // Add all
105                         for (final Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
106                                 // Get next element
107                                 final Employee next = iterator.next();
108
109                                 // Add it to cache
110                                 this.companyEmployeeCache.put(next.getEmployeeId(), next);
111                         }
112                 }
113         }
114
115 }