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 public void addBranchOffice () {
233 final BranchOffice branchOffice = this.createBranchOffice();
235 // Is the branch office not created yet?
236 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
237 // Then show proper faces message
238 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
242 // Delcare updated instance
243 final BranchOffice updatedOffice;
247 updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
248 } catch (final BranchOfficeAlreadyAddedException ex) {
250 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
255 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
262 * Adds opening time to temporary list which will be sent along with the
263 * branch office data to the EJB.
265 public void addOpeningTime () {
266 // Validate all required fields
267 if (this.getOpeningEndDay() == null) {
269 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
270 } else if (this.getOpeningEndTime() == null) {
272 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
273 } else if (this.getOpeningStartDay() == null) {
275 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
276 } else if (this.getOpeningStartTime() == null) {
278 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
281 // Get opening time instance
282 final OpeningTime openingTime = this.createOpeningTimes();
285 if (this.isSameOpeningTimeFound(openingTime)) {
286 // Yes then abort here
287 this.showFacesMessage("form-admin-add-branch-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
291 // Add to temporary list
292 branchOpeningTimes.add(openingTime);
294 // Clear opening time fields
295 this.clearOpeningTime();
303 public String getBranchCity () {
304 return this.branchCity;
310 * @param branchCity City
312 public void setBranchCity (final String branchCity) {
313 this.branchCity = branchCity;
317 * Getter for basic company data
319 * @return Basic company data
321 public BasicData getBranchCompany () {
322 return this.branchCompany;
326 * Setter for basic company data
328 * @param branchCompany Basic company data
330 public void setBranchCompany (final BasicData branchCompany) {
331 this.branchCompany = branchCompany;
335 * Getter for branch office contact person
337 * @return Branch office contact person
339 public Employable getBranchContactEmployee () {
340 return this.branchContactEmployee;
344 * Setter for branch office contact person
346 * @param branchContactEmployee Branch office contact person
348 public void setBranchContactEmployee (final Employable branchContactEmployee) {
349 this.branchContactEmployee = branchContactEmployee;
357 public Country getBranchCountry () {
358 return this.branchCountry;
364 * @param branchCountry Country
366 public void setBranchCountry (final Country branchCountry) {
367 this.branchCountry = branchCountry;
371 * Getter for email address
373 * @return Email address
375 public String getBranchEmailAddress () {
376 return this.branchEmailAddress;
380 * Getter for email address
382 * @param branchEmailAddress Email address
384 public void setBranchEmailAddress (final String branchEmailAddress) {
385 this.branchEmailAddress = branchEmailAddress;
389 * Getter for house number
391 * @return House number
393 public Short getBranchHouseNumber () {
394 return this.branchHouseNumber;
398 * Setter for house number
400 * @param branchHouseNumber House number
402 public void setBranchHouseNumber (final Short branchHouseNumber) {
403 this.branchHouseNumber = branchHouseNumber;
407 * Getter for house number's extension
409 * @return House number's extension
411 public String getBranchHouseNumberExtension () {
412 return this.branchHouseNumberExtension;
416 * Setter for house number's extension
418 * @param branchHouseNumberExtension House number's extension
420 public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
421 this.branchHouseNumberExtension = branchHouseNumberExtension;
425 * Getter for last house number
427 * @return Last house number
429 public Short getBranchLastHouseNumber () {
430 return this.branchLastHouseNumber;
434 * Setter for last house number
436 * @param branchLastHouseNumber Last house number
438 public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
439 this.branchLastHouseNumber = branchLastHouseNumber;
443 * Getter for branch office number
445 * @return Branch office number
447 public Long getBranchNumber () {
448 return this.branchNumber;
452 * Setter for branch office number
454 * @param branchNumber Branch office number
456 public void setBranchNumber (final Long branchNumber) {
457 this.branchNumber = branchNumber;
461 * Getter for opening times of this branch office
463 * @return Opening times
465 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
466 public List<OpeningTime> getBranchOpeningTimes () {
467 return branchOpeningTimes;
471 * Setter for opening times of this branch office
473 * @param branchOpeningTimes Opening times
475 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
476 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
477 FinancialsAdminBranchOfficeWebRequestBean.branchOpeningTimes = branchOpeningTimes;
481 * Getter for branch office contact person
483 * @return Branch office contact person
485 public Employable getBranchOwner () {
486 return this.branchOwner;
490 * Setter for branch office contact person
492 * @param branchOwner Branch office contact person
494 public void setBranchOwner (final Employable branchOwner) {
495 this.branchOwner = branchOwner;
503 public Short getBranchStore () {
504 return this.branchStore;
510 * @param branchStore Store
512 public void setBranchStore (final Short branchStore) {
513 this.branchStore = branchStore;
517 * Getter for street name
519 * @return Street name
521 public String getBranchStreet () {
522 return this.branchStreet;
526 * Setter for street name
528 * @param branchStreet Street name
530 public void setBranchStreet (final String branchStreet) {
531 this.branchStreet = branchStreet;
535 * Getter for suite number
537 * @return Suite number
539 public Short getBranchSuiteNumber () {
540 return this.branchSuiteNumber;
544 * Setter for suite number
546 * @param branchSuiteNumber Suite number
548 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
549 this.branchSuiteNumber = branchSuiteNumber;
553 * Getter for owning user instance
555 * @return Owning user instance
557 public User getBranchUserOwner () {
558 return this.branchUserOwner;
562 * Setter for owning user instance
564 * @param branchUserOwner Owning user instance
566 public void setBranchUserOwner (final User branchUserOwner) {
567 this.branchUserOwner = branchUserOwner;
571 * Getter for ZIP code\
575 public Integer getBranchZipCode () {
576 return this.branchZipCode;
580 * Setter for ZIP code\
582 * @param branchZipCode ZIP code
584 public void setBranchZipCode (final Integer branchZipCode) {
585 this.branchZipCode = branchZipCode;
589 * Getter for fax number's area code
591 * @return Fax number's area code
593 public Integer getFaxAreaCode () {
594 return this.faxAreaCode;
598 * Setter for fax number's area code
600 * @param faxAreaCode Fax number's area code
602 public void setFaxAreaCode (final Integer faxAreaCode) {
603 this.faxAreaCode = faxAreaCode;
607 * Getter for fax's country instance
609 * @return Fax' country instance
611 public Country getFaxCountry () {
612 return this.faxCountry;
616 * Setter for fax's country instance
618 * @param faxCountry Fax' country instance
620 public void setFaxCountry (final Country faxCountry) {
621 this.faxCountry = faxCountry;
625 * Getter for fax number
629 public Long getFaxNumber () {
630 return this.faxNumber;
634 * Setter for fax number
636 * @param faxNumber Fax number
638 public void setFaxNumber (final Long faxNumber) {
639 this.faxNumber = faxNumber;
643 * Getter for land-line number's area code
645 * @return Land-line number's area code
647 public Integer getLandLineAreaCode () {
648 return this.landLineAreaCode;
652 * Setter for land-line number's area code
654 * @param landLineAreaCode Land-line number's area code
656 public void setLandLineAreaCode (final Integer landLineAreaCode) {
657 this.landLineAreaCode = landLineAreaCode;
661 * Getter for land-line number's country instance
663 * @return Land-line number's country instance
665 public Country getLandLineCountry () {
666 return this.landLineCountry;
670 * Setter for land-line number's country instance
672 * @param landLineCountry Land-line number's country instance
674 public void setLandLineCountry (final Country landLineCountry) {
675 this.landLineCountry = landLineCountry;
679 * Getter for land-line number
681 * @return Land-line number
683 public Long getLandLineNumber () {
684 return this.landLineNumber;
688 * Setter for land-line number
690 * @param landLineNumber Land-line number
692 public void setLandLineNumber (final Long landLineNumber) {
693 this.landLineNumber = landLineNumber;
697 * Getter for ending week day
699 * @return Ending week day
701 public DayOfTheWeek getOpeningEndDay () {
702 return this.openingEndDay;
706 * Setter for ending week day
708 * @param openingEndDay Ending week day
710 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
711 this.openingEndDay = openingEndDay;
715 * Getter for ending time
717 * @return Ending time
719 @SuppressWarnings ("ReturnOfDateField")
720 public Date getOpeningEndTime () {
721 return this.openingEndTime;
725 * Getter for ending time
727 * @param openingEndTime Ending time
729 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
730 public void setOpeningEndTime (final Date openingEndTime) {
731 this.openingEndTime = openingEndTime;
735 * Getter for starting week day
737 * @return Starting week day
739 public DayOfTheWeek getOpeningStartDay () {
740 return this.openingStartDay;
744 * Getter for starting week day
746 * @param openingStartDay Starting week day
748 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
749 this.openingStartDay = openingStartDay;
753 * Getter for starting time
755 * @return Starting time
757 @SuppressWarnings ("ReturnOfDateField")
758 public Date getOpeningStartTime () {
759 return this.openingStartTime;
763 * Getter for starting time
765 * @param openingStartTime Starting time
767 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
768 public void setOpeningStartTime (final Date openingStartTime) {
769 this.openingStartTime = openingStartTime;
773 * Clears this bean data
775 private void clear () {
776 // Clear all branch office data
777 this.setBranchCity(null);
778 this.setBranchCompany(null);
779 this.setBranchContactEmployee(null);
780 this.setBranchCountry(null);
781 this.setBranchEmailAddress(null);
782 this.setBranchHouseNumber(null);
783 this.setBranchHouseNumberExtension(null);
784 this.setBranchLastHouseNumber(null);
785 this.setBranchNumber(null);
786 this.setBranchOwner(null);
787 this.setBranchStore(null);
788 this.setBranchStreet(null);
789 this.setBranchSuiteNumber(null);
790 this.setBranchUserOwner(null);
791 this.setBranchZipCode(null);
793 // Opening times list
794 this.setBranchOpeningTimes(new ArrayList<OpeningTime>(1));
796 // Fax and land-line number
797 this.setFaxAreaCode(null);
798 this.setFaxCountry(null);
799 this.setFaxNumber(null);
800 this.setLandLineAreaCode(null);
801 this.setLandLineCountry(null);
802 this.setLandLineNumber(null);
804 // Extra-clear opening time
805 this.clearOpeningTime();
809 * Clears all opening time fields
811 private void clearOpeningTime () {
812 // Clear all opening time fields
813 this.setOpeningEndDay(null);
814 this.setOpeningEndTime(null);
815 this.setOpeningStartDay(null);
816 this.setOpeningStartTime(null);
820 * Prepares an instance of a BranchOffice object (entity) with all data from
821 * this bean. If a complete fax number or land-line number was provided, it
822 * will be set in the instance as well.
824 * @return An instance of a BranchOffice class (entity)
826 private BranchOffice createBranchOffice () {
827 // Create new branch office instance
828 final BranchOffice branchOffice = new BusinessBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
830 // Add all other fields, too
831 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
832 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
833 branchOffice.setBranchHouseNumberExtension(this.getBranchHouseNumberExtension());
834 branchOffice.setBranchLastHouseNumber(this.getBranchLastHouseNumber());
835 branchOffice.setBranchNumber(this.getBranchNumber());
836 branchOffice.setBranchOwnerEmployee(this.getBranchOwner());
837 branchOffice.setBranchStore(this.getBranchStore());
838 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
839 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
841 // Generate phone number
842 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
843 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
845 // Don't set null or wrong references
846 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
847 // Now the number must be given
848 if (landLine.getPhoneAreaCode() == null) {
850 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
851 } else if (landLine.getPhoneAreaCode() < 1) {
853 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
854 } else if (landLine.getPhoneNumber() == null) {
856 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
857 } else if (landLine.getPhoneNumber() < 1) {
859 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
863 branchOffice.setBranchLandLineNumber(landLine);
866 // Don't set null or wrong references
867 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
868 // Now the number must be given
869 if (fax.getPhoneAreaCode() == null) {
871 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
872 } else if (fax.getPhoneAreaCode() < 1) {
874 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
875 } else if (fax.getPhoneNumber() == null) {
877 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
878 } else if (fax.getPhoneNumber() < 1) {
880 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
884 branchOffice.setBranchFaxNumber(fax);
886 // Is the opening times list filled?
887 if (!this.getBranchOpeningTimes().isEmpty()) {
888 // Yes, then set in branch office, too
889 branchOffice.setBranchOpeningTimes(this.getBranchOpeningTimes());
892 // Return fully prepared instance
897 * Prepares an instance of a OpeningTimes object (entity) with all data from
898 * this bean. If a complete fax number or land-line number was provided, it
899 * will be set in the instance as well.
901 * @return An instance of a OpeningTimes class (entity)
903 private OpeningTime createOpeningTimes () {
904 // Create new openingTime instance
905 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
907 // Return fully prepared instance
912 * Checks whether the given branch office's address is already found in
913 * local cache. Please note that this method fully relies on the cache, so
914 * you must always fire proper events that add/update/delete entries in
917 * @param branchOffice Branch office to check it's address
919 * @return Whether the address has been found
921 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
922 // Get full list from other bean
923 final List<BranchOffice> branchOffices = this.branchOfficeController.allBranchOffices();
925 // Default is not found
926 boolean isFound = false;
928 // Now check each entry
929 for (final BranchOffice bo : branchOffices) {
931 if (BranchOffices.isSameAddress(bo, branchOffice)) {
943 * Checks if given opening time is already added
945 * @param openingTime Opening time to be checked
947 * @return Whether it has been added already
949 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
950 // Default is not found
951 boolean isFound = false;
954 for (final OpeningTime ot : this.getBranchOpeningTimes()) {
956 if (Objects.equals(ot, openingTime)) {