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.department.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 javax.annotation.PostConstruct;
25 import javax.cache.Cache;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.ViewScoped;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcontactsbusiness.events.department.added.ObservableDepartmentAddedEvent;
32 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
33 import org.mxchange.jcontactsbusiness.model.department.Department;
34 import org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote;
35 import org.mxchange.jcontactsbusiness.model.department.Departments;
36 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
39 * A list bean for departments
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("departmentListController")
45 public class FinancialsDepartmentListWebViewBean extends BaseFinancialsBean implements FinancialsDepartmentListWebViewController {
50 private static final long serialVersionUID = 5_028_697_360_462L;
53 * A list of all departments
55 private final List<Department> allDepartments;
58 * EJB for administrative purposes
60 @EJB (lookup = "java:global/jfinancials-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
61 private DepartmentSessionBeanRemote departmentBean;
64 * A list of all departments (globally)
67 @NamedCache (cacheName = "departmentCache")
68 private transient Cache<Long, Department> departmentCache;
71 * A list of filtered departments
73 private List<Department> filteredDepartments;
76 * Currently selected department
78 private Department selectedDepartment;
83 public FinancialsDepartmentListWebViewBean () {
84 // Call super constructor
88 this.allDepartments = new LinkedList<>();
92 * Observes events being fired when a department has been added.
94 * @param event Event being fired
96 * @throws NullPointerException If the parameter or it's carried instance is
98 * @throws IllegalArgumentException If the branchId is zero or lower
100 public void afterDepartmentAddedEvent (@Observes final ObservableDepartmentAddedEvent event) {
101 // Validate parameter
104 throw new NullPointerException("event is null"); //NOI18N
105 } else if (event.getDepartment() == null) {
107 throw new NullPointerException("event.department is null"); //NOI18N
108 } else if (event.getDepartment().getDepartmentId() == null) {
110 throw new NullPointerException("event.department.branchId is null"); //NOI18N
111 } else if (event.getDepartment().getDepartmentId() < 1) {
113 throw new IllegalArgumentException(MessageFormat.format("event.department.branchId={0} is not valid", event.getDepartment().getDepartmentId())); //NOI18N
116 // Add instance to cache
117 this.departmentCache.put(event.getDepartment().getDepartmentId(), event.getDepartment());
118 this.getAllDepartments().add(event.getDepartment());
122 public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
123 // Validate parameter
124 if (null == departmentId) {
126 throw new NullPointerException("departmentId is null"); //NOI18N
127 } else if (departmentId < 1) {
129 throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
130 } else if (!this.departmentCache.containsKey(departmentId)) {
132 throw new DepartmentNotFoundException(departmentId);
136 final Department department = this.departmentCache.get(departmentId);
143 * Returns a list of all departments
145 * @return A list of all departments
147 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
148 public List<Department> getAllDepartments () {
149 return this.allDepartments;
153 * Getter for a list of filtered departments
155 * @return Filtered departments
157 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
158 public List<Department> getFilteredDepartments () {
159 return this.filteredDepartments;
163 * Setter for a list of filtered departments
165 * @param filteredDepartments Filtered departments
167 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
168 public void setFilteredDepartments (final List<Department> filteredDepartments) {
169 this.filteredDepartments = filteredDepartments;
173 * Getter for selected department
175 * @return Selected department
177 public Department getSelectedDepartment () {
178 return this.selectedDepartment;
182 * Setter for selected department
184 * @param selectedDepartment Selected department
186 public void setSelectedDepartment (final Department selectedDepartment) {
187 this.selectedDepartment = selectedDepartment;
194 public void initializeList () {
196 if (!this.departmentCache.iterator().hasNext()) {
198 for (final Department department : this.departmentBean.allDepartments()) {
200 this.departmentCache.put(department.getDepartmentId(), department);
204 // Is the list empty, but filled cache?
205 if (this.getAllDepartments().isEmpty() && this.departmentCache.iterator().hasNext()) {
207 for (final Cache.Entry<Long, Department> currentEntry : this.departmentCache) {
209 this.getAllDepartments().add(currentEntry.getValue());
213 this.getAllDepartments().sort(new Comparator<Department>() {
215 public int compare (final Department department1, final Department department2) {
216 return department1.getDepartmentId() > department2.getDepartmentId() ? 1 : department1.getDepartmentId() < department2.getDepartmentId() ? -1 : 0;
223 public boolean isDepartmentAlreadyAdded (final Department department) {
224 // Default is not found
225 boolean isFound = false;
227 // Now check each entry
228 for (final Department currentDepartment : this.getAllDepartments()) {
230 if (Departments.isSameDepartment(currentDepartment, department)) {