]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/business/branchoffice/action/FinancialsAdminBranchOfficeActionWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / business / branchoffice / action / FinancialsAdminBranchOfficeActionWebRequestBean.java
1 /*
2  * Copyright (C) 2017 - 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.jfinancials.beans.business.branchoffice.action;
18
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.ejb.EJB;
25 import javax.enterprise.context.RequestScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.inject.Any;
28 import javax.faces.FacesException;
29 import javax.faces.application.FacesMessage;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontacts.model.contact.Contact;
33 import org.mxchange.jcontactsbusiness.events.branchoffice.added.AdminBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableAdminBranchOfficeAddedEvent;
35 import org.mxchange.jcontactsbusiness.events.branchoffice.updated.AdminBranchOfficeUpdatedEvent;
36 import org.mxchange.jcontactsbusiness.events.branchoffice.updated.ObservableAdminBranchOfficeUpdatedEvent;
37 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
38 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
39 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
40 import org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote;
41 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
42 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
43 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
44 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
45 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
46 import org.mxchange.jcoreee.dates.DayOfTheWeek;
47 import org.mxchange.jcountry.model.data.Country;
48 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
49 import org.mxchange.jfinancials.beans.business.branchoffice.list.FinancialsBranchOfficeListWebViewController;
50 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
51 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
52 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
53 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
54 import org.mxchange.jusercore.model.user.User;
55
56 /**
57  * An administrative bean for branch offices
58  * <p>
59  * @author Roland Häder<roland@mxchange.org>
60  */
61 @Named ("adminBranchOfficeActionController")
62 @RequestScoped
63 public class FinancialsAdminBranchOfficeActionWebRequestBean extends BaseFinancialsBean implements FinancialsAdminBranchOfficeActionWebRequestController {
64
65         /**
66          * Opening times of this branch office
67          */
68         private static List<OpeningTime> branchOpeningTimes;
69
70         /**
71          * Serial number
72          */
73         private static final long serialVersionUID = 5_028_697_360_471L;
74
75         /**
76          * EJB for administrative purposes
77          */
78         @EJB (lookup = "java:global/jfinancials-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
79         private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
80
81         /**
82          * City
83          */
84         private String branchCity;
85
86         /**
87          * Assigned company for this branch office
88          */
89         private BasicData branchCompany;
90
91         /**
92          * Contact person in branch office
93          */
94         private Contact branchContactEmployee;
95
96         /**
97          * Country
98          */
99         private Country branchCountry;
100
101         /**
102          * Email address
103          */
104         private String branchEmailAddress;
105
106         /**
107          * House number
108          */
109         private Short branchHouseNumber;
110
111         /**
112          * House number's extension (a,b,c,...)
113          */
114         private String branchHouseNumberExtension;
115
116         /**
117          * Branch office's id number
118          */
119         private Long branchId;
120
121         /**
122          * Last house number
123          */
124         private Short branchLastHouseNumber;
125
126         /**
127          * Number of branch office
128          */
129         private Long branchNumber;
130
131         /**
132          * An event being fired when a branch office has been successfully added
133          */
134         @Inject
135         @Any
136         private Event<ObservableAdminBranchOfficeAddedEvent> branchOfficeAddedEvent;
137
138         /**
139          * A list branch office controller (backing bean)
140          */
141         @Inject
142         private FinancialsBranchOfficeListWebViewController branchOfficeListController;
143
144         /**
145          * Owner/leader of branch office
146          */
147         private Contact branchOwnerEmployee;
148
149         /**
150          * Store
151          */
152         private Short branchStore;
153
154         /**
155          * Branch office street name
156          */
157         private String branchStreet;
158
159         /**
160          * Suite number
161          */
162         private Short branchSuiteNumber;
163
164         /**
165          * Owning user instance (which this branch office is assigned to)
166          */
167         private User branchUserOwner;
168
169         /**
170          * ZIP code
171          */
172         private Integer branchZipCode;
173
174         /**
175          * Currently worked on branch office
176          */
177         private BranchOffice currentBranchOffice;
178
179         /**
180          * Area code for fax number
181          */
182         private Integer faxAreaCode;
183
184         /**
185          * Country for fax number
186          */
187         private Country faxCountry;
188
189         /**
190          * Dial number for fax number
191          */
192         private Long faxNumber;
193
194         /**
195          * Area code for land-line number
196          */
197         private Integer landLineAreaCode;
198
199         /**
200          * Country for land-line number
201          */
202         private Country landLineCountry;
203
204         /**
205          * Dial number for land-line number
206          */
207         private Long landLineNumber;
208
209         /**
210          * Ending week day
211          */
212         private DayOfTheWeek openingEndDay;
213
214         /**
215          * Ending time
216          */
217         private Date openingEndTime;
218
219         /**
220          * Starting week day
221          */
222         private DayOfTheWeek openingStartDay;
223
224         /**
225          * Starting time
226          */
227         private Date openingStartTime;
228
229         /**
230          * Event being fired when an administrator has updated a branch office
231          */
232         @Any
233         @Inject
234         private Event<ObservableAdminBranchOfficeUpdatedEvent> updatedBranchOfficeEvent;
235
236         /**
237          * Default constructor
238          */
239         public FinancialsAdminBranchOfficeActionWebRequestBean () {
240                 // Call super constructor
241                 super();
242
243                 // Is the opening times list there?
244                 if (null == branchOpeningTimes) {
245                         // Init list
246                         branchOpeningTimes = new ArrayList<>(1);
247                 }
248         }
249
250         /**
251          * Adds branch office with all data from this backing bean. First this
252          * action method will validate if the branch office's address is already
253          * registered and if found, it will output a proper faces message.
254          */
255         public void addBranchOffice () {
256                 // Get instance
257                 final BranchOffice branchOffice = this.createBranchOffice();
258
259                 // Is the branch office not created yet?
260                 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
261                         // Then show proper faces message
262                         this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
263                         return;
264                 }
265
266                 // Delcare updated instance
267                 final BranchOffice updatedOffice;
268
269                 try {
270                         // Try to call EJB
271                         updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
272                 } catch (final BranchOfficeAlreadyAddedException ex) {
273                         // Output message
274                         this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
275                         return;
276                 }
277
278                 // Fire event
279                 this.branchOfficeAddedEvent.fire(new AdminBranchOfficeAddedEvent(updatedOffice));
280
281                 // Clear this bean
282                 this.clear();
283         }
284
285         /**
286          * Adds opening time to temporary list which will be sent along with the
287          * branch office data to the EJB.
288          */
289         public void addOpeningTime () {
290                 // Validate all required fields
291                 if (this.getOpeningEndDay() == null) {
292                         // Throw NPE
293                         throw new NullPointerException("this.openingEndDay is null"); //NOI18N
294                 } else if (this.getOpeningEndTime() == null) {
295                         // Throw NPE
296                         throw new NullPointerException("this.openingEndTime is null"); //NOI18N
297                 } else if (this.getOpeningStartDay() == null) {
298                         // Throw NPE
299                         throw new NullPointerException("this.openingStartDay is null"); //NOI18N
300                 } else if (this.getOpeningStartTime() == null) {
301                         // Throw NPE
302                         throw new NullPointerException("this.openingStartTime is null"); //NOI18N
303                 }
304
305                 // Get opening time instance
306                 final OpeningTime openingTime = this.createOpeningTimes();
307
308                 // Is same found?
309                 if (this.isSameOpeningTimeFound(openingTime)) {
310                         // Yes then abort here
311                         this.showFacesMessage("form-admin-add-branch-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
312                         return;
313                 }
314
315                 // Add to temporary list
316                 branchOpeningTimes.add(openingTime);
317
318                 // Clear opening time fields
319                 this.clearOpeningTime();
320         }
321
322         /**
323          * Copies all current branch office's properties back to this bean.
324          */
325         public void copyAllBranchOfficeProperties () {
326                 // Validate current product instance
327                 if (this.getCurrentBranchOffice() == null) {
328                         // Throw NPE
329                         throw new NullPointerException("this.product is null"); //NOI18N
330                 } else if (this.getCurrentBranchOffice().getBranchId() == null) {
331                         // Throw NPE again
332                         throw new NullPointerException("this.product.branchId is null"); //NOI18N
333                 } else if (this.getCurrentBranchOffice().getBranchId() < 1) {
334                         // Not valid
335                         throw new IllegalStateException(MessageFormat.format("this.product.branchId={0} is not valid.", this.getCurrentBranchOffice().getBranchId())); //NOI18N
336                 }
337
338                 // Now copy all fields
339                 this.setBranchCity(this.getCurrentBranchOffice().getBranchCity());
340                 this.setBranchCompany(this.getCurrentBranchOffice().getBranchCompany());
341                 this.setBranchContactEmployee(this.getCurrentBranchOffice().getBranchContactEmployee());
342                 this.setBranchCountry(this.getCurrentBranchOffice().getBranchCountry());
343                 this.setBranchEmailAddress(this.getCurrentBranchOffice().getBranchEmailAddress());
344                 this.setBranchHouseNumber(this.getCurrentBranchOffice().getBranchHouseNumber());
345                 this.setBranchHouseNumberExtension(this.getCurrentBranchOffice().getBranchHouseNumberExtension());
346                 this.setBranchId(this.getCurrentBranchOffice().getBranchId());
347                 this.setBranchLastHouseNumber(this.getCurrentBranchOffice().getBranchLastHouseNumber());
348                 this.setBranchNumber(this.getCurrentBranchOffice().getBranchNumber());
349                 this.setBranchOpeningTimes(this.getCurrentBranchOffice().getBranchOpeningTimes());
350                 this.setBranchOwnerEmployee(this.getCurrentBranchOffice().getBranchOwnerEmployee());
351                 this.setBranchStore(this.getCurrentBranchOffice().getBranchStore());
352                 this.setBranchStreet(this.getCurrentBranchOffice().getBranchStreet());
353                 this.setBranchSuiteNumber(this.getCurrentBranchOffice().getBranchSuiteNumber());
354                 this.setBranchUserOwner(this.getCurrentBranchOffice().getBranchUserOwner());
355                 this.setBranchZipCode(this.getCurrentBranchOffice().getBranchZipCode());
356         }
357
358         /**
359          * Getter for city
360          * <p>
361          * @return City
362          */
363         public String getBranchCity () {
364                 return this.branchCity;
365         }
366
367         /**
368          * Setter for city
369          * <p>
370          * @param branchCity City
371          */
372         public void setBranchCity (final String branchCity) {
373                 this.branchCity = branchCity;
374         }
375
376         /**
377          * Getter for basic company data
378          * <p>
379          * @return Basic company data
380          */
381         public BasicData getBranchCompany () {
382                 return this.branchCompany;
383         }
384
385         /**
386          * Setter for basic company data
387          * <p>
388          * @param branchCompany Basic company data
389          */
390         public void setBranchCompany (final BasicData branchCompany) {
391                 this.branchCompany = branchCompany;
392         }
393
394         /**
395          * Getter for branch office contact person
396          * <p>
397          * @return Branch office contact person
398          */
399         public Contact getBranchContactEmployee () {
400                 return this.branchContactEmployee;
401         }
402
403         /**
404          * Setter for branch office contact person
405          * <p>
406          * @param branchContactEmployee Branch office contact person
407          */
408         public void setBranchContactEmployee (final Contact branchContactEmployee) {
409                 this.branchContactEmployee = branchContactEmployee;
410         }
411
412         /**
413          * Getter for country
414          * <p>
415          * @return Country
416          */
417         public Country getBranchCountry () {
418                 return this.branchCountry;
419         }
420
421         /**
422          * Setter for country
423          * <p>
424          * @param branchCountry Country
425          */
426         public void setBranchCountry (final Country branchCountry) {
427                 this.branchCountry = branchCountry;
428         }
429
430         /**
431          * Getter for email address
432          * <p>
433          * @return Email address
434          */
435         public String getBranchEmailAddress () {
436                 return this.branchEmailAddress;
437         }
438
439         /**
440          * Getter for email address
441          * <p>
442          * @param branchEmailAddress Email address
443          */
444         public void setBranchEmailAddress (final String branchEmailAddress) {
445                 this.branchEmailAddress = branchEmailAddress;
446         }
447
448         /**
449          * Getter for house number
450          * <p>
451          * @return House number
452          */
453         public Short getBranchHouseNumber () {
454                 return this.branchHouseNumber;
455         }
456
457         /**
458          * Setter for house number
459          * <p>
460          * @param branchHouseNumber House number
461          */
462         public void setBranchHouseNumber (final Short branchHouseNumber) {
463                 this.branchHouseNumber = branchHouseNumber;
464         }
465
466         /**
467          * Getter for house number's extension
468          * <p>
469          * @return House number's extension
470          */
471         public String getBranchHouseNumberExtension () {
472                 return this.branchHouseNumberExtension;
473         }
474
475         /**
476          * Setter for house number's extension
477          * <p>
478          * @param branchHouseNumberExtension House number's extension
479          */
480         public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
481                 this.branchHouseNumberExtension = branchHouseNumberExtension;
482         }
483
484         /**
485          * Getter for branch office's id number
486          * <p>
487          * @return Branch office's id number
488          */
489         public Long getBranchId () {
490                 return this.branchId;
491         }
492
493         /**
494          * Setter for branch office's id number
495          * <p>
496          * @param branchId Branch office's id number
497          */
498         public void setBranchId (final Long branchId) {
499                 this.branchId = branchId;
500         }
501
502         /**
503          * Getter for last house number
504          * <p>
505          * @return Last house number
506          */
507         public Short getBranchLastHouseNumber () {
508                 return this.branchLastHouseNumber;
509         }
510
511         /**
512          * Setter for last house number
513          * <p>
514          * @param branchLastHouseNumber Last house number
515          */
516         public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
517                 this.branchLastHouseNumber = branchLastHouseNumber;
518         }
519
520         /**
521          * Getter for branch office number
522          * <p>
523          * @return Branch office number
524          */
525         public Long getBranchNumber () {
526                 return this.branchNumber;
527         }
528
529         /**
530          * Setter for branch office number
531          * <p>
532          * @param branchNumber Branch office number
533          */
534         public void setBranchNumber (final Long branchNumber) {
535                 this.branchNumber = branchNumber;
536         }
537
538         /**
539          * Getter for opening times of this branch office
540          * <p>
541          * @return Opening times
542          */
543         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
544         public List<OpeningTime> getBranchOpeningTimes () {
545                 return branchOpeningTimes;
546         }
547
548         /**
549          * Setter for opening times of this branch office
550          * <p>
551          * @param branchOpeningTimes Opening times
552          */
553         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
554         public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
555                 FinancialsAdminBranchOfficeActionWebRequestBean.branchOpeningTimes = branchOpeningTimes;
556         }
557
558         /**
559          * Getter for branch office contact person
560          * <p>
561          * @return Branch office contact person
562          */
563         public Contact getBranchOwnerEmployee () {
564                 return this.branchOwnerEmployee;
565         }
566
567         /**
568          * Setter for branch office contact person
569          * <p>
570          * @param branchOwnerEmployee Branch office contact person
571          */
572         public void setBranchOwnerEmployee (final Contact branchOwnerEmployee) {
573                 this.branchOwnerEmployee = branchOwnerEmployee;
574         }
575
576         /**
577          * Getter for store
578          * <p>
579          * @return Store
580          */
581         public Short getBranchStore () {
582                 return this.branchStore;
583         }
584
585         /**
586          * Setter for store
587          * <p>
588          * @param branchStore Store
589          */
590         public void setBranchStore (final Short branchStore) {
591                 this.branchStore = branchStore;
592         }
593
594         /**
595          * Getter for street name
596          * <p>
597          * @return Street name
598          */
599         public String getBranchStreet () {
600                 return this.branchStreet;
601         }
602
603         /**
604          * Setter for street name
605          * <p>
606          * @param branchStreet Street name
607          */
608         public void setBranchStreet (final String branchStreet) {
609                 this.branchStreet = branchStreet;
610         }
611
612         /**
613          * Getter for suite number
614          * <p>
615          * @return Suite number
616          */
617         public Short getBranchSuiteNumber () {
618                 return this.branchSuiteNumber;
619         }
620
621         /**
622          * Setter for suite number
623          * <p>
624          * @param branchSuiteNumber Suite number
625          */
626         public void setBranchSuiteNumber (final Short branchSuiteNumber) {
627                 this.branchSuiteNumber = branchSuiteNumber;
628         }
629
630         /**
631          * Getter for owning user instance
632          * <p>
633          * @return Owning user instance
634          */
635         public User getBranchUserOwner () {
636                 return this.branchUserOwner;
637         }
638
639         /**
640          * Setter for owning user instance
641          * <p>
642          * @param branchUserOwner Owning user instance
643          */
644         public void setBranchUserOwner (final User branchUserOwner) {
645                 this.branchUserOwner = branchUserOwner;
646         }
647
648         /**
649          * Getter for ZIP code\
650          * <p>
651          * @return ZIP code
652          */
653         public Integer getBranchZipCode () {
654                 return this.branchZipCode;
655         }
656
657         /**
658          * Setter for ZIP code\
659          * <p>
660          * @param branchZipCode ZIP code
661          */
662         public void setBranchZipCode (final Integer branchZipCode) {
663                 this.branchZipCode = branchZipCode;
664         }
665
666         /**
667          * Getter for current branch office
668          * <p>
669          * @return Current branch office
670          */
671         public BranchOffice getCurrentBranchOffice () {
672                 return this.currentBranchOffice;
673         }
674
675         /**
676          * Setter for current branch office
677          * <p>
678          * @param currentBranchOffice Current branch office
679          */
680         public void setCurrentBranchOffice (final BranchOffice currentBranchOffice) {
681                 this.currentBranchOffice = currentBranchOffice;
682         }
683
684         /**
685          * Getter for fax number's area code
686          * <p>
687          * @return Fax number's area code
688          */
689         public Integer getFaxAreaCode () {
690                 return this.faxAreaCode;
691         }
692
693         /**
694          * Setter for fax number's area code
695          * <p>
696          * @param faxAreaCode Fax number's area code
697          */
698         public void setFaxAreaCode (final Integer faxAreaCode) {
699                 this.faxAreaCode = faxAreaCode;
700         }
701
702         /**
703          * Getter for fax's country instance
704          * <p>
705          * @return Fax' country instance
706          */
707         public Country getFaxCountry () {
708                 return this.faxCountry;
709         }
710
711         /**
712          * Setter for fax's country instance
713          * <p>
714          * @param faxCountry Fax' country instance
715          */
716         public void setFaxCountry (final Country faxCountry) {
717                 this.faxCountry = faxCountry;
718         }
719
720         /**
721          * Getter for fax number
722          * <p>
723          * @return Fax number
724          */
725         public Long getFaxNumber () {
726                 return this.faxNumber;
727         }
728
729         /**
730          * Setter for fax number
731          * <p>
732          * @param faxNumber Fax number
733          */
734         public void setFaxNumber (final Long faxNumber) {
735                 this.faxNumber = faxNumber;
736         }
737
738         /**
739          * Getter for land-line number's area code
740          * <p>
741          * @return Land-line number's area code
742          */
743         public Integer getLandLineAreaCode () {
744                 return this.landLineAreaCode;
745         }
746
747         /**
748          * Setter for land-line number's area code
749          * <p>
750          * @param landLineAreaCode Land-line number's area code
751          */
752         public void setLandLineAreaCode (final Integer landLineAreaCode) {
753                 this.landLineAreaCode = landLineAreaCode;
754         }
755
756         /**
757          * Getter for land-line number's country instance
758          * <p>
759          * @return Land-line number's country instance
760          */
761         public Country getLandLineCountry () {
762                 return this.landLineCountry;
763         }
764
765         /**
766          * Setter for land-line number's country instance
767          * <p>
768          * @param landLineCountry Land-line number's country instance
769          */
770         public void setLandLineCountry (final Country landLineCountry) {
771                 this.landLineCountry = landLineCountry;
772         }
773
774         /**
775          * Getter for land-line number
776          * <p>
777          * @return Land-line number
778          */
779         public Long getLandLineNumber () {
780                 return this.landLineNumber;
781         }
782
783         /**
784          * Setter for land-line number
785          * <p>
786          * @param landLineNumber Land-line number
787          */
788         public void setLandLineNumber (final Long landLineNumber) {
789                 this.landLineNumber = landLineNumber;
790         }
791
792         /**
793          * Getter for ending week day
794          * <p>
795          * @return Ending week day
796          */
797         public DayOfTheWeek getOpeningEndDay () {
798                 return this.openingEndDay;
799         }
800
801         /**
802          * Setter for ending week day
803          * <p>
804          * @param openingEndDay Ending week day
805          */
806         public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
807                 this.openingEndDay = openingEndDay;
808         }
809
810         /**
811          * Getter for ending time
812          * <p>
813          * @return Ending time
814          */
815         @SuppressWarnings ("ReturnOfDateField")
816         public Date getOpeningEndTime () {
817                 return this.openingEndTime;
818         }
819
820         /**
821          * Getter for ending time
822          * <p>
823          * @param openingEndTime Ending time
824          */
825         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
826         public void setOpeningEndTime (final Date openingEndTime) {
827                 this.openingEndTime = openingEndTime;
828         }
829
830         /**
831          * Getter for starting week day
832          * <p>
833          * @return Starting week day
834          */
835         public DayOfTheWeek getOpeningStartDay () {
836                 return this.openingStartDay;
837         }
838
839         /**
840          * Getter for starting week day
841          * <p>
842          * @param openingStartDay Starting week day
843          */
844         public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
845                 this.openingStartDay = openingStartDay;
846         }
847
848         /**
849          * Getter for starting time
850          * <p>
851          * @return Starting time
852          */
853         @SuppressWarnings ("ReturnOfDateField")
854         public Date getOpeningStartTime () {
855                 return this.openingStartTime;
856         }
857
858         /**
859          * Getter for starting time
860          * <p>
861          * @param openingStartTime Starting time
862          */
863         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
864         public void setOpeningStartTime (final Date openingStartTime) {
865                 this.openingStartTime = openingStartTime;
866         }
867
868         /**
869          * Updates currently worked on branch office data
870          * <p>
871          * @return Redirection outcome
872          */
873         public String updateBranchOffice () {
874                 // Validate current product instance and id
875                 if (this.getCurrentBranchOffice() == null) {
876                         // Throw NPE
877                         throw new NullPointerException("this.product is null"); //NOI18N
878                 } else if (this.getCurrentBranchOffice().getBranchId() == null) {
879                         // Throw NPE again
880                         throw new NullPointerException("this.product.branchId is null"); //NOI18N
881                 } else if (this.getCurrentBranchOffice().getBranchId() < 1) {
882                         // Not valid
883                         throw new IllegalStateException(MessageFormat.format("this.product.branchId={0} is not valid.", this.getCurrentBranchOffice().getBranchId())); //NOI18N
884                 } else if (this.getBranchId() == null) {
885                         // Throw NPE
886                         throw new NullPointerException("this.branchId is null"); //NOI18N
887                 } else if (this.getBranchId() < 1) {
888                         // Throw ISE
889                         throw new IllegalStateException(MessageFormat.format("this.branchId={0} is not valid.", this.getBranchId())); //NOI18N
890                 }
891
892                 // Init instance with fresh data
893                 final BranchOffice branchOffice = this.createBranchOffice();
894
895                 // Does current (not updated) and just created (maybe updated) match?
896                 if (Objects.equals(this.getCurrentBranchOffice(), branchOffice)) {
897                         // Yes, then output message
898                         this.showFacesMessage("form-admin-edit-branch-office:branchCompany", "ADMIN_BRANCH_OFFICE_NOT_UPDATED", FacesMessage.SEVERITY_WARN); //NOI18N
899
900                         // Skip below code
901                         return ""; //NOI18N
902                 }
903
904                 // Copy all fields
905                 BranchOffices.copyBranchOfficeData(this.getCurrentBranchOffice(), branchOffice);
906
907                 // Initialize updated instance
908                 final BranchOffice updatedBranchOffice;
909
910                 // Try it
911                 try {
912                         // Invoke EJB
913                         updatedBranchOffice = this.adminBranchOfficeBean.updateBranchOffice(branchOffice);
914                 } catch (final BranchOfficeNotFoundException ex) {
915                         // Throw as a cause
916                         throw new FacesException(ex);
917                 }
918
919                 // Fire event
920                 this.updatedBranchOfficeEvent.fire(new AdminBranchOfficeUpdatedEvent(updatedBranchOffice));
921
922                 // Redirect to list view
923                 return "admin_list_branch_offices"; //NOI18N
924         }
925
926         /**
927          * Clears this bean data
928          */
929         private void clear () {
930                 // Clear all branch office data
931                 this.setBranchCity(null);
932                 this.setBranchCompany(null);
933                 this.setBranchContactEmployee(null);
934                 this.setBranchCountry(null);
935                 this.setBranchEmailAddress(null);
936                 this.setBranchHouseNumber(null);
937                 this.setBranchHouseNumberExtension(null);
938                 this.setBranchLastHouseNumber(null);
939                 this.setBranchNumber(null);
940                 this.setBranchOwnerEmployee(null);
941                 this.setBranchStore(null);
942                 this.setBranchStreet(null);
943                 this.setBranchSuiteNumber(null);
944                 this.setBranchUserOwner(null);
945                 this.setBranchZipCode(null);
946
947                 // Opening times list
948                 this.setBranchOpeningTimes(new ArrayList<OpeningTime>(1));
949
950                 // Fax and land-line number
951                 this.setFaxAreaCode(null);
952                 this.setFaxCountry(null);
953                 this.setFaxNumber(null);
954                 this.setLandLineAreaCode(null);
955                 this.setLandLineCountry(null);
956                 this.setLandLineNumber(null);
957
958                 // Extra-clear opening time
959                 this.clearOpeningTime();
960         }
961
962         /**
963          * Clears all opening time fields
964          */
965         private void clearOpeningTime () {
966                 // Clear all opening time fields
967                 this.setOpeningEndDay(null);
968                 this.setOpeningEndTime(null);
969                 this.setOpeningStartDay(null);
970                 this.setOpeningStartTime(null);
971         }
972
973         /**
974          * Prepares an instance of a BranchOffice object (entity) with all data from
975          * this bean. If a complete fax number or land-line number was provided, it
976          * will be set in the instance as well.
977          * <p>
978          * @return An instance of a BranchOffice class (entity)
979          */
980         private BranchOffice createBranchOffice () {
981                 // Create new branch office instance
982                 final BranchOffice branchOffice = new BusinessBranchOffice(
983                                                    this.getBranchCity(),
984                                                    this.getBranchCompany(),
985                                                    this.getBranchCountry(),
986                                                    this.getBranchStreet(),
987                                                    this.getBranchZipCode(),
988                                                    this.getBranchHouseNumber()
989                                    );
990
991                 // Add all other fields, too
992                 branchOffice.setBranchId(this.getBranchId());
993                 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
994                 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
995                 branchOffice.setBranchHouseNumberExtension(this.getBranchHouseNumberExtension());
996                 branchOffice.setBranchLastHouseNumber(this.getBranchLastHouseNumber());
997                 branchOffice.setBranchNumber(this.getBranchNumber());
998                 branchOffice.setBranchOwnerEmployee(this.getBranchOwnerEmployee());
999                 branchOffice.setBranchStore(this.getBranchStore());
1000                 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
1001                 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
1002
1003                 // Init variables
1004                 DialableLandLineNumber landLine = null;
1005                 DialableFaxNumber fax = null;
1006
1007                 // Are all required fields set?
1008                 if (this.getLandLineAreaCode() != null && this.getLandLineCountry() instanceof Country && this.getLandLineNumber() != null) {
1009                         // Initialize land-line instance
1010                         landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
1011                 }
1012
1013                 // Are all required fields set?
1014                 if (this.getFaxAreaCode() != null && this.getFaxCountry() instanceof Country && this.getFaxNumber() != null) {
1015                         // Initialize fax instance
1016                         fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
1017                 }
1018
1019                 // Don't set null or wrong references
1020                 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
1021                         // Now the number must be given
1022                         if (landLine.getPhoneAreaCode() == null) {
1023                                 // Is null
1024                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
1025                         } else if (landLine.getPhoneAreaCode() < 1) {
1026                                 // Abort here
1027                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
1028                         } else if (landLine.getPhoneNumber() == null) {
1029                                 // Is null
1030                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
1031                         } else if (landLine.getPhoneNumber() < 1) {
1032                                 // Abort here
1033                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
1034                         }
1035
1036                         // Set phone number
1037                         branchOffice.setBranchLandLineNumber(landLine);
1038                 }
1039
1040                 // Don't set null or wrong references
1041                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
1042                         // Now the number must be given
1043                         if (fax.getPhoneAreaCode() == null) {
1044                                 // Is null
1045                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
1046                         } else if (fax.getPhoneAreaCode() < 1) {
1047                                 // Abort here
1048                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
1049                         } else if (fax.getPhoneNumber() == null) {
1050                                 // Is null
1051                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
1052                         } else if (fax.getPhoneNumber() < 1) {
1053                                 // Abort here
1054                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
1055                         }
1056
1057                         // Set fax number
1058                         branchOffice.setBranchFaxNumber(fax);
1059                 }
1060                 // Is the opening times list filled?
1061                 if (!this.getBranchOpeningTimes().isEmpty()) {
1062                         // Yes, then set in branch office, too
1063                         branchOffice.setBranchOpeningTimes(this.getBranchOpeningTimes());
1064                 }
1065
1066                 // Return fully prepared instance
1067                 return branchOffice;
1068         }
1069
1070         /**
1071          * Prepares an instance of a OpeningTimes object (entity) with all data from
1072          * this bean. If a complete fax number or land-line number was provided, it
1073          * will be set in the instance as well.
1074          * <p>
1075          * @return An instance of a OpeningTimes class (entity)
1076          */
1077         private OpeningTime createOpeningTimes () {
1078                 // Create new openingTime instance
1079                 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
1080
1081                 // Return fully prepared instance
1082                 return openingTime;
1083         }
1084
1085         /**
1086          * Checks whether the given branch office's address is already found in
1087          * local cache. Please note that this method fully relies on the cache, so
1088          * you must always fire proper events that add/update/delete entries in
1089          * cache.
1090          * <p>
1091          * @param branchOffice Branch office to check it's address
1092          * <p>
1093          * @return Whether the address has been found
1094          */
1095         private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
1096                 // Get full list from other bean
1097                 final List<BranchOffice> branchOffices = this.branchOfficeListController.getAllBranchOffices();
1098
1099                 // Default is not found
1100                 boolean isFound = false;
1101
1102                 // Now check each entry
1103                 for (final BranchOffice bo : branchOffices) {
1104                         // Is same address?
1105                         if (BranchOffices.isSameAddress(bo, branchOffice)) {
1106                                 // Found one
1107                                 isFound = true;
1108                                 break;
1109                         }
1110                 }
1111
1112                 // Return flag
1113                 return isFound;
1114         }
1115
1116         /**
1117          * Checks if given opening time is already added
1118          * <p>
1119          * @param openingTime Opening time to be checked
1120          * <p>
1121          * @return Whether it has been added already
1122          */
1123         private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
1124                 // Default is not found
1125                 boolean isFound = false;
1126
1127                 // Loop through list
1128                 for (final OpeningTime ot : this.getBranchOpeningTimes()) {
1129                         // Check it
1130                         if (Objects.equals(ot, openingTime)) {
1131                                 // Found same match
1132                                 isFound = true;
1133                                 break;
1134                         }
1135                 }
1136
1137                 // Return it
1138                 return isFound;
1139         }
1140
1141 }