]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/business/employee/list/FinancialsEmployeeListWebViewBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / business / employee / list / FinancialsEmployeeListWebViewBean.java
1 /*
2  * Copyright (C) 2017 - 2022 Free Software Foundation
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.jfinancials.beans.business.employee.list;
18
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
27 import javax.ejb.EJB;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.ViewScoped;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontactsbusiness.events.employee.added.ObservableEmployeeAddedEvent;
33 import org.mxchange.jcontactsbusiness.exceptions.employee.EmployeeNotFoundException;
34 import org.mxchange.jcontactsbusiness.model.employee.Employable;
35 import org.mxchange.jcontactsbusiness.model.employee.EmployeeSessionBeanRemote;
36 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
37
38 /**
39  * A view-scoped bean for listing purposes for e.g. administrative employee
40  * purposes.
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("employeeListController")
45 @ViewScoped
46 public class FinancialsEmployeeListWebViewBean extends BaseFinancialsBean implements FinancialsEmployeeListWebViewController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 12_886_968_547_362L;
52
53         /**
54          * List of all employees
55          */
56         private final List<Employable> allEmployees;
57
58         /**
59          * EJB for general company employee purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/employee!org.mxchange.jcontactsbusiness.model.employee.EmployeeSessionBeanRemote")
62         private EmployeeSessionBeanRemote employeeBean;
63
64         /**
65          * List of all company employees
66          */
67         @Inject
68         @NamedCache (cacheName = "companyEmployeeCache")
69         private transient Cache<Long, Employable> employeeCache;
70
71         /**
72          * A list of filtered employees
73          */
74         private List<Employable> filteredEmployees;
75
76         /**
77          * Currently selected employee
78          */
79         private Employable selectedEmployee;
80
81         /**
82          * Default constructor
83          */
84         public FinancialsEmployeeListWebViewBean () {
85                 // Call super constructor
86                 super();
87
88                 // Init list
89                 this.allEmployees = new LinkedList<>();
90         }
91
92         /**
93          * Observes events being fired when an employee has been added
94          * <p>
95          * @param event Event being fired
96          */
97         public void afterEmployeeAddedEvent (@Observes final ObservableEmployeeAddedEvent event) {
98                 // Validate parameter
99                 if (null == event) {
100                         // Throw NPE
101                         throw new NullPointerException("event is null"); //NOI18N
102                 } else if (event.getEmployee() == null) {
103                         // Throw it again
104                         throw new NullPointerException("event.employee is null"); //NOI18N
105                 } else if (event.getEmployee().getEmployeeId() == null) {
106                         // Throw it again
107                         throw new NullPointerException("event.employee.employeeId is null"); //NOI18N
108                 } else if (event.getEmployee().getEmployeeId() < 1) {
109                         // Throw IAE
110                         throw new IllegalArgumentException(MessageFormat.format("event.employee.employeeId={0} is invalid", event.getEmployee().getEmployeeId())); //NOI18N
111                 }
112
113                 // Add employee to cache and list
114                 this.employeeCache.put(event.getEmployee().getEmployeeId(), event.getEmployee());
115                 this.getAllEmployees().add(event.getEmployee());
116         }
117
118         @Override
119         public Employable findEmployeeById (final Long employeeId) throws EmployeeNotFoundException {
120                 // Validate parameter
121                 if (null == employeeId) {
122                         // Throw NPE
123                         throw new NullPointerException("employeeId is null"); //NOI18N
124                 } else if (employeeId < 1) {
125                         // Throw IAE
126                         throw new IllegalArgumentException(MessageFormat.format("employeeId={0} is invalid", employeeId)); //NOI18N
127                 } else if (!this.employeeCache.containsKey(employeeId)) {
128                         // Not found
129                         throw new EmployeeNotFoundException(employeeId);
130                 }
131
132                 // Get it from cache
133                 final Employable employee = this.employeeCache.get(employeeId);
134
135                 // Return it
136                 return employee;
137         }
138
139         @Override
140         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
141         public List<Employable> getAllEmployees () {
142                 return this.allEmployees;
143         }
144
145         /**
146          * Getter for filtered list of employees
147          * <p>
148          * @return Filtered list of employees
149          */
150         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
151         public List<Employable> getFilteredEmployees () {
152                 return this.filteredEmployees;
153         }
154
155         /**
156          * Getter for filtered list of employees
157          * <p>
158          * @param filteredEmployees Filtered list of employees
159          */
160         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
161         public void setFilteredEmployees (final List<Employable> filteredEmployees) {
162                 this.filteredEmployees = filteredEmployees;
163         }
164
165         /**
166          * Getter for currently selected employee
167          * <p>
168          * @return Currently selected employee
169          */
170         public Employable getSelectedEmployee () {
171                 return this.selectedEmployee;
172         }
173
174         /**
175          * Setter for currently selected employee
176          * <p>
177          * @param selectedEmployee Currently selected employee
178          */
179         public void setSelectedEmployee (final Employable selectedEmployee) {
180                 this.selectedEmployee = selectedEmployee;
181         }
182
183         /**
184          * Initialization method
185          */
186         @PostConstruct
187         public void initializeList () {
188                 // Is cache there?
189                 if (!this.employeeCache.iterator().hasNext()) {
190                         // Add all
191                         for (final Employable employee : this.employeeBean.fetchAllEmployees()) {
192                                 // Add it to cache
193                                 this.employeeCache.put(employee.getEmployeeId(), employee);
194                         }
195                 }
196
197                 // Is cache filled and list is empty
198                 if ((this.employeeCache.iterator().hasNext()) && (this.getAllEmployees().isEmpty())) {
199                         // Build up list
200                         for (final Cache.Entry<Long, Employable> currentEntry : this.employeeCache) {
201                                 // Add to list
202                                 this.getAllEmployees().add(currentEntry.getValue());
203                         }
204
205                         // Sort list
206                         this.getAllEmployees().sort(new Comparator<Employable>() {
207                                 @Override
208                                 public int compare (final Employable employee1, final Employable employee2) {
209                                         return employee1.getEmployeeId() > employee2.getEmployeeId() ? 1 : employee1.getEmployeeId() < employee2.getEmployeeId() ? -1 : 0;
210                                 }
211                         });
212                 }
213         }
214
215         @Override
216         public Boolean isEmailAddressRegistered (final String emailAddress) {
217                 // Validate parameter
218                 if (null == emailAddress) {
219                         // Throw NPE
220                         throw new NullPointerException("emailAddress is null"); //NOI18N
221                 } else if (emailAddress.isEmpty()) {
222                         // Throw IAE
223                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
224                 }
225
226                 // Default is not found
227                 boolean isFound = false;
228
229                 // Check all entries
230                 for (final Employable basicData : this.getAllEmployees()) {
231                         // Is email address used?
232                         if (Objects.equals(basicData.getEmployeeEmailAddress(), emailAddress)) {
233                                 // Found it
234                                 isFound = true;
235                                 break;
236                         }
237                 }
238
239                 // Return flag
240                 return isFound;
241         }
242
243 }