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