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