2 * Copyright (C) 2016 - 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.jjobs.beans.helper;
19 import java.text.MessageFormat;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent;
26 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
29 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
30 import org.mxchange.jcontactsbusiness.model.department.Department;
31 import org.mxchange.jcontactsbusiness.model.employee.Employable;
32 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
33 import org.mxchange.jjobs.beans.BaseJobsBean;
34 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
35 import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
36 import org.mxchange.jjobs.model.receipt.BillableReceipt;
37 import org.mxchange.jphone.events.fax.created.CreatedFaxNumberEvent;
38 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
39 import org.mxchange.jphone.events.landline.created.CreatedLandLineNumberEvent;
40 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
41 import org.mxchange.jphone.events.mobile.created.CreatedMobileNumberEvent;
42 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
43 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
44 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
45 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
46 import org.mxchange.jproduct.model.product.Product;
47 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
48 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
49 import org.mxchange.jusercore.model.user.User;
50 import org.mxchange.jcontactsbusiness.model.employee.Employable;
53 * A general helper for beans
55 * @author Roland Häder<roland@mxchange.org>
59 public class JobsWebRequestHelperBean extends BaseJobsBean implements JobsWebRequestHelperController {
64 private static final long serialVersionUID = 17_258_793_567_145_701L;
69 private Contact contact;
72 * Event for when a contact instance was created
76 private Event<ObservableCreatedContactEvent> contactCreatedEvent;
81 private DialableFaxNumber faxNumber;
84 * Event for when a fax number instance was created
88 private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
93 private DialableLandLineNumber landLineNumber;
96 * Event for when a land-line number instance was created
100 private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
103 * Localization controller
106 private JobsLocalizationSessionController localizationController;
111 private DialableMobileNumber mobileNumber;
114 * Event for when a mobile number instance was created
118 private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
126 * Regular user controller
129 private JobsUserWebRequestController userController;
132 * Event for when a user instance was created
136 private Event<ObservableCreatedUserEvent> userCreatedEvent;
139 * Default constructor
141 public JobsWebRequestHelperBean () {
142 // Call super constructor
147 * Getter for contact instance
149 * @return Contact instance
151 public Contact getContact () {
156 * Setter for contact instance
158 * @param contact Contact instance
160 public void setContact (final Contact contact) {
161 this.contact = contact;
165 * Returns a message key depending on if this contact is a user and/or a
166 * contact. If this contact is unused, a default key is returned.
168 * @param contact Contact instance to check
170 * @return Message key
172 public String getContactUsageMessageKey (final Contact contact) {
173 // The contact must be valid
174 if (null == contact) {
176 throw new NullPointerException("contact is null"); //NOI18N
177 } else if (contact.getContactId() == null) {
179 throw new NullPointerException("contact.contactId is null"); //NOI18N
180 } else if (contact.getContactId() < 1) {
182 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
185 // Default key is "unused"
186 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
189 if (this.userController.isContactFound(contact)) {
191 messageKey = "CONTACT_IS_USER"; //NOI18N
194 // Return message key
199 * Getter for dialable fax number
201 * @return Dialable fax number
203 public DialableFaxNumber getFaxNumber () {
204 return this.faxNumber;
208 * Setter for dialable fax number
210 * @param faxNumber Dialable fax number
212 public void setFaxNumber (final DialableFaxNumber faxNumber) {
213 this.faxNumber = faxNumber;
217 * Getter for dialable land-line number
219 * @return Dialable land-line number
221 public DialableLandLineNumber getLandLineNumber () {
222 return this.landLineNumber;
226 * Setter for dialable land-line number
228 * @param landLineNumber Dialable land-line number
230 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
231 this.landLineNumber = landLineNumber;
235 * Getter for dialable mobile number
237 * @return Dialable mobile number
239 public DialableMobileNumber getMobileNumber () {
240 return this.mobileNumber;
244 * Setter for dialable mobile number
246 * @param mobileNumber Dialable mobile number
248 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
249 this.mobileNumber = mobileNumber;
253 * Getter for user instance
255 * @return User instance
257 public User getUser () {
262 * Setter for user instance
264 * @param user User instance
266 public void setUser (final User user) {
271 * Notifies other controllers (backing beans) if a contact id has been
272 * successfully converted to a Contact instance.
274 public void notifyControllerContactConverted () {
275 // Validate contact instance
276 if (this.getContact() == null) {
278 throw new NullPointerException("this.contact is null"); //NOI18N
279 } else if (this.getContact().getContactId() == null) {
281 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
282 } else if (this.getContact().getContactId() < 1) {
284 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
287 // Set all phone instances
288 this.setPhoneInstances(this.getContact());
290 // Set all fields: user
291 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
295 * Notifies other controllers (backing beans) if a phone id has been
296 * successfully converted to a DialableFaxNumber instance.
298 public void notifyControllerFaxNumberConverted () {
299 // Validate fax instance
300 if (this.getFaxNumber() == null) {
302 throw new NullPointerException("this.faxNumber is null"); //NOI18N
303 } else if (this.getFaxNumber().getPhoneId() == null) {
305 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
306 } else if (this.getFaxNumber().getPhoneId() < 1) {
308 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
309 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
311 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
312 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
314 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
315 } else if (this.getFaxNumber().getPhoneCountry() == null) {
317 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
318 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
320 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
321 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
323 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
324 } else if (this.getFaxNumber().getPhoneNumber() == null) {
325 // Throw NPE again ...
326 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
327 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
329 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
333 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
337 * Notifies other controllers (backing beans) if a phone id has been
338 * successfully converted to a DialableLandLineNumber instance.
340 public void notifyControllerLandLineNumberConverted () {
341 // Validate land-line instance
342 if (this.getLandLineNumber() == null) {
344 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
345 } else if (this.getLandLineNumber().getPhoneId() == null) {
347 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
348 } else if (this.getLandLineNumber().getPhoneId() < 1) {
350 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
351 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
353 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
354 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
356 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
357 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
359 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
360 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
362 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
363 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
365 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
366 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
367 // Throw NPE again ...
368 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
369 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
371 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
375 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
379 * Notifies other controllers (backing beans) if a phone id has been
380 * successfully converted to a DialableMobileNumber instance.
382 public void notifyControllerMobileNumberConverted () {
383 // Validate mobile instance
384 if (this.getMobileNumber() == null) {
386 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
387 } else if (this.getMobileNumber().getPhoneId() == null) {
389 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
390 } else if (this.getMobileNumber().getPhoneId() < 1) {
392 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
393 } else if (this.getMobileNumber().getMobileProvider() == null) {
395 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
396 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
398 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
399 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
401 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
402 } else if (this.getMobileNumber().getPhoneNumber() == null) {
403 // Throw NPE again ...
404 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
405 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
407 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
411 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
415 * Notifies other controllers (backing beans) if a user id has been
416 * successfully converted to a User instance.
418 public void notifyControllerUserConverted () {
419 // Validate user instance
420 if (this.getUser() == null) {
422 throw new NullPointerException("this.user is null"); //NOI18N
423 } else if (this.getUser().getUserId() == null) {
425 throw new NullPointerException("this.user.userId is null"); //NOI18N
426 } else if (this.getUser().getUserId() < 1) {
428 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
432 final Contact userContact = this.getUser().getUserContact();
434 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
435 this.setContact(userContact);
437 // Set all phone instances
438 this.setPhoneInstances(userContact);
441 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
445 * Renders data of basic company data
447 * @param basicData Basic company data instance
449 * @return Basic company data as string
451 public String renderBasicData (final BasicData basicData) {
452 // Default is empty string, so let's get started
453 final StringBuilder sb = new StringBuilder(30);
455 // Is basic company data set?
456 if (basicData instanceof BasicData) {
458 sb.append(basicData.getCompanyName()); //NOI18N
460 // Is email address set?
461 if (basicData.getCompanyEmailAddress() != null) {
463 sb.append(", ").append(basicData.getCompanyEmailAddress()); //NOI18N
466 // Is tax number set?
467 if (basicData.getCompanyTaxNumber() != null) {
469 sb.append(", ").append(basicData.getCompanyTaxNumber()); //NOI18N
474 return sb.toString();
478 * Returns the branch office's full address. If null is provided, an empty
479 * string is returned.
481 * @param branchOffice Branch office instance
483 * @return Branch office's address
485 public String renderBranchOffice (final BranchOffice branchOffice) {
486 // Default is empty string, so let's get started
487 final StringBuilder sb = new StringBuilder(30);
489 // Is a branch office instance given?
490 if (branchOffice instanceof BranchOffice) {
491 // This should not happen:
492 assert (branchOffice.getBranchCompany() instanceof BasicData) : "branchOffice.branchCompany is null"; //NOI18N
494 // Yes, then append all data
495 sb.append(branchOffice.getBranchCompany().getCompanyName());
496 sb.append(", "); //NOI18N
497 sb.append(branchOffice.getBranchStreet());
498 sb.append(" "); //NOI18N
499 sb.append(branchOffice.getBranchHouseNumber());
500 sb.append(", "); //NOI18N
501 sb.append(branchOffice.getBranchCountry().getCountryCode());
502 sb.append(" "); //NOI18N
503 sb.append(branchOffice.getBranchZipCode());
504 sb.append(" "); //NOI18N
505 sb.append(branchOffice.getBranchCity());
509 return sb.toString();
513 * Returns the contact's personal title, family name and name. If null is
514 * provided, an empty string is returned.
516 * @param contact Contact instance
518 * @return Contact's full name
520 public String renderContact (final Contact contact) {
521 // Default is empty string, so let's get started
522 final StringBuilder sb = new StringBuilder(20);
525 if (contact instanceof Contact) {
527 sb.append(this.getMessageFromBundle(contact.getContactPersonalTitle().getMessageKey()));
528 sb.append(" "); //NOI18N
529 sb.append(contact.getContactFirstName());
530 sb.append(" "); //NOI18N
531 sb.append(contact.getContactFamilyName());
535 return sb.toString();
539 * Returns the department's name and name of assigned company. If null is
540 * provided, an empty string is returned.
542 * @param department Department instance
544 * @return Department's full name
546 public String renderDepartment (final Department department) {
547 // Default is empty string, so let's get started
548 final StringBuilder sb = new StringBuilder(10);
550 // Is a department set?
551 if (department instanceof Department) {
553 sb.append(this.getMessageFromBundle(department.getDepartmentI18nKey()));
554 sb.append(" ("); //NOI18N
555 sb.append(department.getDepartmentCompany().getCompanyName());
556 sb.append(")"); //NOI18N
560 return sb.toString();
564 * Returns the employee's number, personal title, family name and name if
565 * available. If null is provided, an empty string is returned.
567 * @param employee Employable instance
569 * @return Contact's full name
571 public String renderEmployee (final Employable employee) {
572 // Default is empty string, so let's get started
573 final StringBuilder sb = new StringBuilder(20);
576 if (employee instanceof Employable) {
578 sb.append(employee.getEmployeeNumber());
580 // Is a department found?
581 if (employee.getEmployeeDepartment() instanceof Department) {
582 // Then add it's name to it
583 sb.append(" ("); //NOI18N
584 sb.append(this.renderDepartment(employee.getEmployeeDepartment()));
585 sb.append(")"); //NOI18N
588 // Is contact data found?
589 if (employee.getEmployeePersonalData() instanceof Contact) {
590 // Yes, then render it
591 final String contactName = this.renderContact(employee.getEmployeePersonalData());
594 if (contactName != null) {
596 sb.append(" ("); //NOI18N
597 sb.append(contactName);
598 sb.append(")"); //NOI18N
604 return sb.toString();
608 * Returns the product name and price. If null is provided, an empty string
611 * @param product Product instance
613 * @return Product name
615 public String renderGenericProduct (final Product product) {
616 // Default is empty string, so let's get started
617 final StringBuilder sb = new StringBuilder(10);
620 if (product instanceof Product) {
621 // Add name and price
622 sb.append(this.getMessageFromBundle(product.getProductI18nKey()));
623 sb.append(" ("); //NOI18N
624 sb.append(this.localizationController.formatCurrency(product.getProductGrossPrice()));
625 sb.append(" "); //NOI18N
626 sb.append(product.getProductCurrencyCode());
627 sb.append(")"); //NOI18N
631 return sb.toString();
635 * Returns the headquarter address. If null is provided, an empty string is
638 * @param headquarter Headquarter instance
640 * @return Headquarter address
642 public String renderHeadquarter (final Headquarter headquarter) {
643 // Default is empty string, so let's get started
644 final StringBuilder sb = new StringBuilder(10);
646 // Is a headquarter set?
647 if (headquarter instanceof Headquarter) {
649 sb.append(headquarter.getHeadquarterStreet());
650 sb.append(" "); //NOI18N
651 sb.append(headquarter.getHeadquarterHouseNumber());
653 // Is store/suite number set?
654 if (headquarter.getHeadquarterStore() instanceof Short) {
655 sb.append(" ("); //NOI18N
656 sb.append(this.getMessageFromBundle("DATA_STORE")); //NOI18N
657 sb.append(" "); //NOI18N
658 sb.append(headquarter.getHeadquarterStore());
659 sb.append(", "); //NOI18N
660 sb.append(this.getMessageFromBundle("DATA_SUITE_NUMBER")); //NOI18N
661 sb.append(" "); //NOI18N
662 sb.append(headquarter.getHeadquarterSuiteNumber());
663 sb.append(")"); //NOI18N
666 // Continue with country, ZIP code and city
667 sb.append(", "); //NOI18N
668 sb.append(headquarter.getHeadquarterCountry().getCountryCode());
669 sb.append(" "); //NOI18N
670 sb.append(headquarter.getHeadquarterZipCode());
671 sb.append(" "); //NOI18N
672 sb.append(headquarter.getHeadquarterCity());
676 return sb.toString();
680 * Returns the product name and price. If null is provided, an empty string
683 * @param product Product instance
685 * @return Product name
687 public String renderProduct (final Product product) {
688 // Default is empty string, so let's get started
689 final StringBuilder sb = new StringBuilder(10);
692 if (product instanceof Product) {
693 // Add name and price
694 sb.append(product.getProductTitle());
695 sb.append(" ("); //NOI18N
696 sb.append(this.localizationController.formatCurrency(product.getProductPrice()));
697 sb.append(")"); //NOI18N
701 return sb.toString();
705 * Returns the receipt. If null is provided, an empty string
708 * @param receipt Receipt instance
710 * @return Product name
712 public String renderReceipt (final BillableReceipt receipt) {
713 // Default is empty string, so let's get started
714 final StringBuilder sb = new StringBuilder(10);
717 if (receipt instanceof BillableReceipt) {
719 sb.append(this.getMessageFromBundle("RECEIPT_ISSUED")).append(" "); //NOI18N
720 sb.append(this.getMessageFromBundle(receipt.getReceiptPaymentType().getI18nKey()));
722 // Is receipt number included?
723 if (receipt.getReceiptNumber() !=null) {
725 sb.append(", ").append(this.getMessageFromBundle("RECEIPT_NUMBER")).append(" "); //NOI18N
726 sb.append(receipt.getReceiptNumber());
729 // Add company (over branch office)
730 sb.append(" ("); //NOI18N
731 sb.append(receipt.getReceiptBranchOffice().getBranchCompany().getCompanyName());
732 sb.append(")"); //NOI18N
736 return sb.toString();
740 * Set's all given contact's phone instances: land-line, mobile and
743 * @param contact Contact to set phone instances for
745 private void setPhoneInstances (final Contact contact) {
746 // The contact must be valid
747 if (null == contact) {
749 throw new NullPointerException("contact is null"); //NOI18N
750 } else if (contact.getContactId() == null) {
752 throw new NullPointerException("contact.contactId is null"); //NOI18N
753 } else if (contact.getContactId() < 1) {
755 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
759 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
760 // Yes, then set it in admin controller
761 this.setMobileNumber(contact.getContactMobileNumber());
765 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
766 // Yes, then set it in admin controller
767 this.setLandLineNumber(contact.getContactLandLineNumber());
771 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
772 // Yes, then set it in admin controller
773 this.setFaxNumber(contact.getContactFaxNumber());