]> git.mxchange.org Git - jfinancials-war.git/blob
f8e5f30214a27b7123d545913cccd9f3335f1905
[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.headquarter;
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.headquarter.added.ObservableHeadquarterAddedEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
36 import org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38
39 /**
40  * A general bean for headquarter
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("headquarterController")
45 @RequestScoped
46 public class FinancialsHeadquarterWebRequestBean extends BaseFinancialsBean implements FinancialsHeadquarterWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 5_028_697_360_465L;
52
53         /**
54          * A list of all headquarter
55          */
56         private final List<Headquarter> allHeadquarter;
57
58         /**
59          * A list of filtered headquarter
60          */
61         private List<Headquarter> filteredHeadquarter;
62
63         /**
64          * EJB for administrative purposes
65          */
66         @EJB (lookup = "java:global/jfinancials-ejb/headquarter!org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote")
67         private HeadquarterSessionBeanRemote headquarterBean;
68
69         /**
70          * A list of all headquarter (globally)
71          */
72         @Inject
73         @NamedCache (cacheName = "headquarterCache")
74         private Cache<Long, Headquarter> headquarterCache;
75
76         /**
77          * Default constructor
78          */
79         public FinancialsHeadquarterWebRequestBean () {
80                 // Call super constructor
81                 super();
82
83                 // Init list
84                 this.allHeadquarter = 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 afterHeadquarterAddedEvent (@Observes final ObservableHeadquarterAddedEvent event) {
97                 // Validate parameter
98                 if (null == event) {
99                         // Throw NPE
100                         throw new NullPointerException("event is null"); //NOI18N
101                 } else if (event.getHeadquarter() == null) {
102                         // Throw NPE again
103                         throw new NullPointerException("event.headquarter is null"); //NOI18N
104                 } else if (event.getHeadquarter().getHeadquarterId() == null) {
105                         // Throw it again
106                         throw new NullPointerException("event.headquarter.branchId is null"); //NOI18N
107                 } else if (event.getHeadquarter().getHeadquarterId() < 1) {
108                         // Throw IAE
109                         throw new IllegalArgumentException(MessageFormat.format("event.headquarter.branchId={0} is not valid", event.getHeadquarter().getHeadquarterId())); //NOI18N
110                 }
111
112                 // Add instance to cache
113                 this.headquarterCache.put(event.getHeadquarter().getHeadquarterId(), event.getHeadquarter());
114                 this.allHeadquarter.add(event.getHeadquarter());
115         }
116
117         @Override
118         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
119         public List<Headquarter> allHeadquarter () {
120                 return this.allHeadquarter;
121         }
122
123         @Override
124         public Headquarter findHeadquarterById (final Long headquarterId) throws HeadquarterNotFoundException {
125                 // Validate parameter
126                 if (null == headquarterId) {
127                         // Throw NPE
128                         throw new NullPointerException("headquarterId is null"); //NOI18N
129                 } else if (headquarterId < 1) {
130                         // Throw IAE
131                         throw new IllegalArgumentException(MessageFormat.format("headquarterId={0} is invalid", headquarterId)); //NOI18N
132                 } else if (!this.headquarterCache.containsKey(headquarterId)) {
133                         // Not found
134                         throw new HeadquarterNotFoundException(headquarterId);
135                 }
136
137                 // Get it from cache
138                 final Headquarter headquarter = this.headquarterCache.get(headquarterId);
139
140                 // Return it
141                 return headquarter;
142         }
143
144         /**
145          * Getter for a list of filtered headquarter
146          * <p>
147          * @return Filtered headquarter
148          */
149         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
150         public List<Headquarter> getFilteredHeadquarter () {
151                 return this.filteredHeadquarter;
152         }
153
154         /**
155          * Setter for a list of filtered headquarter
156          * <p>
157          * @param filteredHeadquarter Filtered headquarter
158          */
159         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
160         public void setFilteredHeadquarter (final List<Headquarter> filteredHeadquarter) {
161                 this.filteredHeadquarter = filteredHeadquarter;
162         }
163
164         /**
165          * Initializer method
166          */
167         @PostConstruct
168         public void initializeList () {
169                 // Is cache there?
170                 if (!this.headquarterCache.iterator().hasNext()) {
171                         // Get whole list from EJB
172                         final List<Headquarter> Headquarter = this.headquarterBean.allHeadquarters();
173
174                         // Add all
175                         for (final Headquarter headquarter : Headquarter) {
176                                 // Add it to cache
177                                 this.headquarterCache.put(headquarter.getHeadquarterId(), headquarter);
178                         }
179                 }
180
181                 // Is the list empty, but filled cache?
182                 if (this.allHeadquarter.isEmpty() && this.headquarterCache.iterator().hasNext()) {
183                         // Get iterator
184                         final Iterator<Cache.Entry<Long, Headquarter>> iterator = this.headquarterCache.iterator();
185
186                         // Build up list
187                         while (iterator.hasNext()) {
188                                 // GEt next element
189                                 final Cache.Entry<Long, Headquarter> next = iterator.next();
190
191                                 // Add to list
192                                 this.allHeadquarter.add(next.getValue());
193                         }
194
195                         // Sort list
196                         this.allHeadquarter.sort(new Comparator<Headquarter>() {
197                                 @Override
198                                 public int compare (final Headquarter o1, final Headquarter o2) {
199                                         return o1.getHeadquarterId() > o2.getHeadquarterId() ? 1 : o1.getHeadquarterId() < o2.getHeadquarterId() ? -1 : 0;
200                                 }
201                         });
202                 }
203         }
204
205         @Override
206         public Boolean isCompanyNameUsed (final String companyName) {
207                 // Validate parameter
208                 if (null == companyName) {
209                         // Throw NPE
210                         throw new NullPointerException("companyName is null"); //NOI18N
211                 } else if (companyName.isEmpty()) {
212                         // Throw IAE
213                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
214                 }
215
216                 // Default is not found
217                 boolean isFound = false;
218
219                 // Check all entries
220                 for (final Headquarter headquarter : this.allHeadquarter()) {
221                         // Is same company name?
222                         if (Objects.equals(headquarter.getHeadquarterCompanyName(), companyName)) {
223                                 // Found it
224                                 isFound = true;
225                                 break;
226                         }
227                 }
228
229                 // Return flag
230                 return isFound;
231         }
232
233         @Override
234         public Boolean isEmailAddressRegistered (final String emailAddress) {
235                 // Validate parameter
236                 if (null == emailAddress) {
237                         // Throw NPE
238                         throw new NullPointerException("emailAddress is null"); //NOI18N
239                 } else if (emailAddress.isEmpty()) {
240                         // Throw IAE
241                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
242                 }
243
244                 // Default is not found
245                 boolean isFound = false;
246
247                 // Check all entries
248                 for (final Headquarter headquarter : this.allHeadquarter()) {
249                         // Is email address used?
250                         if (Objects.equals(headquarter.getHeadquarterEmailAddress(), emailAddress)) {
251                                 // Found it
252                                 isFound = true;
253                                 break;
254                         }
255                 }
256
257                 // Return flag
258                 return isFound;
259         }
260
261 }