2 * Copyright (C) 2017 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.beans.business.employee.list;
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;
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;
39 * A view-scoped bean for listing purposes for e.g. administrative employee
42 * @author Roland Häder<roland@mxchange.org>
44 @Named ("employeeListController")
46 public class FinancialsEmployeeListWebViewBean extends BaseFinancialsBean implements FinancialsEmployeeListWebViewController {
51 private static final long serialVersionUID = 12_886_968_547_362L;
54 * List of all employees
56 private final List<Employable> allEmployees;
59 * EJB for general company employee purposes
61 @EJB (lookup = "java:global/jfinancials-ejb/employee!org.mxchange.jcontactsbusiness.model.employee.EmployeeSessionBeanRemote")
62 private EmployeeSessionBeanRemote employeeBean;
65 * List of all company employees
68 @NamedCache (cacheName = "companyEmployeeCache")
69 private transient Cache<Long, Employable> employeeCache;
72 * A list of filtered employees
74 private List<Employable> filteredEmployees;
77 * Currently selected employee
79 private Employable selectedEmployee;
84 public FinancialsEmployeeListWebViewBean () {
85 // Call super constructor
89 this.allEmployees = new LinkedList<>();
93 * Observes events being fired when an employee has been added
95 * @param event Event being fired
97 public void afterEmployeeAddedEvent (@Observes final ObservableEmployeeAddedEvent event) {
101 throw new NullPointerException("event is null"); //NOI18N
102 } else if (event.getEmployee() == null) {
104 throw new NullPointerException("event.employee is null"); //NOI18N
105 } else if (event.getEmployee().getEmployeeId() == null) {
107 throw new NullPointerException("event.employee.employeeId is null"); //NOI18N
108 } else if (event.getEmployee().getEmployeeId() < 1) {
110 throw new IllegalArgumentException(MessageFormat.format("event.employee.employeeId={0} is invalid", event.getEmployee().getEmployeeId())); //NOI18N
113 // Add employee to cache and list
114 this.employeeCache.put(event.getEmployee().getEmployeeId(), event.getEmployee());
115 this.getAllEmployees().add(event.getEmployee());
119 public Employable findEmployeeById (final Long employeeId) throws EmployeeNotFoundException {
120 // Validate parameter
121 if (null == employeeId) {
123 throw new NullPointerException("employeeId is null"); //NOI18N
124 } else if (employeeId < 1) {
126 throw new IllegalArgumentException(MessageFormat.format("employeeId={0} is invalid", employeeId)); //NOI18N
127 } else if (!this.employeeCache.containsKey(employeeId)) {
129 throw new EmployeeNotFoundException(employeeId);
133 final Employable employee = this.employeeCache.get(employeeId);
140 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
141 public List<Employable> getAllEmployees () {
142 return this.allEmployees;
146 * Getter for filtered list of employees
148 * @return Filtered list of employees
150 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
151 public List<Employable> getFilteredEmployees () {
152 return this.filteredEmployees;
156 * Getter for filtered list of employees
158 * @param filteredEmployees Filtered list of employees
160 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
161 public void setFilteredEmployees (final List<Employable> filteredEmployees) {
162 this.filteredEmployees = filteredEmployees;
166 * Getter for currently selected employee
168 * @return Currently selected employee
170 public Employable getSelectedEmployee () {
171 return this.selectedEmployee;
175 * Setter for currently selected employee
177 * @param selectedEmployee Currently selected employee
179 public void setSelectedEmployee (final Employable selectedEmployee) {
180 this.selectedEmployee = selectedEmployee;
184 * Initialization method
187 public void initializeList () {
189 if (!this.employeeCache.iterator().hasNext()) {
191 for (final Employable employee : this.employeeBean.fetchAllEmployees()) {
193 this.employeeCache.put(employee.getEmployeeId(), employee);
197 // Is cache filled and list is empty
198 if ((this.employeeCache.iterator().hasNext()) && (this.getAllEmployees().isEmpty())) {
200 for (final Cache.Entry<Long, Employable> currentEntry : this.employeeCache) {
202 this.getAllEmployees().add(currentEntry.getValue());
206 this.getAllEmployees().sort(new Comparator<Employable>() {
208 public int compare (final Employable employee1, final Employable employee2) {
209 return employee1.getEmployeeId() > employee2.getEmployeeId() ? 1 : employee1.getEmployeeId() < employee2.getEmployeeId() ? -1 : 0;
216 public Boolean isEmailAddressRegistered (final String emailAddress) {
217 // Validate parameter
218 if (null == emailAddress) {
220 throw new NullPointerException("emailAddress is null"); //NOI18N
221 } else if (emailAddress.isEmpty()) {
223 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
226 // Default is not found
227 boolean isFound = false;
230 for (final Employable basicData : this.getAllEmployees()) {
231 // Is email address used?
232 if (Objects.equals(basicData.getEmployeeEmailAddress(), emailAddress)) {