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