2 * Copyright (C) 2017 Roland Häder
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.SessionScoped;
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.branchoffice.AdminBranchOfficeSessionBeanRemote;
33 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
36 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
37 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
38 import org.mxchange.jcontactsbusiness.model.opening_time.dayofweek.DayOfTheWeek;
39 import org.mxchange.jcountry.model.data.Country;
40 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
41 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
42 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
43 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
44 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
45 import org.mxchange.jusercore.model.user.User;
46 import org.mxchange.jcontactsbusiness.model.employee.Employable;
47 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
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 {
61 private static final long serialVersionUID = 5_028_697_360_461L;
64 * EJB for administrative purposes
66 @EJB (lookup = "java:global/jfinancials-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
67 private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
72 private String branchCity;
75 * Assigned company for this branch office
77 private BasicData branchCompany;
80 * Contact person in branch office
82 private Employable branchContactEmployee;
87 private Country branchCountry;
92 private String branchEmailAddress;
97 private Short branchHouseNumber;
100 * Number of branch office
102 private Long branchNumber;
105 * An event being fired when a branch office has been successfully added
109 private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
112 * A general branch office controller (backing bean)
115 private FinancialsBranchOfficeWebRequestController branchOfficeController;
118 * Opening times of this branch office
120 private static List<OpeningTime> branchOpeningTimes;
125 private Short branchStore;
128 * Branch office street name
130 private String branchStreet;
135 private Short branchSuiteNumber;
138 * Owning user instance (which this branch office is assigned to)
140 private User branchUserOwner;
145 private Integer branchZipCode;
148 * Area code for fax number
150 private Integer faxAreaCode;
153 * Country for fax number
155 private Country faxCountry;
158 * Dial number for fax number
160 private Long faxNumber;
163 * Area code for land-line number
165 private Integer landLineAreaCode;
168 * Country for land-line number
170 private Country landLineCountry;
173 * Dial number for land-line number
175 private Long landLineNumber;
180 private DayOfTheWeek openingEndDay;
185 private Date openingEndTime;
190 private DayOfTheWeek openingStartDay;
195 private Date openingStartTime;
198 * Default constructor
200 public FinancialsAdminBranchOfficeWebRequestBean () {
201 // Call super constructor
204 // Is the opening times list there?
205 if (null == branchOpeningTimes) {
207 branchOpeningTimes = new ArrayList<>(1);
212 * Adds branch office with all data from this backing bean. First this
213 * action method will validate if the branch office's address is already
214 * registered and if found, it will output a proper faces message.
216 * @return Redirect outcome
218 public String addBranchOffice () {
220 final BranchOffice branchOffice = this.createBranchOffice();
222 // Is the branch office not created yet?
223 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
224 // Then show proper faces message
225 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
229 // Delcare updated instance
230 final BranchOffice updatedOffice;
234 updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
235 } catch (final BranchOfficeAlreadyAddedException ex) {
237 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
242 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
248 return "admin_list_branch_office"; //NOI18N
252 * Adds opening time to temporary list which will be sent along with the
253 * branch office data to the EJB.
255 public void addOpeningTime () {
256 // Validate all required fields
257 if (this.getOpeningEndDay() == null) {
259 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
260 } else if (this.getOpeningEndTime() == null) {
262 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
263 } else if (this.getOpeningStartDay() == null) {
265 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
266 } else if (this.getOpeningStartTime() == null) {
268 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
271 // Get opening time instance
272 final OpeningTime openingTime = this.createOpeningTimes();
275 if (this.isSameOpeningTimeFound(openingTime)) {
276 // Yes then abort here
277 this.showFacesMessage("form-admin-add-branch-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
281 // Add to temporary list
282 branchOpeningTimes.add(openingTime);
284 // Clear opening time fields
285 this.clearOpeningTime();
293 public String getBranchCity () {
294 return this.branchCity;
300 * @param branchCity City
302 public void setBranchCity (final String branchCity) {
303 this.branchCity = branchCity;
307 * Getter for basic company data
309 * @return Basic company data
311 public BasicData getBranchCompany () {
312 return this.branchCompany;
316 * Setter for basic company data
318 * @param branchCompany Basic company data
320 public void setBranchCompany (final BasicData branchCompany) {
321 this.branchCompany = branchCompany;
325 * Getter for branch office contact person
327 * @return Branch office contact person
329 public Employable getBranchContactEmployee () {
330 return this.branchContactEmployee;
334 * Setter for branch office contact person
336 * @param branchContactEmployee Branch office contact person
338 public void setBranchContactEmployee (final Employable branchContactEmployee) {
339 this.branchContactEmployee = branchContactEmployee;
347 public Country getBranchCountry () {
348 return this.branchCountry;
354 * @param branchCountry Country
356 public void setBranchCountry (final Country branchCountry) {
357 this.branchCountry = branchCountry;
361 * Getter for email address
363 * @return Email address
365 public String getBranchEmailAddress () {
366 return this.branchEmailAddress;
370 * Getter for email address
372 * @param branchEmailAddress Email address
374 public void setBranchEmailAddress (final String branchEmailAddress) {
375 this.branchEmailAddress = branchEmailAddress;
379 * Getter for house number
381 * @return House number
383 public Short getBranchHouseNumber () {
384 return this.branchHouseNumber;
388 * Setter for house number
390 * @param branchHouseNumber House number
392 public void setBranchHouseNumber (final Short branchHouseNumber) {
393 this.branchHouseNumber = branchHouseNumber;
397 * Getter for branch office number
399 * @return Branch office number
401 public Long getBranchNumber () {
402 return this.branchNumber;
406 * Setter for branch office number
408 * @param branchNumber Branch office number
410 public void setBranchNumber (final Long branchNumber) {
411 this.branchNumber = branchNumber;
415 * Getter for opening times of this branch office
417 * @return Opening times
419 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
420 public List<OpeningTime> getBranchOpeningTimes () {
421 return branchOpeningTimes;
425 * Setter for opening times of this branch office
427 * @param branchOpeningTimes Opening times
429 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
430 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
431 FinancialsAdminBranchOfficeWebRequestBean.branchOpeningTimes = branchOpeningTimes;
439 public Short getBranchStore () {
440 return this.branchStore;
446 * @param branchStore Store
448 public void setBranchStore (final Short branchStore) {
449 this.branchStore = branchStore;
453 * Getter for street name
455 * @return Street name
457 public String getBranchStreet () {
458 return this.branchStreet;
462 * Setter for street name
464 * @param branchStreet Street name
466 public void setBranchStreet (final String branchStreet) {
467 this.branchStreet = branchStreet;
471 * Getter for suite number
473 * @return Suite number
475 public Short getBranchSuiteNumber () {
476 return this.branchSuiteNumber;
480 * Setter for suite number
482 * @param branchSuiteNumber Suite number
484 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
485 this.branchSuiteNumber = branchSuiteNumber;
489 * Getter for owning user instance
491 * @return Owning user instance
493 public User getBranchUserOwner () {
494 return this.branchUserOwner;
498 * Setter for owning user instance
500 * @param branchUserOwner Owning user instance
502 public void setBranchUserOwner (final User branchUserOwner) {
503 this.branchUserOwner = branchUserOwner;
507 * Getter for ZIP code\
511 public Integer getBranchZipCode () {
512 return this.branchZipCode;
516 * Setter for ZIP code\
518 * @param branchZipCode ZIP code
520 public void setBranchZipCode (final Integer branchZipCode) {
521 this.branchZipCode = branchZipCode;
525 * Getter for fax number's area code
527 * @return Fax number's area code
529 public Integer getFaxAreaCode () {
530 return this.faxAreaCode;
534 * Setter for fax number's area code
536 * @param faxAreaCode Fax number's area code
538 public void setFaxAreaCode (final Integer faxAreaCode) {
539 this.faxAreaCode = faxAreaCode;
543 * Getter for fax's country instance
545 * @return Fax' country instance
547 public Country getFaxCountry () {
548 return this.faxCountry;
552 * Setter for fax's country instance
554 * @param faxCountry Fax' country instance
556 public void setFaxCountry (final Country faxCountry) {
557 this.faxCountry = faxCountry;
561 * Getter for fax number
565 public Long getFaxNumber () {
566 return this.faxNumber;
570 * Setter for fax number
572 * @param faxNumber Fax number
574 public void setFaxNumber (final Long faxNumber) {
575 this.faxNumber = faxNumber;
579 * Getter for land-line number's area code
581 * @return Land-line number's area code
583 public Integer getLandLineAreaCode () {
584 return this.landLineAreaCode;
588 * Setter for land-line number's area code
590 * @param landLineAreaCode Land-line number's area code
592 public void setLandLineAreaCode (final Integer landLineAreaCode) {
593 this.landLineAreaCode = landLineAreaCode;
597 * Getter for land-line number's country instance
599 * @return Land-line number's country instance
601 public Country getLandLineCountry () {
602 return this.landLineCountry;
606 * Setter for land-line number's country instance
608 * @param landLineCountry Land-line number's country instance
610 public void setLandLineCountry (final Country landLineCountry) {
611 this.landLineCountry = landLineCountry;
615 * Getter for land-line number
617 * @return Land-line number
619 public Long getLandLineNumber () {
620 return this.landLineNumber;
624 * Setter for land-line number
626 * @param landLineNumber Land-line number
628 public void setLandLineNumber (final Long landLineNumber) {
629 this.landLineNumber = landLineNumber;
633 * Getter for ending week day
635 * @return Ending week day
637 public DayOfTheWeek getOpeningEndDay () {
638 return this.openingEndDay;
642 * Setter for ending week day
644 * @param openingEndDay Ending week day
646 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
647 this.openingEndDay = openingEndDay;
651 * Getter for ending time
653 * @return Ending time
655 @SuppressWarnings ("ReturnOfDateField")
656 public Date getOpeningEndTime () {
657 return this.openingEndTime;
661 * Getter for ending time
663 * @param openingEndTime Ending time
665 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
666 public void setOpeningEndTime (final Date openingEndTime) {
667 this.openingEndTime = openingEndTime;
671 * Getter for starting week day
673 * @return Starting week day
675 public DayOfTheWeek getOpeningStartDay () {
676 return this.openingStartDay;
680 * Getter for starting week day
682 * @param openingStartDay Starting week day
684 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
685 this.openingStartDay = openingStartDay;
689 * Getter for starting time
691 * @return Starting time
693 @SuppressWarnings ("ReturnOfDateField")
694 public Date getOpeningStartTime () {
695 return this.openingStartTime;
699 * Getter for starting time
701 * @param openingStartTime Starting time
703 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
704 public void setOpeningStartTime (final Date openingStartTime) {
705 this.openingStartTime = openingStartTime;
709 * Clears this bean data
711 private void clear () {
712 // Clear all branch office data
713 this.setBranchCity(null);
714 this.setBranchCompany(null);
715 this.setBranchContactEmployee(null);
716 this.setBranchCountry(null);
717 this.setBranchEmailAddress(null);
718 this.setBranchHouseNumber(null);
719 this.setBranchNumber(null);
720 this.setBranchStore(null);
721 this.setBranchStreet(null);
722 this.setBranchSuiteNumber(null);
723 this.setBranchUserOwner(null);
724 this.setBranchZipCode(null);
725 this.setBranchOpeningTimes(null);
727 // Extra-clear opening time
728 this.clearOpeningTime();
732 * Clears all opening time fields
734 private void clearOpeningTime () {
735 // Clear all opening time fields
736 this.setOpeningEndDay(null);
737 this.setOpeningEndTime(null);
738 this.setOpeningStartDay(null);
739 this.setOpeningStartTime(null);
743 * Prepares an instance of a BranchOffice object (entity) with all data from
744 * this bean. If a complete fax number or land-line number was provided, it
745 * will be set in the instance as well.
747 * @return An instance of a BranchOffice class (entity)
749 private BranchOffice createBranchOffice () {
750 // Create new branch office instance
751 final BranchOffice branchOffice = new BusinessBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
753 // Add all other fields, too
754 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
755 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
756 branchOffice.setBranchNumber(this.getBranchNumber());
757 branchOffice.setBranchStore(this.getBranchStore());
758 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
759 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
761 // Generate phone number
762 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
763 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
765 // Don't set null or wrong references
766 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
767 // Now the number must be given
768 if (landLine.getPhoneAreaCode() == null) {
770 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
771 } else if (landLine.getPhoneAreaCode() < 1) {
773 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
774 } else if (landLine.getPhoneNumber() == null) {
776 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
777 } else if (landLine.getPhoneNumber() < 1) {
779 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
783 branchOffice.setBranchLandLineNumber(landLine);
786 // Don't set null or wrong references
787 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
788 // Now the number must be given
789 if (fax.getPhoneAreaCode() == null) {
791 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
792 } else if (fax.getPhoneAreaCode() < 1) {
794 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
795 } else if (fax.getPhoneNumber() == null) {
797 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
798 } else if (fax.getPhoneNumber() < 1) {
800 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
804 branchOffice.setBranchFaxNumber(fax);
806 // Is the opening times list filled?
807 if (!this.getBranchOpeningTimes().isEmpty()) {
808 // Yes, then set in branch office, too
809 branchOffice.setBranchOpeningTimes(this.getBranchOpeningTimes());
812 // Return fully prepared instance
817 * Prepares an instance of a OpeningTimes object (entity) with all data from
818 * this bean. If a complete fax number or land-line number was provided, it
819 * will be set in the instance as well.
821 * @return An instance of a OpeningTimes class (entity)
823 private OpeningTime createOpeningTimes () {
824 // Create new openingTime instance
825 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
827 // Return fully prepared instance
832 * Checks whether the given branch office's address is already found in
833 * local cache. Please note that this method fully relies on the cache, so
834 * you must always fire proper events that add/update/delete entries in
837 * @param branchOffice Branch office to check it's address
839 * @return Whether the address has been found
841 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
842 // Get full list from other bean
843 final List<BranchOffice> branchOffices = this.branchOfficeController.allBranchOffices();
845 // Default is not found
846 boolean isFound = false;
848 // Now check each entry
849 for (final BranchOffice bo : branchOffices) {
851 if (BranchOffices.isSameAddress(bo, branchOffice)) {
863 * Checks if given opening time is already added
865 * @param openingTime Opening time to be checked
867 * @return Whether it has been added already
869 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
870 // Default is not found
871 boolean isFound = false;
874 for (final OpeningTime ot : this.getBranchOpeningTimes()) {
876 if (Objects.equals(ot, openingTime)) {