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