]> git.mxchange.org Git - jfinancials-war.git/blob
be23270b6bf7ed58d7af28c1e56ded5a9aba8811
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2020 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.department.list;
18
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;
28 import javax.ejb.EJB;
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;
40
41 /**
42  * A list bean for departments
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("departmentListController")
47 @ViewScoped
48 public class FinancialsDepartmentListWebViewBean extends BaseFinancialsBean implements FinancialsDepartmentListWebViewController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 5_028_697_360_469L;
54
55         /**
56          * A list of all departments
57          */
58         private final List<Department> allDepartments;
59
60         /**
61          * EJB for administrative purposes
62          */
63         @EJB (lookup = "java:global/jfinancials-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
64         private DepartmentSessionBeanRemote departmentBean;
65
66         /**
67          * A list of all departments (globally)
68          */
69         @Inject
70         @NamedCache (cacheName = "departmentCache")
71         private transient Cache<Long, Department> departmentCache;
72
73         /**
74          * A list of filtered departments
75          */
76         private List<Department> filteredDepartments;
77
78         /**
79          * Currently selected department
80          */
81         private Department selectedDepartment;
82
83         /**
84          * Default constructor
85          */
86         public FinancialsDepartmentListWebViewBean () {
87                 // Call super constructor
88                 super();
89
90                 // Init list
91                 this.allDepartments = new LinkedList<>();
92         }
93
94         /**
95          * Observes events being fired when a department has been added by an
96          * administrator.
97          * <p>
98          * @param event Event being fired
99          */
100         public void afterAdminDepartmentAddedEvent (@Observes final ObservableAdminDepartmentAddedEvent event) {
101                 // Validate parameter
102                 if (null == event) {
103                         // Throw NPE
104                         throw new NullPointerException("event is null"); //NOI18N
105                 } else if (event.getAddedDepartment() == null) {
106                         // Throw NPE again
107                         throw new NullPointerException("event.addedDepartment is null"); //NOI18N
108                 } else if (event.getAddedDepartment().getDepartmentId() == null) {
109                         // Throw it again
110                         throw new NullPointerException("event.addedDepartment.branchId is null"); //NOI18N
111                 } else if (event.getAddedDepartment().getDepartmentId() < 1) {
112                         // Throw IAE
113                         throw new IllegalArgumentException(MessageFormat.format("event.addedDepartment.branchId={0} is not valid", event.getAddedDepartment().getDepartmentId())); //NOI18N
114                 }
115
116                 // Add instance to cache
117                 this.departmentCache.put(event.getAddedDepartment().getDepartmentId(), event.getAddedDepartment());
118                 this.uniqueAddDepartment(event.getAddedDepartment());
119         }
120
121         /**
122          * Observes events being fired when a department has been updated by an
123          * administrator.
124          * <p>
125          * @param event Event being fired
126          */
127         public void afterAdminDepartmentUpdatedEvent (@Observes final ObservableAdminDepartmentUpdatedEvent event) {
128                 // Validate parameter
129                 if (null == event) {
130                         // Throw NPE
131                         throw new NullPointerException("event is null"); //NOI18N
132                 } else if (event.getUpdatedDepartment() == null) {
133                         // Throw NPE again
134                         throw new NullPointerException("event.updatedDepartment is null"); //NOI18N
135                 } else if (event.getUpdatedDepartment().getDepartmentId() == null) {
136                         // Throw it again
137                         throw new NullPointerException("event.updatedDepartment.branchId is null"); //NOI18N
138                 } else if (event.getUpdatedDepartment().getDepartmentId() < 1) {
139                         // Throw IAE
140                         throw new IllegalArgumentException(MessageFormat.format("event.updatedDepartment.branchId={0} is not valid", event.getUpdatedDepartment().getDepartmentId())); //NOI18N
141                 }
142
143                 // Add instance to cache
144                 this.departmentCache.put(event.getUpdatedDepartment().getDepartmentId(), event.getUpdatedDepartment());
145
146                 // Uniquely add (update) instance
147                 this.uniqueAddDepartment(event.getUpdatedDepartment());
148         }
149
150         @Override
151         public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
152                 // Validate parameter
153                 if (null == departmentId) {
154                         // Throw NPE
155                         throw new NullPointerException("departmentId is null"); //NOI18N
156                 } else if (departmentId < 1) {
157                         // Throw IAE
158                         throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
159                 } else if (!this.departmentCache.containsKey(departmentId)) {
160                         // Not found
161                         throw new DepartmentNotFoundException(departmentId);
162                 }
163
164                 // Get it from cache
165                 final Department department = this.departmentCache.get(departmentId);
166
167                 // Return it
168                 return department;
169         }
170
171         /**
172          * Returns a list of all departments
173          * <p>
174          * @return A list of all departments
175          */
176         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
177         public List<Department> getAllDepartments () {
178                 return this.allDepartments;
179         }
180
181         /**
182          * Getter for a list of filtered departments
183          * <p>
184          * @return Filtered departments
185          */
186         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
187         public List<Department> getFilteredDepartments () {
188                 return this.filteredDepartments;
189         }
190
191         /**
192          * Setter for a list of filtered departments
193          * <p>
194          * @param filteredDepartments Filtered departments
195          */
196         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
197         public void setFilteredDepartments (final List<Department> filteredDepartments) {
198                 this.filteredDepartments = filteredDepartments;
199         }
200
201         /**
202          * Getter for selected department
203          * <p>
204          * @return Selected department
205          */
206         public Department getSelectedDepartment () {
207                 return this.selectedDepartment;
208         }
209
210         /**
211          * Setter for selected department
212          * <p>
213          * @param selectedDepartment Selected department
214          */
215         public void setSelectedDepartment (final Department selectedDepartment) {
216                 this.selectedDepartment = selectedDepartment;
217         }
218
219         /**
220          * Initializer method
221          */
222         @PostConstruct
223         public void initializeList () {
224                 // Is cache there?
225                 if (!this.departmentCache.iterator().hasNext()) {
226                         // Add all
227                         for (final Department department : this.departmentBean.fetchAllDepartments()) {
228                                 // Add it to cache
229                                 this.departmentCache.put(department.getDepartmentId(), department);
230                         }
231                 }
232
233                 // Is the list empty, but filled cache?
234                 if (this.getAllDepartments().isEmpty() && this.departmentCache.iterator().hasNext()) {
235                         // Build up list
236                         for (final Cache.Entry<Long, Department> currentEntry : this.departmentCache) {
237                                 // Add to list
238                                 this.getAllDepartments().add(currentEntry.getValue());
239                         }
240
241                         // Sort list
242                         this.getAllDepartments().sort(new Comparator<Department>() {
243                                 @Override
244                                 public int compare (final Department department1, final Department department2) {
245                                         return department1.getDepartmentId() > department2.getDepartmentId() ? 1 : department1.getDepartmentId() < department2.getDepartmentId() ? -1 : 0;
246                                 }
247                         });
248                 }
249         }
250
251         @Override
252         public boolean isDepartmentAlreadyAdded (final Department department) {
253                 // Default is not found
254                 boolean isFound = false;
255
256                 // Now check each entry
257                 for (final Department currentDepartment : this.getAllDepartments()) {
258                         // Is same address?
259                         if (Departments.isSameDepartment(currentDepartment, department)) {
260                                 // Found one
261                                 isFound = true;
262                                 break;
263                         }
264                 }
265
266                 // Return flag
267                 return isFound;
268         }
269
270         /**
271          * Uniquely adds given department instance to full list
272          * <p>
273          * @param department Department being uniquely added
274          */
275         private void uniqueAddDepartment (final Department department) {
276                 // Get iterator
277                 final Iterator<Department> iterator = this.getAllDepartments().iterator();
278
279                 // Loop through all
280                 while (iterator.hasNext()) {
281                         // Get current entry
282                         final Department currentDepartment = iterator.next();
283
284                         // Does the primary key match?
285                         if (Objects.equals(department.getDepartmentId(), currentDepartment.getDepartmentId())) {
286                                 // Yes, remove it and abort iteration
287                                 iterator.remove();
288                                 break;
289                         }
290                 }
291
292                 // (Re-) Add department
293                 this.getAllDepartments().add(department);
294         }
295 }