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.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.jcontactsbusiness.basicdata.BusinessBasicData;
27 import org.mxchange.jcontactsbusiness.branchoffice.AdminBranchOfficeSessionBeanRemote;
28 import org.mxchange.jcontactsbusiness.branchoffice.BranchOffice;
29 import org.mxchange.jcontactsbusiness.branchoffice.BranchOfficeUtils;
30 import org.mxchange.jcontactsbusiness.branchoffice.CompanyBranchOffice;
31 import org.mxchange.jcontactsbusiness.employee.Employee;
32 import org.mxchange.jcontactsbusiness.events.branchoffice.added.BranchOfficeAddedEvent;
33 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
35 import org.mxchange.jcountry.data.Country;
36 import org.mxchange.jfinancials.beans.BaseFinancialsController;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.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 FinancialsAdminBranchOfficeWebRequestBean extends BaseFinancialsController implements FinancialsAdminBranchOfficeWebRequestController {
55 private static final long serialVersionUID = 5_028_697_360_461L;
58 * EJB for administrative purposes
60 @EJB (lookup = "java:global/jfinancials-ejb/adminBranchOffice!org.mxchange.jcontactsbusiness.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 * An event being fired when a branch office has been successfully added
98 private Event<ObservableBranchOfficeAddedEvent> branchOfficeAddedEvent;
101 * A general branch office controller (backing bean)
104 private FinancialsBranchOfficeWebRequestController branchOfficeController;
109 private Short branchStore;
112 * Branch office street name
114 private String branchStreet;
119 private Short branchSuiteNumber;
122 * Owning user instance (which this branch office is assigned to)
124 private User branchUserOwner;
129 private Integer branchZipCode;
132 * Area code for fax number
134 private Integer faxAreaCode;
137 * Country for fax number
139 private Country faxCountry;
142 * Dial number for fax number
144 private Long faxNumber;
147 * Area code for land-line number
149 private Integer landLineAreaCode;
152 * Country for land-line number
154 private Country landLineCountry;
157 * Dial number for land-line number
159 private Long landLineNumber;
162 * Default constructor
164 public FinancialsAdminBranchOfficeWebRequestBean () {
165 // Call super constructor
170 * Adds branch office with all data from this backing bean. First this
171 * action method will validate if the branch office's address is already
172 * registered and if found, it will output a proper faces message.
174 * @return Redirect outcome
176 public String addBranchOffice () {
178 final BranchOffice branchOffice = this.createBranchOffice();
180 // Is the branch office not created yet?
181 if (this.isBranchOfficeCreatedByRequiredData(branchOffice)) {
182 // Then show proper faces message
183 this.showFacesMessage("form_admin_add_branch_office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
187 // Delcare updated instance
188 final BranchOffice updatedOffice;
192 updatedOffice = this.adminBranchOfficeBean.addBranchOffice(branchOffice);
193 } catch (final BranchOfficeAlreadyAddedException ex) {
195 this.showFacesMessage("form_admin_add_branch_office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
200 this.branchOfficeAddedEvent.fire(new BranchOfficeAddedEvent(updatedOffice));
203 return "admin_list_branch_offices"; //NOI18N
211 public String getBranchCity () {
212 return this.branchCity;
218 * @param branchCity City
220 public void setBranchCity (final String branchCity) {
221 this.branchCity = branchCity;
225 * Getter for basic company data
227 * @return Basic company data
229 public BusinessBasicData getBranchCompany () {
230 return this.branchCompany;
234 * Setter for basic company data
236 * @param branchCompany Basic company data
238 public void setBranchCompany (final BusinessBasicData branchCompany) {
239 this.branchCompany = branchCompany;
243 * Getter for branch office contact person
245 * @return Branch office contact person
247 public Employee getBranchContactEmployee () {
248 return this.branchContactEmployee;
252 * Setter for branch office contact person
254 * @param branchContactEmployee Branch office contact person
256 public void setBranchContactEmployee (final Employee branchContactEmployee) {
257 this.branchContactEmployee = branchContactEmployee;
265 public Country getBranchCountry () {
266 return this.branchCountry;
272 * @param branchCountry Country
274 public void setBranchCountry (final Country branchCountry) {
275 this.branchCountry = branchCountry;
279 * Getter for email address
281 * @return Email address
283 public String getBranchEmailAddress () {
284 return this.branchEmailAddress;
288 * Getter for email address
290 * @param branchEmailAddress Email address
292 public void setBranchEmailAddress (final String branchEmailAddress) {
293 this.branchEmailAddress = branchEmailAddress;
297 * Getter for house number
299 * @return House number
301 public Short getBranchHouseNumber () {
302 return this.branchHouseNumber;
306 * Setter for house number
308 * @param branchHouseNumber House number
310 public void setBranchHouseNumber (final Short branchHouseNumber) {
311 this.branchHouseNumber = branchHouseNumber;
319 public Short getBranchStore () {
320 return this.branchStore;
326 * @param branchStore Store
328 public void setBranchStore (final Short branchStore) {
329 this.branchStore = branchStore;
333 * Getter for street name
335 * @return Street name
337 public String getBranchStreet () {
338 return this.branchStreet;
342 * Setter for street name
344 * @param branchStreet Street name
346 public void setBranchStreet (final String branchStreet) {
347 this.branchStreet = branchStreet;
351 * Getter for suite number
353 * @return Suite number
355 public Short getBranchSuiteNumber () {
356 return this.branchSuiteNumber;
360 * Setter for suite number
362 * @param branchSuiteNumber Suite number
364 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
365 this.branchSuiteNumber = branchSuiteNumber;
369 * Getter for owning user instance
371 * @return Owning user instance
373 public User getBranchUserOwner () {
374 return this.branchUserOwner;
378 * Setter for owning user instance
380 * @param branchUserOwner Owning user instance
382 public void setBranchUserOwner (final User branchUserOwner) {
383 this.branchUserOwner = branchUserOwner;
387 * Getter for ZIP code\
391 public Integer getBranchZipCode () {
392 return this.branchZipCode;
396 * Setter for ZIP code\
398 * @param branchZipCode ZIP code
400 public void setBranchZipCode (final Integer branchZipCode) {
401 this.branchZipCode = branchZipCode;
405 * Getter for fax number's area code
407 * @return Fax number's area code
409 public Integer getFaxAreaCode () {
410 return this.faxAreaCode;
414 * Setter for fax number's area code
416 * @param faxAreaCode Fax number's area code
418 public void setFaxAreaCode (final Integer faxAreaCode) {
419 this.faxAreaCode = faxAreaCode;
423 * Getter for fax's country instance
425 * @return Fax' country instance
427 public Country getFaxCountry () {
428 return this.faxCountry;
432 * Setter for fax's country instance
434 * @param faxCountry Fax' country instance
436 public void setFaxCountry (final Country faxCountry) {
437 this.faxCountry = faxCountry;
441 * Getter for fax number
445 public Long getFaxNumber () {
446 return this.faxNumber;
450 * Setter for fax number
452 * @param faxNumber Fax number
454 public void setFaxNumber (final Long faxNumber) {
455 this.faxNumber = faxNumber;
459 * Getter for land-line number's area code
461 * @return Land-line number's area code
463 public Integer getLandLineAreaCode () {
464 return this.landLineAreaCode;
468 * Setter for land-line number's area code
470 * @param landLineAreaCode Land-line number's area code
472 public void setLandLineAreaCode (final Integer landLineAreaCode) {
473 this.landLineAreaCode = landLineAreaCode;
477 * Getter for land-line number's country instance
479 * @return Land-line number's country instance
481 public Country getLandLineCountry () {
482 return this.landLineCountry;
486 * Setter for land-line number's country instance
488 * @param landLineCountry Land-line number's country instance
490 public void setLandLineCountry (final Country landLineCountry) {
491 this.landLineCountry = landLineCountry;
495 * Getter for land-line number
497 * @return Land-line number
499 public Long getLandLineNumber () {
500 return this.landLineNumber;
504 * Setter for land-line number
506 * @param landLineNumber Land-line number
508 public void setLandLineNumber (final Long landLineNumber) {
509 this.landLineNumber = landLineNumber;
513 * Prepares an instance of a BranchOffice object (entity) with all data from
514 * this bean. If a complete fax number or land-line number was provided, it
515 * will be set in the instance as well.
517 * @return An instance of a BranchOffice class (entity)
519 private BranchOffice createBranchOffice () {
520 // Create new branch office instance
521 final BranchOffice branchOffice = new CompanyBranchOffice(this.getBranchCity(), this.getBranchCompany(), this.getBranchCountry(), this.getBranchStreet(), this.getBranchZipCode(), this.getBranchHouseNumber());
523 // Add all other fields, too
524 branchOffice.setBranchContactEmployee(this.getBranchContactEmployee());
525 branchOffice.setBranchEmailAddress(this.getBranchEmailAddress());
526 branchOffice.setBranchStore(this.getBranchStore());
527 branchOffice.setBranchSuiteNumber(this.getBranchSuiteNumber());
528 branchOffice.setBranchUserOwner(this.getBranchUserOwner());
530 // Generate phone number
531 final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
532 final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
534 // Don't set null or wrong references
535 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
536 // Now the number must be given
537 if (landLine.getPhoneAreaCode() == null) {
539 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
540 } else if (landLine.getPhoneAreaCode() < 1) {
542 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
543 } else if (landLine.getPhoneNumber() == null) {
545 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
546 } else if (landLine.getPhoneNumber() < 1) {
548 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
552 branchOffice.setBranchLandLineNumber(landLine);
555 // Don't set null or wrong references
556 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
557 // Now the number must be given
558 if (fax.getPhoneAreaCode() == null) {
560 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
561 } else if (fax.getPhoneAreaCode() < 1) {
563 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
564 } else if (fax.getPhoneNumber() == null) {
566 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
567 } else if (fax.getPhoneNumber() < 1) {
569 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
573 branchOffice.setBranchFaxNumber(fax);
576 // Return fully prepared instance
581 * Checks whether the given branch office's address is already found in
582 * local cache. Please note that this method fully relies on the cache, so
583 * you must always fire proper events that add/update/delete entries in
586 * @param branchOffice Branch office to check it's address
588 * @return Whether the address has been found
590 private boolean isBranchOfficeCreatedByRequiredData (final BranchOffice branchOffice) {
591 // Get full list from other bean
592 final List<BranchOffice> branchOffices = this.branchOfficeController.allBranchOffices();
594 // Default is not found
595 boolean isFound = false;
597 // Now check each entry
598 for (final BranchOffice bo : branchOffices) {
600 if (BranchOfficeUtils.isSameAddress(bo, branchOffice)) {