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.jjobs.beans.business.headquarter.action;
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.headquarter.added.HeadquarterAddedEvent;
31 import org.mxchange.jcontactsbusiness.events.headquarter.added.ObservableHeadquarterAddedEvent;
32 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterAlreadyAddedException;
33 import org.mxchange.jcontactsbusiness.model.employee.Employable;
34 import org.mxchange.jcontactsbusiness.model.headquarter.AdminHeadquarterSessionBeanRemote;
35 import org.mxchange.jcontactsbusiness.model.headquarter.BusinessHeadquarter;
36 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
37 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
38 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
39 import org.mxchange.jcoreee.dates.DayOfTheWeek;
40 import org.mxchange.jcountry.model.data.Country;
41 import org.mxchange.jjobs.beans.BaseJobsBean;
42 import org.mxchange.jjobs.beans.business.headquarter.list.JobsHeadquarterListWebViewController;
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 action bean for headquarter
52 * @author Roland Häder<roland@mxchange.org>
54 @Named ("adminHeadquarterActionController")
56 public class JobsAdminHeadquarterActionWebRequestBean extends BaseJobsBean implements JobsAdminHeadquarterActionWebRequestController {
59 * Opening times of this headquarter
61 private static List<OpeningTime> headquarterOpeningTimes;
66 private static final long serialVersionUID = 5_028_697_360_472L;
69 * EJB for administrative purposes
71 @EJB (lookup = "java:global/jjobs-ejb/adminHeadquarter!org.mxchange.jcontactsbusiness.model.headquarter.AdminHeadquarterSessionBeanRemote")
72 private AdminHeadquarterSessionBeanRemote adminHeadquarterBean;
75 * Area code for fax number
77 private Integer faxAreaCode;
80 * Country for fax number
82 private Country faxCountry;
85 * Dial number for fax number
87 private Long faxNumber;
90 * An event being fired when a headquarter has been successfully added
94 private Event<ObservableHeadquarterAddedEvent> headquarterAddedEvent;
99 private String headquarterCity;
102 * Headquarter's company name
104 private String headquarterCompanyName;
107 * Contact person in headquarter
109 private Employable headquarterContactEmployee;
114 private Country headquarterCountry;
119 private String headquarterEmailAddress;
124 private Short headquarterHouseNumber;
127 * House number's extension
129 private String headquarterHouseNumberExtension;
134 private Short headquarterLastHouseNumber;
137 * A general headquarter controller (backing bean)
140 private JobsHeadquarterListWebViewController headquarterListController;
145 private Short headquarterStore;
148 * Headquarter street name
150 private String headquarterStreet;
155 private Short headquarterSuiteNumber;
158 * Owning user instance (which this headquarter is assigned to)
160 private User headquarterUserOwner;
165 private Integer headquarterZipCode;
168 * Area code for land-line number
170 private Integer landLineAreaCode;
173 * Country for land-line number
175 private Country landLineCountry;
178 * Dial number for land-line number
180 private Long landLineNumber;
185 private DayOfTheWeek openingEndDay;
190 private Date openingEndTime;
195 private DayOfTheWeek openingStartDay;
200 private Date openingStartTime;
203 * Default constructor
205 public JobsAdminHeadquarterActionWebRequestBean () {
206 // Call super constructor
209 // Is the opening times list there?
210 if (null == headquarterOpeningTimes) {
212 headquarterOpeningTimes = new ArrayList<>(1);
217 * Adds headquarter with all data from this backing bean. First this action
218 * method will validate if the headquarter's address is already registered
219 * and if found, it will output a proper faces message.
221 public void addHeadquarter () {
223 final Headquarter headquarter = this.createHeadquarter();
225 // Is the headquarter not created yet?
226 if (this.headquarterListController.isHeadquarterCreatedByRequiredData(headquarter)) {
227 // Then show proper faces message
228 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_HEADQUARTER_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
232 // Delcare updated instance
233 final Headquarter updatedHeadquarter;
237 updatedHeadquarter = this.adminHeadquarterBean.addHeadquarter(headquarter);
238 } catch (final HeadquarterAlreadyAddedException ex) {
240 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_HEADQUARTER_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
245 this.headquarterAddedEvent.fire(new HeadquarterAddedEvent(updatedHeadquarter));
252 * Adds opening time to temporary list which will be sent along with the
253 * headquarter 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-headquarter-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
281 // Add to temporary list
282 headquarterOpeningTimes.add(openingTime);
284 // Clear opening time fields
285 this.clearOpeningTime();
289 * Getter for fax number's area code
291 * @return Fax number's area code
293 public Integer getFaxAreaCode () {
294 return this.faxAreaCode;
298 * Setter for fax number's area code
300 * @param faxAreaCode Fax number's area code
302 public void setFaxAreaCode (final Integer faxAreaCode) {
303 this.faxAreaCode = faxAreaCode;
307 * Getter for fax's country instance
309 * @return Fax' country instance
311 public Country getFaxCountry () {
312 return this.faxCountry;
316 * Setter for fax's country instance
318 * @param faxCountry Fax' country instance
320 public void setFaxCountry (final Country faxCountry) {
321 this.faxCountry = faxCountry;
325 * Getter for fax number
329 public Long getFaxNumber () {
330 return this.faxNumber;
334 * Setter for fax number
336 * @param faxNumber Fax number
338 public void setFaxNumber (final Long faxNumber) {
339 this.faxNumber = faxNumber;
347 public String getHeadquarterCity () {
348 return this.headquarterCity;
354 * @param headquarterCity City
356 public void setHeadquarterCity (final String headquarterCity) {
357 this.headquarterCity = headquarterCity;
361 * Getter for headquarter's company name
363 * @return Headquarter's company name
365 public String getHeadquarterCompanyName () {
366 return this.headquarterCompanyName;
370 * Setter for headquarter's company name
372 * @param headquarterCompanyName Headquarter's company name
374 public void setHeadquarterCompanyName (final String headquarterCompanyName) {
375 this.headquarterCompanyName = headquarterCompanyName;
379 * Getter for headquarter contact person
381 * @return Headquarter contact person
383 public Employable getHeadquarterContactEmployee () {
384 return this.headquarterContactEmployee;
388 * Setter for headquarter contact person
390 * @param headquarterContactEmployee Headquarter contact person
392 public void setHeadquarterContactEmployee (final Employable headquarterContactEmployee) {
393 this.headquarterContactEmployee = headquarterContactEmployee;
401 public Country getHeadquarterCountry () {
402 return this.headquarterCountry;
408 * @param headquarterCountry Country
410 public void setHeadquarterCountry (final Country headquarterCountry) {
411 this.headquarterCountry = headquarterCountry;
415 * Getter for email address
417 * @return Email address
419 public String getHeadquarterEmailAddress () {
420 return this.headquarterEmailAddress;
424 * Getter for email address
426 * @param headquarterEmailAddress Email address
428 public void setHeadquarterEmailAddress (final String headquarterEmailAddress) {
429 this.headquarterEmailAddress = headquarterEmailAddress;
433 * Getter for house number
435 * @return House number
437 public Short getHeadquarterHouseNumber () {
438 return this.headquarterHouseNumber;
442 * Setter for house number
444 * @param headquarterHouseNumber House number
446 public void setHeadquarterHouseNumber (final Short headquarterHouseNumber) {
447 this.headquarterHouseNumber = headquarterHouseNumber;
451 * Getter for house number's extension
453 * @return House number's extension
455 public String getHeadquarterHouseNumberExtension () {
456 return this.headquarterHouseNumberExtension;
460 * Setter for house number's extension
462 * @param headquarterHouseNumberExtension House number's extension
464 public void setHeadquarterHouseNumberExtension (final String headquarterHouseNumberExtension) {
465 this.headquarterHouseNumberExtension = headquarterHouseNumberExtension;
469 * Getter for last house number
471 * @return Last house number
473 public Short getHeadquarterLastHouseNumber () {
474 return this.headquarterLastHouseNumber;
478 * Setter for last house number
480 * @param headquarterLastHouseNumber Last house number
482 public void setHeadquarterLastHouseNumber (final Short headquarterLastHouseNumber) {
483 this.headquarterLastHouseNumber = headquarterLastHouseNumber;
487 * Getter for opening times of this headquarter
489 * @return Opening times
491 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
492 public List<OpeningTime> getHeadquarterOpeningTimes () {
493 return headquarterOpeningTimes;
497 * Setter for opening times of this headquarter
499 * @param headquarterOpeningTimes Opening times
501 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
502 public void setHeadquarterOpeningTimes (final List<OpeningTime> headquarterOpeningTimes) {
503 JobsAdminHeadquarterActionWebRequestBean.headquarterOpeningTimes = headquarterOpeningTimes;
511 public Short getHeadquarterStore () {
512 return this.headquarterStore;
518 * @param headquarterStore Store
520 public void setHeadquarterStore (final Short headquarterStore) {
521 this.headquarterStore = headquarterStore;
525 * Getter for street name
527 * @return Street name
529 public String getHeadquarterStreet () {
530 return this.headquarterStreet;
534 * Setter for street name
536 * @param headquarterStreet Street name
538 public void setHeadquarterStreet (final String headquarterStreet) {
539 this.headquarterStreet = headquarterStreet;
543 * Getter for suite number
545 * @return Suite number
547 public Short getHeadquarterSuiteNumber () {
548 return this.headquarterSuiteNumber;
552 * Setter for suite number
554 * @param headquarterSuiteNumber Suite number
556 public void setHeadquarterSuiteNumber (final Short headquarterSuiteNumber) {
557 this.headquarterSuiteNumber = headquarterSuiteNumber;
561 * Getter for owning user instance
563 * @return Owning user instance
565 public User getHeadquarterUserOwner () {
566 return this.headquarterUserOwner;
570 * Setter for owning user instance
572 * @param headquarterUserOwner Owning user instance
574 public void setHeadquarterUserOwner (final User headquarterUserOwner) {
575 this.headquarterUserOwner = headquarterUserOwner;
579 * Getter for ZIP code\
583 public Integer getHeadquarterZipCode () {
584 return this.headquarterZipCode;
588 * Setter for ZIP code\
590 * @param headquarterZipCode ZIP code
592 public void setHeadquarterZipCode (final Integer headquarterZipCode) {
593 this.headquarterZipCode = headquarterZipCode;
597 * Getter for land-line number's area code
599 * @return Land-line number's area code
601 public Integer getLandLineAreaCode () {
602 return this.landLineAreaCode;
606 * Setter for land-line number's area code
608 * @param landLineAreaCode Land-line number's area code
610 public void setLandLineAreaCode (final Integer landLineAreaCode) {
611 this.landLineAreaCode = landLineAreaCode;
615 * Getter for land-line number's country instance
617 * @return Land-line number's country instance
619 public Country getLandLineCountry () {
620 return this.landLineCountry;
624 * Setter for land-line number's country instance
626 * @param landLineCountry Land-line number's country instance
628 public void setLandLineCountry (final Country landLineCountry) {
629 this.landLineCountry = landLineCountry;
633 * Getter for land-line number
635 * @return Land-line number
637 public Long getLandLineNumber () {
638 return this.landLineNumber;
642 * Setter for land-line number
644 * @param landLineNumber Land-line number
646 public void setLandLineNumber (final Long landLineNumber) {
647 this.landLineNumber = landLineNumber;
651 * Getter for ending week day
653 * @return Ending week day
655 public DayOfTheWeek getOpeningEndDay () {
656 return this.openingEndDay;
660 * Setter for ending week day
662 * @param openingEndDay Ending week day
664 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
665 this.openingEndDay = openingEndDay;
669 * Getter for ending time
671 * @return Ending time
673 @SuppressWarnings ("ReturnOfDateField")
674 public Date getOpeningEndTime () {
675 return this.openingEndTime;
679 * Getter for ending time
681 * @param openingEndTime Ending time
683 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
684 public void setOpeningEndTime (final Date openingEndTime) {
685 this.openingEndTime = openingEndTime;
689 * Getter for starting week day
691 * @return Starting week day
693 public DayOfTheWeek getOpeningStartDay () {
694 return this.openingStartDay;
698 * Getter for starting week day
700 * @param openingStartDay Starting week day
702 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
703 this.openingStartDay = openingStartDay;
707 * Getter for starting time
709 * @return Starting time
711 @SuppressWarnings ("ReturnOfDateField")
712 public Date getOpeningStartTime () {
713 return this.openingStartTime;
717 * Getter for starting time
719 * @param openingStartTime Starting time
721 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
722 public void setOpeningStartTime (final Date openingStartTime) {
723 this.openingStartTime = openingStartTime;
727 * Clears this bean data
729 private void clear () {
730 // Clear all headquarter data
731 this.setHeadquarterCity(null);
732 this.setHeadquarterCompanyName(null);
733 this.setHeadquarterContactEmployee(null);
734 this.setHeadquarterCountry(null);
735 this.setHeadquarterEmailAddress(null);
736 this.setHeadquarterHouseNumber(null);
737 this.setHeadquarterHouseNumberExtension(null);
738 this.setHeadquarterLastHouseNumber(null);
739 this.setHeadquarterStore(null);
740 this.setHeadquarterStreet(null);
741 this.setHeadquarterSuiteNumber(null);
742 this.setHeadquarterUserOwner(null);
743 this.setHeadquarterZipCode(null);
744 this.setHeadquarterOpeningTimes(null);
746 // Extra-clear opening time
747 this.clearOpeningTime();
751 * Clears all opening time fields
753 private void clearOpeningTime () {
754 // Clear all opening time fields
755 this.setOpeningEndDay(null);
756 this.setOpeningEndTime(null);
757 this.setOpeningStartDay(null);
758 this.setOpeningStartTime(null);
762 * Prepares an instance of a Headquarter object (entity) with all data from
763 * this bean. If a complete fax number or land-line number was provided, it
764 * will be set in the instance as well.
766 * @return An instance of a Headquarter class (entity)
768 private Headquarter createHeadquarter () {
769 // Create new headquarter instance
770 final Headquarter headquarter = new BusinessHeadquarter(
771 this.getHeadquarterCompanyName(),
772 this.getHeadquarterCity(),
773 this.getHeadquarterCountry(),
774 this.getHeadquarterStreet(),
775 this.getHeadquarterZipCode(),
776 this.getHeadquarterHouseNumber()
779 // Add all other fields, too
780 headquarter.setHeadquarterContactEmployee(this.getHeadquarterContactEmployee());
781 headquarter.setHeadquarterEmailAddress(this.getHeadquarterEmailAddress());
782 headquarter.setHeadquarterHouseNumberExtension(this.getHeadquarterHouseNumberExtension());
783 headquarter.setHeadquarterLastHouseNumber(this.getHeadquarterLastHouseNumber());
784 headquarter.setHeadquarterStore(this.getHeadquarterStore());
785 headquarter.setHeadquarterSuiteNumber(this.getHeadquarterSuiteNumber());
786 headquarter.setHeadquarterUserOwner(this.getHeadquarterUserOwner());
788 // Initialize variables
789 DialableLandLineNumber landLine = null;
790 DialableFaxNumber fax = null;
792 // Are all fields set?
793 if (this.getLandLineAreaCode() != null && this.getLandLineCountry() instanceof Country && this.getLandLineNumber() != null) {
794 // Initialize instance
795 landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
798 // Are all fields set?
799 if (this.getFaxAreaCode() != null && this.getFaxCountry() instanceof Country && this.getFaxNumber() != null) {
800 // Initialize instance
801 fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
804 // Don't set null or wrong references
805 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
806 // Now the number must be given
807 if (landLine.getPhoneAreaCode() == null) {
809 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
810 } else if (landLine.getPhoneAreaCode() < 1) {
812 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
813 } else if (landLine.getPhoneNumber() == null) {
815 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
816 } else if (landLine.getPhoneNumber() < 1) {
818 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
822 headquarter.setHeadquarterLandLineNumber(landLine);
825 // Don't set null or wrong references
826 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
827 // Now the number must be given
828 if (fax.getPhoneAreaCode() == null) {
830 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
831 } else if (fax.getPhoneAreaCode() < 1) {
833 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
834 } else if (fax.getPhoneNumber() == null) {
836 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
837 } else if (fax.getPhoneNumber() < 1) {
839 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
843 headquarter.setHeadquarterFaxNumber(fax);
845 // Is the opening times list filled?
846 if (!this.getHeadquarterOpeningTimes().isEmpty()) {
847 // Yes, then set in headquarter, too
848 headquarter.setHeadquarterOpeningTimes(this.getHeadquarterOpeningTimes());
851 // Return fully prepared instance
856 * Prepares an instance of a OpeningTimes object (entity) with all data from
857 * this bean. If a complete fax number or land-line number was provided, it
858 * will be set in the instance as well.
860 * @return An instance of a OpeningTimes class (entity)
862 private OpeningTime createOpeningTimes () {
863 // Create new openingTime instance
864 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
866 // Return fully prepared instance
871 * Checks if given opening time is already added
873 * @param openingTime Opening time to be checked
875 * @return Whether it has been added already
877 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
878 // Default is not found
879 boolean isFound = false;
882 for (final OpeningTime ot : this.getHeadquarterOpeningTimes()) {
884 if (Objects.equals(ot, openingTime)) {