]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/business/branchoffice/list/JobsBranchOfficeListWebViewBean.java
Updated copyright year
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / business / branchoffice / list / JobsBranchOfficeListWebViewBean.java
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.jjobs.beans.business.branchoffice.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.branchoffice.added.ObservableAdminBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.events.branchoffice.updated.ObservableAdminBranchOfficeUpdatedEvent;
35 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
37 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
38 import org.mxchange.jjobs.beans.BaseJobsBean;
39
40 /**
41  * A list bean for branch offices
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("branchOfficeListController")
46 @ViewScoped
47 public class JobsBranchOfficeListWebViewBean extends BaseJobsBean implements JobsBranchOfficeListWebViewController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 5_028_697_360_468L;
53
54         /**
55          * A list of all branch offices
56          */
57         private final List<BranchOffice> allBranchOffices;
58
59         /**
60          * EJB for administrative purposes
61          */
62         @EJB (lookup = "java:global/jjobs-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
63         private BranchOfficeSessionBeanRemote branchOfficeBean;
64
65         /**
66          * A list of all branch offices (globally)
67          */
68         @Inject
69         @NamedCache (cacheName = "branchOfficeCache")
70         private transient Cache<Long, BranchOffice> branchOfficeCache;
71
72         /**
73          * A list of filtered branch offices
74          */
75         private List<BranchOffice> filteredBranchOffices;
76
77         /**
78          * Selected branch office instance
79          */
80         private BranchOffice selectedBranchOffice;
81
82         /**
83          * Default constructor
84          */
85         public JobsBranchOfficeListWebViewBean () {
86                 // Call super constructor
87                 super();
88
89                 // Init list
90                 this.allBranchOffices = new LinkedList<>();
91         }
92
93         /**
94          * Observes events being fired when a branch office has been added by an
95          * administrator.
96          * <p>
97          * @param event Event being fired
98          */
99         public void afterAdminBranchOfficeAddedEvent (@Observes final ObservableAdminBranchOfficeAddedEvent event) {
100                 // Validate parameter
101                 if (null == event) {
102                         // Throw NPE
103                         throw new NullPointerException("event is null"); //NOI18N
104                 } else if (event.getAddedBranchOffice() == null) {
105                         // Throw NPE again
106                         throw new NullPointerException("event.addedBranchOffice is null"); //NOI18N
107                 } else if (event.getAddedBranchOffice().getBranchId() == null) {
108                         // Throw it again
109                         throw new NullPointerException("event.addedBranchOffice .branchId is null"); //NOI18N
110                 } else if (event.getAddedBranchOffice().getBranchId() < 1) {
111                         // Throw IAE
112                         throw new IllegalArgumentException(MessageFormat.format("event.addedBranchOffice .branchId={0} is not valid", event.getAddedBranchOffice().getBranchId())); //NOI18N
113                 }
114
115                 // Uniquely add branch office
116                 this.uniqueAddBranchOffice(event.getAddedBranchOffice());
117         }
118
119         /**
120          * Observes events being fired when a branch office has been updated by an
121          * administrator.
122          * <p>
123          * @param event Event being fired
124          */
125         public void afterAdminBranchOfficeUpdatedEvent (@Observes final ObservableAdminBranchOfficeUpdatedEvent event) {
126                 // Validate parameter
127                 if (null == event) {
128                         // Throw NPE
129                         throw new NullPointerException("event is null"); //NOI18N
130                 } else if (event.getUpdatedBranchOffice() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("event.updatedBranchOffice is null"); //NOI18N
133                 } else if (event.getUpdatedBranchOffice().getBranchId() == null) {
134                         // Throw it again
135                         throw new NullPointerException("event.updatedBranchOffice .branchId is null"); //NOI18N
136                 } else if (event.getUpdatedBranchOffice().getBranchId() < 1) {
137                         // Throw IAE
138                         throw new IllegalArgumentException(MessageFormat.format("event.updatedBranchOffice .branchId={0} is not valid", event.getUpdatedBranchOffice().getBranchId())); //NOI18N
139                 }
140
141                 // Uniquely update branch office
142                 this.uniqueAddBranchOffice(event.getUpdatedBranchOffice());
143         }
144
145         @Override
146         public BranchOffice findBranchOfficeById (final Long branchId) throws BranchOfficeNotFoundException {
147                 // Validate parameter
148                 if (null == branchId) {
149                         // Throw NPE
150                         throw new NullPointerException("branchId is null"); //NOI18N
151                 } else if (branchId < 1) {
152                         // Throw IAE
153                         throw new IllegalArgumentException(MessageFormat.format("branchId={0} is invalid", branchId)); //NOI18N
154                 } else if (!this.branchOfficeCache.containsKey(branchId)) {
155                         // Not found
156                         throw new BranchOfficeNotFoundException(branchId);
157                 }
158
159                 // Get it from cache
160                 final BranchOffice branchOffice = this.branchOfficeCache.get(branchId);
161
162                 // Return it
163                 return branchOffice;
164         }
165
166         @Override
167         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
168         public List<BranchOffice> getAllBranchOffices () {
169                 return this.allBranchOffices;
170         }
171
172         /**
173          * Getter for a list of filtered branch offices
174          * <p>
175          * @return Filtered branch offices
176          */
177         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
178         public List<BranchOffice> getFilteredBranchOffices () {
179                 return this.filteredBranchOffices;
180         }
181
182         /**
183          * Setter for a list of filtered branch offices
184          * <p>
185          * @param filteredBranchOffices Filtered branch offices
186          */
187         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
188         public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
189                 this.filteredBranchOffices = filteredBranchOffices;
190         }
191
192         /**
193          * Getter for selected branch office
194          * <p>
195          * @return Selected branch office
196          */
197         public BranchOffice getSelectedBranchOffice () {
198                 return this.selectedBranchOffice;
199         }
200
201         /**
202          * Setter for selected branch office
203          * <p>
204          * @param selectedBranchOffice Selected branch office
205          */
206         public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
207                 this.selectedBranchOffice = selectedBranchOffice;
208         }
209
210         /**
211          * Initializer method
212          */
213         @PostConstruct
214         public void initializeList () {
215                 // Is cache there?
216                 if (!this.branchOfficeCache.iterator().hasNext()) {
217                         // Add all
218                         for (final BranchOffice branchOffice : this.branchOfficeBean.fetchAllBranchOffices()) {
219                                 // Add it to cache
220                                 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
221                         }
222                 }
223
224                 // Is the list empty, but filled cache?
225                 if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
226                         // Build up list
227                         for (final Cache.Entry<Long, BranchOffice> currentEntry : this.branchOfficeCache) {
228                                 // Add to list
229                                 this.getAllBranchOffices().add(currentEntry.getValue());
230                         }
231
232                         // Sort list
233                         this.getAllBranchOffices().sort(new Comparator<BranchOffice>() {
234                                 @Override
235                                 public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) {
236                                         return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0;
237                                 }
238                         });
239                 }
240         }
241
242         @Override
243         public Boolean isEmailAddressRegistered (final String emailAddress) {
244                 // Validate parameter
245                 if (null == emailAddress) {
246                         // Throw NPE
247                         throw new NullPointerException("emailAddress is null"); //NOI18N
248                 } else if (emailAddress.isEmpty()) {
249                         // Throw IAE
250                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
251                 }
252
253                 // Default is not found
254                 boolean isFound = false;
255
256                 // Check all entries
257                 for (final BranchOffice branchOffice : this.getAllBranchOffices()) {
258                         // Is email address used?
259                         if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
260                                 // Found it
261                                 isFound = true;
262                                 break;
263                         }
264                 }
265
266                 // Return flag
267                 return isFound;
268         }
269
270         /**
271          * Uniquely add branch office instance to allBranchOffices property
272          * <p>
273          * @param branchOffice Branch office being added
274          */
275         private void uniqueAddBranchOffice (final BranchOffice branchOffice) {
276                 // Add instance to cache
277                 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
278
279                 // Get iterator
280                 final Iterator<BranchOffice> iterator = this.getAllBranchOffices().iterator();
281
282                 // Iterate over all
283                 while (iterator.hasNext()) {
284                         // Get current element
285                         final BranchOffice currentBranchOffice = iterator.next();
286
287                         // Does primary key match?
288                         if (Objects.equals(branchOffice.getBranchId(), currentBranchOffice.getBranchId())) {
289                                 // Yes then remove this one
290                                 iterator.remove();
291
292                                 // Re-add maybe updated version
293                                 this.getAllBranchOffices().add(branchOffice);
294
295                                 // Stop iteration
296                                 break;
297                         }
298                 }
299         }
300
301         @Override
302         protected Object clone () throws CloneNotSupportedException {
303                 return super.clone(); //To change body of generated methods, choose Tools | Templates.
304         }
305
306 }