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