]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/business/department/FinancialsDepartmentWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / business / department / FinancialsDepartmentWebRequestBean.java
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;
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 javax.annotation.PostConstruct;
26 import javax.cache.Cache;
27 import javax.ejb.EJB;
28 import javax.enterprise.context.RequestScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontactsbusiness.events.department.added.ObservableDepartmentAddedEvent;
33 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
34 import org.mxchange.jcontactsbusiness.model.department.Department;
35 import org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote;
36 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
37
38 /**
39  * A general bean for departments
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @Named ("departmentController")
44 @RequestScoped
45 public class FinancialsDepartmentWebRequestBean extends BaseFinancialsBean implements FinancialsDepartmentWebRequestController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 5_028_697_360_461L;
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 Cache<Long, Department> departmentCache;
69
70         /**
71          * A list of filtered departments
72          */
73         private List<Department> filteredDepartments;
74
75         /**
76          * Default constructor
77          */
78         public FinancialsDepartmentWebRequestBean () {
79                 // Call super constructor
80                 super();
81
82                 // Init list
83                 this.allDepartments = new LinkedList<>();
84         }
85
86         /**
87          * Observes events being fired when a department has been added.
88          * <p>
89          * @param event Event being fired
90          * <p>
91          * @throws NullPointerException If the parameter or it's carried instance is
92          * null
93          * @throws IllegalArgumentException If the branchId is zero or lower
94          */
95         public void afterDepartmentAddedEvent (@Observes final ObservableDepartmentAddedEvent event) {
96                 // Validate parameter
97                 if (null == event) {
98                         // Throw NPE
99                         throw new NullPointerException("event is null"); //NOI18N
100                 } else if (event.getDepartment() == null) {
101                         // Throw NPE again
102                         throw new NullPointerException("event.department is null"); //NOI18N
103                 } else if (event.getDepartment().getDepartmentId() == null) {
104                         // Throw it again
105                         throw new NullPointerException("event.department.branchId is null"); //NOI18N
106                 } else if (event.getDepartment().getDepartmentId() < 1) {
107                         // Throw IAE
108                         throw new IllegalArgumentException(MessageFormat.format("event.department.branchId={0} is not valid", event.getDepartment().getDepartmentId())); //NOI18N
109                 }
110
111                 // Add instance to cache
112                 this.departmentCache.put(event.getDepartment().getDepartmentId(), event.getDepartment());
113                 this.allDepartments.add(event.getDepartment());
114         }
115
116         @Override
117         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
118         public List<Department> allDepartments () {
119                 return this.allDepartments;
120         }
121
122         @Override
123         public Department findDepartmentById (final Long departmentId) throws DepartmentNotFoundException {
124                 // Validate parameter
125                 if (null == departmentId) {
126                         // Throw NPE
127                         throw new NullPointerException("departmentId is null"); //NOI18N
128                 } else if (departmentId < 1) {
129                         // Throw IAE
130                         throw new IllegalArgumentException("departmentId=" + departmentId + " is invalid"); //NOI18N
131                 } else if (!this.departmentCache.containsKey(departmentId)) {
132                         // Not found
133                         throw new DepartmentNotFoundException(departmentId);
134                 }
135
136                 // Get it from cache
137                 final Department department = this.departmentCache.get(departmentId);
138
139                 // Return it
140                 return department;
141         }
142
143         /**
144          * Getter for a list of filtered departments
145          * <p>
146          * @return Filtered departments
147          */
148         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149         public List<Department> getFilteredDepartments () {
150                 return this.filteredDepartments;
151         }
152
153         /**
154          * Setter for a list of filtered departments
155          * <p>
156          * @param filteredDepartments Filtered departments
157          */
158         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159         public void setFilteredDepartments (final List<Department> filteredDepartments) {
160                 this.filteredDepartments = filteredDepartments;
161         }
162
163         /**
164          * Initializer method
165          */
166         @PostConstruct
167         public void initializeList () {
168                 // Is cache there?
169                 if (!this.departmentCache.iterator().hasNext()) {
170                         // Get whole list from EJB
171                         final List<Department> departments = this.departmentBean.allDepartments();
172
173                         // Add all
174                         for (final Department department : departments) {
175                                 // Add it to cache
176                                 this.departmentCache.put(department.getDepartmentId(), department);
177                         }
178                 }
179
180                 // Is the list empty, but filled cache?
181                 if (this.allDepartments.isEmpty() && this.departmentCache.iterator().hasNext()) {
182                         // Get iterator
183                         final Iterator<Cache.Entry<Long, Department>> iterator = this.departmentCache.iterator();
184
185                         // Build up list
186                         while (iterator.hasNext()) {
187                                 // GEt next element
188                                 final Cache.Entry<Long, Department> next = iterator.next();
189
190                                 // Add to list
191                                 this.allDepartments.add(next.getValue());
192                         }
193
194                         // Sort list
195                         this.allDepartments.sort(new Comparator<Department>() {
196                                 @Override
197                                 public int compare (final Department o1, final Department o2) {
198                                         return o1.getDepartmentId() > o2.getDepartmentId() ? 1 : o1.getDepartmentId() < o2.getDepartmentId() ? -1 : 0;
199                                 }
200                         });
201                 }
202         }
203
204 }