]> git.mxchange.org Git - jfinancials-war.git/blob
7408d4f8ade04009ef977880091d5a58ef0224da
[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.LinkedList;
23 import java.util.List;
24 import javax.annotation.PostConstruct;
25 import javax.cache.Cache;
26 import javax.ejb.EJB;
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.ObservableAdminDepartmentAddedEvent;
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;
37
38 /**
39  * A list bean for departments
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @Named ("departmentListController")
44 @ViewScoped
45 public class FinancialsDepartmentListWebViewBean extends BaseFinancialsBean implements FinancialsDepartmentListWebViewController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 5_028_697_360_469L;
51
52         /**
53          * A list of all departments
54          */
55         private final List<Department> allDepartments;
56
57         /**
58          * EJB for administrative purposes
59          */
60         @EJB (lookup = "java:global/jfinancials-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
61         private DepartmentSessionBeanRemote departmentBean;
62
63         /**
64          * A list of all departments (globally)
65          */
66         @Inject
67         @NamedCache (cacheName = "departmentCache")
68         private transient Cache<Long, Department> departmentCache;
69
70         /**
71          * A list of filtered departments
72          */
73         private List<Department> filteredDepartments;
74
75         /**
76          * Currently selected department
77          */
78         private Department selectedDepartment;
79
80         /**
81          * Default constructor
82          */
83         public FinancialsDepartmentListWebViewBean () {
84                 // Call super constructor
85                 super();
86
87                 // Init list
88                 this.allDepartments = new LinkedList<>();
89         }
90
91         /**
92          * Observes events being fired when a department has been added.
93          * <p>
94          * @param event Event being fired
95          * <p>
96          * @throws NullPointerException If the parameter or it's carried instance is
97          * null
98          * @throws IllegalArgumentException If the branchId is zero or lower
99          */
100         public void afterDepartmentAddedEvent (@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.department is null"); //NOI18N
108                 } else if (event.getAddedDepartment().getDepartmentId() == null) {
109                         // Throw it again
110                         throw new NullPointerException("event.department.branchId is null"); //NOI18N
111                 } else if (event.getAddedDepartment().getDepartmentId() < 1) {
112                         // Throw IAE
113                         throw new IllegalArgumentException(MessageFormat.format("event.department.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.getAllDepartments().add(event.getAddedDepartment());
119         }
120
121         @Override
122         public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
123                 // Validate parameter
124                 if (null == departmentId) {
125                         // Throw NPE
126                         throw new NullPointerException("departmentId is null"); //NOI18N
127                 } else if (departmentId < 1) {
128                         // Throw IAE
129                         throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
130                 } else if (!this.departmentCache.containsKey(departmentId)) {
131                         // Not found
132                         throw new DepartmentNotFoundException(departmentId);
133                 }
134
135                 // Get it from cache
136                 final Department department = this.departmentCache.get(departmentId);
137
138                 // Return it
139                 return department;
140         }
141
142         /**
143          * Returns a list of all departments
144          * <p>
145          * @return A list of all departments
146          */
147         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
148         public List<Department> getAllDepartments () {
149                 return this.allDepartments;
150         }
151
152         /**
153          * Getter for a list of filtered departments
154          * <p>
155          * @return Filtered departments
156          */
157         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
158         public List<Department> getFilteredDepartments () {
159                 return this.filteredDepartments;
160         }
161
162         /**
163          * Setter for a list of filtered departments
164          * <p>
165          * @param filteredDepartments Filtered departments
166          */
167         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
168         public void setFilteredDepartments (final List<Department> filteredDepartments) {
169                 this.filteredDepartments = filteredDepartments;
170         }
171
172         /**
173          * Getter for selected department
174          * <p>
175          * @return Selected department
176          */
177         public Department getSelectedDepartment () {
178                 return this.selectedDepartment;
179         }
180
181         /**
182          * Setter for selected department
183          * <p>
184          * @param selectedDepartment Selected department
185          */
186         public void setSelectedDepartment (final Department selectedDepartment) {
187                 this.selectedDepartment = selectedDepartment;
188         }
189
190         /**
191          * Initializer method
192          */
193         @PostConstruct
194         public void initializeList () {
195                 // Is cache there?
196                 if (!this.departmentCache.iterator().hasNext()) {
197                         // Add all
198                         for (final Department department : this.departmentBean.fetchAllDepartments()) {
199                                 // Add it to cache
200                                 this.departmentCache.put(department.getDepartmentId(), department);
201                         }
202                 }
203
204                 // Is the list empty, but filled cache?
205                 if (this.getAllDepartments().isEmpty() && this.departmentCache.iterator().hasNext()) {
206                         // Build up list
207                         for (final Cache.Entry<Long, Department> currentEntry : this.departmentCache) {
208                                 // Add to list
209                                 this.getAllDepartments().add(currentEntry.getValue());
210                         }
211
212                         // Sort list
213                         this.getAllDepartments().sort(new Comparator<Department>() {
214                                 @Override
215                                 public int compare (final Department department1, final Department department2) {
216                                         return department1.getDepartmentId() > department2.getDepartmentId() ? 1 : department1.getDepartmentId() < department2.getDepartmentId() ? -1 : 0;
217                                 }
218                         });
219                 }
220         }
221
222         @Override
223         public boolean isDepartmentAlreadyAdded (final Department department) {
224                 // Default is not found
225                 boolean isFound = false;
226
227                 // Now check each entry
228                 for (final Department currentDepartment : this.getAllDepartments()) {
229                         // Is same address?
230                         if (Departments.isSameDepartment(currentDepartment, department)) {
231                                 // Found one
232                                 isFound = true;
233                                 break;
234                         }
235                 }
236
237                 // Return flag
238                 return isFound;
239         }
240
241 }