2 * Copyright (C) 2016 - 2022 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.pizzaapplication.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.jcustomercore.events.customer.created.CreatedCustomerEvent;
29 import org.mxchange.jcustomercore.events.customer.created.ObservableCreatedCustomerEvent;
30 import org.mxchange.jcustomercore.model.customer.Customer;
31 import org.mxchange.jphone.events.fax.created.CreatedFaxNumberEvent;
32 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
33 import org.mxchange.jphone.events.landline.created.CreatedLandLineNumberEvent;
34 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
35 import org.mxchange.jphone.events.mobile.created.CreatedMobileNumberEvent;
36 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
37 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
40 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
41 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
42 import org.mxchange.jusercore.model.user.User;
43 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
44 import org.mxchange.pizzaapplication.beans.customer.PizzaAdminCustomerWebRequestController;
45 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
46 import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController;
47 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebRequestController;
50 * A general helper for beans
52 * @author Roland Häder<roland@mxchange.org>
56 public class PizzaWebRequestHelperBean implements PizzaWebRequestHelperController {
59 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
61 private static final short THREAD_STACK = 5;
66 private static final long serialVersionUID = 17_258_793_567_145_701L;
69 * Administrative contact controller
72 private PizzaAdminContactWebRequestController adminContactController;
75 * Administrative user controller
78 private PizzaAdminCustomerWebRequestController adminCustomerController;
81 * Administrative phone controller
84 private PizzaAdminPhoneWebRequestController adminPhoneController;
89 private Contact contact;
92 * Event for when a contact instance was created
96 private Event<ObservableCreatedContactEvent> contactCreatedEvent;
101 private Customer customer;
104 * General customer controller
107 private PizzaCustomerWebSessionController customerController;
110 * An event being fired when a customer has been created
114 private Event<ObservableCreatedCustomerEvent> customerCreatedEvent;
119 private DialableFaxNumber faxNumber;
122 * Event for when a fax number instance was created
126 private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
131 private DialableLandLineNumber landLineNumber;
134 * Event for when a land-line number instance was created
138 private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
143 private DialableMobileNumber mobileNumber;
146 * Event for when a mobile number instance was created
150 private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
158 * Regular user controller
161 private PizzaUserWebRequestController userController;
164 * Event for when a user instance was created
168 private Event<ObservableCreatedUserEvent> userCreatedEvent;
171 * Default constructor
173 public PizzaWebRequestHelperBean () {
174 // Call super constructor
177 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
178 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
182 * Copies currently set customer instance's data to adminCustomerController
184 public void copyCustomerToController () {
185 // Validate customer instance
186 if (this.getCustomer() == null) {
188 throw new NullPointerException("this.customer is null"); //NOI18N
189 } else if (this.getCustomer().getCustomerId() == null) {
191 throw new NullPointerException("this.customer.customerId is null"); //NOI18N
192 } else if (this.getContact().getContactId() < 1) {
194 throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getContact().getContactId())); //NOI18N
197 // Set all phone instances
198 this.setPhoneInstances(this.getContact());
200 // Set all fields: user
201 this.customerCreatedEvent.fire(new CreatedCustomerEvent(this.getCustomer()));
205 * Getter for contact instance
207 * @return Contact instance
210 public Contact getContact () {
215 * Setter for contact instance
217 * @param contact Contact instance
219 public void setContact (final Contact contact) {
220 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
221 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
222 this.contact = contact;
226 * Returns a message key depending on if this contact is a user and/or a
227 * contact. If this contact is unused, a default key is returned.
229 * @param contact Contact instance to check
231 * @return Message key
233 public String getContactUsageMessageKey (final Contact contact) {
234 // The contact must be valid
235 if (null == contact) {
237 throw new NullPointerException("contact is null"); //NOI18N
238 } else if (contact.getContactId() == null) {
240 throw new NullPointerException("contact.contactId is null"); //NOI18N
241 } else if (contact.getContactId() < 1) {
243 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
246 // Default key is "unused"
247 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
249 // Check user/customer contact
250 boolean isUserContact = this.userController.isContactFound(contact);
251 boolean isCustomerContact = this.customerController.isContactFound(contact);
254 if (isUserContact && isCustomerContact) {
256 messageKey = "CONTACT_IS_CUSTOMER_USER"; //NOI18N
257 } else if (isUserContact) {
259 messageKey = "CONTACT_IS_USER"; //NOI18N
260 } else if (isCustomerContact) {
262 messageKey = "CONTACT_IS_CUSTOMER"; //NOI18N
265 // Return message key
270 * Getter for customer instance
272 * @return Customer instance
274 public Customer getCustomer () {
275 return this.customer;
279 * Setter for customer instance
281 * @param customer Contact instance
283 public void setCustomer (final Customer customer) {
284 this.customer = customer;
288 * Getter for dialable fax number
290 * @return Dialable fax number
293 public DialableFaxNumber getFaxNumber () {
294 return this.faxNumber;
298 * Setter for dialable fax number
300 * @param faxNumber Dialable fax number
302 public void setFaxNumber (final DialableFaxNumber faxNumber) {
303 this.faxNumber = faxNumber;
307 * Getter for dialable land-line number
309 * @return Dialable land-line number
312 public DialableLandLineNumber getLandLineNumber () {
313 return this.landLineNumber;
317 * Setter for dialable land-line number
319 * @param landLineNumber Dialable land-line number
321 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
322 this.landLineNumber = landLineNumber;
326 * Getter for dialable mobile number
328 * @return Dialable mobile number
331 public DialableMobileNumber getMobileNumber () {
332 return this.mobileNumber;
336 * Setter for dialable mobile number
338 * @param mobileNumber Dialable mobile number
340 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
341 this.mobileNumber = mobileNumber;
345 * Getter for user instance
347 * @return User instance
350 public User getUser () {
355 * Setter for user instance
357 * @param user User instance
359 public void setUser (final User user) {
364 * Notifies other controllers (backing beans) if a contact id has been
365 * successfully converted to a Contact instance.
367 public void notifyControllerContactConverted () {
368 // Validate contact instance
369 if (this.getContact() == null) {
371 throw new NullPointerException("this.contact is null"); //NOI18N
372 } else if (this.getContact().getContactId() == null) {
374 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
375 } else if (this.getContact().getContactId() < 1) {
377 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
380 // Set all phone instances
381 this.setPhoneInstances(this.getContact());
383 // Set all fields: user
384 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
388 * Notifies other controllers (backing beans) if a phone id has been
389 * successfully converted to a DialableFaxNumber instance.
391 public void notifyControllerFaxNumberConverted () {
392 // Validate fax instance
393 if (this.getFaxNumber() == null) {
395 throw new NullPointerException("this.faxNumber is null"); //NOI18N
396 } else if (this.getFaxNumber().getPhoneId() == null) {
398 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
399 } else if (this.getFaxNumber().getPhoneId() < 1) {
401 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
402 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
404 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
405 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
407 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
408 } else if (this.getFaxNumber().getPhoneCountry() == null) {
410 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
411 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
413 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
414 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
416 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
417 } else if (this.getFaxNumber().getPhoneNumber() == null) {
418 // Throw NPE again ...
419 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
420 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
422 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
426 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
430 * Notifies other controllers (backing beans) if a phone id has been
431 * successfully converted to a DialableLandLineNumber instance.
433 public void notifyControllerLandLineNumberConverted () {
434 // Validate land-line instance
435 if (this.getLandLineNumber() == null) {
437 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
438 } else if (this.getLandLineNumber().getPhoneId() == null) {
440 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
441 } else if (this.getLandLineNumber().getPhoneId() < 1) {
443 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
444 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
446 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
447 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
449 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
450 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
452 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
453 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
455 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
456 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
458 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
459 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
460 // Throw NPE again ...
461 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
462 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
464 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
468 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
472 * Notifies other controllers (backing beans) if a phone id has been
473 * successfully converted to a DialableMobileNumber instance.
475 public void notifyControllerMobileNumberConverted () {
476 // Validate mobile instance
477 if (this.getMobileNumber() == null) {
479 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
480 } else if (this.getMobileNumber().getPhoneId() == null) {
482 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
483 } else if (this.getMobileNumber().getPhoneId() < 1) {
485 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
486 } else if (this.getMobileNumber().getMobileProvider() == null) {
488 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
489 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
491 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
492 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
494 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
495 } else if (this.getMobileNumber().getPhoneNumber() == null) {
496 // Throw NPE again ...
497 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
498 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
500 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
504 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
508 * Notifies other controllers (backing beans) if a user id has been
509 * successfully converted to a User instance.
511 public void notifyControllerUserConverted () {
512 // Validate user instance
513 if (this.getUser() == null) {
515 throw new NullPointerException("this.user is null"); //NOI18N
516 } else if (this.getUser().getUserId() == null) {
518 throw new NullPointerException("this.user.userId is null"); //NOI18N
519 } else if (this.getUser().getUserId() < 1) {
521 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
525 final Contact userContact = this.getUser().getUserContact();
527 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
528 this.setContact(userContact);
530 // Set all phone instances
531 this.setPhoneInstances(userContact);
534 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
538 * Set's all given contact's phone instances: land-line, mobile and
541 * @param contact Contact to set phone instances for
543 private void setPhoneInstances (final Contact contact) {
544 // The contact must be valid
545 if (null == contact) {
547 throw new NullPointerException("contact is null"); //NOI18N
548 } else if (contact.getContactId() == null) {
550 throw new NullPointerException("contact.contactId is null"); //NOI18N
551 } else if (contact.getContactId() < 1) {
553 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
557 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
558 // Yes, then set it in admin controller
559 this.setMobileNumber(contact.getContactMobileNumber());
563 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
564 // Yes, then set it in admin controller
565 this.setLandLineNumber(contact.getContactLandLineNumber());
569 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
570 // Yes, then set it in admin controller
571 this.setFaxNumber(contact.getContactFaxNumber());