2 * Copyright (C) 2017, 2018 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.jphone.model.phonenumbers.fax.DialableFaxNumber;
44 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
45 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
47 import org.mxchange.jusercore.model.user.User;
50 * An administrative bean for branch offices
52 * @author Roland Häder<roland@mxchange.org>
54 @Named ("adminBranchOfficeController")
56 public class FinancialsAdminBranchOfficeWebRequestBean extends BaseFinancialsBean implements FinancialsAdminBranchOfficeWebRequestController {
59 * Opening times of this branch office
61 private static List<OpeningTime> branchOpeningTimes;
66 private static final long serialVersionUID = 5_028_697_360_461L;
69 * EJB for administrative purposes
71 @EJB (lookup = "java:global/jfinancials-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
72 private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
77 private String branchCity;
80 * Assigned company for this branch office
82 private BasicData branchCompany;
85 * Contact person in branch office
87 private Employable branchContactEmployee;
92 private Country branchCountry;
97 private String branchEmailAddress;
102 private Short branchHouseNumber;
105 * House number's extension (a,b,c,...)
107 private String branchHouseNumberExtension;
112 private Short branchLastHouseNumber;
115 * Number of branch office
117 private Long branchNumber;
120 * An event being fired when a branch office has been successfully added
124 private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
127 * A general branch office controller (backing bean)
130 private FinancialsBranchOfficeWebRequestController branchOfficeController;
133 * Owner/leader of branch office
135 private Employable branchOwner;
140 private Short branchStore;
143 * Branch office street name
145 private String branchStreet;
150 private Short branchSuiteNumber;
153 * Owning user instance (which this branch office is assigned to)
155 private User branchUserOwner;
160 private Integer branchZipCode;
163 * Area code for fax number
165 private Integer faxAreaCode;
168 * Country for fax number
170 private Country faxCountry;
173 * Dial number for fax number
175 private Long faxNumber;
178 * Area code for land-line number
180 private Integer landLineAreaCode;
183 * Country for land-line number
185 private Country landLineCountry;
188 * Dial number for land-line number
190 private Long landLineNumber;
195 private DayOfTheWeek openingEndDay;
200 private Date openingEndTime;
205 private DayOfTheWeek openingStartDay;
210 private Date openingStartTime;
213 * Default constructor
215 public FinancialsAdminBranchOfficeWebRequestBean () {
216 // Call super constructor
219 // Is the opening times list there?
220 if (null == branchOpeningTimes) {
222 branchOpeningTimes = new ArrayList<>(1);
227 * Adds branch office with all data from this backing bean. First this
228 * action method will validate if the branch office's address is already
229 * registered and if found, it will output a proper faces message.
231 * @return Redirect outcome
233 public String addBranchOffice () {
235 final BranchOffice branchOffice = this.createBranchOffice();
237 // Is the branch office not created yet?
238 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
239 // Then show proper faces message
240 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
244 // Delcare updated instance
245 final BranchOffice updatedOffice;
249 updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
250 } catch (final BranchOfficeAlreadyAddedException ex) {
252 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
257 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
263 return "admin_list_branch_office"; //NOI18N
267 * Adds opening time to temporary list which will be sent along with the
268 * branch office data to the EJB.
270 public void addOpeningTime () {
271 // Validate all required fields
272 if (this.getOpeningEndDay() == null) {
274 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
275 } else if (this.getOpeningEndTime() == null) {
277 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
278 } else if (this.getOpeningStartDay() == null) {
280 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
281 } else if (this.getOpeningStartTime() == null) {
283 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
286 // Get opening time instance
287 final OpeningTime openingTime = this.createOpeningTimes();
290 if (this.isSameOpeningTimeFound(openingTime)) {
291 // Yes then abort here
292 this.showFacesMessage("form-admin-add-branch-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
296 // Add to temporary list
297 branchOpeningTimes.add(openingTime);
299 // Clear opening time fields
300 this.clearOpeningTime();
308 public String getBranchCity () {
309 return this.branchCity;
315 * @param branchCity City
317 public void setBranchCity (final String branchCity) {
318 this.branchCity = branchCity;
322 * Getter for basic company data
324 * @return Basic company data
326 public BasicData getBranchCompany () {
327 return this.branchCompany;
331 * Setter for basic company data
333 * @param branchCompany Basic company data
335 public void setBranchCompany (final BasicData branchCompany) {
336 this.branchCompany = branchCompany;
340 * Getter for branch office contact person
342 * @return Branch office contact person
344 public Employable getBranchContactEmployee () {
345 return this.branchContactEmployee;
349 * Setter for branch office contact person
351 * @param branchContactEmployee Branch office contact person
353 public void setBranchContactEmployee (final Employable branchContactEmployee) {
354 this.branchContactEmployee = branchContactEmployee;
362 public Country getBranchCountry () {
363 return this.branchCountry;
369 * @param branchCountry Country
371 public void setBranchCountry (final Country branchCountry) {
372 this.branchCountry = branchCountry;
376 * Getter for email address
378 * @return Email address
380 public String getBranchEmailAddress () {
381 return this.branchEmailAddress;
385 * Getter for email address
387 * @param branchEmailAddress Email address
389 public void setBranchEmailAddress (final String branchEmailAddress) {
390 this.branchEmailAddress = branchEmailAddress;
394 * Getter for house number
396 * @return House number
398 public Short getBranchHouseNumber () {
399 return this.branchHouseNumber;
403 * Setter for house number
405 * @param branchHouseNumber House number
407 public void setBranchHouseNumber (final Short branchHouseNumber) {
408 this.branchHouseNumber = branchHouseNumber;
412 * Getter for house number's extension
414 * @return House number's extension
416 public String getBranchHouseNumberExtension () {
417 return this.branchHouseNumberExtension;
421 * Setter for house number's extension
423 * @param branchHouseNumberExtension House number's extension
425 public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
426 this.branchHouseNumberExtension = branchHouseNumberExtension;
430 * Getter for last house number
432 * @return Last house number
434 public Short getBranchLastHouseNumber () {
435 return this.branchLastHouseNumber;
439 * Setter for last house number
441 * @param branchLastHouseNumber Last house number
443 public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
444 this.branchLastHouseNumber = branchLastHouseNumber;
448 * Getter for branch office number
450 * @return Branch office number
452 public Long getBranchNumber () {
453 return this.branchNumber;
457 * Setter for branch office number
459 * @param branchNumber Branch office number
461 public void setBranchNumber (final Long branchNumber) {
462 this.branchNumber = branchNumber;
466 * Getter for opening times of this branch office
468 * @return Opening times
470 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
471 public List<OpeningTime> getBranchOpeningTimes () {
472 return branchOpeningTimes;
476 * Setter for opening times of this branch office
478 * @param branchOpeningTimes Opening times
480 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
481 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
482 FinancialsAdminBranchOfficeWebRequestBean.branchOpeningTimes = branchOpeningTimes;
486 * Getter for branch office contact person
488 * @return Branch office contact person
490 public Employable getBranchOwner () {
491 return this.branchOwner;
495 * Setter for branch office contact person
497 * @param branchOwner Branch office contact person
499 public void setBranchOwner (final Employable branchOwner) {
500 this.branchOwner = branchOwner;
508 public Short getBranchStore () {
509 return this.branchStore;
515 * @param branchStore Store
517 public void setBranchStore (final Short branchStore) {
518 this.branchStore = branchStore;
522 * Getter for street name
524 * @return Street name
526 public String getBranchStreet () {
527 return this.branchStreet;
531 * Setter for street name
533 * @param branchStreet Street name
535 public void setBranchStreet (final String branchStreet) {
536 this.branchStreet = branchStreet;
540 * Getter for suite number
542 * @return Suite number
544 public Short getBranchSuiteNumber () {
545 return this.branchSuiteNumber;
549 * Setter for suite number
551 * @param branchSuiteNumber Suite number
553 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
554 this.branchSuiteNumber = branchSuiteNumber;
558 * Getter for owning user instance
560 * @return Owning user instance
562 public User getBranchUserOwner () {
563 return this.branchUserOwner;
567 * Setter for owning user instance
569 * @param branchUserOwner Owning user instance
571 public void setBranchUserOwner (final User branchUserOwner) {
572 this.branchUserOwner = branchUserOwner;
576 * Getter for ZIP code\
580 public Integer getBranchZipCode () {
581 return this.branchZipCode;
585 * Setter for ZIP code\
587 * @param branchZipCode ZIP code
589 public void setBranchZipCode (final Integer branchZipCode) {
590 this.branchZipCode = branchZipCode;
594 * Getter for fax number's area code
596 * @return Fax number's area code
598 public Integer getFaxAreaCode () {
599 return this.faxAreaCode;
603 * Setter for fax number's area code
605 * @param faxAreaCode Fax number's area code
607 public void setFaxAreaCode (final Integer faxAreaCode) {
608 this.faxAreaCode = faxAreaCode;
612 * Getter for fax's country instance
614 * @return Fax' country instance
616 public Country getFaxCountry () {
617 return this.faxCountry;
621 * Setter for fax's country instance
623 * @param faxCountry Fax' country instance
625 public void setFaxCountry (final Country faxCountry) {
626 this.faxCountry = faxCountry;
630 * Getter for fax number
634 public Long getFaxNumber () {
635 return this.faxNumber;
639 * Setter for fax number
641 * @param faxNumber Fax number
643 public void setFaxNumber (final Long faxNumber) {
644 this.faxNumber = faxNumber;
648 * Getter for land-line number's area code
650 * @return Land-line number's area code
652 public Integer getLandLineAreaCode () {
653 return this.landLineAreaCode;
657 * Setter for land-line number's area code
659 * @param landLineAreaCode Land-line number's area code
661 public void setLandLineAreaCode (final Integer landLineAreaCode) {
662 this.landLineAreaCode = landLineAreaCode;
666 * Getter for land-line number's country instance
668 * @return Land-line number's country instance
670 public Country getLandLineCountry () {
671 return this.landLineCountry;
675 * Setter for land-line number's country instance
677 * @param landLineCountry Land-line number's country instance
679 public void setLandLineCountry (final Country landLineCountry) {
680 this.landLineCountry = landLineCountry;
684 * Getter for land-line number
686 * @return Land-line number
688 public Long getLandLineNumber () {
689 return this.landLineNumber;
693 * Setter for land-line number
695 * @param landLineNumber Land-line number
697 public void setLandLineNumber (final Long landLineNumber) {
698 this.landLineNumber = landLineNumber;
702 * Getter for ending week day
704 * @return Ending week day
706 public DayOfTheWeek getOpeningEndDay () {
707 return this.openingEndDay;
711 * Setter for ending week day
713 * @param openingEndDay Ending week day
715 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
716 this.openingEndDay = openingEndDay;
720 * Getter for ending time
722 * @return Ending time
724 @SuppressWarnings ("ReturnOfDateField")
725 public Date getOpeningEndTime () {
726 return this.openingEndTime;
730 * Getter for ending time
732 * @param openingEndTime Ending time
734 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
735 public void setOpeningEndTime (final Date openingEndTime) {
736 this.openingEndTime = openingEndTime;
740 * Getter for starting week day
742 * @return Starting week day
744 public DayOfTheWeek getOpeningStartDay () {
745 return this.openingStartDay;
749 * Getter for starting week day
751 * @param openingStartDay Starting week day
753 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
754 this.openingStartDay = openingStartDay;
758 * Getter for starting time
760 * @return Starting time
762 @SuppressWarnings ("ReturnOfDateField")
763 public Date getOpeningStartTime () {
764 return this.openingStartTime;
768 * Getter for starting time
770 * @param openingStartTime Starting time
772 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
773 public void setOpeningStartTime (final Date openingStartTime) {
774 this.openingStartTime = openingStartTime;
778 * Clears this bean data
780 private void clear () {
781 // Clear all branch office data
782 this.setBranchCity(null);
783 this.setBranchCompany(null);
784 this.setBranchContactEmployee(null);
785 this.setBranchCountry(null);
786 this.setBranchEmailAddress(null);
787 this.setBranchHouseNumber(null);
788 this.setBranchHouseNumberExtension(null);
789 this.setBranchLastHouseNumber(null);
790 this.setBranchNumber(null);
791 this.setBranchOwner(null);
792 this.setBranchStore(null);
793 this.setBranchStreet(null);
794 this.setBranchSuiteNumber(null);
795 this.setBranchUserOwner(null);
796 this.setBranchZipCode(null);
798 // Opening times list
799 this.setBranchOpeningTimes(new ArrayList<OpeningTime>(1));
801 // Fax and land-line number
802 this.setFaxAreaCode(null);
803 this.setFaxCountry(null);
804 this.setFaxNumber(null);
805 this.setLandLineAreaCode(null);
806 this.setLandLineCountry(null);
807 this.setLandLineNumber(null);
809 // Extra-clear opening time
810 this.clearOpeningTime();
814 * Clears all opening time fields
816 private void clearOpeningTime () {
817 // Clear all opening time fields
818 this.setOpeningEndDay(null);
819 this.setOpeningEndTime(null);
820 this.setOpeningStartDay(null);
821 this.setOpeningStartTime(null);
825 * Prepares an instance of a BranchOffice object (entity) with all data from
826 * this bean. If a complete fax number or land-line number was provided, it
827 * will be set in the instance as well.
829 * @return An instance of a BranchOffice class (entity)
831 private BranchOffice createBranchOffice () {
832 // Create new branch office instance
833 final BranchOffice branchOffice = new BusinessBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
835 // Add all other fields, too
836 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
837 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
838 branchOffice.setBranchHouseNumberExtension(this.getBranchHouseNumberExtension());
839 branchOffice.setBranchLastHouseNumber(this.getBranchLastHouseNumber());
840 branchOffice.setBranchNumber(this.getBranchNumber());
841 branchOffice.setBranchOwnerEmployee(this.getBranchOwner());
842 branchOffice.setBranchStore(this.getBranchStore());
843 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
844 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
846 // Generate phone number
847 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
848 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
850 // Don't set null or wrong references
851 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
852 // Now the number must be given
853 if (landLine.getPhoneAreaCode() == null) {
855 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
856 } else if (landLine.getPhoneAreaCode() < 1) {
858 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
859 } else if (landLine.getPhoneNumber() == null) {
861 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
862 } else if (landLine.getPhoneNumber() < 1) {
864 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
868 branchOffice.setBranchLandLineNumber(landLine);
871 // Don't set null or wrong references
872 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
873 // Now the number must be given
874 if (fax.getPhoneAreaCode() == null) {
876 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
877 } else if (fax.getPhoneAreaCode() < 1) {
879 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
880 } else if (fax.getPhoneNumber() == null) {
882 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
883 } else if (fax.getPhoneNumber() < 1) {
885 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
889 branchOffice.setBranchFaxNumber(fax);
891 // Is the opening times list filled?
892 if (!this.getBranchOpeningTimes().isEmpty()) {
893 // Yes, then set in branch office, too
894 branchOffice.setBranchOpeningTimes(this.getBranchOpeningTimes());
897 // Return fully prepared instance
902 * Prepares an instance of a OpeningTimes object (entity) with all data from
903 * this bean. If a complete fax number or land-line number was provided, it
904 * will be set in the instance as well.
906 * @return An instance of a OpeningTimes class (entity)
908 private OpeningTime createOpeningTimes () {
909 // Create new openingTime instance
910 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
912 // Return fully prepared instance
917 * Checks whether the given branch office's address is already found in
918 * local cache. Please note that this method fully relies on the cache, so
919 * you must always fire proper events that add/update/delete entries in
922 * @param branchOffice Branch office to check it's address
924 * @return Whether the address has been found
926 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
927 // Get full list from other bean
928 final List<BranchOffice> branchOffices = this.branchOfficeController.allBranchOffices();
930 // Default is not found
931 boolean isFound = false;
933 // Now check each entry
934 for (final BranchOffice bo : branchOffices) {
936 if (BranchOffices.isSameAddress(bo, branchOffice)) {
948 * Checks if given opening time is already added
950 * @param openingTime Opening time to be checked
952 * @return Whether it has been added already
954 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
955 // Default is not found
956 boolean isFound = false;
959 for (final OpeningTime ot : this.getBranchOpeningTimes()) {
961 if (Objects.equals(ot, openingTime)) {