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