]> git.mxchange.org Git - jfinancials-war.git/blob
d1590740cbe0b50886147dfe22fe0c8ee01c11d4
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 Roland Häder
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;
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.context.RequestScoped;
30 import javax.enterprise.event.Observes;
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 general bean for branch offices
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("branchOfficeController")
45 @RequestScoped
46 public class FinancialsBranchOfficeWebRequestBean extends BaseFinancialsBean implements FinancialsBranchOfficeWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 5_028_697_360_461L;
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 Cache<Long, BranchOffice> branchOfficeCache;
70
71         /**
72          * A list of filtered branch offices
73          */
74         private List<BranchOffice> filteredBranchOffices;
75
76         /**
77          * Default constructor
78          */
79         public FinancialsBranchOfficeWebRequestBean () {
80                 // Call super constructor
81                 super();
82
83                 // Init list
84                 this.allBranchOffices = new LinkedList<>();
85         }
86
87         /**
88          * Observes events being fired when a branch office has been added.
89          * <p>
90          * @param event Event being fired
91          * <p>
92          * @throws NullPointerException If the parameter or it's carried instance is
93          * null
94          * @throws IllegalArgumentException If the branchId is zero or lower
95          */
96         public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
97                 // Validate parameter
98                 if (null == event) {
99                         // Throw NPE
100                         throw new NullPointerException("event is null"); //NOI18N
101                 } else if (event.getBranchOffice() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("event.branchOffice is null"); //NOI18N
104                 } else if (event.getBranchOffice().getBranchId() == null) {
105                         // Throw it again
106                         throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
107                 } else if (event.getBranchOffice().getBranchId() < 1) {
108                         // Throw IAE
109                         throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
110                 }
111
112                 // Add instance to cache
113                 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
114                 this.allBranchOffices.add(event.getBranchOffice());
115         }
116
117         @Override
118         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
119         public List<BranchOffice> allBranchOffices () {
120                 return this.allBranchOffices;
121         }
122
123         @Override
124         public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
125                 // Validate parameter
126                 if (null == branchOfficeId) {
127                         // Throw NPE
128                         throw new NullPointerException("branchOfficeId is null"); //NOI18N
129                 } else if (branchOfficeId < 1) {
130                         // Throw IAE
131                         throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
132                 } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
133                         // Not found
134                         throw new BranchOfficeNotFoundException(branchOfficeId);
135                 }
136
137                 // Get it from cache
138                 final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
139
140                 // Return it
141                 return branchOffice;
142         }
143
144         /**
145          * Getter for a list of filtered branch offices
146          * <p>
147          * @return Filtered branch offices
148          */
149         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
150         public List<BranchOffice> getFilteredBranchOffices () {
151                 return this.filteredBranchOffices;
152         }
153
154         /**
155          * Setter for a list of filtered branch offices
156          * <p>
157          * @param filteredBranchOffices Filtered branch offices
158          */
159         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
160         public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
161                 this.filteredBranchOffices = filteredBranchOffices;
162         }
163
164         /**
165          * Initializer method
166          */
167         @PostConstruct
168         public void initializeList () {
169                 // Is cache there?
170                 if (!this.branchOfficeCache.iterator().hasNext()) {
171                         // Get whole list
172                         final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
173
174                         // Add all
175                         for (final BranchOffice branchOffice : branchOffices) {
176                                 // Add it to cache
177                                 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
178                         }
179                 }
180
181                 // Is the list empty, but filled cache?
182                 if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
183                         // Get iterator
184                         final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
185
186                         // Build up list
187                         while (iterator.hasNext()) {
188                                 // GEt next element
189                                 final Cache.Entry<Long, BranchOffice> next = iterator.next();
190
191                                 // Add to list
192                                 this.allBranchOffices.add(next.getValue());
193                         }
194
195                         // Sort list
196                         this.allBranchOffices.sort(new Comparator<BranchOffice>() {
197                                 @Override
198                                 public int compare (final BranchOffice o1, final BranchOffice o2) {
199                                         return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;
200                                 }
201                         });
202                 }
203         }
204
205         @Override
206         public Boolean isEmailAddressRegistered (final String emailAddress) {
207                 // Default is not found
208                 boolean isFound = false;
209
210                 // Check all entries
211                 for (final BranchOffice branchOffice : this.allBranchOffices()) {
212                         // Is email address used?
213                         if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
214                                 // Found it
215                                 isFound = true;
216                                 break;
217                         }
218                 }
219
220                 // Return flag
221                 return isFound;
222         }
223
224 }