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 // Uniquely add department
117 this.uniqueAddDepartment(event.getAddedDepartment());
121 * Observes events being fired when a department has been updated by an
124 * @param event Event being fired
126 public void afterAdminDepartmentUpdatedEvent (@Observes final ObservableAdminDepartmentUpdatedEvent event) {
127 // Validate parameter
130 throw new NullPointerException("event is null"); //NOI18N
131 } else if (event.getUpdatedDepartment() == null) {
133 throw new NullPointerException("event.updatedDepartment is null"); //NOI18N
134 } else if (event.getUpdatedDepartment().getDepartmentId() == null) {
136 throw new NullPointerException("event.updatedDepartment.branchId is null"); //NOI18N
137 } else if (event.getUpdatedDepartment().getDepartmentId() < 1) {
139 throw new IllegalArgumentException(MessageFormat.format("event.updatedDepartment.branchId={0} is not valid", event.getUpdatedDepartment().getDepartmentId())); //NOI18N
142 // Uniquely add (update) instance
143 this.uniqueAddDepartment(event.getUpdatedDepartment());
147 public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
148 // Validate parameter
149 if (null == departmentId) {
151 throw new NullPointerException("departmentId is null"); //NOI18N
152 } else if (departmentId < 1) {
154 throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
155 } else if (!this.departmentCache.containsKey(departmentId)) {
157 throw new DepartmentNotFoundException(departmentId);
161 final Department department = this.departmentCache.get(departmentId);
168 * Returns a list of all departments
170 * @return A list of all departments
172 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
173 public List<Department> getAllDepartments () {
174 return this.allDepartments;
178 * Getter for a list of filtered departments
180 * @return Filtered departments
182 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
183 public List<Department> getFilteredDepartments () {
184 return this.filteredDepartments;
188 * Setter for a list of filtered departments
190 * @param filteredDepartments Filtered departments
192 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
193 public void setFilteredDepartments (final List<Department> filteredDepartments) {
194 this.filteredDepartments = filteredDepartments;
198 * Getter for selected department
200 * @return Selected department
202 public Department getSelectedDepartment () {
203 return this.selectedDepartment;
207 * Setter for selected department
209 * @param selectedDepartment Selected department
211 public void setSelectedDepartment (final Department selectedDepartment) {
212 this.selectedDepartment = selectedDepartment;
219 public void initializeList () {
221 if (!this.departmentCache.iterator().hasNext()) {
223 for (final Department department : this.departmentBean.fetchAllDepartments()) {
225 this.departmentCache.put(department.getDepartmentId(), department);
229 // Is the list empty, but filled cache?
230 if (this.getAllDepartments().isEmpty() && this.departmentCache.iterator().hasNext()) {
232 for (final Cache.Entry<Long, Department> currentEntry : this.departmentCache) {
234 this.getAllDepartments().add(currentEntry.getValue());
238 this.getAllDepartments().sort(new Comparator<Department>() {
240 public int compare (final Department department1, final Department department2) {
241 return department1.getDepartmentId() > department2.getDepartmentId() ? 1 : department1.getDepartmentId() < department2.getDepartmentId() ? -1 : 0;
248 public boolean isDepartmentAlreadyAdded (final Department department) {
249 // Default is not found
250 boolean isFound = false;
252 // Now check each entry
253 for (final Department currentDepartment : this.getAllDepartments()) {
255 if (Departments.isSameDepartment(currentDepartment, department)) {
267 * Uniquely adds given department instance to full list and updates cache.
269 * @param department Department being uniquely added
271 private void uniqueAddDepartment (final Department department) {
272 // Add instance to cache
273 this.departmentCache.put(department.getDepartmentId(), department);
276 final Iterator<Department> iterator = this.getAllDepartments().iterator();
279 while (iterator.hasNext()) {
281 final Department currentDepartment = iterator.next();
283 // Does the primary key match?
284 if (Objects.equals(department.getDepartmentId(), currentDepartment.getDepartmentId())) {
285 // Yes, remove it and abort iteration
291 // (Re-) Add department
292 this.getAllDepartments().add(department);