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.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 * @return Redirect outcome
222 public String addHeadquarter () {
224 final Headquarter headquarter = this.createHeadquarter();
226 // Is the headquarter not created yet?
227 if (this.isHeadquarterCreatedByRequiredData(headquarter)) {
228 // Then show proper faces message
229 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_HEADQUARTER_ALREADY_CREATED"); //NOI18N
233 // Delcare updated instance
234 final Headquarter updatedHeadquarter;
238 updatedHeadquarter = this.adminHeadquarterBean.addHeadquarter(headquarter);
239 } catch (final HeadquarterAlreadyAddedException ex) {
241 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_HEADQUARTER_ALREADY_CREATED"); //NOI18N
246 this.headquarterAddedEvent.fire(new HeadquarterAddedEvent(updatedHeadquarter));
252 return "admin_list_headquarter"; //NOI18N
256 * Adds opening time to temporary list which will be sent along with the
257 * headquarter data to the EJB.
259 public void addOpeningTime () {
260 // Validate all required fields
261 if (this.getOpeningEndDay() == null) {
263 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
264 } else if (this.getOpeningEndTime() == null) {
266 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
267 } else if (this.getOpeningStartDay() == null) {
269 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
270 } else if (this.getOpeningStartTime() == null) {
272 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
275 // Get opening time instance
276 final OpeningTime openingTime = this.createOpeningTimes();
279 if (this.isSameOpeningTimeFound(openingTime)) {
280 // Yes then abort here
281 this.showFacesMessage("form-admin-add-headquarter-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
285 // Add to temporary list
286 headquarterOpeningTimes.add(openingTime);
288 // Clear opening time fields
289 this.clearOpeningTime();
293 * Getter for fax number's area code
295 * @return Fax number's area code
297 public Integer getFaxAreaCode () {
298 return this.faxAreaCode;
302 * Setter for fax number's area code
304 * @param faxAreaCode Fax number's area code
306 public void setFaxAreaCode (final Integer faxAreaCode) {
307 this.faxAreaCode = faxAreaCode;
311 * Getter for fax's country instance
313 * @return Fax' country instance
315 public Country getFaxCountry () {
316 return this.faxCountry;
320 * Setter for fax's country instance
322 * @param faxCountry Fax' country instance
324 public void setFaxCountry (final Country faxCountry) {
325 this.faxCountry = faxCountry;
329 * Getter for fax number
333 public Long getFaxNumber () {
334 return this.faxNumber;
338 * Setter for fax number
340 * @param faxNumber Fax number
342 public void setFaxNumber (final Long faxNumber) {
343 this.faxNumber = faxNumber;
351 public String getHeadquarterCity () {
352 return this.headquarterCity;
358 * @param headquarterCity City
360 public void setHeadquarterCity (final String headquarterCity) {
361 this.headquarterCity = headquarterCity;
365 * Getter for headquarter's company name
367 * @return Headquarter's company name
369 public String getHeadquarterCompanyName () {
370 return this.headquarterCompanyName;
374 * Setter for headquarter's company name
376 * @param headquarterCompanyName Headquarter's company name
378 public void setHeadquarterCompanyName (final String headquarterCompanyName) {
379 this.headquarterCompanyName = headquarterCompanyName;
383 * Getter for headquarter contact person
385 * @return Headquarter contact person
387 public Employable getHeadquarterContactEmployee () {
388 return this.headquarterContactEmployee;
392 * Setter for headquarter contact person
394 * @param headquarterContactEmployee Headquarter contact person
396 public void setHeadquarterContactEmployee (final Employable headquarterContactEmployee) {
397 this.headquarterContactEmployee = headquarterContactEmployee;
405 public Country getHeadquarterCountry () {
406 return this.headquarterCountry;
412 * @param headquarterCountry Country
414 public void setHeadquarterCountry (final Country headquarterCountry) {
415 this.headquarterCountry = headquarterCountry;
419 * Getter for email address
421 * @return Email address
423 public String getHeadquarterEmailAddress () {
424 return this.headquarterEmailAddress;
428 * Getter for email address
430 * @param headquarterEmailAddress Email address
432 public void setHeadquarterEmailAddress (final String headquarterEmailAddress) {
433 this.headquarterEmailAddress = headquarterEmailAddress;
437 * Getter for house number
439 * @return House number
441 public Short getHeadquarterHouseNumber () {
442 return this.headquarterHouseNumber;
446 * Setter for house number
448 * @param headquarterHouseNumber House number
450 public void setHeadquarterHouseNumber (final Short headquarterHouseNumber) {
451 this.headquarterHouseNumber = headquarterHouseNumber;
455 * Getter for house number's extension
457 * @return House number's extension
459 public String getHeadquarterHouseNumberExtension () {
460 return this.headquarterHouseNumberExtension;
464 * Setter for house number's extension
466 * @param headquarterHouseNumberExtension House number's extension
468 public void setHeadquarterHouseNumberExtension (final String headquarterHouseNumberExtension) {
469 this.headquarterHouseNumberExtension = headquarterHouseNumberExtension;
473 * Getter for last house number
475 * @return Last house number
477 public Short getHeadquarterLastHouseNumber () {
478 return this.headquarterLastHouseNumber;
482 * Setter for last house number
484 * @param headquarterLastHouseNumber Last house number
486 public void setHeadquarterLastHouseNumber (final Short headquarterLastHouseNumber) {
487 this.headquarterLastHouseNumber = headquarterLastHouseNumber;
491 * Getter for opening times of this headquarter
493 * @return Opening times
495 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
496 public List<OpeningTime> getHeadquarterOpeningTimes () {
497 return headquarterOpeningTimes;
501 * Setter for opening times of this headquarter
503 * @param headquarterOpeningTimes Opening times
505 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
506 public void setHeadquarterOpeningTimes (final List<OpeningTime> headquarterOpeningTimes) {
507 FinancialsAdminHeadquarterWebRequestBean.headquarterOpeningTimes = headquarterOpeningTimes;
515 public Short getHeadquarterStore () {
516 return this.headquarterStore;
522 * @param headquarterStore Store
524 public void setHeadquarterStore (final Short headquarterStore) {
525 this.headquarterStore = headquarterStore;
529 * Getter for street name
531 * @return Street name
533 public String getHeadquarterStreet () {
534 return this.headquarterStreet;
538 * Setter for street name
540 * @param headquarterStreet Street name
542 public void setHeadquarterStreet (final String headquarterStreet) {
543 this.headquarterStreet = headquarterStreet;
547 * Getter for suite number
549 * @return Suite number
551 public Short getHeadquarterSuiteNumber () {
552 return this.headquarterSuiteNumber;
556 * Setter for suite number
558 * @param headquarterSuiteNumber Suite number
560 public void setHeadquarterSuiteNumber (final Short headquarterSuiteNumber) {
561 this.headquarterSuiteNumber = headquarterSuiteNumber;
565 * Getter for owning user instance
567 * @return Owning user instance
569 public User getHeadquarterUserOwner () {
570 return this.headquarterUserOwner;
574 * Setter for owning user instance
576 * @param headquarterUserOwner Owning user instance
578 public void setHeadquarterUserOwner (final User headquarterUserOwner) {
579 this.headquarterUserOwner = headquarterUserOwner;
583 * Getter for ZIP code\
587 public Integer getHeadquarterZipCode () {
588 return this.headquarterZipCode;
592 * Setter for ZIP code\
594 * @param headquarterZipCode ZIP code
596 public void setHeadquarterZipCode (final Integer headquarterZipCode) {
597 this.headquarterZipCode = headquarterZipCode;
601 * Getter for land-line number's area code
603 * @return Land-line number's area code
605 public Integer getLandLineAreaCode () {
606 return this.landLineAreaCode;
610 * Setter for land-line number's area code
612 * @param landLineAreaCode Land-line number's area code
614 public void setLandLineAreaCode (final Integer landLineAreaCode) {
615 this.landLineAreaCode = landLineAreaCode;
619 * Getter for land-line number's country instance
621 * @return Land-line number's country instance
623 public Country getLandLineCountry () {
624 return this.landLineCountry;
628 * Setter for land-line number's country instance
630 * @param landLineCountry Land-line number's country instance
632 public void setLandLineCountry (final Country landLineCountry) {
633 this.landLineCountry = landLineCountry;
637 * Getter for land-line number
639 * @return Land-line number
641 public Long getLandLineNumber () {
642 return this.landLineNumber;
646 * Setter for land-line number
648 * @param landLineNumber Land-line number
650 public void setLandLineNumber (final Long landLineNumber) {
651 this.landLineNumber = landLineNumber;
655 * Getter for ending week day
657 * @return Ending week day
659 public DayOfTheWeek getOpeningEndDay () {
660 return this.openingEndDay;
664 * Setter for ending week day
666 * @param openingEndDay Ending week day
668 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
669 this.openingEndDay = openingEndDay;
673 * Getter for ending time
675 * @return Ending time
677 @SuppressWarnings ("ReturnOfDateField")
678 public Date getOpeningEndTime () {
679 return this.openingEndTime;
683 * Getter for ending time
685 * @param openingEndTime Ending time
687 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
688 public void setOpeningEndTime (final Date openingEndTime) {
689 this.openingEndTime = openingEndTime;
693 * Getter for starting week day
695 * @return Starting week day
697 public DayOfTheWeek getOpeningStartDay () {
698 return this.openingStartDay;
702 * Getter for starting week day
704 * @param openingStartDay Starting week day
706 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
707 this.openingStartDay = openingStartDay;
711 * Getter for starting time
713 * @return Starting time
715 @SuppressWarnings ("ReturnOfDateField")
716 public Date getOpeningStartTime () {
717 return this.openingStartTime;
721 * Getter for starting time
723 * @param openingStartTime Starting time
725 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
726 public void setOpeningStartTime (final Date openingStartTime) {
727 this.openingStartTime = openingStartTime;
731 * Clears this bean data
733 private void clear () {
734 // Clear all headquarter data
735 this.setHeadquarterCity(null);
736 this.setHeadquarterCompanyName(null);
737 this.setHeadquarterContactEmployee(null);
738 this.setHeadquarterCountry(null);
739 this.setHeadquarterEmailAddress(null);
740 this.setHeadquarterHouseNumber(null);
741 this.setHeadquarterHouseNumberExtension(null);
742 this.setHeadquarterLastHouseNumber(null);
743 this.setHeadquarterStore(null);
744 this.setHeadquarterStreet(null);
745 this.setHeadquarterSuiteNumber(null);
746 this.setHeadquarterUserOwner(null);
747 this.setHeadquarterZipCode(null);
748 this.setHeadquarterOpeningTimes(null);
750 // Extra-clear opening time
751 this.clearOpeningTime();
755 * Clears all opening time fields
757 private void clearOpeningTime () {
758 // Clear all opening time fields
759 this.setOpeningEndDay(null);
760 this.setOpeningEndTime(null);
761 this.setOpeningStartDay(null);
762 this.setOpeningStartTime(null);
766 * Prepares an instance of a Headquarter object (entity) with all data from
767 * this bean. If a complete fax number or land-line number was provided, it
768 * will be set in the instance as well.
770 * @return An instance of a Headquarter class (entity)
772 private Headquarter createHeadquarter () {
773 // Create new headquarter instance
774 final Headquarter headquarter = new BusinessHeadquarter(this.getHeadquarterCompanyName(), this.getHeadquarterCity(), this.getHeadquarterCountry(), this.getHeadquarterStreet(), this.getHeadquarterZipCode(), this.getHeadquarterHouseNumber());
776 // Add all other fields, too
777 headquarter.setHeadquarterContactEmployee(this.getHeadquarterContactEmployee());
778 headquarter.setHeadquarterEmailAddress(this.getHeadquarterEmailAddress());
779 headquarter.setHeadquarterHouseNumberExtension(this.getHeadquarterHouseNumberExtension());
780 headquarter.setHeadquarterLastHouseNumber(this.getHeadquarterLastHouseNumber());
781 headquarter.setHeadquarterStore(this.getHeadquarterStore());
782 headquarter.setHeadquarterSuiteNumber(this.getHeadquarterSuiteNumber());
783 headquarter.setHeadquarterUserOwner(this.getHeadquarterUserOwner());
785 // Generate phone number
786 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
787 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
789 // Don't set null or wrong references
790 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
791 // Now the number must be given
792 if (landLine.getPhoneAreaCode() == null) {
794 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
795 } else if (landLine.getPhoneAreaCode() < 1) {
797 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
798 } else if (landLine.getPhoneNumber() == null) {
800 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
801 } else if (landLine.getPhoneNumber() < 1) {
803 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
807 headquarter.setHeadquarterLandLineNumber(landLine);
810 // Don't set null or wrong references
811 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
812 // Now the number must be given
813 if (fax.getPhoneAreaCode() == null) {
815 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
816 } else if (fax.getPhoneAreaCode() < 1) {
818 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
819 } else if (fax.getPhoneNumber() == null) {
821 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
822 } else if (fax.getPhoneNumber() < 1) {
824 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
828 headquarter.setHeadquarterFaxNumber(fax);
830 // Is the opening times list filled?
831 if (!this.getHeadquarterOpeningTimes().isEmpty()) {
832 // Yes, then set in headquarter, too
833 headquarter.setHeadquarterOpeningTimes(this.getHeadquarterOpeningTimes());
836 // Return fully prepared instance
841 * Prepares an instance of a OpeningTimes object (entity) with all data from
842 * this bean. If a complete fax number or land-line number was provided, it
843 * will be set in the instance as well.
845 * @return An instance of a OpeningTimes class (entity)
847 private OpeningTime createOpeningTimes () {
848 // Create new openingTime instance
849 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
851 // Return fully prepared instance
856 * Checks whether the given headquarter' address is already found in local
857 * cache. Please note that this method fully relies on the cache, so you
858 * must always fire proper events that add/update/delete entries in cache.
860 * @param headquarter Headquarter to check it's address
862 * @return Whether the address has been found
864 private boolean isHeadquarterCreatedByRequiredData (final Headquarter headquarter) {
865 // Get full list from other bean
866 final List<Headquarter> allHeadquarter = this.headquarterController.allHeadquarter();
868 // Default is not found
869 boolean isFound = false;
871 // Now check each entry
872 for (final Headquarter hq : allHeadquarter) {
874 if (Headquarters.isSameAddress(hq, headquarter)) {
886 * Checks if given opening time is already added
888 * @param openingTime Opening time to be checked
890 * @return Whether it has been added already
892 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
893 // Default is not found
894 boolean isFound = false;
897 for (final OpeningTime ot : this.getHeadquarterOpeningTimes()) {
899 if (Objects.equals(ot, openingTime)) {