]> git.mxchange.org Git - addressbook-war.git/blob
d34273361b52b7e74682fe025541cf04ea9e1b36
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2017 - 2022 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.addressbook.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.addressbook.beans.BaseAddressbookBean;
34 import org.mxchange.jcontactsbusiness.events.department.added.ObservableAdminDepartmentAddedEvent;
35 import org.mxchange.jcontactsbusiness.events.department.updated.ObservableAdminDepartmentUpdatedEvent;
36 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
37 import org.mxchange.jcontactsbusiness.model.department.Department;
38 import org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote;
39 import org.mxchange.jcontactsbusiness.model.utils.DepartmentUtils;
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 AddressbookDepartmentListWebViewBean extends BaseAddressbookBean implements AddressbookDepartmentListWebViewController {
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/addressbook-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 AddressbookDepartmentListWebViewBean () {
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                 // Uniquely add department
117                 this.uniqueAddDepartment(event.getAddedDepartment());
118         }
119
120         /**
121          * Observes events being fired when a department has been updated by an
122          * administrator.
123          * <p>
124          * @param event Event being fired
125          */
126         public void afterAdminDepartmentUpdatedEvent (@Observes final ObservableAdminDepartmentUpdatedEvent event) {
127                 // Validate parameter
128                 if (null == event) {
129                         // Throw NPE
130                         throw new NullPointerException("event is null"); //NOI18N
131                 } else if (event.getUpdatedDepartment() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("event.updatedDepartment is null"); //NOI18N
134                 } else if (event.getUpdatedDepartment().getDepartmentId() == null) {
135                         // Throw it again
136                         throw new NullPointerException("event.updatedDepartment.branchId is null"); //NOI18N
137                 } else if (event.getUpdatedDepartment().getDepartmentId() < 1) {
138                         // Throw IAE
139                         throw new IllegalArgumentException(MessageFormat.format("event.updatedDepartment.branchId={0} is not valid", event.getUpdatedDepartment().getDepartmentId())); //NOI18N
140                 }
141
142                 // Uniquely add (update) instance
143                 this.uniqueAddDepartment(event.getUpdatedDepartment());
144         }
145
146         @Override
147         public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
148                 // Validate parameter
149                 if (null == departmentId) {
150                         // Throw NPE
151                         throw new NullPointerException("departmentId is null"); //NOI18N
152                 } else if (departmentId < 1) {
153                         // Throw IAE
154                         throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
155                 } else if (!this.departmentCache.containsKey(departmentId)) {
156                         // Not found
157                         throw new DepartmentNotFoundException(departmentId);
158                 }
159
160                 // Get it from cache
161                 final Department department = this.departmentCache.get(departmentId);
162
163                 // Return it
164                 return department;
165         }
166
167         /**
168          * Returns a list of all departments
169          * <p>
170          * @return A list of all departments
171          */
172         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
173         public List<Department> getAllDepartments () {
174                 return this.allDepartments;
175         }
176
177         /**
178          * Getter for a list of filtered departments
179          * <p>
180          * @return Filtered departments
181          */
182         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
183         public List<Department> getFilteredDepartments () {
184                 return this.filteredDepartments;
185         }
186
187         /**
188          * Setter for a list of filtered departments
189          * <p>
190          * @param filteredDepartments Filtered departments
191          */
192         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
193         public void setFilteredDepartments (final List<Department> filteredDepartments) {
194                 this.filteredDepartments = filteredDepartments;
195         }
196
197         /**
198          * Getter for selected department
199          * <p>
200          * @return Selected department
201          */
202         public Department getSelectedDepartment () {
203                 return this.selectedDepartment;
204         }
205
206         /**
207          * Setter for selected department
208          * <p>
209          * @param selectedDepartment Selected department
210          */
211         public void setSelectedDepartment (final Department selectedDepartment) {
212                 this.selectedDepartment = selectedDepartment;
213         }
214
215         /**
216          * Initializer method
217          */
218         @PostConstruct
219         public void initializeList () {
220                 // Is cache there?
221                 if (!this.departmentCache.iterator().hasNext()) {
222                         // Add all
223                         for (final Department department : this.departmentBean.fetchAllDepartments()) {
224                                 // Add it to cache
225                                 this.departmentCache.put(department.getDepartmentId(), department);
226                         }
227                 }
228
229                 // Is the list empty, but filled cache?
230                 if (this.getAllDepartments().isEmpty() && this.departmentCache.iterator().hasNext()) {
231                         // Build up list
232                         for (final Cache.Entry<Long, Department> currentEntry : this.departmentCache) {
233                                 // Add to list
234                                 this.getAllDepartments().add(currentEntry.getValue());
235                         }
236
237                         // Sort list
238                         this.getAllDepartments().sort(new Comparator<Department>() {
239                                 @Override
240                                 public int compare (final Department department1, final Department department2) {
241                                         return department1.getDepartmentId() > department2.getDepartmentId() ? 1 : department1.getDepartmentId() < department2.getDepartmentId() ? -1 : 0;
242                                 }
243                         });
244                 }
245         }
246
247         @Override
248         public boolean isDepartmentAlreadyAdded (final Department department) {
249                 // Default is not found
250                 boolean isFound = false;
251
252                 // Now check each entry
253                 for (final Department currentDepartment : this.getAllDepartments()) {
254                         // Is same address?
255                         if (DepartmentUtils.isSameDepartment(currentDepartment, department)) {
256                                 // Found one
257                                 isFound = true;
258                                 break;
259                         }
260                 }
261
262                 // Return flag
263                 return isFound;
264         }
265
266         /**
267          * Uniquely adds given department instance to full list and updates cache.
268          * <p>
269          * @param department Department being uniquely added
270          */
271         private void uniqueAddDepartment (final Department department) {
272                 // Add instance to cache
273                 this.departmentCache.put(department.getDepartmentId(), department);
274
275                 // Get iterator
276                 final Iterator<Department> iterator = this.getAllDepartments().iterator();
277
278                 // Loop through all
279                 while (iterator.hasNext()) {
280                         // Get current entry
281                         final Department currentDepartment = iterator.next();
282
283                         // Does the primary key match?
284                         if (Objects.equals(department.getDepartmentId(), currentDepartment.getDepartmentId())) {
285                                 // Yes, remove it and abort iteration
286                                 iterator.remove();
287                                 break;
288                         }
289                 }
290
291                 // (Re-) Add department
292                 this.getAllDepartments().add(department);
293         }
294 }