]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/business/basicdata/FinancialsBasicDataWebRequestBean.java
Please cherry-pick:
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / business / basicdata / FinancialsBasicDataWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 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.basicdata;
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.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote;
36 import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
37 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
38 import org.mxchange.jcountry.model.data.Country;
39 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
40 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
41
42 /**
43  * A business contact bean (controller)
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Named ("basicCompanyDataController")
48 @RequestScoped
49 public class FinancialsBasicDataWebRequestBean extends BaseFinancialsBean implements FinancialsBasicDataWebRequestController {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 56_189_028_928_371L;
55
56         /**
57          * EJB for administrative basic business data purposes
58          */
59         @EJB (lookup = "java:global/jfinancials-ejb/adminBasicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote", description = "A stateless session bean for administrative purposes.")
60         private AdminBasicCompanyDataSessionBeanRemote adminBasicCompanyDataBean;
61
62         /**
63          * List of all basic company data
64          */
65         private final List<BasicData> allBasicData;
66
67         /**
68          * A list of all registered companies (globally)
69          */
70         @Inject
71         @NamedCache (cacheName = "basicDataCache")
72         private Cache<Long, BasicData> basicDataCache;
73
74         /**
75          * EJB for general basic business data purposes
76          */
77         @EJB (lookup = "java:global/jfinancials-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote", description = "A stateless session bean for general purposes.")
78         private BasicCompanyDataSessionBeanRemote businessDataBean;
79
80         /**
81          * Comments for this company
82          */
83         private String companyComments;
84
85         /**
86          * Companies (main) email address (example: info@company.example)
87          */
88         private String companyEmailAddress;
89
90         /**
91          * Company cacheName
92          */
93         private String companyName;
94
95         /**
96          * Area code for fax number
97          */
98         private Integer faxAreaCode;
99
100         /**
101          * Country for fax number
102          */
103         private Country faxCountry;
104
105         /**
106          * Dial number for fax number
107          */
108         private Long faxNumber;
109
110         /**
111          * List of filtered basic company data
112          */
113         private List<BasicData> filteredBasicCompanyData;
114
115         /**
116          * Area code for land-line number
117          */
118         private Integer landLineAreaCode;
119
120         /**
121          * Country for land-line number
122          */
123         private Country landLineCountry;
124
125         /**
126          * Dial number for land-line number
127          */
128         private Long landLineNumber;
129
130         /**
131          * User instance
132          */
133         @Inject
134         private FinancialsUserLoginWebSessionController userLoginController;
135
136         /**
137          * Constructor
138          */
139         public FinancialsBasicDataWebRequestBean () {
140                 // Call super constructor
141                 super();
142
143                 // Init list
144                 this.allBasicData = new LinkedList<>();
145         }
146
147         /**
148          * Observers events being fired when an administrator has added company
149          * basic data.
150          * <p>
151          * @param event Event being fired
152          */
153         public void afterAdminAddedBasicCompanyDataEvent (@Observes final ObservableAdminAddedBusinessBasicDataEvent event) {
154                 // Is the parameter valid?
155                 if (null == event) {
156                         // Throw NPE
157                         throw new NullPointerException("event is null"); //NOI18N
158                 } else if (event.getBasicData() == null) {
159                         // Throw NPE again
160                         throw new NullPointerException("event.basicData is null"); //NOI18N
161                 } else if (event.getBasicData().getBasicDataId() == null) {
162                         // Throw NPE again
163                         throw new NullPointerException("event.basicData.basicDataId is null"); //NOI18N
164                 } else if (event.getBasicData().getBasicDataId() < 1) {
165                         // Throw IAE
166                         throw new IllegalArgumentException(MessageFormat.format("event.basicData.basicDataId={0} is invalid", event.getBasicData().getBasicDataId())); //NOI18N
167                 } else if (event.getBasicData().getCompanyName() == null) {
168                         // Throw NPE again
169                         throw new NullPointerException("event.basicData.companyName is null"); //NOI18N
170                 } else if (event.getBasicData().getCompanyName().isEmpty()) {
171                         // Throw IAE again
172                         throw new IllegalArgumentException("event.basicData.companyName is empty"); //NOI18N
173                 }
174
175                 // Add it to list
176                 this.basicDataCache.put(event.getBasicData().getBasicDataId(), event.getBasicData());
177                 this.allBasicData.add(event.getBasicData());
178         }
179
180         /**
181          * Getter for a list of all business contacts
182          * <p>
183          * @return A list of all business contacts
184          */
185         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
186         public List<BasicData> allBasicData () {
187                 return this.allBasicData;
188         }
189
190         @Override
191         public BasicData findBasicDataById (final Long basicDataId) throws BasicDataNotFoundException {
192                 // Validate parameter
193                 if (null == basicDataId) {
194                         // Throw NPE
195                         throw new NullPointerException("basicDataId is null"); //NOI18N
196                 } else if (basicDataId < 1) {
197                         // Throw IAE
198                         throw new IllegalArgumentException(MessageFormat.format("basicDataId={0} is invalid", basicDataId)); //NOI18N
199                 } else if (!this.basicDataCache.containsKey(basicDataId)) {
200                         // Not found
201                         throw new BasicDataNotFoundException(basicDataId);
202                 }
203
204                 // Get it from cache
205                 final BasicData basicData = this.basicDataCache.get(basicDataId);
206
207                 // Return it
208                 return basicData;
209         }
210
211         /**
212          * Getter for comments
213          * <p>
214          * @return Comments
215          */
216         public String getCompanyComments () {
217                 return this.companyComments;
218         }
219
220         /**
221          * Setter for comments
222          * <p>
223          * @param companyComments Comments
224          */
225         public void setCompanyComments (final String companyComments) {
226                 this.companyComments = companyComments;
227         }
228
229         /**
230          * Getter for company's (main) email address
231          * <p>
232          * @return Company's (main) email address
233          */
234         public String getCompanyEmailAddress () {
235                 return this.companyEmailAddress;
236         }
237
238         /**
239          * Setter for company's (main) email address
240          * <p>
241          * @param companyEmailAddress Company's (main) email address
242          */
243         public void setCompanyEmailAddress (final String companyEmailAddress) {
244                 this.companyEmailAddress = companyEmailAddress;
245         }
246
247         /**
248          * Getter for company cacheName
249          * <p>
250          * @return Company cacheName
251          */
252         public String getCompanyName () {
253                 return this.companyName;
254         }
255
256         /**
257          * Setter for company cacheName
258          * <p>
259          * @param companyName Company cacheName
260          */
261         public void setCompanyName (final String companyName) {
262                 this.companyName = companyName;
263         }
264
265         /**
266          * Getter for fax number's area code
267          * <p>
268          * @return Fax number's area code
269          */
270         public Integer getFaxAreaCode () {
271                 return this.faxAreaCode;
272         }
273
274         /**
275          * Setter for fax number's area code
276          * <p>
277          * @param faxAreaCode Fax number's area code
278          */
279         public void setFaxAreaCode (final Integer faxAreaCode) {
280                 this.faxAreaCode = faxAreaCode;
281         }
282
283         /**
284          * Getter for fax's country instance
285          * <p>
286          * @return Fax' country instance
287          */
288         public Country getFaxCountry () {
289                 return this.faxCountry;
290         }
291
292         /**
293          * Setter for fax's country instance
294          * <p>
295          * @param faxCountry Fax' country instance
296          */
297         public void setFaxCountry (final Country faxCountry) {
298                 this.faxCountry = faxCountry;
299         }
300
301         /**
302          * Getter for fax number
303          * <p>
304          * @return Fax number
305          */
306         public Long getFaxNumber () {
307                 return this.faxNumber;
308         }
309
310         /**
311          * Setter for fax number
312          * <p>
313          * @param faxNumber Fax number
314          */
315         public void setFaxNumber (final Long faxNumber) {
316                 this.faxNumber = faxNumber;
317         }
318
319         /**
320          * Getter for filtered basic company data
321          * <p>
322          * @return Filtered basic company data
323          */
324         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
325         public List<BasicData> getFilteredBasicCompanyData () {
326                 return this.filteredBasicCompanyData;
327         }
328
329         /**
330          * Setter for filtered basic company data
331          * <p>
332          * @param filteredBasicCompanyData Filtered basic company data
333          */
334         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
335         public void setFilteredBasicCompanyData (final List<BasicData> filteredBasicCompanyData) {
336                 this.filteredBasicCompanyData = filteredBasicCompanyData;
337         }
338
339         /**
340          * Getter for land-line number's area code
341          * <p>
342          * @return Land-line number's area code
343          */
344         public Integer getLandLineAreaCode () {
345                 return this.landLineAreaCode;
346         }
347
348         /**
349          * Setter for land-line number's area code
350          * <p>
351          * @param landLineAreaCode Land-line number's area code
352          */
353         public void setLandLineAreaCode (final Integer landLineAreaCode) {
354                 this.landLineAreaCode = landLineAreaCode;
355         }
356
357         /**
358          * Getter for land-line number's country instance
359          * <p>
360          * @return Land-line number's country instance
361          */
362         public Country getLandLineCountry () {
363                 return this.landLineCountry;
364         }
365
366         /**
367          * Setter for land-line number's country instance
368          * <p>
369          * @param landLineCountry Land-line number's country instance
370          */
371         public void setLandLineCountry (final Country landLineCountry) {
372                 this.landLineCountry = landLineCountry;
373         }
374
375         /**
376          * Getter for land-line number
377          * <p>
378          * @return Land-line number
379          */
380         public Long getLandLineNumber () {
381                 return this.landLineNumber;
382         }
383
384         /**
385          * Setter for land-line number
386          * <p>
387          * @param landLineNumber Land-line number
388          */
389         public void setLandLineNumber (final Long landLineNumber) {
390                 this.landLineNumber = landLineNumber;
391         }
392
393         /**
394          * Initializer method
395          */
396         @PostConstruct
397         public void initializeList () {
398                 // Is cache there?
399                 if (!this.basicDataCache.iterator().hasNext()) {
400                         // Get whole list
401                         final List<BasicData> basicDatas = this.businessDataBean.allBusinessBasicData();
402
403                         // Add all
404                         for (final BasicData basicData : basicDatas) {
405                                 // Add it to cache
406                                 this.basicDataCache.put(basicData.getBasicDataId(), basicData);
407                         }
408                 }
409
410                 // Is cache there and list is not full?
411                 if ((this.allBasicData.isEmpty()) && (this.basicDataCache.iterator().hasNext())) {
412                         // Get iterator
413                         final Iterator<Cache.Entry<Long, BasicData>> iterator = this.basicDataCache.iterator();
414
415                         // Build up list
416                         while (iterator.hasNext()) {
417                                 // GEt next element
418                                 final Cache.Entry<Long, BasicData> next = iterator.next();
419
420                                 // Add to list
421                                 this.allBasicData.add(next.getValue());
422                         }
423
424                         // Sort list
425                         this.allBasicData.sort(new Comparator<BasicData>() {
426                                 @Override
427                                 public int compare (final BasicData o1, final BasicData o2) {
428                                         return o1.getBasicDataId() > o2.getBasicDataId() ? 1 : o1.getBasicDataId() < o2.getBasicDataId() ? -1 : 0;
429                                 }
430                         });
431                 }
432         }
433
434         @Override
435         public Boolean isCompanyNameUsed (final String companyName) {
436                 // Validate parameter
437                 if (null == companyName) {
438                         // Throw NPE
439                         throw new NullPointerException("companyName is null"); //NOI18N
440                 } else if (companyName.isEmpty()) {
441                         // Throw IAE
442                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
443                 }
444
445                 // Default is not found
446                 boolean isFound = false;
447
448                 // Check all entries
449                 for (final BasicData basicData : this.allBasicData()) {
450                         // Is same company name?
451                         if (Objects.equals(basicData.getCompanyName(), companyName)) {
452                                 // Found it
453                                 isFound = true;
454                                 break;
455                         }
456                 }
457
458                 // Return flag
459                 return isFound;
460         }
461
462         @Override
463         public Boolean isEmailAddressRegistered (final String emailAddress) {
464                 // Validate parameter
465                 if (null == emailAddress) {
466                         // Throw NPE
467                         throw new NullPointerException("emailAddress is null"); //NOI18N
468                 } else if (emailAddress.isEmpty()) {
469                         // Throw IAE
470                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
471                 }
472
473                 // Default is not found
474                 boolean isFound = false;
475
476                 // Check all entries
477                 for (final BasicData basicData : this.allBasicData()) {
478                         // Is email address used?
479                         if (Objects.equals(basicData.getCompanyEmailAddress(), emailAddress)) {
480                                 // Found it
481                                 isFound = true;
482                                 break;
483                         }
484                 }
485
486                 // Return flag
487                 return isFound;
488         }
489
490         /**
491          * Clears this bean
492          */
493         private void clear () {
494                 // Clear all data:
495                 this.setCompanyComments(null);
496                 this.setCompanyEmailAddress(null);
497                 this.setCompanyName(null);
498                 this.setFaxAreaCode(null);
499                 this.setFaxCountry(null);
500                 this.setFaxNumber(null);
501                 this.setLandLineAreaCode(null);
502                 this.setLandLineCountry(null);
503                 this.setLandLineNumber(null);
504         }
505
506 }