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