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