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.headquarter;
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.headquarter.added.HeadquarterAddedEvent;
30 import org.mxchange.jcontactsbusiness.events.headquarter.added.ObservableHeadquarterAddedEvent;
31 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterAlreadyAddedException;
32 import org.mxchange.jcontactsbusiness.model.employee.Employable;
33 import org.mxchange.jcontactsbusiness.model.headquarter.AdminHeadquarterSessionBeanRemote;
34 import org.mxchange.jcontactsbusiness.model.headquarter.BusinessHeadquarter;
35 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
36 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarters;
37 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
38 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
39 import org.mxchange.jcontactsbusiness.model.opening_time.dayofweek.DayOfTheWeek;
40 import org.mxchange.jcountry.model.data.Country;
41 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
42 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
43 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
44 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
45 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
46 import org.mxchange.jusercore.model.user.User;
49 * An administrative bean for headquarter
51 * @author Roland Häder<roland@mxchange.org>
53 @Named ("adminHeadquarterController")
55 public class FinancialsAdminHeadquarterWebRequestBean extends BaseFinancialsBean implements FinancialsAdminHeadquarterWebRequestController {
58 * Opening times of this headquarter
60 private static List<OpeningTime> headquarterOpeningTimes;
65 private static final long serialVersionUID = 5_028_697_360_462L;
68 * EJB for administrative purposes
70 @EJB (lookup = "java:global/jfinancials-ejb/adminHeadquarter!org.mxchange.jcontactsbusiness.model.headquarter.AdminHeadquarterSessionBeanRemote")
71 private AdminHeadquarterSessionBeanRemote adminHeadquarterBean;
74 * Area code for fax number
76 private Integer faxAreaCode;
79 * Country for fax number
81 private Country faxCountry;
84 * Dial number for fax number
86 private Long faxNumber;
89 * An event being fired when a headquarter has been successfully added
93 private Event<ObservableHeadquarterAddedEvent> headquarterAddedEvent;
98 private String headquarterCity;
101 * Headquarter's company name
103 private String headquarterCompanyName;
106 * Contact person in headquarter
108 private Employable headquarterContactEmployee;
111 * A general headquarter controller (backing bean)
114 private FinancialsHeadquarterWebRequestController headquarterController;
119 private Country headquarterCountry;
124 private String headquarterEmailAddress;
129 private Short headquarterHouseNumber;
132 * House number's extension
134 private String headquarterHouseNumberExtension;
139 private Short headquarterLastHouseNumber;
144 private Short headquarterStore;
147 * Headquarter street name
149 private String headquarterStreet;
154 private Short headquarterSuiteNumber;
157 * Owning user instance (which this headquarter is assigned to)
159 private User headquarterUserOwner;
164 private Integer headquarterZipCode;
167 * Area code for land-line number
169 private Integer landLineAreaCode;
172 * Country for land-line number
174 private Country landLineCountry;
177 * Dial number for land-line number
179 private Long landLineNumber;
184 private DayOfTheWeek openingEndDay;
189 private Date openingEndTime;
194 private DayOfTheWeek openingStartDay;
199 private Date openingStartTime;
202 * Default constructor
204 public FinancialsAdminHeadquarterWebRequestBean () {
205 // Call super constructor
208 // Is the opening times list there?
209 if (null == headquarterOpeningTimes) {
211 headquarterOpeningTimes = new ArrayList<>(1);
216 * Adds headquarter with all data from this backing bean. First this action
217 * method will validate if the headquarter's address is already registered
218 * and if found, it will output a proper faces message.
220 public void addHeadquarter () {
222 final Headquarter headquarter = this.createHeadquarter();
224 // Is the headquarter not created yet?
225 if (this.isHeadquarterCreatedByRequiredData(headquarter)) {
226 // Then show proper faces message
227 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_HEADQUARTER_ALREADY_CREATED"); //NOI18N
231 // Delcare updated instance
232 final Headquarter updatedHeadquarter;
236 updatedHeadquarter = this.adminHeadquarterBean.addHeadquarter(headquarter);
237 } catch (final HeadquarterAlreadyAddedException ex) {
239 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_HEADQUARTER_ALREADY_CREATED"); //NOI18N
244 this.headquarterAddedEvent.fire(new HeadquarterAddedEvent(updatedHeadquarter));
251 * Adds opening time to temporary list which will be sent along with the
252 * headquarter data to the EJB.
254 public void addOpeningTime () {
255 // Validate all required fields
256 if (this.getOpeningEndDay() == null) {
258 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
259 } else if (this.getOpeningEndTime() == null) {
261 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
262 } else if (this.getOpeningStartDay() == null) {
264 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
265 } else if (this.getOpeningStartTime() == null) {
267 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
270 // Get opening time instance
271 final OpeningTime openingTime = this.createOpeningTimes();
274 if (this.isSameOpeningTimeFound(openingTime)) {
275 // Yes then abort here
276 this.showFacesMessage("form-admin-add-headquarter-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
280 // Add to temporary list
281 headquarterOpeningTimes.add(openingTime);
283 // Clear opening time fields
284 this.clearOpeningTime();
288 * Getter for fax number's area code
290 * @return Fax number's area code
292 public Integer getFaxAreaCode () {
293 return this.faxAreaCode;
297 * Setter for fax number's area code
299 * @param faxAreaCode Fax number's area code
301 public void setFaxAreaCode (final Integer faxAreaCode) {
302 this.faxAreaCode = faxAreaCode;
306 * Getter for fax's country instance
308 * @return Fax' country instance
310 public Country getFaxCountry () {
311 return this.faxCountry;
315 * Setter for fax's country instance
317 * @param faxCountry Fax' country instance
319 public void setFaxCountry (final Country faxCountry) {
320 this.faxCountry = faxCountry;
324 * Getter for fax number
328 public Long getFaxNumber () {
329 return this.faxNumber;
333 * Setter for fax number
335 * @param faxNumber Fax number
337 public void setFaxNumber (final Long faxNumber) {
338 this.faxNumber = faxNumber;
346 public String getHeadquarterCity () {
347 return this.headquarterCity;
353 * @param headquarterCity City
355 public void setHeadquarterCity (final String headquarterCity) {
356 this.headquarterCity = headquarterCity;
360 * Getter for headquarter's company name
362 * @return Headquarter's company name
364 public String getHeadquarterCompanyName () {
365 return this.headquarterCompanyName;
369 * Setter for headquarter's company name
371 * @param headquarterCompanyName Headquarter's company name
373 public void setHeadquarterCompanyName (final String headquarterCompanyName) {
374 this.headquarterCompanyName = headquarterCompanyName;
378 * Getter for headquarter contact person
380 * @return Headquarter contact person
382 public Employable getHeadquarterContactEmployee () {
383 return this.headquarterContactEmployee;
387 * Setter for headquarter contact person
389 * @param headquarterContactEmployee Headquarter contact person
391 public void setHeadquarterContactEmployee (final Employable headquarterContactEmployee) {
392 this.headquarterContactEmployee = headquarterContactEmployee;
400 public Country getHeadquarterCountry () {
401 return this.headquarterCountry;
407 * @param headquarterCountry Country
409 public void setHeadquarterCountry (final Country headquarterCountry) {
410 this.headquarterCountry = headquarterCountry;
414 * Getter for email address
416 * @return Email address
418 public String getHeadquarterEmailAddress () {
419 return this.headquarterEmailAddress;
423 * Getter for email address
425 * @param headquarterEmailAddress Email address
427 public void setHeadquarterEmailAddress (final String headquarterEmailAddress) {
428 this.headquarterEmailAddress = headquarterEmailAddress;
432 * Getter for house number
434 * @return House number
436 public Short getHeadquarterHouseNumber () {
437 return this.headquarterHouseNumber;
441 * Setter for house number
443 * @param headquarterHouseNumber House number
445 public void setHeadquarterHouseNumber (final Short headquarterHouseNumber) {
446 this.headquarterHouseNumber = headquarterHouseNumber;
450 * Getter for house number's extension
452 * @return House number's extension
454 public String getHeadquarterHouseNumberExtension () {
455 return this.headquarterHouseNumberExtension;
459 * Setter for house number's extension
461 * @param headquarterHouseNumberExtension House number's extension
463 public void setHeadquarterHouseNumberExtension (final String headquarterHouseNumberExtension) {
464 this.headquarterHouseNumberExtension = headquarterHouseNumberExtension;
468 * Getter for last house number
470 * @return Last house number
472 public Short getHeadquarterLastHouseNumber () {
473 return this.headquarterLastHouseNumber;
477 * Setter for last house number
479 * @param headquarterLastHouseNumber Last house number
481 public void setHeadquarterLastHouseNumber (final Short headquarterLastHouseNumber) {
482 this.headquarterLastHouseNumber = headquarterLastHouseNumber;
486 * Getter for opening times of this headquarter
488 * @return Opening times
490 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
491 public List<OpeningTime> getHeadquarterOpeningTimes () {
492 return headquarterOpeningTimes;
496 * Setter for opening times of this headquarter
498 * @param headquarterOpeningTimes Opening times
500 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
501 public void setHeadquarterOpeningTimes (final List<OpeningTime> headquarterOpeningTimes) {
502 FinancialsAdminHeadquarterWebRequestBean.headquarterOpeningTimes = headquarterOpeningTimes;
510 public Short getHeadquarterStore () {
511 return this.headquarterStore;
517 * @param headquarterStore Store
519 public void setHeadquarterStore (final Short headquarterStore) {
520 this.headquarterStore = headquarterStore;
524 * Getter for street name
526 * @return Street name
528 public String getHeadquarterStreet () {
529 return this.headquarterStreet;
533 * Setter for street name
535 * @param headquarterStreet Street name
537 public void setHeadquarterStreet (final String headquarterStreet) {
538 this.headquarterStreet = headquarterStreet;
542 * Getter for suite number
544 * @return Suite number
546 public Short getHeadquarterSuiteNumber () {
547 return this.headquarterSuiteNumber;
551 * Setter for suite number
553 * @param headquarterSuiteNumber Suite number
555 public void setHeadquarterSuiteNumber (final Short headquarterSuiteNumber) {
556 this.headquarterSuiteNumber = headquarterSuiteNumber;
560 * Getter for owning user instance
562 * @return Owning user instance
564 public User getHeadquarterUserOwner () {
565 return this.headquarterUserOwner;
569 * Setter for owning user instance
571 * @param headquarterUserOwner Owning user instance
573 public void setHeadquarterUserOwner (final User headquarterUserOwner) {
574 this.headquarterUserOwner = headquarterUserOwner;
578 * Getter for ZIP code\
582 public Integer getHeadquarterZipCode () {
583 return this.headquarterZipCode;
587 * Setter for ZIP code\
589 * @param headquarterZipCode ZIP code
591 public void setHeadquarterZipCode (final Integer headquarterZipCode) {
592 this.headquarterZipCode = headquarterZipCode;
596 * Getter for land-line number's area code
598 * @return Land-line number's area code
600 public Integer getLandLineAreaCode () {
601 return this.landLineAreaCode;
605 * Setter for land-line number's area code
607 * @param landLineAreaCode Land-line number's area code
609 public void setLandLineAreaCode (final Integer landLineAreaCode) {
610 this.landLineAreaCode = landLineAreaCode;
614 * Getter for land-line number's country instance
616 * @return Land-line number's country instance
618 public Country getLandLineCountry () {
619 return this.landLineCountry;
623 * Setter for land-line number's country instance
625 * @param landLineCountry Land-line number's country instance
627 public void setLandLineCountry (final Country landLineCountry) {
628 this.landLineCountry = landLineCountry;
632 * Getter for land-line number
634 * @return Land-line number
636 public Long getLandLineNumber () {
637 return this.landLineNumber;
641 * Setter for land-line number
643 * @param landLineNumber Land-line number
645 public void setLandLineNumber (final Long landLineNumber) {
646 this.landLineNumber = landLineNumber;
650 * Getter for ending week day
652 * @return Ending week day
654 public DayOfTheWeek getOpeningEndDay () {
655 return this.openingEndDay;
659 * Setter for ending week day
661 * @param openingEndDay Ending week day
663 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
664 this.openingEndDay = openingEndDay;
668 * Getter for ending time
670 * @return Ending time
672 @SuppressWarnings ("ReturnOfDateField")
673 public Date getOpeningEndTime () {
674 return this.openingEndTime;
678 * Getter for ending time
680 * @param openingEndTime Ending time
682 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
683 public void setOpeningEndTime (final Date openingEndTime) {
684 this.openingEndTime = openingEndTime;
688 * Getter for starting week day
690 * @return Starting week day
692 public DayOfTheWeek getOpeningStartDay () {
693 return this.openingStartDay;
697 * Getter for starting week day
699 * @param openingStartDay Starting week day
701 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
702 this.openingStartDay = openingStartDay;
706 * Getter for starting time
708 * @return Starting time
710 @SuppressWarnings ("ReturnOfDateField")
711 public Date getOpeningStartTime () {
712 return this.openingStartTime;
716 * Getter for starting time
718 * @param openingStartTime Starting time
720 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
721 public void setOpeningStartTime (final Date openingStartTime) {
722 this.openingStartTime = openingStartTime;
726 * Clears this bean data
728 private void clear () {
729 // Clear all headquarter data
730 this.setHeadquarterCity(null);
731 this.setHeadquarterCompanyName(null);
732 this.setHeadquarterContactEmployee(null);
733 this.setHeadquarterCountry(null);
734 this.setHeadquarterEmailAddress(null);
735 this.setHeadquarterHouseNumber(null);
736 this.setHeadquarterHouseNumberExtension(null);
737 this.setHeadquarterLastHouseNumber(null);
738 this.setHeadquarterStore(null);
739 this.setHeadquarterStreet(null);
740 this.setHeadquarterSuiteNumber(null);
741 this.setHeadquarterUserOwner(null);
742 this.setHeadquarterZipCode(null);
743 this.setHeadquarterOpeningTimes(null);
745 // Extra-clear opening time
746 this.clearOpeningTime();
750 * Clears all opening time fields
752 private void clearOpeningTime () {
753 // Clear all opening time fields
754 this.setOpeningEndDay(null);
755 this.setOpeningEndTime(null);
756 this.setOpeningStartDay(null);
757 this.setOpeningStartTime(null);
761 * Prepares an instance of a Headquarter object (entity) with all data from
762 * this bean. If a complete fax number or land-line number was provided, it
763 * will be set in the instance as well.
765 * @return An instance of a Headquarter class (entity)
767 private Headquarter createHeadquarter () {
768 // Create new headquarter instance
769 final Headquarter headquarter = new BusinessHeadquarter(this.getHeadquarterCompanyName(), this.getHeadquarterCity(), this.getHeadquarterCountry(), this.getHeadquarterStreet(), this.getHeadquarterZipCode(), this.getHeadquarterHouseNumber());
771 // Add all other fields, too
772 headquarter.setHeadquarterContactEmployee(this.getHeadquarterContactEmployee());
773 headquarter.setHeadquarterEmailAddress(this.getHeadquarterEmailAddress());
774 headquarter.setHeadquarterHouseNumberExtension(this.getHeadquarterHouseNumberExtension());
775 headquarter.setHeadquarterLastHouseNumber(this.getHeadquarterLastHouseNumber());
776 headquarter.setHeadquarterStore(this.getHeadquarterStore());
777 headquarter.setHeadquarterSuiteNumber(this.getHeadquarterSuiteNumber());
778 headquarter.setHeadquarterUserOwner(this.getHeadquarterUserOwner());
780 // Generate phone number
781 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
782 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
784 // Don't set null or wrong references
785 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
786 // Now the number must be given
787 if (landLine.getPhoneAreaCode() == null) {
789 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
790 } else if (landLine.getPhoneAreaCode() < 1) {
792 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
793 } else if (landLine.getPhoneNumber() == null) {
795 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
796 } else if (landLine.getPhoneNumber() < 1) {
798 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
802 headquarter.setHeadquarterLandLineNumber(landLine);
805 // Don't set null or wrong references
806 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
807 // Now the number must be given
808 if (fax.getPhoneAreaCode() == null) {
810 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
811 } else if (fax.getPhoneAreaCode() < 1) {
813 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
814 } else if (fax.getPhoneNumber() == null) {
816 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
817 } else if (fax.getPhoneNumber() < 1) {
819 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
823 headquarter.setHeadquarterFaxNumber(fax);
825 // Is the opening times list filled?
826 if (!this.getHeadquarterOpeningTimes().isEmpty()) {
827 // Yes, then set in headquarter, too
828 headquarter.setHeadquarterOpeningTimes(this.getHeadquarterOpeningTimes());
831 // Return fully prepared instance
836 * Prepares an instance of a OpeningTimes object (entity) with all data from
837 * this bean. If a complete fax number or land-line number was provided, it
838 * will be set in the instance as well.
840 * @return An instance of a OpeningTimes class (entity)
842 private OpeningTime createOpeningTimes () {
843 // Create new openingTime instance
844 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
846 // Return fully prepared instance
851 * Checks whether the given headquarter' address is already found in local
852 * cache. Please note that this method fully relies on the cache, so you
853 * must always fire proper events that add/update/delete entries in cache.
855 * @param headquarter Headquarter to check it's address
857 * @return Whether the address has been found
859 private boolean isHeadquarterCreatedByRequiredData (final Headquarter headquarter) {
860 // Get full list from other bean
861 final List<Headquarter> allHeadquarter = this.headquarterController.allHeadquarter();
863 // Default is not found
864 boolean isFound = false;
866 // Now check each entry
867 for (final Headquarter hq : allHeadquarter) {
869 if (Headquarters.isSameAddress(hq, headquarter)) {
881 * Checks if given opening time is already added
883 * @param openingTime Opening time to be checked
885 * @return Whether it has been added already
887 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
888 // Default is not found
889 boolean isFound = false;
892 for (final OpeningTime ot : this.getHeadquarterOpeningTimes()) {
894 if (Objects.equals(ot, openingTime)) {