]> git.mxchange.org Git - jfinancials-war.git/blob
f525430897cb315c87ec38c82b404d7bf87062f0
[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.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.jfinancials.beans.BaseFinancialsBean;
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 FinancialsBranchOfficeListWebViewBean extends BaseFinancialsBean implements FinancialsBranchOfficeListWebViewController {
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/jfinancials-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 FinancialsBranchOfficeListWebViewBean () {
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                 // Add instance to cache
116                 this.branchOfficeCache.put(event.getAddedBranchOffice().getBranchId(), event.getAddedBranchOffice());
117                 this.uniqueAddBranchOffice(event.getAddedBranchOffice());
118         }
119
120         /**
121          * Observes events being fired when a branch office has been updated by an
122          * administrator.
123          * <p>
124          * @param event Event being fired
125          */
126         public void afterAdminBranchOfficeUpdatedEvent (@Observes final ObservableAdminBranchOfficeUpdatedEvent event) {
127                 // Validate parameter
128                 if (null == event) {
129                         // Throw NPE
130                         throw new NullPointerException("event is null"); //NOI18N
131                 } else if (event.getUpdatedBranchOffice() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("event.updatedBranchOffice is null"); //NOI18N
134                 } else if (event.getUpdatedBranchOffice().getBranchId() == null) {
135                         // Throw it again
136                         throw new NullPointerException("event.updatedBranchOffice .branchId is null"); //NOI18N
137                 } else if (event.getUpdatedBranchOffice().getBranchId() < 1) {
138                         // Throw IAE
139                         throw new IllegalArgumentException(MessageFormat.format("event.updatedBranchOffice .branchId={0} is not valid", event.getUpdatedBranchOffice().getBranchId())); //NOI18N
140                 }
141
142                 // Add instance to cache
143                 this.branchOfficeCache.put(event.getUpdatedBranchOffice().getBranchId(), event.getUpdatedBranchOffice());
144                 this.uniqueAddBranchOffice(event.getUpdatedBranchOffice());
145         }
146
147         @Override
148         public BranchOffice findBranchOfficeById (final Long branchId) throws BranchOfficeNotFoundException {
149                 // Validate parameter
150                 if (null == branchId) {
151                         // Throw NPE
152                         throw new NullPointerException("branchId is null"); //NOI18N
153                 } else if (branchId < 1) {
154                         // Throw IAE
155                         throw new IllegalArgumentException(MessageFormat.format("branchId={0} is invalid", branchId)); //NOI18N
156                 } else if (!this.branchOfficeCache.containsKey(branchId)) {
157                         // Not found
158                         throw new BranchOfficeNotFoundException(branchId);
159                 }
160
161                 // Get it from cache
162                 final BranchOffice branchOffice = this.branchOfficeCache.get(branchId);
163
164                 // Return it
165                 return branchOffice;
166         }
167
168         @Override
169         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
170         public List<BranchOffice> getAllBranchOffices () {
171                 return this.allBranchOffices;
172         }
173
174         /**
175          * Getter for a list of filtered branch offices
176          * <p>
177          * @return Filtered branch offices
178          */
179         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
180         public List<BranchOffice> getFilteredBranchOffices () {
181                 return this.filteredBranchOffices;
182         }
183
184         /**
185          * Setter for a list of filtered branch offices
186          * <p>
187          * @param filteredBranchOffices Filtered branch offices
188          */
189         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
190         public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
191                 this.filteredBranchOffices = filteredBranchOffices;
192         }
193
194         /**
195          * Getter for selected branch office
196          * <p>
197          * @return Selected branch office
198          */
199         public BranchOffice getSelectedBranchOffice () {
200                 return this.selectedBranchOffice;
201         }
202
203         /**
204          * Setter for selected branch office
205          * <p>
206          * @param selectedBranchOffice Selected branch office
207          */
208         public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
209                 this.selectedBranchOffice = selectedBranchOffice;
210         }
211
212         /**
213          * Initializer method
214          */
215         @PostConstruct
216         public void initializeList () {
217                 // Is cache there?
218                 if (!this.branchOfficeCache.iterator().hasNext()) {
219                         // Add all
220                         for (final BranchOffice branchOffice : this.branchOfficeBean.fetchAllBranchOffices()) {
221                                 // Add it to cache
222                                 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
223                         }
224                 }
225
226                 // Is the list empty, but filled cache?
227                 if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
228                         // Build up list
229                         for (final Cache.Entry<Long, BranchOffice> currentEntry : this.branchOfficeCache) {
230                                 // Add to list
231                                 this.getAllBranchOffices().add(currentEntry.getValue());
232                         }
233
234                         // Sort list
235                         this.getAllBranchOffices().sort(new Comparator<BranchOffice>() {
236                                 @Override
237                                 public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) {
238                                         return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0;
239                                 }
240                         });
241                 }
242         }
243
244         @Override
245         public Boolean isEmailAddressRegistered (final String emailAddress) {
246                 // Validate parameter
247                 if (null == emailAddress) {
248                         // Throw NPE
249                         throw new NullPointerException("emailAddress is null"); //NOI18N
250                 } else if (emailAddress.isEmpty()) {
251                         // Throw IAE
252                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
253                 }
254
255                 // Default is not found
256                 boolean isFound = false;
257
258                 // Check all entries
259                 for (final BranchOffice branchOffice : this.getAllBranchOffices()) {
260                         // Is email address used?
261                         if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
262                                 // Found it
263                                 isFound = true;
264                                 break;
265                         }
266                 }
267
268                 // Return flag
269                 return isFound;
270         }
271
272         /**
273          * Uniquely add branch office instance to allBranchOffices property
274          * <p>
275          * @param branchOffice Branch office being added
276          */
277         private void uniqueAddBranchOffice (final BranchOffice branchOffice) {
278                 // Get iterator
279                 final Iterator<BranchOffice> iterator = this.getAllBranchOffices().iterator();
280
281                 // Iterate over all
282                 while (iterator.hasNext()) {
283                         // Get current element
284                         final BranchOffice currentBranchOffice = iterator.next();
285
286                         // Does primary key match?
287                         if (Objects.equals(branchOffice.getBranchId(), currentBranchOffice.getBranchId())) {
288                                 // Yes then remove this one
289                                 iterator.remove();
290
291                                 // Re-add maybe updated version
292                                 this.getAllBranchOffices().add(branchOffice);
293
294                                 // Stop iteration
295                                 break;
296                         }
297                 }
298         }
299
300         @Override
301         protected Object clone () throws CloneNotSupportedException {
302                 return super.clone(); //To change body of generated methods, choose Tools | Templates.
303         }
304
305 }