]> git.mxchange.org Git - pizzaservice-war.git/blob
3b99e8721ef12f5b1eca1a0d5c462406b31595e7
[pizzaservice-war.git] /
1 /*
2  * Copyright (C) 2022 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.branchoffice;
18
19 import java.util.List;
20 import javax.ejb.EJB;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.jcontactsbusiness.events.branchoffice.added.BranchOfficeAddedEvent;
27 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
28 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
29 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote;
31 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
32 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
33 import org.mxchange.jcontactsbusiness.model.branchoffice.CompanyBranchOffice;
34 import org.mxchange.jcontactsbusiness.model.employee.Employee;
35 import org.mxchange.jcountry.model.data.Country;
36 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
37 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
38 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
42
43 /**
44  * An administrative bean for branch offices
45  * <p>
46  * @author Roland Häder<roland@mxchange.org>
47  */
48 @Named ("adminBranchOfficeController")
49 @RequestScoped
50 public class PizzaAdminBranchOfficeWebRequestBean extends BasePizzaBean implements PizzaAdminBranchOfficeWebRequestController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 5_028_697_360_461L;
56
57         /**
58          * EJB for administrative purposes
59          */
60         @EJB (lookup = "java:global/pizzaservice-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
61         private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
62
63         /**
64          * City
65          */
66         private String branchCity;
67
68         /**
69          * Assigned company for this branch office
70          */
71         private BusinessBasicData branchCompany;
72
73         /**
74          * Contact person in branch office
75          */
76         private Employee branchContactEmployee;
77
78         /**
79          * Country
80          */
81         private Country branchCountry;
82
83         /**
84          * Email address
85          */
86         private String branchEmailAddress;
87
88         /**
89          * House number
90          */
91         private Short branchHouseNumber;
92
93         /**
94          * An event being fired when a branch office has been successfully added
95          */
96         @Inject
97         @Any
98         private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
99
100         /**
101          * A general branch office controller (backing bean)
102          */
103         @Inject
104         private PizzaBranchOfficeWebRequestController branchOfficeController;
105
106         /**
107          * Store
108          */
109         private Short branchStore;
110
111         /**
112          * Branch office street name
113          */
114         private String branchStreet;
115
116         /**
117          * Suite number
118          */
119         private Short branchSuiteNumber;
120
121         /**
122          * Owning user instance (which this branch office is assigned to)
123          */
124         private User branchUserOwner;
125
126         /**
127          * ZIP code
128          */
129         private Integer branchZipCode;
130
131         /**
132          * Area code for fax number
133          */
134         private Integer faxAreaCode;
135
136         /**
137          * Country for fax number
138          */
139         private Country faxCountry;
140
141         /**
142          * Dial number for fax number
143          */
144         private Long faxNumber;
145
146         /**
147          * Area code for land-line number
148          */
149         private Integer landLineAreaCode;
150
151         /**
152          * Country for land-line number
153          */
154         private Country landLineCountry;
155
156         /**
157          * Dial number for land-line number
158          */
159         private Long landLineNumber;
160
161         /**
162          * Default constructor
163          */
164         public PizzaAdminBranchOfficeWebRequestBean () {
165                 // Call super constructor
166                 super();
167         }
168
169         /**
170          * Adds branch office with all data from this backing bean. First this
171          * action method will validate if the branch office's address is already
172          * registered and if found, it will output a proper faces message.
173          * <p>
174          * @return Redirect outcome
175          */
176         public String addBranchOffice () {
177                 // Get instance
178                 final BranchOffice branchOffice = this.createBranchOffice();
179
180                 // Is the branch office not created yet?
181                 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
182                         // Then show proper faces message
183                         this.showFacesMessage("form_admin_add_branch_office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
184                         return ""; //NOI18N
185                 }
186
187                 // Delcare updated instance
188                 final BranchOffice updatedOffice;
189
190                 try {
191                         // Try to call EJB
192                         updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
193                 } catch (final BranchOfficeAlreadyAddedException ex) {
194                         // Output message
195                         this.showFacesMessage("form_admin_add_branch_office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
196                         return ""; //NOI18N
197                 }
198
199                 // Fire event
200                 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
201
202                 // Redirect to list
203                 return "admin_list_branch_offices"; //NOI18N
204         }
205
206         /**
207          * Getter for city
208          * <p>
209          * @return City
210          */
211         public String getBranchCity () {
212                 return this.branchCity;
213         }
214
215         /**
216          * Setter for city
217          * <p>
218          * @param branchCity City
219          */
220         public void setBranchCity (final String branchCity) {
221                 this.branchCity = branchCity;
222         }
223
224         /**
225          * Getter for basic company data
226          * <p>
227          * @return Basic company data
228          */
229         public BusinessBasicData getBranchCompany () {
230                 return this.branchCompany;
231         }
232
233         /**
234          * Setter for basic company data
235          * <p>
236          * @param branchCompany Basic company data
237          */
238         public void setBranchCompany (final BusinessBasicData branchCompany) {
239                 this.branchCompany = branchCompany;
240         }
241
242         /**
243          * Getter for branch office contact person
244          * <p>
245          * @return Branch office contact person
246          */
247         public Employee getBranchContactEmployee () {
248                 return this.branchContactEmployee;
249         }
250
251         /**
252          * Setter for branch office contact person
253          * <p>
254          * @param branchContactEmployee Branch office contact person
255          */
256         public void setBranchContactEmployee (final Employee branchContactEmployee) {
257                 this.branchContactEmployee = branchContactEmployee;
258         }
259
260         /**
261          * Getter for country
262          * <p>
263          * @return Country
264          */
265         public Country getBranchCountry () {
266                 return this.branchCountry;
267         }
268
269         /**
270          * Setter for country
271          * <p>
272          * @param branchCountry Country
273          */
274         public void setBranchCountry (final Country branchCountry) {
275                 this.branchCountry = branchCountry;
276         }
277
278         /**
279          * Getter for email address
280          * <p>
281          * @return Email address
282          */
283         public String getBranchEmailAddress () {
284                 return this.branchEmailAddress;
285         }
286
287         /**
288          * Getter for email address
289          * <p>
290          * @param branchEmailAddress Email address
291          */
292         public void setBranchEmailAddress (final String branchEmailAddress) {
293                 this.branchEmailAddress = branchEmailAddress;
294         }
295
296         /**
297          * Getter for house number
298          * <p>
299          * @return House number
300          */
301         public Short getBranchHouseNumber () {
302                 return this.branchHouseNumber;
303         }
304
305         /**
306          * Setter for house number
307          * <p>
308          * @param branchHouseNumber House number
309          */
310         public void setBranchHouseNumber (final Short branchHouseNumber) {
311                 this.branchHouseNumber = branchHouseNumber;
312         }
313
314         /**
315          * Getter for store
316          * <p>
317          * @return Store
318          */
319         public Short getBranchStore () {
320                 return this.branchStore;
321         }
322
323         /**
324          * Setter for store
325          * <p>
326          * @param branchStore Store
327          */
328         public void setBranchStore (final Short branchStore) {
329                 this.branchStore = branchStore;
330         }
331
332         /**
333          * Getter for street name
334          * <p>
335          * @return Street name
336          */
337         public String getBranchStreet () {
338                 return this.branchStreet;
339         }
340
341         /**
342          * Setter for street name
343          * <p>
344          * @param branchStreet Street name
345          */
346         public void setBranchStreet (final String branchStreet) {
347                 this.branchStreet = branchStreet;
348         }
349
350         /**
351          * Getter for suite number
352          * <p>
353          * @return Suite number
354          */
355         public Short getBranchSuiteNumber () {
356                 return this.branchSuiteNumber;
357         }
358
359         /**
360          * Setter for suite number
361          * <p>
362          * @param branchSuiteNumber Suite number
363          */
364         public void setBranchSuiteNumber (final Short branchSuiteNumber) {
365                 this.branchSuiteNumber = branchSuiteNumber;
366         }
367
368         /**
369          * Getter for owning user instance
370          * <p>
371          * @return Owning user instance
372          */
373         public User getBranchUserOwner () {
374                 return this.branchUserOwner;
375         }
376
377         /**
378          * Setter for owning user instance
379          * <p>
380          * @param branchUserOwner Owning user instance
381          */
382         public void setBranchUserOwner (final User branchUserOwner) {
383                 this.branchUserOwner = branchUserOwner;
384         }
385
386         /**
387          * Getter for ZIP code\
388          * <p>
389          * @return ZIP code
390          */
391         public Integer getBranchZipCode () {
392                 return this.branchZipCode;
393         }
394
395         /**
396          * Setter for ZIP code\
397          * <p>
398          * @param branchZipCode ZIP code
399          */
400         public void setBranchZipCode (final Integer branchZipCode) {
401                 this.branchZipCode = branchZipCode;
402         }
403
404         /**
405          * Getter for fax number's area code
406          * <p>
407          * @return Fax number's area code
408          */
409         public Integer getFaxAreaCode () {
410                 return this.faxAreaCode;
411         }
412
413         /**
414          * Setter for fax number's area code
415          * <p>
416          * @param faxAreaCode Fax number's area code
417          */
418         public void setFaxAreaCode (final Integer faxAreaCode) {
419                 this.faxAreaCode = faxAreaCode;
420         }
421
422         /**
423          * Getter for fax's country instance
424          * <p>
425          * @return Fax' country instance
426          */
427         public Country getFaxCountry () {
428                 return this.faxCountry;
429         }
430
431         /**
432          * Setter for fax's country instance
433          * <p>
434          * @param faxCountry Fax' country instance
435          */
436         public void setFaxCountry (final Country faxCountry) {
437                 this.faxCountry = faxCountry;
438         }
439
440         /**
441          * Getter for fax number
442          * <p>
443          * @return Fax number
444          */
445         public Long getFaxNumber () {
446                 return this.faxNumber;
447         }
448
449         /**
450          * Setter for fax number
451          * <p>
452          * @param faxNumber Fax number
453          */
454         public void setFaxNumber (final Long faxNumber) {
455                 this.faxNumber = faxNumber;
456         }
457
458         /**
459          * Getter for land-line number's area code
460          * <p>
461          * @return Land-line number's area code
462          */
463         public Integer getLandLineAreaCode () {
464                 return this.landLineAreaCode;
465         }
466
467         /**
468          * Setter for land-line number's area code
469          * <p>
470          * @param landLineAreaCode Land-line number's area code
471          */
472         public void setLandLineAreaCode (final Integer landLineAreaCode) {
473                 this.landLineAreaCode = landLineAreaCode;
474         }
475
476         /**
477          * Getter for land-line number's country instance
478          * <p>
479          * @return Land-line number's country instance
480          */
481         public Country getLandLineCountry () {
482                 return this.landLineCountry;
483         }
484
485         /**
486          * Setter for land-line number's country instance
487          * <p>
488          * @param landLineCountry Land-line number's country instance
489          */
490         public void setLandLineCountry (final Country landLineCountry) {
491                 this.landLineCountry = landLineCountry;
492         }
493
494         /**
495          * Getter for land-line number
496          * <p>
497          * @return Land-line number
498          */
499         public Long getLandLineNumber () {
500                 return this.landLineNumber;
501         }
502
503         /**
504          * Setter for land-line number
505          * <p>
506          * @param landLineNumber Land-line number
507          */
508         public void setLandLineNumber (final Long landLineNumber) {
509                 this.landLineNumber = landLineNumber;
510         }
511
512         /**
513          * Prepares an instance of a BranchOffice object (entity) with all data from
514          * this bean. If a complete fax number or land-line number was provided, it
515          * will be set in the instance as well.
516          * <p>
517          * @return An instance of a BranchOffice class (entity)
518          */
519         private BranchOffice createBranchOffice () {
520                 // Create new branch office instance
521                 final BranchOffice branchOffice = new CompanyBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
522
523                 // Add all other fields, too
524                 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
525                 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
526                 branchOffice.setBranchStore(this.getBranchStore());
527                 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
528                 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
529
530                 // Generate phone number
531                 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
532                 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
533
534                 // Don't set null or wrong references
535                 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
536                         // Now the number must be given
537                         if (landLine.getPhoneAreaCode() == null) {
538                                 // Is null
539                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
540                         } else if (landLine.getPhoneAreaCode() < 1) {
541                                 // Abort here
542                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
543                         } else if (landLine.getPhoneNumber() == null) {
544                                 // Is null
545                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
546                         } else if (landLine.getPhoneNumber() < 1) {
547                                 // Abort here
548                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
549                         }
550
551                         // Set phone number
552                         branchOffice.setBranchLandLineNumber(landLine);
553                 }
554
555                 // Don't set null or wrong references
556                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
557                         // Now the number must be given
558                         if (fax.getPhoneAreaCode() == null) {
559                                 // Is null
560                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
561                         } else if (fax.getPhoneAreaCode() < 1) {
562                                 // Abort here
563                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
564                         } else if (fax.getPhoneNumber() == null) {
565                                 // Is null
566                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
567                         } else if (fax.getPhoneNumber() < 1) {
568                                 // Abort here
569                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
570                         }
571
572                         // Set fax number
573                         branchOffice.setBranchFaxNumber(fax);
574                 }
575
576                 // Return fully prepared instance
577                 return branchOffice;
578         }
579
580         /**
581          * Checks whether the given branch office's address is already found in
582          * local cache. Please note that this method fully relies on the cache, so
583          * you must always fire proper events that add/update/delete entries in
584          * cache.
585          * <p>
586          * @param branchOffice Branch office to check it's address
587          * <p>
588          * @return Whether the address has been found
589          */
590         private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
591                 // Get full list from other bean
592                 final List<BranchOffice> branchOffices = this.branchOfficeController.allBranchOffices();
593
594                 // Default is not found
595                 boolean isFound = false;
596
597                 // Now check each entry
598                 for (final BranchOffice bo : branchOffices) {
599                         // Is same address?
600                         if (BranchOffices.isSameAddress(bo, branchOffice)) {
601                                 // Found one
602                                 isFound = true;
603                                 break;
604                         }
605                 }
606
607                 // Return flag
608                 return isFound;
609         }
610
611 }