2 * Copyright (C) 2016, 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.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.contact.Contact;
26 import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent;
27 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
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.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jphone.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.PizzaUserWebSessionController;
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 PizzaUserWebSessionController 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
209 public Contact getContact () {
214 * Setter for contact instance
216 * @param contact Contact instance
218 public void setContact (final Contact contact) {
219 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
220 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
221 this.contact = contact;
225 * Returns a message key depending on if this contact is a user and/or a
226 * contact. If this contact is unused, a default key is returned.
228 * @param contact Contact instance to check
230 * @return Message key
232 public String getContactUsageMessageKey (final Contact contact) {
233 // The contact must be valid
234 if (null == contact) {
236 throw new NullPointerException("contact is null"); //NOI18N
237 } else if (contact.getContactId() == null) {
239 throw new NullPointerException("contact.contactId is null"); //NOI18N
240 } else if (contact.getContactId() < 1) {
242 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
245 // Default key is "unused"
246 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
248 // Check user/customer contact
249 boolean isUserContact = this.userController.isContactFound(contact);
250 boolean isCustomerContact = this.customerController.isContactFound(contact);
253 if (isUserContact && isCustomerContact) {
255 messageKey = "CONTACT_IS_CUSTOMER_USER"; //NOI18N
256 } else if (isUserContact) {
258 messageKey = "CONTACT_IS_USER"; //NOI18N
259 } else if (isCustomerContact) {
261 messageKey = "CONTACT_IS_CUSTOMER"; //NOI18N
264 // Return message key
269 * Getter for customer instance
271 * @return Customer instance
273 public Customer getCustomer () {
274 return this.customer;
278 * Setter for customer instance
280 * @param customer Contact instance
282 public void setCustomer (final Customer customer) {
283 this.customer = customer;
287 * Getter for dialable fax number
289 * @return Dialable fax number
291 public DialableFaxNumber getFaxNumber () {
292 return this.faxNumber;
296 * Setter for dialable fax number
298 * @param faxNumber Dialable fax number
300 public void setFaxNumber (final DialableFaxNumber faxNumber) {
301 this.faxNumber = faxNumber;
305 * Getter for dialable land-line number
307 * @return Dialable land-line number
309 public DialableLandLineNumber getLandLineNumber () {
310 return this.landLineNumber;
314 * Setter for dialable land-line number
316 * @param landLineNumber Dialable land-line number
318 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
319 this.landLineNumber = landLineNumber;
323 * Getter for dialable mobile number
325 * @return Dialable mobile number
327 public DialableMobileNumber getMobileNumber () {
328 return this.mobileNumber;
332 * Setter for dialable mobile number
334 * @param mobileNumber Dialable mobile number
336 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
337 this.mobileNumber = mobileNumber;
341 * Getter for user instance
343 * @return User instance
345 public User getUser () {
350 * Setter for user instance
352 * @param user User instance
354 public void setUser (final User user) {
359 * Notifies other controllers (backing beans) if a contact id has been
360 * successfully converted to a Contact instance.
362 public void notifyControllerContactConverted () {
363 // Validate contact instance
364 if (this.getContact() == null) {
366 throw new NullPointerException("this.contact is null"); //NOI18N
367 } else if (this.getContact().getContactId() == null) {
369 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
370 } else if (this.getContact().getContactId() < 1) {
372 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
375 // Set all phone instances
376 this.setPhoneInstances(this.getContact());
378 // Set all fields: user
379 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
383 * Notifies other controllers (backing beans) if a phone id has been
384 * successfully converted to a DialableFaxNumber instance.
386 public void notifyControllerFaxNumberConverted () {
387 // Validate fax instance
388 if (this.getFaxNumber() == null) {
390 throw new NullPointerException("this.faxNumber is null"); //NOI18N
391 } else if (this.getFaxNumber().getPhoneId() == null) {
393 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
394 } else if (this.getFaxNumber().getPhoneId() < 1) {
396 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
397 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
399 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
400 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
402 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
403 } else if (this.getFaxNumber().getPhoneCountry() == null) {
405 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
406 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
408 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
409 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
411 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
412 } else if (this.getFaxNumber().getPhoneNumber() == null) {
413 // Throw NPE again ...
414 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
415 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
417 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
421 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
425 * Notifies other controllers (backing beans) if a phone id has been
426 * successfully converted to a DialableLandLineNumber instance.
428 public void notifyControllerLandLineNumberConverted () {
429 // Validate land-line instance
430 if (this.getLandLineNumber() == null) {
432 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
433 } else if (this.getLandLineNumber().getPhoneId() == null) {
435 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
436 } else if (this.getLandLineNumber().getPhoneId() < 1) {
438 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
439 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
441 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
442 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
444 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
445 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
447 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
448 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
450 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
451 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
453 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
454 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
455 // Throw NPE again ...
456 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
457 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
459 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
463 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
467 * Notifies other controllers (backing beans) if a phone id has been
468 * successfully converted to a DialableMobileNumber instance.
470 public void notifyControllerMobileNumberConverted () {
471 // Validate mobile instance
472 if (this.getMobileNumber() == null) {
474 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
475 } else if (this.getMobileNumber().getPhoneId() == null) {
477 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
478 } else if (this.getMobileNumber().getPhoneId() < 1) {
480 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
481 } else if (this.getMobileNumber().getMobileProvider() == null) {
483 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
484 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
486 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
487 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
489 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
490 } else if (this.getMobileNumber().getPhoneNumber() == null) {
491 // Throw NPE again ...
492 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
493 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
495 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
499 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
503 * Notifies other controllers (backing beans) if a user id has been
504 * successfully converted to a User instance.
506 public void notifyControllerUserConverted () {
507 // Validate user instance
508 if (this.getUser() == null) {
510 throw new NullPointerException("this.user is null"); //NOI18N
511 } else if (this.getUser().getUserId() == null) {
513 throw new NullPointerException("this.user.userId is null"); //NOI18N
514 } else if (this.getUser().getUserId() < 1) {
516 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
520 Contact userContact = this.getUser().getUserContact();
522 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
523 this.setContact(userContact);
525 // Set all phone instances
526 this.setPhoneInstances(userContact);
529 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
533 * Set's all given contact's phone instances: land-line, mobile and
536 * @param contact Contact to set phone instances for
538 private void setPhoneInstances (final Contact contact) {
539 // The contact must be valid
540 if (null == contact) {
542 throw new NullPointerException("contact is null"); //NOI18N
543 } else if (contact.getContactId() == null) {
545 throw new NullPointerException("contact.contactId is null"); //NOI18N
546 } else if (contact.getContactId() < 1) {
548 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
552 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
553 // Yes, then set it in admin controller
554 this.setMobileNumber(contact.getContactMobileNumber());
558 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
559 // Yes, then set it in admin controller
560 this.setLandLineNumber(contact.getContactLandLineNumber());
564 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
565 // Yes, then set it in admin controller
566 this.setFaxNumber(contact.getContactFaxNumber());