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.faces.application.FacesMessage;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jcontactsbusiness.events.branchoffice.added.BranchOfficeAddedEvent;
31 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
32 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
33 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
37 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
38 import org.mxchange.jcontactsbusiness.model.employee.Employable;
39 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
40 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
41 import org.mxchange.jcontactsbusiness.model.opening_time.dayofweek.DayOfTheWeek;
42 import org.mxchange.jcountry.model.data.Country;
43 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
44 import org.mxchange.jfinancials.beans.business.branchoffice.list.FinancialsBranchOfficeListWebViewController;
45 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
46 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
47 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
48 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
49 import org.mxchange.jusercore.model.user.User;
52 * An administrative bean for branch offices
54 * @author Roland Häder<roland@mxchange.org>
56 @Named ("adminBranchOfficeController")
58 public class FinancialsAdminBranchOfficeWebRequestBean extends BaseFinancialsBean implements FinancialsAdminBranchOfficeWebRequestController {
61 * Opening times of this branch office
63 private static List<OpeningTime> branchOpeningTimes;
68 private static final long serialVersionUID = 5_028_697_360_461L;
71 * EJB for administrative purposes
73 @EJB (lookup = "java:global/jfinancials-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
74 private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
79 private String branchCity;
82 * Assigned company for this branch office
84 private BasicData branchCompany;
87 * Contact person in branch office
89 private Employable branchContactEmployee;
94 private Country branchCountry;
99 private String branchEmailAddress;
104 private Short branchHouseNumber;
107 * House number's extension (a,b,c,...)
109 private String branchHouseNumberExtension;
114 private Short branchLastHouseNumber;
117 * Number of branch office
119 private Long branchNumber;
122 * An event being fired when a branch office has been successfully added
126 private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
129 * A list branch office controller (backing bean)
132 private FinancialsBranchOfficeListWebViewController branchOfficeListController;
135 * Owner/leader of branch office
137 private Employable branchOwner;
142 private Short branchStore;
145 * Branch office street name
147 private String branchStreet;
152 private Short branchSuiteNumber;
155 * Owning user instance (which this branch office is assigned to)
157 private User branchUserOwner;
162 private Integer branchZipCode;
165 * Area code for fax number
167 private Integer faxAreaCode;
170 * Country for fax number
172 private Country faxCountry;
175 * Dial number for fax number
177 private Long faxNumber;
180 * Area code for land-line number
182 private Integer landLineAreaCode;
185 * Country for land-line number
187 private Country landLineCountry;
190 * Dial number for land-line number
192 private Long landLineNumber;
197 private DayOfTheWeek openingEndDay;
202 private Date openingEndTime;
207 private DayOfTheWeek openingStartDay;
212 private Date openingStartTime;
215 * Default constructor
217 public FinancialsAdminBranchOfficeWebRequestBean () {
218 // Call super constructor
221 // Is the opening times list there?
222 if (null == branchOpeningTimes) {
224 branchOpeningTimes = new ArrayList<>(1);
229 * Adds branch office with all data from this backing bean. First this
230 * action method will validate if the branch office's address is already
231 * registered and if found, it will output a proper faces message.
233 public void 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", FacesMessage.SEVERITY_WARN); //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", FacesMessage.SEVERITY_ERROR); //NOI18N
257 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
264 * Adds opening time to temporary list which will be sent along with the
265 * branch office data to the EJB.
267 public void addOpeningTime () {
268 // Validate all required fields
269 if (this.getOpeningEndDay() == null) {
271 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
272 } else if (this.getOpeningEndTime() == null) {
274 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
275 } else if (this.getOpeningStartDay() == null) {
277 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
278 } else if (this.getOpeningStartTime() == null) {
280 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
283 // Get opening time instance
284 final OpeningTime openingTime = this.createOpeningTimes();
287 if (this.isSameOpeningTimeFound(openingTime)) {
288 // Yes then abort here
289 this.showFacesMessage("form-admin-add-branch-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
293 // Add to temporary list
294 branchOpeningTimes.add(openingTime);
296 // Clear opening time fields
297 this.clearOpeningTime();
305 public String getBranchCity () {
306 return this.branchCity;
312 * @param branchCity City
314 public void setBranchCity (final String branchCity) {
315 this.branchCity = branchCity;
319 * Getter for basic company data
321 * @return Basic company data
323 public BasicData getBranchCompany () {
324 return this.branchCompany;
328 * Setter for basic company data
330 * @param branchCompany Basic company data
332 public void setBranchCompany (final BasicData branchCompany) {
333 this.branchCompany = branchCompany;
337 * Getter for branch office contact person
339 * @return Branch office contact person
341 public Employable getBranchContactEmployee () {
342 return this.branchContactEmployee;
346 * Setter for branch office contact person
348 * @param branchContactEmployee Branch office contact person
350 public void setBranchContactEmployee (final Employable branchContactEmployee) {
351 this.branchContactEmployee = branchContactEmployee;
359 public Country getBranchCountry () {
360 return this.branchCountry;
366 * @param branchCountry Country
368 public void setBranchCountry (final Country branchCountry) {
369 this.branchCountry = branchCountry;
373 * Getter for email address
375 * @return Email address
377 public String getBranchEmailAddress () {
378 return this.branchEmailAddress;
382 * Getter for email address
384 * @param branchEmailAddress Email address
386 public void setBranchEmailAddress (final String branchEmailAddress) {
387 this.branchEmailAddress = branchEmailAddress;
391 * Getter for house number
393 * @return House number
395 public Short getBranchHouseNumber () {
396 return this.branchHouseNumber;
400 * Setter for house number
402 * @param branchHouseNumber House number
404 public void setBranchHouseNumber (final Short branchHouseNumber) {
405 this.branchHouseNumber = branchHouseNumber;
409 * Getter for house number's extension
411 * @return House number's extension
413 public String getBranchHouseNumberExtension () {
414 return this.branchHouseNumberExtension;
418 * Setter for house number's extension
420 * @param branchHouseNumberExtension House number's extension
422 public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
423 this.branchHouseNumberExtension = branchHouseNumberExtension;
427 * Getter for last house number
429 * @return Last house number
431 public Short getBranchLastHouseNumber () {
432 return this.branchLastHouseNumber;
436 * Setter for last house number
438 * @param branchLastHouseNumber Last house number
440 public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
441 this.branchLastHouseNumber = branchLastHouseNumber;
445 * Getter for branch office number
447 * @return Branch office number
449 public Long getBranchNumber () {
450 return this.branchNumber;
454 * Setter for branch office number
456 * @param branchNumber Branch office number
458 public void setBranchNumber (final Long branchNumber) {
459 this.branchNumber = branchNumber;
463 * Getter for opening times of this branch office
465 * @return Opening times
467 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
468 public List<OpeningTime> getBranchOpeningTimes () {
469 return branchOpeningTimes;
473 * Setter for opening times of this branch office
475 * @param branchOpeningTimes Opening times
477 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
478 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
479 FinancialsAdminBranchOfficeWebRequestBean.branchOpeningTimes = branchOpeningTimes;
483 * Getter for branch office contact person
485 * @return Branch office contact person
487 public Employable getBranchOwner () {
488 return this.branchOwner;
492 * Setter for branch office contact person
494 * @param branchOwner Branch office contact person
496 public void setBranchOwner (final Employable branchOwner) {
497 this.branchOwner = branchOwner;
505 public Short getBranchStore () {
506 return this.branchStore;
512 * @param branchStore Store
514 public void setBranchStore (final Short branchStore) {
515 this.branchStore = branchStore;
519 * Getter for street name
521 * @return Street name
523 public String getBranchStreet () {
524 return this.branchStreet;
528 * Setter for street name
530 * @param branchStreet Street name
532 public void setBranchStreet (final String branchStreet) {
533 this.branchStreet = branchStreet;
537 * Getter for suite number
539 * @return Suite number
541 public Short getBranchSuiteNumber () {
542 return this.branchSuiteNumber;
546 * Setter for suite number
548 * @param branchSuiteNumber Suite number
550 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
551 this.branchSuiteNumber = branchSuiteNumber;
555 * Getter for owning user instance
557 * @return Owning user instance
559 public User getBranchUserOwner () {
560 return this.branchUserOwner;
564 * Setter for owning user instance
566 * @param branchUserOwner Owning user instance
568 public void setBranchUserOwner (final User branchUserOwner) {
569 this.branchUserOwner = branchUserOwner;
573 * Getter for ZIP code\
577 public Integer getBranchZipCode () {
578 return this.branchZipCode;
582 * Setter for ZIP code\
584 * @param branchZipCode ZIP code
586 public void setBranchZipCode (final Integer branchZipCode) {
587 this.branchZipCode = branchZipCode;
591 * Getter for fax number's area code
593 * @return Fax number's area code
595 public Integer getFaxAreaCode () {
596 return this.faxAreaCode;
600 * Setter for fax number's area code
602 * @param faxAreaCode Fax number's area code
604 public void setFaxAreaCode (final Integer faxAreaCode) {
605 this.faxAreaCode = faxAreaCode;
609 * Getter for fax's country instance
611 * @return Fax' country instance
613 public Country getFaxCountry () {
614 return this.faxCountry;
618 * Setter for fax's country instance
620 * @param faxCountry Fax' country instance
622 public void setFaxCountry (final Country faxCountry) {
623 this.faxCountry = faxCountry;
627 * Getter for fax number
631 public Long getFaxNumber () {
632 return this.faxNumber;
636 * Setter for fax number
638 * @param faxNumber Fax number
640 public void setFaxNumber (final Long faxNumber) {
641 this.faxNumber = faxNumber;
645 * Getter for land-line number's area code
647 * @return Land-line number's area code
649 public Integer getLandLineAreaCode () {
650 return this.landLineAreaCode;
654 * Setter for land-line number's area code
656 * @param landLineAreaCode Land-line number's area code
658 public void setLandLineAreaCode (final Integer landLineAreaCode) {
659 this.landLineAreaCode = landLineAreaCode;
663 * Getter for land-line number's country instance
665 * @return Land-line number's country instance
667 public Country getLandLineCountry () {
668 return this.landLineCountry;
672 * Setter for land-line number's country instance
674 * @param landLineCountry Land-line number's country instance
676 public void setLandLineCountry (final Country landLineCountry) {
677 this.landLineCountry = landLineCountry;
681 * Getter for land-line number
683 * @return Land-line number
685 public Long getLandLineNumber () {
686 return this.landLineNumber;
690 * Setter for land-line number
692 * @param landLineNumber Land-line number
694 public void setLandLineNumber (final Long landLineNumber) {
695 this.landLineNumber = landLineNumber;
699 * Getter for ending week day
701 * @return Ending week day
703 public DayOfTheWeek getOpeningEndDay () {
704 return this.openingEndDay;
708 * Setter for ending week day
710 * @param openingEndDay Ending week day
712 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
713 this.openingEndDay = openingEndDay;
717 * Getter for ending time
719 * @return Ending time
721 @SuppressWarnings ("ReturnOfDateField")
722 public Date getOpeningEndTime () {
723 return this.openingEndTime;
727 * Getter for ending time
729 * @param openingEndTime Ending time
731 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
732 public void setOpeningEndTime (final Date openingEndTime) {
733 this.openingEndTime = openingEndTime;
737 * Getter for starting week day
739 * @return Starting week day
741 public DayOfTheWeek getOpeningStartDay () {
742 return this.openingStartDay;
746 * Getter for starting week day
748 * @param openingStartDay Starting week day
750 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
751 this.openingStartDay = openingStartDay;
755 * Getter for starting time
757 * @return Starting time
759 @SuppressWarnings ("ReturnOfDateField")
760 public Date getOpeningStartTime () {
761 return this.openingStartTime;
765 * Getter for starting time
767 * @param openingStartTime Starting time
769 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
770 public void setOpeningStartTime (final Date openingStartTime) {
771 this.openingStartTime = openingStartTime;
775 * Clears this bean data
777 private void clear () {
778 // Clear all branch office data
779 this.setBranchCity(null);
780 this.setBranchCompany(null);
781 this.setBranchContactEmployee(null);
782 this.setBranchCountry(null);
783 this.setBranchEmailAddress(null);
784 this.setBranchHouseNumber(null);
785 this.setBranchHouseNumberExtension(null);
786 this.setBranchLastHouseNumber(null);
787 this.setBranchNumber(null);
788 this.setBranchOwner(null);
789 this.setBranchStore(null);
790 this.setBranchStreet(null);
791 this.setBranchSuiteNumber(null);
792 this.setBranchUserOwner(null);
793 this.setBranchZipCode(null);
795 // Opening times list
796 this.setBranchOpeningTimes(new ArrayList<OpeningTime>(1));
798 // Fax and land-line number
799 this.setFaxAreaCode(null);
800 this.setFaxCountry(null);
801 this.setFaxNumber(null);
802 this.setLandLineAreaCode(null);
803 this.setLandLineCountry(null);
804 this.setLandLineNumber(null);
806 // Extra-clear opening time
807 this.clearOpeningTime();
811 * Clears all opening time fields
813 private void clearOpeningTime () {
814 // Clear all opening time fields
815 this.setOpeningEndDay(null);
816 this.setOpeningEndTime(null);
817 this.setOpeningStartDay(null);
818 this.setOpeningStartTime(null);
822 * Prepares an instance of a BranchOffice object (entity) with all data from
823 * this bean. If a complete fax number or land-line number was provided, it
824 * will be set in the instance as well.
826 * @return An instance of a BranchOffice class (entity)
828 private BranchOffice createBranchOffice () {
829 // Create new branch office instance
830 final BranchOffice branchOffice = new BusinessBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
832 // Add all other fields, too
833 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
834 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
835 branchOffice.setBranchHouseNumberExtension(this.getBranchHouseNumberExtension());
836 branchOffice.setBranchLastHouseNumber(this.getBranchLastHouseNumber());
837 branchOffice.setBranchNumber(this.getBranchNumber());
838 branchOffice.setBranchOwnerEmployee(this.getBranchOwner());
839 branchOffice.setBranchStore(this.getBranchStore());
840 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
841 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
843 // Generate phone number
844 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
845 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
847 // Don't set null or wrong references
848 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
849 // Now the number must be given
850 if (landLine.getPhoneAreaCode() == null) {
852 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
853 } else if (landLine.getPhoneAreaCode() < 1) {
855 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
856 } else if (landLine.getPhoneNumber() == null) {
858 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
859 } else if (landLine.getPhoneNumber() < 1) {
861 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
865 branchOffice.setBranchLandLineNumber(landLine);
868 // Don't set null or wrong references
869 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
870 // Now the number must be given
871 if (fax.getPhoneAreaCode() == null) {
873 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
874 } else if (fax.getPhoneAreaCode() < 1) {
876 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
877 } else if (fax.getPhoneNumber() == null) {
879 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
880 } else if (fax.getPhoneNumber() < 1) {
882 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
886 branchOffice.setBranchFaxNumber(fax);
888 // Is the opening times list filled?
889 if (!this.getBranchOpeningTimes().isEmpty()) {
890 // Yes, then set in branch office, too
891 branchOffice.setBranchOpeningTimes(this.getBranchOpeningTimes());
894 // Return fully prepared instance
899 * Prepares an instance of a OpeningTimes object (entity) with all data from
900 * this bean. If a complete fax number or land-line number was provided, it
901 * will be set in the instance as well.
903 * @return An instance of a OpeningTimes class (entity)
905 private OpeningTime createOpeningTimes () {
906 // Create new openingTime instance
907 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
909 // Return fully prepared instance
914 * Checks whether the given branch office's address is already found in
915 * local cache. Please note that this method fully relies on the cache, so
916 * you must always fire proper events that add/update/delete entries in
919 * @param branchOffice Branch office to check it's address
921 * @return Whether the address has been found
923 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
924 // Get full list from other bean
925 final List<BranchOffice> branchOffices = this.branchOfficeListController.getAllBranchOffices();
927 // Default is not found
928 boolean isFound = false;
930 // Now check each entry
931 for (final BranchOffice bo : branchOffices) {
933 if (BranchOffices.isSameAddress(bo, branchOffice)) {
945 * Checks if given opening time is already added
947 * @param openingTime Opening time to be checked
949 * @return Whether it has been added already
951 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
952 // Default is not found
953 boolean isFound = false;
956 for (final OpeningTime ot : this.getBranchOpeningTimes()) {
958 if (Objects.equals(ot, openingTime)) {