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.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.event.Observes;
30 import javax.faces.view.ViewScoped;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jcontactsbusiness.events.department.added.ObservableAdminDepartmentAddedEvent;
34 import org.mxchange.jcontactsbusiness.events.department.updated.ObservableAdminDepartmentUpdatedEvent;
35 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
36 import org.mxchange.jcontactsbusiness.model.department.Department;
37 import org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote;
38 import org.mxchange.jcontactsbusiness.model.department.Departments;
39 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
42 * A list bean for departments
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("departmentListController")
48 public class FinancialsDepartmentListWebViewBean extends BaseFinancialsBean implements FinancialsDepartmentListWebViewController {
53 private static final long serialVersionUID = 5_028_697_360_469L;
56 * A list of all departments
58 private final List<Department> allDepartments;
61 * EJB for administrative purposes
63 @EJB (lookup = "java:global/jfinancials-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
64 private DepartmentSessionBeanRemote departmentBean;
67 * A list of all departments (globally)
70 @NamedCache (cacheName = "departmentCache")
71 private transient Cache<Long, Department> departmentCache;
74 * A list of filtered departments
76 private List<Department> filteredDepartments;
79 * Currently selected department
81 private Department selectedDepartment;
86 public FinancialsDepartmentListWebViewBean () {
87 // Call super constructor
91 this.allDepartments = new LinkedList<>();
95 * Observes events being fired when a department has been added by an
98 * @param event Event being fired
100 public void afterAdminDepartmentAddedEvent (@Observes final ObservableAdminDepartmentAddedEvent event) {
101 // Validate parameter
104 throw new NullPointerException("event is null"); //NOI18N
105 } else if (event.getAddedDepartment() == null) {
107 throw new NullPointerException("event.addedDepartment is null"); //NOI18N
108 } else if (event.getAddedDepartment().getDepartmentId() == null) {
110 throw new NullPointerException("event.addedDepartment.branchId is null"); //NOI18N
111 } else if (event.getAddedDepartment().getDepartmentId() < 1) {
113 throw new IllegalArgumentException(MessageFormat.format("event.addedDepartment.branchId={0} is not valid", event.getAddedDepartment().getDepartmentId())); //NOI18N
116 // Add instance to cache
117 this.departmentCache.put(event.getAddedDepartment().getDepartmentId(), event.getAddedDepartment());
118 this.uniqueAddDepartment(event.getAddedDepartment());
122 * Observes events being fired when a department has been updated by an
125 * @param event Event being fired
127 public void afterAdminDepartmentUpdatedEvent (@Observes final ObservableAdminDepartmentUpdatedEvent event) {
128 // Validate parameter
131 throw new NullPointerException("event is null"); //NOI18N
132 } else if (event.getUpdatedDepartment() == null) {
134 throw new NullPointerException("event.updatedDepartment is null"); //NOI18N
135 } else if (event.getUpdatedDepartment().getDepartmentId() == null) {
137 throw new NullPointerException("event.updatedDepartment.branchId is null"); //NOI18N
138 } else if (event.getUpdatedDepartment().getDepartmentId() < 1) {
140 throw new IllegalArgumentException(MessageFormat.format("event.updatedDepartment.branchId={0} is not valid", event.getUpdatedDepartment().getDepartmentId())); //NOI18N
143 // Add instance to cache
144 this.departmentCache.put(event.getUpdatedDepartment().getDepartmentId(), event.getUpdatedDepartment());
146 // Uniquely add (update) instance
147 this.uniqueAddDepartment(event.getUpdatedDepartment());
151 public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
152 // Validate parameter
153 if (null == departmentId) {
155 throw new NullPointerException("departmentId is null"); //NOI18N
156 } else if (departmentId < 1) {
158 throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
159 } else if (!this.departmentCache.containsKey(departmentId)) {
161 throw new DepartmentNotFoundException(departmentId);
165 final Department department = this.departmentCache.get(departmentId);
172 * Returns a list of all departments
174 * @return A list of all departments
176 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
177 public List<Department> getAllDepartments () {
178 return this.allDepartments;
182 * Getter for a list of filtered departments
184 * @return Filtered departments
186 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
187 public List<Department> getFilteredDepartments () {
188 return this.filteredDepartments;
192 * Setter for a list of filtered departments
194 * @param filteredDepartments Filtered departments
196 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
197 public void setFilteredDepartments (final List<Department> filteredDepartments) {
198 this.filteredDepartments = filteredDepartments;
202 * Getter for selected department
204 * @return Selected department
206 public Department getSelectedDepartment () {
207 return this.selectedDepartment;
211 * Setter for selected department
213 * @param selectedDepartment Selected department
215 public void setSelectedDepartment (final Department selectedDepartment) {
216 this.selectedDepartment = selectedDepartment;
223 public void initializeList () {
225 if (!this.departmentCache.iterator().hasNext()) {
227 for (final Department department : this.departmentBean.fetchAllDepartments()) {
229 this.departmentCache.put(department.getDepartmentId(), department);
233 // Is the list empty, but filled cache?
234 if (this.getAllDepartments().isEmpty() && this.departmentCache.iterator().hasNext()) {
236 for (final Cache.Entry<Long, Department> currentEntry : this.departmentCache) {
238 this.getAllDepartments().add(currentEntry.getValue());
242 this.getAllDepartments().sort(new Comparator<Department>() {
244 public int compare (final Department department1, final Department department2) {
245 return department1.getDepartmentId() > department2.getDepartmentId() ? 1 : department1.getDepartmentId() < department2.getDepartmentId() ? -1 : 0;
252 public boolean isDepartmentAlreadyAdded (final Department department) {
253 // Default is not found
254 boolean isFound = false;
256 // Now check each entry
257 for (final Department currentDepartment : this.getAllDepartments()) {
259 if (Departments.isSameDepartment(currentDepartment, department)) {
271 * Uniquely adds given department instance to full list
273 * @param department Department being uniquely added
275 private void uniqueAddDepartment (final Department department) {
277 final Iterator<Department> iterator = this.getAllDepartments().iterator();
280 while (iterator.hasNext()) {
282 final Department currentDepartment = iterator.next();
284 // Does the primary key match?
285 if (Objects.equals(department.getDepartmentId(), currentDepartment.getDepartmentId())) {
286 // Yes, remove it and abort iteration
292 // (Re-) Add department
293 this.getAllDepartments().add(department);