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.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.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.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 * Contact person in headquarter
103 private Employable headquarterContactEmployee;
106 * A general headquarter controller (backing bean)
109 private FinancialsHeadquarterWebRequestController headquarterController;
114 private Country headquarterCountry;
119 private String headquarterEmailAddress;
124 private Short headquarterHouseNumber;
129 private Short headquarterStore;
132 * Headquarter street name
134 private String headquarterStreet;
139 private Short headquarterSuiteNumber;
142 * Owning user instance (which this headquarter is assigned to)
144 private User headquarterUserOwner;
149 private Integer headquarterZipCode;
152 * Area code for land-line number
154 private Integer landLineAreaCode;
157 * Country for land-line number
159 private Country landLineCountry;
162 * Dial number for land-line number
164 private Long landLineNumber;
169 private DayOfTheWeek openingEndDay;
174 private Date openingEndTime;
179 private DayOfTheWeek openingStartDay;
184 private Date openingStartTime;
187 * Default constructor
189 public FinancialsAdminHeadquarterWebRequestBean () {
190 // Call super constructor
193 // Is the opening times list there?
194 if (null == headquarterOpeningTimes) {
196 headquarterOpeningTimes = new ArrayList<>(1);
201 * Adds headquarter with all data from this backing bean. First this action
202 * method will validate if the headquarter's address is already registered
203 * and if found, it will output a proper faces message.
205 * @return Redirect outcome
207 public String addHeadquarter () {
209 final Headquarter headquarter = this.createHeadquarter();
211 // Is the headquarter not created yet?
212 if (this.isHeadquarterCreatedByRequiredData(headquarter)) {
213 // Then show proper faces message
214 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
218 // Delcare updated instance
219 final Headquarter updatedHeadquarter;
223 updatedHeadquarter = this.adminHeadquarterBean.addHeadquarter(headquarter);
224 } catch (final HeadquarterAlreadyAddedException ex) {
226 this.showFacesMessage("form-admin-add-headquarter-office:headquarterStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
231 this.headquarterAddedEvent.fire(new HeadquarterAddedEvent(updatedHeadquarter));
237 return "admin_list_headquarter_office"; //NOI18N
241 * Adds opening time to temporary list which will be sent along with the
242 * headquarter data to the EJB.
244 public void addOpeningTime () {
245 // Validate all required fields
246 if (this.getOpeningEndDay() == null) {
248 throw new NullPointerException("this.openingEndDay is null"); //NOI18N
249 } else if (this.getOpeningEndTime() == null) {
251 throw new NullPointerException("this.openingEndTime is null"); //NOI18N
252 } else if (this.getOpeningStartDay() == null) {
254 throw new NullPointerException("this.openingStartDay is null"); //NOI18N
255 } else if (this.getOpeningStartTime() == null) {
257 throw new NullPointerException("this.openingStartTime is null"); //NOI18N
260 // Get opening time instance
261 final OpeningTime openingTime = this.createOpeningTimes();
264 if (this.isSameOpeningTimeFound(openingTime)) {
265 // Yes then abort here
266 this.showFacesMessage("form-admin-add-headquarter-opening-time:openingStartDay", "ADMIN_OPENING_TIME_ALREADY_CREATED"); //NOI18N
270 // Add to temporary list
271 headquarterOpeningTimes.add(openingTime);
273 // Clear opening time fields
274 this.clearOpeningTime();
278 * Getter for fax number's area code
280 * @return Fax number's area code
282 public Integer getFaxAreaCode () {
283 return this.faxAreaCode;
287 * Setter for fax number's area code
289 * @param faxAreaCode Fax number's area code
291 public void setFaxAreaCode (final Integer faxAreaCode) {
292 this.faxAreaCode = faxAreaCode;
296 * Getter for fax's country instance
298 * @return Fax' country instance
300 public Country getFaxCountry () {
301 return this.faxCountry;
305 * Setter for fax's country instance
307 * @param faxCountry Fax' country instance
309 public void setFaxCountry (final Country faxCountry) {
310 this.faxCountry = faxCountry;
314 * Getter for fax number
318 public Long getFaxNumber () {
319 return this.faxNumber;
323 * Setter for fax number
325 * @param faxNumber Fax number
327 public void setFaxNumber (final Long faxNumber) {
328 this.faxNumber = faxNumber;
336 public String getHeadquarterCity () {
337 return this.headquarterCity;
343 * @param headquarterCity City
345 public void setHeadquarterCity (final String headquarterCity) {
346 this.headquarterCity = headquarterCity;
350 * Getter for headquarter contact person
352 * @return Headquarter contact person
354 public Employable getHeadquarterContactEmployee () {
355 return this.headquarterContactEmployee;
359 * Setter for headquarter contact person
361 * @param headquarterContactEmployee Headquarter contact person
363 public void setHeadquarterContactEmployee (final Employable headquarterContactEmployee) {
364 this.headquarterContactEmployee = headquarterContactEmployee;
372 public Country getHeadquarterCountry () {
373 return this.headquarterCountry;
379 * @param headquarterCountry Country
381 public void setHeadquarterCountry (final Country headquarterCountry) {
382 this.headquarterCountry = headquarterCountry;
386 * Getter for email address
388 * @return Email address
390 public String getHeadquarterEmailAddress () {
391 return this.headquarterEmailAddress;
395 * Getter for email address
397 * @param headquarterEmailAddress Email address
399 public void setHeadquarterEmailAddress (final String headquarterEmailAddress) {
400 this.headquarterEmailAddress = headquarterEmailAddress;
404 * Getter for house number
406 * @return House number
408 public Short getHeadquarterHouseNumber () {
409 return this.headquarterHouseNumber;
413 * Setter for house number
415 * @param headquarterHouseNumber House number
417 public void setHeadquarterHouseNumber (final Short headquarterHouseNumber) {
418 this.headquarterHouseNumber = headquarterHouseNumber;
422 * Getter for opening times of this headquarter
424 * @return Opening times
426 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
427 public List<OpeningTime> getHeadquarterOpeningTimes () {
428 return headquarterOpeningTimes;
432 * Setter for opening times of this headquarter
434 * @param headquarterOpeningTimes Opening times
436 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
437 public void setHeadquarterOpeningTimes (final List<OpeningTime> headquarterOpeningTimes) {
438 FinancialsAdminHeadquarterWebRequestBean.headquarterOpeningTimes = headquarterOpeningTimes;
446 public Short getHeadquarterStore () {
447 return this.headquarterStore;
453 * @param headquarterStore Store
455 public void setHeadquarterStore (final Short headquarterStore) {
456 this.headquarterStore = headquarterStore;
460 * Getter for street name
462 * @return Street name
464 public String getHeadquarterStreet () {
465 return this.headquarterStreet;
469 * Setter for street name
471 * @param headquarterStreet Street name
473 public void setHeadquarterStreet (final String headquarterStreet) {
474 this.headquarterStreet = headquarterStreet;
478 * Getter for suite number
480 * @return Suite number
482 public Short getHeadquarterSuiteNumber () {
483 return this.headquarterSuiteNumber;
487 * Setter for suite number
489 * @param headquarterSuiteNumber Suite number
491 public void setHeadquarterSuiteNumber (final Short headquarterSuiteNumber) {
492 this.headquarterSuiteNumber = headquarterSuiteNumber;
496 * Getter for owning user instance
498 * @return Owning user instance
500 public User getHeadquarterUserOwner () {
501 return this.headquarterUserOwner;
505 * Setter for owning user instance
507 * @param headquarterUserOwner Owning user instance
509 public void setHeadquarterUserOwner (final User headquarterUserOwner) {
510 this.headquarterUserOwner = headquarterUserOwner;
514 * Getter for ZIP code\
518 public Integer getHeadquarterZipCode () {
519 return this.headquarterZipCode;
523 * Setter for ZIP code\
525 * @param headquarterZipCode ZIP code
527 public void setHeadquarterZipCode (final Integer headquarterZipCode) {
528 this.headquarterZipCode = headquarterZipCode;
532 * Getter for land-line number's area code
534 * @return Land-line number's area code
536 public Integer getLandLineAreaCode () {
537 return this.landLineAreaCode;
541 * Setter for land-line number's area code
543 * @param landLineAreaCode Land-line number's area code
545 public void setLandLineAreaCode (final Integer landLineAreaCode) {
546 this.landLineAreaCode = landLineAreaCode;
550 * Getter for land-line number's country instance
552 * @return Land-line number's country instance
554 public Country getLandLineCountry () {
555 return this.landLineCountry;
559 * Setter for land-line number's country instance
561 * @param landLineCountry Land-line number's country instance
563 public void setLandLineCountry (final Country landLineCountry) {
564 this.landLineCountry = landLineCountry;
568 * Getter for land-line number
570 * @return Land-line number
572 public Long getLandLineNumber () {
573 return this.landLineNumber;
577 * Setter for land-line number
579 * @param landLineNumber Land-line number
581 public void setLandLineNumber (final Long landLineNumber) {
582 this.landLineNumber = landLineNumber;
586 * Getter for ending week day
588 * @return Ending week day
590 public DayOfTheWeek getOpeningEndDay () {
591 return this.openingEndDay;
595 * Setter for ending week day
597 * @param openingEndDay Ending week day
599 public void setOpeningEndDay (final DayOfTheWeek openingEndDay) {
600 this.openingEndDay = openingEndDay;
604 * Getter for ending time
606 * @return Ending time
608 @SuppressWarnings ("ReturnOfDateField")
609 public Date getOpeningEndTime () {
610 return this.openingEndTime;
614 * Getter for ending time
616 * @param openingEndTime Ending time
618 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
619 public void setOpeningEndTime (final Date openingEndTime) {
620 this.openingEndTime = openingEndTime;
624 * Getter for starting week day
626 * @return Starting week day
628 public DayOfTheWeek getOpeningStartDay () {
629 return this.openingStartDay;
633 * Getter for starting week day
635 * @param openingStartDay Starting week day
637 public void setOpeningStartDay (final DayOfTheWeek openingStartDay) {
638 this.openingStartDay = openingStartDay;
642 * Getter for starting time
644 * @return Starting time
646 @SuppressWarnings ("ReturnOfDateField")
647 public Date getOpeningStartTime () {
648 return this.openingStartTime;
652 * Getter for starting time
654 * @param openingStartTime Starting time
656 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
657 public void setOpeningStartTime (final Date openingStartTime) {
658 this.openingStartTime = openingStartTime;
662 * Clears this bean data
664 private void clear () {
665 // Clear all headquarter data
666 this.setHeadquarterCity(null);
667 this.setHeadquarterContactEmployee(null);
668 this.setHeadquarterCountry(null);
669 this.setHeadquarterEmailAddress(null);
670 this.setHeadquarterHouseNumber(null);
671 this.setHeadquarterStore(null);
672 this.setHeadquarterStreet(null);
673 this.setHeadquarterSuiteNumber(null);
674 this.setHeadquarterUserOwner(null);
675 this.setHeadquarterZipCode(null);
676 this.setHeadquarterOpeningTimes(null);
678 // Extra-clear opening time
679 this.clearOpeningTime();
683 * Clears all opening time fields
685 private void clearOpeningTime () {
686 // Clear all opening time fields
687 this.setOpeningEndDay(null);
688 this.setOpeningEndTime(null);
689 this.setOpeningStartDay(null);
690 this.setOpeningStartTime(null);
694 * Prepares an instance of a Headquarter object (entity) with all data from
695 * this bean. If a complete fax number or land-line number was provided, it
696 * will be set in the instance as well.
698 * @return An instance of a Headquarter class (entity)
700 private Headquarter createHeadquarter () {
701 // Create new headquarter instance
702 final Headquarter headquarter = new BusinessHeadquarter(this.getHeadquarterCity(), this.getHeadquarterCountry(), this.getHeadquarterStreet(), this.getHeadquarterZipCode(), this.getHeadquarterHouseNumber());
704 // Add all other fields, too
705 headquarter.setHeadquarterContactEmployee(this.getHeadquarterContactEmployee());
706 headquarter.setHeadquarterEmailAddress(this.getHeadquarterEmailAddress());
707 headquarter.setHeadquarterStore(this.getHeadquarterStore());
708 headquarter.setHeadquarterSuiteNumber(this.getHeadquarterSuiteNumber());
709 headquarter.setHeadquarterUserOwner(this.getHeadquarterUserOwner());
711 // Generate phone number
712 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
713 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
715 // Don't set null or wrong references
716 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
717 // Now the number must be given
718 if (landLine.getPhoneAreaCode() == null) {
720 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
721 } else if (landLine.getPhoneAreaCode() < 1) {
723 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
724 } else if (landLine.getPhoneNumber() == null) {
726 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
727 } else if (landLine.getPhoneNumber() < 1) {
729 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
733 headquarter.setHeadquarterLandLineNumber(landLine);
736 // Don't set null or wrong references
737 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
738 // Now the number must be given
739 if (fax.getPhoneAreaCode() == null) {
741 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
742 } else if (fax.getPhoneAreaCode() < 1) {
744 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
745 } else if (fax.getPhoneNumber() == null) {
747 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
748 } else if (fax.getPhoneNumber() < 1) {
750 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
754 headquarter.setHeadquarterFaxNumber(fax);
756 // Is the opening times list filled?
757 if (!this.getHeadquarterOpeningTimes().isEmpty()) {
758 // Yes, then set in headquarter, too
759 headquarter.setHeadquarterOpeningTimes(this.getHeadquarterOpeningTimes());
762 // Return fully prepared instance
767 * Prepares an instance of a OpeningTimes object (entity) with all data from
768 * this bean. If a complete fax number or land-line number was provided, it
769 * will be set in the instance as well.
771 * @return An instance of a OpeningTimes class (entity)
773 private OpeningTime createOpeningTimes () {
774 // Create new openingTime instance
775 final OpeningTime openingTime = new BusinessOpeningTime(this.getOpeningEndDay(), this.getOpeningEndTime(), this.getOpeningStartDay(), this.getOpeningStartTime());
777 // Return fully prepared instance
782 * Checks whether the given headquarter' address is already found in local
783 * cache. Please note that this method fully relies on the cache, so you
784 * must always fire proper events that add/update/delete entries in cache.
786 * @param headquarter Headquarter to check it's address
788 * @return Whether the address has been found
790 private boolean isHeadquarterCreatedByRequiredData (final Headquarter headquarter) {
791 // Get full list from other bean
792 final List<Headquarter> allHeadquarter = this.headquarterController.allHeadquarter();
794 // Default is not found
795 boolean isFound = false;
797 // Now check each entry
798 for (final Headquarter hq : allHeadquarter) {
800 if (Headquarters.isSameAddress(hq, headquarter)) {
812 * Checks if given opening time is already added
814 * @param openingTime Opening time to be checked
816 * @return Whether it has been added already
818 private boolean isSameOpeningTimeFound (final OpeningTime openingTime) {
819 // Default is not found
820 boolean isFound = false;
823 for (final OpeningTime ot : this.getHeadquarterOpeningTimes()) {
825 if (Objects.equals(ot, openingTime)) {