2 * Copyright (C) 2017, 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.beans.business.branchoffice;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.inject.Any;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontactsbusiness.events.branchoffice.added.BranchOfficeAddedEvent;
30 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
31 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
32 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
33 import org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
37 import org.mxchange.jcontactsbusiness.model.employee.Employable;
38 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
39 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
40 import org.mxchange.jcontactsbusiness.model.opening_time.dayofweek.DayOfTheWeek;
41 import org.mxchange.jcountry.model.data.Country;
42 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
43 import org.mxchange.jfinancials.beans.business.branchoffice.list.FinancialsBranchOfficeListWebViewController;
44 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
46 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
47 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
48 import org.mxchange.jusercore.model.user.User;
51 * An administrative bean for branch offices
53 * @author Roland Häder<roland@mxchange.org>
55 @Named ("adminBranchOfficeController")
57 public class FinancialsAdminBranchOfficeWebRequestBean extends BaseFinancialsBean implements FinancialsAdminBranchOfficeWebRequestController {
60 * Opening times of this branch office
62 private static List<OpeningTime> branchOpeningTimes;
67 private static final long serialVersionUID = 5_028_697_360_461L;
70 * EJB for administrative purposes
72 @EJB (lookup = "java:global/jfinancials-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
73 private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
78 private String branchCity;
81 * Assigned company for this branch office
83 private BasicData branchCompany;
86 * Contact person in branch office
88 private Employable branchContactEmployee;
93 private Country branchCountry;
98 private String branchEmailAddress;
103 private Short branchHouseNumber;
106 * House number's extension (a,b,c,...)
108 private String branchHouseNumberExtension;
113 private Short branchLastHouseNumber;
116 * Number of branch office
118 private Long branchNumber;
121 * An event being fired when a branch office has been successfully added
125 private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
128 * A list branch office controller (backing bean)
131 private FinancialsBranchOfficeListWebViewController branchOfficeListController;
134 * Owner/leader of branch office
136 private Employable branchOwner;
141 private Short branchStore;
144 * Branch office street name
146 private String branchStreet;
151 private Short branchSuiteNumber;
154 * Owning user instance (which this branch office is assigned to)
156 private User branchUserOwner;
161 private Integer branchZipCode;
164 * Area code for fax number
166 private Integer faxAreaCode;
169 * Country for fax number
171 private Country faxCountry;
174 * Dial number for fax number
176 private Long faxNumber;
179 * Area code for land-line number
181 private Integer landLineAreaCode;
184 * Country for land-line number
186 private Country landLineCountry;
189 * Dial number for land-line number
191 private Long landLineNumber;
196 private DayOfTheWeek openingEndDay;
201 private Date openingEndTime;
206 private DayOfTheWeek openingStartDay;
211 private Date openingStartTime;
214 * Default constructor
216 public FinancialsAdminBranchOfficeWebRequestBean () {
217 // Call super constructor
220 // Is the opening times list there?
221 if (null == branchOpeningTimes) {
223 branchOpeningTimes = new ArrayList<>(1);
228 * Adds branch office with all data from this backing bean. First this
229 * action method will validate if the branch office's address is already
230 * registered and if found, it will output a proper faces message.
232 public void addBranchOffice () {
234 final BranchOffice branchOffice = this.createBranchOffice();
236 // Is the branch office not created yet?
237 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
238 // Then show proper faces message
239 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
243 // Delcare updated instance
244 final BranchOffice updatedOffice;
248 updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
249 } catch (final BranchOfficeAlreadyAddedException ex) {
251 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
256 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
263 * Adds opening time to temporary list which will be sent along with the
264 * branch office data to the EJB.
266 public void addOpeningTime () {
267 // Validate all required fields
268 if (this.getOpeningEndDay() == null) {
270 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
271 } else if (this.getOpeningEndTime() == null) {
273 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
274 } else if (this.getOpeningStartDay() == null) {
276 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
277 } else if (this.getOpeningStartTime() == null) {
279 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
282 // Get opening time instance
283 final OpeningTime openingTime = this.createOpeningTimes();
286 if (this.isSameOpeningTimeFound(openingTime)) {
287 // Yes then abort here
288 this.showFacesMessage("form-admin-add-branch-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
292 // Add to temporary list
293 branchOpeningTimes.add(openingTime);
295 // Clear opening time fields
296 this.clearOpeningTime();
304 public String getBranchCity () {
305 return this.branchCity;
311 * @param branchCity City
313 public void setBranchCity (final String branchCity) {
314 this.branchCity = branchCity;
318 * Getter for basic company data
320 * @return Basic company data
322 public BasicData getBranchCompany () {
323 return this.branchCompany;
327 * Setter for basic company data
329 * @param branchCompany Basic company data
331 public void setBranchCompany (final BasicData branchCompany) {
332 this.branchCompany = branchCompany;
336 * Getter for branch office contact person
338 * @return Branch office contact person
340 public Employable getBranchContactEmployee () {
341 return this.branchContactEmployee;
345 * Setter for branch office contact person
347 * @param branchContactEmployee Branch office contact person
349 public void setBranchContactEmployee (final Employable branchContactEmployee) {
350 this.branchContactEmployee = branchContactEmployee;
358 public Country getBranchCountry () {
359 return this.branchCountry;
365 * @param branchCountry Country
367 public void setBranchCountry (final Country branchCountry) {
368 this.branchCountry = branchCountry;
372 * Getter for email address
374 * @return Email address
376 public String getBranchEmailAddress () {
377 return this.branchEmailAddress;
381 * Getter for email address
383 * @param branchEmailAddress Email address
385 public void setBranchEmailAddress (final String branchEmailAddress) {
386 this.branchEmailAddress = branchEmailAddress;
390 * Getter for house number
392 * @return House number
394 public Short getBranchHouseNumber () {
395 return this.branchHouseNumber;
399 * Setter for house number
401 * @param branchHouseNumber House number
403 public void setBranchHouseNumber (final Short branchHouseNumber) {
404 this.branchHouseNumber = branchHouseNumber;
408 * Getter for house number's extension
410 * @return House number's extension
412 public String getBranchHouseNumberExtension () {
413 return this.branchHouseNumberExtension;
417 * Setter for house number's extension
419 * @param branchHouseNumberExtension House number's extension
421 public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
422 this.branchHouseNumberExtension = branchHouseNumberExtension;
426 * Getter for last house number
428 * @return Last house number
430 public Short getBranchLastHouseNumber () {
431 return this.branchLastHouseNumber;
435 * Setter for last house number
437 * @param branchLastHouseNumber Last house number
439 public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
440 this.branchLastHouseNumber = branchLastHouseNumber;
444 * Getter for branch office number
446 * @return Branch office number
448 public Long getBranchNumber () {
449 return this.branchNumber;
453 * Setter for branch office number
455 * @param branchNumber Branch office number
457 public void setBranchNumber (final Long branchNumber) {
458 this.branchNumber = branchNumber;
462 * Getter for opening times of this branch office
464 * @return Opening times
466 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
467 public List<OpeningTime> getBranchOpeningTimes () {
468 return branchOpeningTimes;
472 * Setter for opening times of this branch office
474 * @param branchOpeningTimes Opening times
476 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
477 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
478 FinancialsAdminBranchOfficeWebRequestBean.branchOpeningTimes = branchOpeningTimes;
482 * Getter for branch office contact person
484 * @return Branch office contact person
486 public Employable getBranchOwner () {
487 return this.branchOwner;
491 * Setter for branch office contact person
493 * @param branchOwner Branch office contact person
495 public void setBranchOwner (final Employable branchOwner) {
496 this.branchOwner = branchOwner;
504 public Short getBranchStore () {
505 return this.branchStore;
511 * @param branchStore Store
513 public void setBranchStore (final Short branchStore) {
514 this.branchStore = branchStore;
518 * Getter for street name
520 * @return Street name
522 public String getBranchStreet () {
523 return this.branchStreet;
527 * Setter for street name
529 * @param branchStreet Street name
531 public void setBranchStreet (final String branchStreet) {
532 this.branchStreet = branchStreet;
536 * Getter for suite number
538 * @return Suite number
540 public Short getBranchSuiteNumber () {
541 return this.branchSuiteNumber;
545 * Setter for suite number
547 * @param branchSuiteNumber Suite number
549 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
550 this.branchSuiteNumber = branchSuiteNumber;
554 * Getter for owning user instance
556 * @return Owning user instance
558 public User getBranchUserOwner () {
559 return this.branchUserOwner;
563 * Setter for owning user instance
565 * @param branchUserOwner Owning user instance
567 public void setBranchUserOwner (final User branchUserOwner) {
568 this.branchUserOwner = branchUserOwner;
572 * Getter for ZIP code\
576 public Integer getBranchZipCode () {
577 return this.branchZipCode;
581 * Setter for ZIP code\
583 * @param branchZipCode ZIP code
585 public void setBranchZipCode (final Integer branchZipCode) {
586 this.branchZipCode = branchZipCode;
590 * Getter for fax number's area code
592 * @return Fax number's area code
594 public Integer getFaxAreaCode () {
595 return this.faxAreaCode;
599 * Setter for fax number's area code
601 * @param faxAreaCode Fax number's area code
603 public void setFaxAreaCode (final Integer faxAreaCode) {
604 this.faxAreaCode = faxAreaCode;
608 * Getter for fax's country instance
610 * @return Fax' country instance
612 public Country getFaxCountry () {
613 return this.faxCountry;
617 * Setter for fax's country instance
619 * @param faxCountry Fax' country instance
621 public void setFaxCountry (final Country faxCountry) {
622 this.faxCountry = faxCountry;
626 * Getter for fax number
630 public Long getFaxNumber () {
631 return this.faxNumber;
635 * Setter for fax number
637 * @param faxNumber Fax number
639 public void setFaxNumber (final Long faxNumber) {
640 this.faxNumber = faxNumber;
644 * Getter for land-line number's area code
646 * @return Land-line number's area code
648 public Integer getLandLineAreaCode () {
649 return this.landLineAreaCode;
653 * Setter for land-line number's area code
655 * @param landLineAreaCode Land-line number's area code
657 public void setLandLineAreaCode (final Integer landLineAreaCode) {
658 this.landLineAreaCode = landLineAreaCode;
662 * Getter for land-line number's country instance
664 * @return Land-line number's country instance
666 public Country getLandLineCountry () {
667 return this.landLineCountry;
671 * Setter for land-line number's country instance
673 * @param landLineCountry Land-line number's country instance
675 public void setLandLineCountry (final Country landLineCountry) {
676 this.landLineCountry = landLineCountry;
680 * Getter for land-line number
682 * @return Land-line number
684 public Long getLandLineNumber () {
685 return this.landLineNumber;
689 * Setter for land-line number
691 * @param landLineNumber Land-line number
693 public void setLandLineNumber (final Long landLineNumber) {
694 this.landLineNumber = landLineNumber;
698 * Getter for ending week day
700 * @return Ending week day
702 public DayOfTheWeek getOpeningEndDay () {
703 return this.openingEndDay;
707 * Setter for ending week day
709 * @param openingEndDay Ending week day
711 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
712 this.openingEndDay = openingEndDay;
716 * Getter for ending time
718 * @return Ending time
720 @SuppressWarnings ("ReturnOfDateField")
721 public Date getOpeningEndTime () {
722 return this.openingEndTime;
726 * Getter for ending time
728 * @param openingEndTime Ending time
730 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
731 public void setOpeningEndTime (final Date openingEndTime) {
732 this.openingEndTime = openingEndTime;
736 * Getter for starting week day
738 * @return Starting week day
740 public DayOfTheWeek getOpeningStartDay () {
741 return this.openingStartDay;
745 * Getter for starting week day
747 * @param openingStartDay Starting week day
749 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
750 this.openingStartDay = openingStartDay;
754 * Getter for starting time
756 * @return Starting time
758 @SuppressWarnings ("ReturnOfDateField")
759 public Date getOpeningStartTime () {
760 return this.openingStartTime;
764 * Getter for starting time
766 * @param openingStartTime Starting time
768 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
769 public void setOpeningStartTime (final Date openingStartTime) {
770 this.openingStartTime = openingStartTime;
774 * Clears this bean data
776 private void clear () {
777 // Clear all branch office data
778 this.setBranchCity(null);
779 this.setBranchCompany(null);
780 this.setBranchContactEmployee(null);
781 this.setBranchCountry(null);
782 this.setBranchEmailAddress(null);
783 this.setBranchHouseNumber(null);
784 this.setBranchHouseNumberExtension(null);
785 this.setBranchLastHouseNumber(null);
786 this.setBranchNumber(null);
787 this.setBranchOwner(null);
788 this.setBranchStore(null);
789 this.setBranchStreet(null);
790 this.setBranchSuiteNumber(null);
791 this.setBranchUserOwner(null);
792 this.setBranchZipCode(null);
794 // Opening times list
795 this.setBranchOpeningTimes(new ArrayList<OpeningTime>(1));
797 // Fax and land-line number
798 this.setFaxAreaCode(null);
799 this.setFaxCountry(null);
800 this.setFaxNumber(null);
801 this.setLandLineAreaCode(null);
802 this.setLandLineCountry(null);
803 this.setLandLineNumber(null);
805 // Extra-clear opening time
806 this.clearOpeningTime();
810 * Clears all opening time fields
812 private void clearOpeningTime () {
813 // Clear all opening time fields
814 this.setOpeningEndDay(null);
815 this.setOpeningEndTime(null);
816 this.setOpeningStartDay(null);
817 this.setOpeningStartTime(null);
821 * Prepares an instance of a BranchOffice object (entity) with all data from
822 * this bean. If a complete fax number or land-line number was provided, it
823 * will be set in the instance as well.
825 * @return An instance of a BranchOffice class (entity)
827 private BranchOffice createBranchOffice () {
828 // Create new branch office instance
829 final BranchOffice branchOffice = new BusinessBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
831 // Add all other fields, too
832 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
833 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
834 branchOffice.setBranchHouseNumberExtension(this.getBranchHouseNumberExtension());
835 branchOffice.setBranchLastHouseNumber(this.getBranchLastHouseNumber());
836 branchOffice.setBranchNumber(this.getBranchNumber());
837 branchOffice.setBranchOwnerEmployee(this.getBranchOwner());
838 branchOffice.setBranchStore(this.getBranchStore());
839 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
840 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
842 // Generate phone number
843 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
844 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
846 // Don't set null or wrong references
847 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
848 // Now the number must be given
849 if (landLine.getPhoneAreaCode() == null) {
851 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
852 } else if (landLine.getPhoneAreaCode() < 1) {
854 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
855 } else if (landLine.getPhoneNumber() == null) {
857 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
858 } else if (landLine.getPhoneNumber() < 1) {
860 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
864 branchOffice.setBranchLandLineNumber(landLine);
867 // Don't set null or wrong references
868 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
869 // Now the number must be given
870 if (fax.getPhoneAreaCode() == null) {
872 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
873 } else if (fax.getPhoneAreaCode() < 1) {
875 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
876 } else if (fax.getPhoneNumber() == null) {
878 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
879 } else if (fax.getPhoneNumber() < 1) {
881 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
885 branchOffice.setBranchFaxNumber(fax);
887 // Is the opening times list filled?
888 if (!this.getBranchOpeningTimes().isEmpty()) {
889 // Yes, then set in branch office, too
890 branchOffice.setBranchOpeningTimes(this.getBranchOpeningTimes());
893 // Return fully prepared instance
898 * Prepares an instance of a OpeningTimes object (entity) with all data from
899 * this bean. If a complete fax number or land-line number was provided, it
900 * will be set in the instance as well.
902 * @return An instance of a OpeningTimes class (entity)
904 private OpeningTime createOpeningTimes () {
905 // Create new openingTime instance
906 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
908 // Return fully prepared instance
913 * Checks whether the given branch office's address is already found in
914 * local cache. Please note that this method fully relies on the cache, so
915 * you must always fire proper events that add/update/delete entries in
918 * @param branchOffice Branch office to check it's address
920 * @return Whether the address has been found
922 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
923 // Get full list from other bean
924 final List<BranchOffice> branchOffices = this.branchOfficeListController.allBranchOffices();
926 // Default is not found
927 boolean isFound = false;
929 // Now check each entry
930 for (final BranchOffice bo : branchOffices) {
932 if (BranchOffices.isSameAddress(bo, branchOffice)) {
944 * Checks if given opening time is already added
946 * @param openingTime Opening time to be checked
948 * @return Whether it has been added already
950 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
951 // Default is not found
952 boolean isFound = false;
955 for (final OpeningTime ot : this.getBranchOpeningTimes()) {
957 if (Objects.equals(ot, openingTime)) {