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.helper.created.HelperCreatedContactEvent;
27 import org.mxchange.jcontacts.events.contact.helper.created.ObservableHelperCreatedContactEvent;
28 import org.mxchange.jcustomercore.model.customer.Customer;
29 import org.mxchange.jphone.events.helper.fax.created.HelperCreatedFaxNumberEvent;
30 import org.mxchange.jphone.events.helper.fax.created.ObservableHelperCreatedFaxNumberEvent;
31 import org.mxchange.jphone.events.helper.landline.created.HelperCreatedLandLineNumberEvent;
32 import org.mxchange.jphone.events.helper.landline.created.ObservableHelperCreatedLandLineNumberEvent;
33 import org.mxchange.jphone.events.helper.mobile.created.HelperCreatedMobileNumberEvent;
34 import org.mxchange.jphone.events.helper.mobile.created.ObservableHelperCreatedMobileNumberEvent;
35 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
36 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
37 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
38 import org.mxchange.jusercore.events.user.helper.created.HelperCreatedUserEvent;
39 import org.mxchange.jusercore.events.user.helper.created.ObservableHelperCreatedUserEvent;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
42 import org.mxchange.pizzaapplication.beans.customer.PizzaAdminCustomerWebRequestController;
43 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
44 import org.mxchange.pizzaapplication.beans.phone.PizzaAdminPhoneWebRequestController;
45 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
48 * A general helper for beans
50 * @author Roland Häder<roland@mxchange.org>
54 public class PizzaWebRequestHelperBean implements PizzaWebRequestHelperController {
57 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
59 private static final short THREAD_STACK = 5;
64 private static final long serialVersionUID = 17_258_793_567_145_701L;
67 * Administrative contact controller
70 private PizzaAdminContactWebRequestController adminContactController;
73 * Administrative user controller
76 private PizzaAdminCustomerWebRequestController adminCustomerController;
79 * Administrative phone controller
82 private PizzaAdminPhoneWebRequestController adminPhoneController;
87 private Contact contact;
90 * Event for when a contact instance was created
94 private Event<ObservableHelperCreatedContactEvent> contactCreatedEvent;
99 private Customer customer;
102 <<<<<<< HEAD:src/java/org/mxchange/pizzaapplication/beans/helper/PizzaWebRequestHelperBean.java
103 * General user controller
106 private PizzaCustomerWebSessionController customerController;
111 private DialableFaxNumber faxNumber;
114 * Event for when a fax number instance was created
118 private Event<ObservableHelperCreatedFaxNumberEvent> faxNumberCreatedEvent;
123 private DialableLandLineNumber landLineNumber;
126 * Event for when a land-line number instance was created
130 private Event<ObservableHelperCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
135 private DialableMobileNumber mobileNumber;
138 * Event for when a mobile number instance was created
142 private Event<ObservableHelperCreatedMobileNumberEvent> mobileNumberCreatedEvent;
150 * Regular user controller
153 private PizzaUserWebSessionController userController;
156 * Event for when a fax number instance was created
160 private Event<ObservableHelperCreatedFaxNumberEvent> faxNumberCreatedEvent;
163 * Event for when a land-line number instance was created
167 private Event<ObservableHelperCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
170 * Event for when a mobile number instance was created
174 private Event<ObservableHelperCreatedMobileNumberEvent> mobileNumberCreatedEvent;
177 * Regular user controller
180 private TrainingsUserWebSessionController userController;
183 * Event for when a user instance was created
187 private Event<ObservableHelperCreatedUserEvent> userCreatedEvent;
190 * Default constructor
192 public PizzaWebRequestHelperBean () {
193 // Call super constructor
196 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
197 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
201 * Copies currently set contact instances data to adminContactController
203 public void copyContactToController () {
204 // Validate user instance
205 if (this.getContact() == null) {
207 throw new NullPointerException("this.contact is null"); //NOI18N
208 } else if (this.getContact().getContactId() == null) {
210 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
211 } else if (this.getContact().getContactId() < 1) {
213 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
216 // Set all phone instances
217 this.setPhoneInstances(this.getContact());
219 // Set all fields: user
220 this.contactCreatedEvent.fire(new HelperCreatedContactEvent(this.getContact()));
224 public void copyCustomerToController () {
225 // Validate user instance
226 if (this.getCustomer() == null) {
228 throw new NullPointerException("this.customer is null"); //NOI18N
229 } else if (this.getCustomer().getCustomerId() == null) {
231 throw new NullPointerException("this.customer.customerId is null"); //NOI18N
232 } else if (this.getCustomer().getCustomerId() < 1) {
234 throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getCustomer().getCustomerId())); //NOI18N
237 // Set all fields: user
238 this.adminCustomerController.copyCustomerToController(this.getCustomer());
242 * Copies currently set fax number's data to admin phone controller
244 public void copyFaxNumberToController () {
245 // Validate fax instance
246 if (this.getFaxNumber() == null) {
248 throw new NullPointerException("this.faxNumber is null"); //NOI18N
249 } else if (this.getFaxNumber().getPhoneId() == null) {
251 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
252 } else if (this.getFaxNumber().getPhoneId() < 1) {
254 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
255 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
257 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
258 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
260 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
261 } else if (this.getFaxNumber().getPhoneCountry() == null) {
263 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
264 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
266 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
267 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
269 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
270 } else if (this.getFaxNumber().getPhoneNumber() == null) {
271 // Throw NPE again ...
272 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
273 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
275 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
279 this.faxNumberCreatedEvent.fire(new HelperCreatedFaxNumberEvent(this.getFaxNumber()));
283 * Copies currently set land-line number's data to admin phone controller
285 public void copyLandLineNumberToController () {
286 // Validate land-line instance
287 if (this.getLandLineNumber() == null) {
289 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
290 } else if (this.getLandLineNumber().getPhoneId() == null) {
292 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
293 } else if (this.getLandLineNumber().getPhoneId() < 1) {
295 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
296 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
298 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
299 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
301 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
302 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
304 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
305 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
307 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
308 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
310 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
311 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
312 // Throw NPE again ...
313 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
314 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
316 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
320 this.landLineNumberCreatedEvent.fire(new HelperCreatedLandLineNumberEvent(this.getLandLineNumber()));
324 * Copies currently set mobile number's data to admin phone controller
326 public void copyMobileNumberToController () {
327 // Validate mobile instance
328 if (this.getMobileNumber() == null) {
330 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
331 } else if (this.getMobileNumber().getPhoneId() == null) {
333 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
334 } else if (this.getMobileNumber().getPhoneId() < 1) {
336 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
337 } else if (this.getMobileNumber().getMobileProvider() == null) {
339 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
340 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
342 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
343 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
345 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
346 } else if (this.getMobileNumber().getPhoneNumber() == null) {
347 // Throw NPE again ...
348 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
349 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
351 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
355 this.mobileNumberCreatedEvent.fire(new HelperCreatedMobileNumberEvent(this.getMobileNumber()));
359 * Copies currently set user instances data to adminUserController
361 public void copyUserToController () {
362 // Validate user instance
363 if (this.getUser() == null) {
365 throw new NullPointerException("this.user is null"); //NOI18N
366 } else if (this.getUser().getUserId() == null) {
368 throw new NullPointerException("this.user.userId is null"); //NOI18N
369 } else if (this.getUser().getUserId() < 1) {
371 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
375 Contact userContact = this.getUser().getUserContact();
377 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
378 this.setContact(userContact);
380 // Set all phone instances
381 this.setPhoneInstances(userContact);
384 this.userCreatedEvent.fire(new HelperCreatedUserEvent(this.getUser()));
388 * Getter for contact instance
390 * @return Contact instance
392 public Contact getContact () {
397 * Setter for contact instance
399 * @param contact Contact instance
401 public void setContact (final Contact contact) {
402 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
403 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
404 this.contact = contact;
408 * Returns a message key depending on if this contact is a user and/or a
409 * contact. If this contact is unused, a default key is returned.
411 * @param contact Contact instance to check
413 * @return Message key
415 public String getContactUsageMessageKey (final Contact contact) {
416 // The contact must be valid
417 if (null == contact) {
419 throw new NullPointerException("contact is null"); //NOI18N
420 } else if (contact.getContactId() == null) {
422 throw new NullPointerException("contact.contactId is null"); //NOI18N
423 } else if (contact.getContactId() < 1) {
425 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
428 // Default key is "unused"
429 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
431 // Check user/customer contact
432 boolean isUserContact = this.userController.isContactFound(contact);
433 boolean isCustomerContact = this.customerController.isContactFound(contact);
436 if (isUserContact && isCustomerContact) {
438 messageKey = "CONTACT_IS_CUSTOMER_USER"; //NOI18N
439 } else if (isUserContact) {
441 messageKey = "CONTACT_IS_USER"; //NOI18N
442 } else if (isCustomerContact) {
444 messageKey = "CONTACT_IS_CUSTOMER"; //NOI18N
447 // Return message key
452 public Customer getCustomer () {
453 return this.customer;
457 public void setCustomer (final Customer customer) {
458 this.customer = customer;
462 * Getter for dialable fax number
464 * @return Dialable fax number
466 public DialableFaxNumber getFaxNumber () {
467 return this.faxNumber;
471 * Setter for dialable fax number
473 * @param faxNumber Dialable fax number
475 public void setFaxNumber (final DialableFaxNumber faxNumber) {
476 this.faxNumber = faxNumber;
480 * Getter for dialable land-line number
482 * @return Dialable land-line number
484 public DialableLandLineNumber getLandLineNumber () {
485 return this.landLineNumber;
489 * Setter for dialable land-line number
491 * @param landLineNumber Dialable land-line number
493 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
494 this.landLineNumber = landLineNumber;
498 * Getter for dialable mobile number
500 * @return Dialable mobile number
502 public DialableMobileNumber getMobileNumber () {
503 return this.mobileNumber;
507 * Setter for dialable mobile number
509 * @param mobileNumber Dialable mobile number
511 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
512 this.mobileNumber = mobileNumber;
516 * Set's all given contact's phone instances: land-line, cellphone and fax
518 * @param contact Contact to set phone instances for
520 private void setPhoneInstances (final Contact contact) {
521 // The contact must be valid
522 if (null == contact) {
524 throw new NullPointerException("contact is null"); //NOI18N
525 } else if (contact.getContactId() == null) {
527 throw new NullPointerException("contact.contactId is null"); //NOI18N
528 } else if (contact.getContactId() < 1) {
530 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
534 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
535 // Yes, then set it in admin controller
536 this.adminPhoneController.setMobileNumber(contact.getContactMobileNumber());
540 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
541 // Yes, then set it in admin controller
542 this.adminPhoneController.setLandLineNumber(contact.getContactLandLineNumber());
546 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
547 // Yes, then set it in admin controller
548 this.adminPhoneController.setFaxNumber(contact.getContactFaxNumber());