2 * Copyright (C) 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.addressbook.beans.business.branchoffice;
19 import java.util.List;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.addressbook.beans.BaseAddressbookBean;
27 import org.mxchange.jcontactsbusiness.events.branchoffice.added.BranchOfficeAddedEvent;
28 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
29 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
30 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
31 import org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote;
32 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
33 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.CompanyBranchOffice;
35 import org.mxchange.jcontactsbusiness.model.employee.Employee;
36 import org.mxchange.jcountry.model.data.Country;
37 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
39 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
41 import org.mxchange.jusercore.model.user.User;
44 * An administrative bean for branch offices
46 * @author Roland Häder<roland@mxchange.org>
48 @Named ("adminBranchOfficeController")
50 public class AddressbookAdminBranchOfficeWebRequestBean extends BaseAddressbookBean implements AddressbookAdminBranchOfficeWebRequestController {
55 private static final long serialVersionUID = 5_028_697_360_461L;
58 * EJB for administrative purposes
60 @EJB (lookup = "java:global/addressbook-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote")
61 private AdminBranchOfficeSessionBeanRemote adminBranchOfficeBean;
66 private String branchCity;
69 * Assigned company for this branch office
71 private BusinessBasicData branchCompany;
74 * Contact person in branch office
76 private Employee branchContactEmployee;
81 private Country branchCountry;
86 private String branchEmailAddress;
91 private Short branchHouseNumber;
94 * Number of branch office
96 private Long branchNumber;
99 * An event being fired when a branch office has been successfully added
103 private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
106 * A general branch office controller (backing bean)
109 private AddressbookBranchOfficeWebRequestController branchOfficeController;
114 private Short branchStore;
117 * Branch office street name
119 private String branchStreet;
124 private Short branchSuiteNumber;
127 * Owning user instance (which this branch office is assigned to)
129 private User branchUserOwner;
134 private Integer branchZipCode;
137 * Area code for fax number
139 private Integer faxAreaCode;
142 * Country for fax number
144 private Country faxCountry;
147 * Dial number for fax number
149 private Long faxNumber;
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;
167 * Default constructor
169 public AddressbookAdminBranchOfficeWebRequestBean () {
170 // Call super constructor
175 * Adds branch office with all data from this backing bean. First this
176 * action method will validate if the branch office's address is already
177 * registered and if found, it will output a proper faces message.
179 * @return Redirect outcome
181 public String addBranchOffice () {
183 final BranchOffice branchOffice = this.createBranchOffice();
185 // Is the branch office not created yet?
186 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
187 // Then show proper faces message
188 this.showFacesMessage("form_admin_add_branch_office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
192 // Delcare updated instance
193 final BranchOffice updatedOffice;
197 updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
198 } catch (final BranchOfficeAlreadyAddedException ex) {
200 this.showFacesMessage("form_admin_add_branch_office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
205 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
208 return "admin_list_branch_offices"; //NOI18N
216 public String getBranchCity () {
217 return this.branchCity;
223 * @param branchCity City
225 public void setBranchCity (final String branchCity) {
226 this.branchCity = branchCity;
230 * Getter for basic company data
232 * @return Basic company data
234 public BusinessBasicData getBranchCompany () {
235 return this.branchCompany;
239 * Setter for basic company data
241 * @param branchCompany Basic company data
243 public void setBranchCompany (final BusinessBasicData branchCompany) {
244 this.branchCompany = branchCompany;
248 * Getter for branch office contact person
250 * @return Branch office contact person
252 public Employee getBranchContactEmployee () {
253 return this.branchContactEmployee;
257 * Setter for branch office contact person
259 * @param branchContactEmployee Branch office contact person
261 public void setBranchContactEmployee (final Employee branchContactEmployee) {
262 this.branchContactEmployee = branchContactEmployee;
270 public Country getBranchCountry () {
271 return this.branchCountry;
277 * @param branchCountry Country
279 public void setBranchCountry (final Country branchCountry) {
280 this.branchCountry = branchCountry;
284 * Getter for email address
286 * @return Email address
288 public String getBranchEmailAddress () {
289 return this.branchEmailAddress;
293 * Getter for email address
295 * @param branchEmailAddress Email address
297 public void setBranchEmailAddress (final String branchEmailAddress) {
298 this.branchEmailAddress = branchEmailAddress;
302 * Getter for house number
304 * @return House number
306 public Short getBranchHouseNumber () {
307 return this.branchHouseNumber;
311 * Setter for house number
313 * @param branchHouseNumber House number
315 public void setBranchHouseNumber (final Short branchHouseNumber) {
316 this.branchHouseNumber = branchHouseNumber;
320 * Getter for branch office number
322 * @return Branch office number
324 public Long getBranchNumber () {
325 return this.branchNumber;
329 * Setter for branch office number
331 * @param branchNumber Branch office number
333 public void setBranchNumber (final Long branchNumber) {
334 this.branchNumber = branchNumber;
342 public Short getBranchStore () {
343 return this.branchStore;
349 * @param branchStore Store
351 public void setBranchStore (final Short branchStore) {
352 this.branchStore = branchStore;
356 * Getter for street name
358 * @return Street name
360 public String getBranchStreet () {
361 return this.branchStreet;
365 * Setter for street name
367 * @param branchStreet Street name
369 public void setBranchStreet (final String branchStreet) {
370 this.branchStreet = branchStreet;
374 * Getter for suite number
376 * @return Suite number
378 public Short getBranchSuiteNumber () {
379 return this.branchSuiteNumber;
383 * Setter for suite number
385 * @param branchSuiteNumber Suite number
387 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
388 this.branchSuiteNumber = branchSuiteNumber;
392 * Getter for owning user instance
394 * @return Owning user instance
396 public User getBranchUserOwner () {
397 return this.branchUserOwner;
401 * Setter for owning user instance
403 * @param branchUserOwner Owning user instance
405 public void setBranchUserOwner (final User branchUserOwner) {
406 this.branchUserOwner = branchUserOwner;
410 * Getter for ZIP code\
414 public Integer getBranchZipCode () {
415 return this.branchZipCode;
419 * Setter for ZIP code\
421 * @param branchZipCode ZIP code
423 public void setBranchZipCode (final Integer branchZipCode) {
424 this.branchZipCode = branchZipCode;
428 * Getter for fax number's area code
430 * @return Fax number's area code
432 public Integer getFaxAreaCode () {
433 return this.faxAreaCode;
437 * Setter for fax number's area code
439 * @param faxAreaCode Fax number's area code
441 public void setFaxAreaCode (final Integer faxAreaCode) {
442 this.faxAreaCode = faxAreaCode;
446 * Getter for fax's country instance
448 * @return Fax' country instance
450 public Country getFaxCountry () {
451 return this.faxCountry;
455 * Setter for fax's country instance
457 * @param faxCountry Fax' country instance
459 public void setFaxCountry (final Country faxCountry) {
460 this.faxCountry = faxCountry;
464 * Getter for fax number
468 public Long getFaxNumber () {
469 return this.faxNumber;
473 * Setter for fax number
475 * @param faxNumber Fax number
477 public void setFaxNumber (final Long faxNumber) {
478 this.faxNumber = faxNumber;
482 * Getter for land-line number's area code
484 * @return Land-line number's area code
486 public Integer getLandLineAreaCode () {
487 return this.landLineAreaCode;
491 * Setter for land-line number's area code
493 * @param landLineAreaCode Land-line number's area code
495 public void setLandLineAreaCode (final Integer landLineAreaCode) {
496 this.landLineAreaCode = landLineAreaCode;
500 * Getter for land-line number's country instance
502 * @return Land-line number's country instance
504 public Country getLandLineCountry () {
505 return this.landLineCountry;
509 * Setter for land-line number's country instance
511 * @param landLineCountry Land-line number's country instance
513 public void setLandLineCountry (final Country landLineCountry) {
514 this.landLineCountry = landLineCountry;
518 * Getter for land-line number
520 * @return Land-line number
522 public Long getLandLineNumber () {
523 return this.landLineNumber;
527 * Setter for land-line number
529 * @param landLineNumber Land-line number
531 public void setLandLineNumber (final Long landLineNumber) {
532 this.landLineNumber = landLineNumber;
536 * Prepares an instance of a BranchOffice object (entity) with all data from
537 * this bean. If a complete fax number or land-line number was provided, it
538 * will be set in the instance as well.
540 * @return An instance of a BranchOffice class (entity)
542 private BranchOffice createBranchOffice () {
543 // Create new branch office instance
544 final BranchOffice branchOffice = new CompanyBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
546 // Add all other fields, too
547 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
548 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
549 branchOffice.setBranchNumber(this.getBranchNumber());
550 branchOffice.setBranchStore(this.getBranchStore());
551 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
552 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
554 // Generate phone number
555 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
556 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
558 // Don't set null or wrong references
559 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
560 // Now the number must be given
561 if (landLine.getPhoneAreaCode() == null) {
563 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
564 } else if (landLine.getPhoneAreaCode() < 1) {
566 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
567 } else if (landLine.getPhoneNumber() == null) {
569 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
570 } else if (landLine.getPhoneNumber() < 1) {
572 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
576 branchOffice.setBranchLandLineNumber(landLine);
579 // Don't set null or wrong references
580 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
581 // Now the number must be given
582 if (fax.getPhoneAreaCode() == null) {
584 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
585 } else if (fax.getPhoneAreaCode() < 1) {
587 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
588 } else if (fax.getPhoneNumber() == null) {
590 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
591 } else if (fax.getPhoneNumber() < 1) {
593 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
597 branchOffice.setBranchFaxNumber(fax);
600 // Return fully prepared instance
605 * Checks whether the given branch office's address is already found in
606 * local cache. Please note that this method fully relies on the cache, so
607 * you must always fire proper events that add/update/delete entries in
610 * @param branchOffice Branch office to check it's address
612 * @return Whether the address has been found
614 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
615 // Get full list from other bean
616 final List<BranchOffice> branchOffices = this.branchOfficeController.allBranchOffices();
618 // Default is not found
619 boolean isFound = false;
621 // Now check each entry
622 for (final BranchOffice bo : branchOffices) {
624 if (BranchOffices.isSameAddress(bo, branchOffice)) {