]> git.mxchange.org Git - jfinancials-war.git/blob
ad8a2f1c00de33cf1ca133c11bfcdb15b8b0e835
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017, 2018 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.ObservableBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38
39 /**
40  * A list bean for branch offices
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("branchOfficeListController")
45 @ViewScoped
46 public class FinancialsBranchOfficeListWebViewBean extends BaseFinancialsBean implements FinancialsBranchOfficeListWebViewController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 5_028_697_360_462L;
52
53         /**
54          * A list of all branch offices
55          */
56         private final List<BranchOffice> allBranchOffices;
57
58         /**
59          * EJB for administrative purposes
60          */
61         @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
62         private BranchOfficeSessionBeanRemote branchOfficeBean;
63
64         /**
65          * A list of all branch offices (globally)
66          */
67         @Inject
68         @NamedCache (cacheName = "branchOfficeCache")
69         private transient Cache<Long, BranchOffice> branchOfficeCache;
70
71         /**
72          * A list of filtered branch offices
73          */
74         private List<BranchOffice> filteredBranchOffices;
75
76         /**
77          * Selected branch office instance
78          */
79         private BranchOffice selectedBranchOffice;
80
81         /**
82          * Default constructor
83          */
84         public FinancialsBranchOfficeListWebViewBean () {
85                 // Call super constructor
86                 super();
87
88                 // Init list
89                 this.allBranchOffices = new LinkedList<>();
90         }
91
92         /**
93          * Observes events being fired when a branch office has been added.
94          * <p>
95          * @param event Event being fired
96          * <p>
97          * @throws NullPointerException If the parameter or it's carried instance is
98          * null
99          * @throws IllegalArgumentException If the branchId is zero or lower
100          */
101         public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
102                 // Validate parameter
103                 if (null == event) {
104                         // Throw NPE
105                         throw new NullPointerException("event is null"); //NOI18N
106                 } else if (event.getBranchOffice() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("event.branchOffice is null"); //NOI18N
109                 } else if (event.getBranchOffice().getBranchId() == null) {
110                         // Throw it again
111                         throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
112                 } else if (event.getBranchOffice().getBranchId() < 1) {
113                         // Throw IAE
114                         throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
115                 }
116
117                 // Add instance to cache
118                 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
119                 this.allBranchOffices.add(event.getBranchOffice());
120         }
121
122         @Override
123         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
124         public List<BranchOffice> allBranchOffices () {
125                 return this.allBranchOffices;
126         }
127
128         @Override
129         public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
130                 // Validate parameter
131                 if (null == branchOfficeId) {
132                         // Throw NPE
133                         throw new NullPointerException("branchOfficeId is null"); //NOI18N
134                 } else if (branchOfficeId < 1) {
135                         // Throw IAE
136                         throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
137                 } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
138                         // Not found
139                         throw new BranchOfficeNotFoundException(branchOfficeId);
140                 }
141
142                 // Get it from cache
143                 final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
144
145                 // Return it
146                 return branchOffice;
147         }
148
149         /**
150          * Getter for a list of filtered branch offices
151          * <p>
152          * @return Filtered branch offices
153          */
154         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
155         public List<BranchOffice> getFilteredBranchOffices () {
156                 return this.filteredBranchOffices;
157         }
158
159         /**
160          * Setter for a list of filtered branch offices
161          * <p>
162          * @param filteredBranchOffices Filtered branch offices
163          */
164         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
165         public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
166                 this.filteredBranchOffices = filteredBranchOffices;
167         }
168
169         /**
170          * Getter for selected branch office
171          * <p>
172          * @return Selected branch office
173          */
174         public BranchOffice getSelectedBranchOffice () {
175                 return this.selectedBranchOffice;
176         }
177
178         /**
179          * Setter for selected branch office
180          * <p>
181          * @param selectedBranchOffice Selected branch office
182          */
183         public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
184                 this.selectedBranchOffice = selectedBranchOffice;
185         }
186
187         /**
188          * Initializer method
189          */
190         @PostConstruct
191         public void initializeList () {
192                 // Is cache there?
193                 if (!this.branchOfficeCache.iterator().hasNext()) {
194                         // Get whole list
195                         final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
196
197                         // Add all
198                         for (final BranchOffice branchOffice : branchOffices) {
199                                 // Add it to cache
200                                 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
201                         }
202                 }
203
204                 // Is the list empty, but filled cache?
205                 if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
206                         // Get iterator
207                         final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
208
209                         // Build up list
210                         while (iterator.hasNext()) {
211                                 // GEt next element
212                                 final Cache.Entry<Long, BranchOffice> next = iterator.next();
213
214                                 // Add to list
215                                 this.allBranchOffices.add(next.getValue());
216                         }
217
218                         // Sort list
219                         this.allBranchOffices.sort(new Comparator<BranchOffice>() {
220                                 @Override
221                                 public int compare (final BranchOffice o1, final BranchOffice o2) {
222                                         return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;
223                                 }
224                         });
225                 }
226         }
227
228         @Override
229         public Boolean isEmailAddressRegistered (final String emailAddress) {
230                 // Validate parameter
231                 if (null == emailAddress) {
232                         // Throw NPE
233                         throw new NullPointerException("emailAddress is null"); //NOI18N
234                 } else if (emailAddress.isEmpty()) {
235                         // Throw IAE
236                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
237                 }
238
239                 // Default is not found
240                 boolean isFound = false;
241
242                 // Check all entries
243                 for (final BranchOffice branchOffice : this.allBranchOffices()) {
244                         // Is email address used?
245                         if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
246                                 // Found it
247                                 isFound = true;
248                                 break;
249                         }
250                 }
251
252                 // Return flag
253                 return isFound;
254         }
255
256         @Override
257         protected Object clone () throws CloneNotSupportedException {
258                 return super.clone(); //To change body of generated methods, choose Tools | Templates.
259         }
260
261 }