]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/helper/PizzaWebRequestHelperBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / helper / PizzaWebRequestHelperBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.beans.helper;
18
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;
48
49 /**
50  * A general helper for beans
51  * <p>
52  * @author Roland Häder<roland@mxchange.org>
53  */
54 @Named ("beanHelper")
55 @RequestScoped
56 public class PizzaWebRequestHelperBean implements PizzaWebRequestHelperController {
57
58         /**
59          * Call-stack instance (5 may show BeanELResolver.getValue as caller)
60          */
61         private static final short THREAD_STACK = 5;
62
63         /**
64          * Serial number
65          */
66         private static final long serialVersionUID = 17_258_793_567_145_701L;
67
68         /**
69          * Administrative contact controller
70          */
71         @Inject
72         private PizzaAdminContactWebRequestController adminContactController;
73
74         /**
75          * Administrative user controller
76          */
77         @Inject
78         private PizzaAdminCustomerWebRequestController adminCustomerController;
79
80         /**
81          * Administrative phone controller
82          */
83         @Inject
84         private PizzaAdminPhoneWebRequestController adminPhoneController;
85
86         /**
87          * Contact instance
88          */
89         private Contact contact;
90
91         /**
92          * Event for when a contact instance was created
93          */
94         @Any
95         @Inject
96         private Event<ObservableCreatedContactEvent> contactCreatedEvent;
97
98         /**
99          * Customer instance
100          */
101         private Customer customer;
102
103         /**
104          * General customer controller
105          */
106         @Inject
107         private PizzaCustomerWebSessionController customerController;
108
109         /**
110          * An event being fired when a customer has been created
111          */
112         @Any
113         @Inject
114         private Event<ObservableCreatedCustomerEvent> customerCreatedEvent;
115
116         /**
117          * Fax number
118          */
119         private DialableFaxNumber faxNumber;
120
121         /**
122          * Event for when a fax number instance was created
123          */
124         @Any
125         @Inject
126         private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
127
128         /**
129          * Land-line number
130          */
131         private DialableLandLineNumber landLineNumber;
132
133         /**
134          * Event for when a land-line number instance was created
135          */
136         @Any
137         @Inject
138         private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
139
140         /**
141          * Mobile number
142          */
143         private DialableMobileNumber mobileNumber;
144
145         /**
146          * Event for when a mobile number instance was created
147          */
148         @Any
149         @Inject
150         private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
151
152         /**
153          * User instance
154          */
155         private User user;
156
157         /**
158          * Regular user controller
159          */
160         @Inject
161         private PizzaUserWebRequestController userController;
162
163         /**
164          * Event for when a user instance was created
165          */
166         @Any
167         @Inject
168         private Event<ObservableCreatedUserEvent> userCreatedEvent;
169
170         /**
171          * Default constructor
172          */
173         public PizzaWebRequestHelperBean () {
174                 // Call super constructor
175                 super();
176
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));
179         }
180
181         /**
182          * Copies currently set customer instance's data to adminCustomerController
183          */
184         public void copyCustomerToController () {
185                 // Validate customer instance
186                 if (this.getCustomer() == null) {
187                         // Throw NPE
188                         throw new NullPointerException("this.customer is null"); //NOI18N
189                 } else if (this.getCustomer().getCustomerId() == null) {
190                         // Throw NPE again
191                         throw new NullPointerException("this.customer.customerId is null"); //NOI18N
192                 } else if (this.getContact().getContactId() < 1) {
193                         // Not valid
194                         throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getContact().getContactId())); //NOI18N
195                 }
196
197                 // Set all phone instances
198                 this.setPhoneInstances(this.getContact());
199
200                 // Set all fields: user
201                 this.customerCreatedEvent.fire(new CreatedCustomerEvent(this.getCustomer()));
202         }
203
204         /**
205          * Getter for contact instance
206          * <p>
207          * @return Contact instance
208          */
209         public Contact getContact () {
210                 return this.contact;
211         }
212
213         /**
214          * Setter for contact instance
215          * <p>
216          * @param contact Contact instance
217          */
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;
222         }
223
224         /**
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.
227          * <p>
228          * @param contact Contact instance to check
229          * <p>
230          * @return Message key
231          */
232         public String getContactUsageMessageKey (final Contact contact) {
233                 // The contact must be valid
234                 if (null == contact) {
235                         // Throw NPE
236                         throw new NullPointerException("contact is null"); //NOI18N
237                 } else if (contact.getContactId() == null) {
238                         // Throw again ...
239                         throw new NullPointerException("contact.contactId is null"); //NOI18N
240                 } else if (contact.getContactId() < 1) {
241                         // Not valid
242                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
243                 }
244
245                 // Default key is "unused"
246                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
247
248                 // Check user/customer contact
249                 boolean isUserContact = this.userController.isContactFound(contact);
250                 boolean isCustomerContact = this.customerController.isContactFound(contact);
251
252                 // Check user first
253                 if (isUserContact && isCustomerContact) {
254                         // Is both
255                         messageKey = "CONTACT_IS_CUSTOMER_USER"; //NOI18N
256                 } else if (isUserContact) {
257                         // Only user
258                         messageKey = "CONTACT_IS_USER"; //NOI18N
259                 } else if (isCustomerContact) {
260                         // Only customer
261                         messageKey = "CONTACT_IS_CUSTOMER"; //NOI18N
262                 }
263
264                 // Return message key
265                 return messageKey;
266         }
267
268         /**
269          * Getter for customer instance
270          * <p>
271          * @return Customer instance
272          */
273         public Customer getCustomer () {
274                 return this.customer;
275         }
276
277         /**
278          * Setter for customer instance
279          * <p>
280          * @param customer Contact instance
281          */
282         public void setCustomer (final Customer customer) {
283                 this.customer = customer;
284         }
285
286         /**
287          * Getter for dialable fax number
288          * <p>
289          * @return Dialable fax number
290          */
291         public DialableFaxNumber getFaxNumber () {
292                 return this.faxNumber;
293         }
294
295         /**
296          * Setter for dialable fax number
297          * <p>
298          * @param faxNumber Dialable fax number
299          */
300         public void setFaxNumber (final DialableFaxNumber faxNumber) {
301                 this.faxNumber = faxNumber;
302         }
303
304         /**
305          * Getter for dialable land-line number
306          * <p>
307          * @return Dialable land-line number
308          */
309         public DialableLandLineNumber getLandLineNumber () {
310                 return this.landLineNumber;
311         }
312
313         /**
314          * Setter for dialable land-line number
315          * <p>
316          * @param landLineNumber Dialable land-line number
317          */
318         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
319                 this.landLineNumber = landLineNumber;
320         }
321
322         /**
323          * Getter for dialable mobile number
324          * <p>
325          * @return Dialable mobile number
326          */
327         public DialableMobileNumber getMobileNumber () {
328                 return this.mobileNumber;
329         }
330
331         /**
332          * Setter for dialable mobile number
333          * <p>
334          * @param mobileNumber Dialable mobile number
335          */
336         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
337                 this.mobileNumber = mobileNumber;
338         }
339
340         /**
341          * Getter for user instance
342          * <p>
343          * @return User instance
344          */
345         public User getUser () {
346                 return this.user;
347         }
348
349         /**
350          * Setter for user instance
351          * <p>
352          * @param user User instance
353          */
354         public void setUser (final User user) {
355                 this.user = user;
356         }
357
358         /**
359          * Notifies other controllers (backing beans) if a contact id has been
360          * successfully converted to a Contact instance.
361          */
362         public void notifyControllerContactConverted () {
363                 // Validate contact instance
364                 if (this.getContact() == null) {
365                         // Throw NPE
366                         throw new NullPointerException("this.contact is null"); //NOI18N
367                 } else if (this.getContact().getContactId() == null) {
368                         // Throw NPE again
369                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
370                 } else if (this.getContact().getContactId() < 1) {
371                         // Not valid
372                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
373                 }
374
375                 // Set all phone instances
376                 this.setPhoneInstances(this.getContact());
377
378                 // Set all fields: user
379                 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
380         }
381
382         /**
383          * Notifies other controllers (backing beans) if a phone id has been
384          * successfully converted to a DialableFaxNumber instance.
385          */
386         public void notifyControllerFaxNumberConverted () {
387                 // Validate fax instance
388                 if (this.getFaxNumber() == null) {
389                         // Throw NPE
390                         throw new NullPointerException("this.faxNumber is null"); //NOI18N
391                 } else if (this.getFaxNumber().getPhoneId() == null) {
392                         // Throw again
393                         throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
394                 } else if (this.getFaxNumber().getPhoneId() < 1) {
395                         // Invalid id number
396                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
397                 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
398                         // Throw again
399                         throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
400                 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
401                         // Invalid id number
402                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
403                 } else if (this.getFaxNumber().getPhoneCountry() == null) {
404                         // Throw NPE again
405                         throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
406                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
407                         // ... throw again
408                         throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
409                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
410                         // Invalid id
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) {
416                         // Invalid id number
417                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
418                 }
419
420                 // Fire event
421                 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
422         }
423
424         /**
425          * Notifies other controllers (backing beans) if a phone id has been
426          * successfully converted to a DialableLandLineNumber instance.
427          */
428         public void notifyControllerLandLineNumberConverted () {
429                 // Validate land-line instance
430                 if (this.getLandLineNumber() == null) {
431                         // Throw NPE
432                         throw new NullPointerException("this.landLineNumber is null"); //NOI18N
433                 } else if (this.getLandLineNumber().getPhoneId() == null) {
434                         // Throw again
435                         throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
436                 } else if (this.getLandLineNumber().getPhoneId() < 1) {
437                         // Invalid id number
438                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
439                 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
440                         // Throw again
441                         throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
442                 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
443                         // Invalid id number
444                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
445                 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
446                         // Throw NPE again
447                         throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
448                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
449                         // ... throw again
450                         throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
451                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
452                         // Invalid id
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) {
458                         // Invalid id number
459                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
460                 }
461
462                 // Fire event
463                 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
464         }
465
466         /**
467          * Notifies other controllers (backing beans) if a phone id has been
468          * successfully converted to a DialableMobileNumber instance.
469          */
470         public void notifyControllerMobileNumberConverted () {
471                 // Validate mobile instance
472                 if (this.getMobileNumber() == null) {
473                         // Throw NPE
474                         throw new NullPointerException("this.mobileNumber is null"); //NOI18N
475                 } else if (this.getMobileNumber().getPhoneId() == null) {
476                         // Throw again
477                         throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
478                 } else if (this.getMobileNumber().getPhoneId() < 1) {
479                         // Invalid id number
480                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
481                 } else if (this.getMobileNumber().getMobileProvider() == null) {
482                         // Throw NPE again
483                         throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
484                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
485                         // ... throw again
486                         throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
487                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
488                         // Invalid id
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) {
494                         // Invalid id number
495                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
496                 }
497
498                 // Fire event
499                 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
500         }
501
502         /**
503          * Notifies other controllers (backing beans) if a user id has been
504          * successfully converted to a User instance.
505          */
506         public void notifyControllerUserConverted () {
507                 // Validate user instance
508                 if (this.getUser() == null) {
509                         // Throw NPE
510                         throw new NullPointerException("this.user is null"); //NOI18N
511                 } else if (this.getUser().getUserId() == null) {
512                         // Throw NPE again
513                         throw new NullPointerException("this.user.userId is null"); //NOI18N
514                 } else if (this.getUser().getUserId() < 1) {
515                         // Not valid
516                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
517                 }
518
519                 // Get contact
520                 final Contact userContact = this.getUser().getUserContact();
521
522                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
523                 this.setContact(userContact);
524
525                 // Set all phone instances
526                 this.setPhoneInstances(userContact);
527
528                 // Fire event
529                 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
530         }
531
532         /**
533          * Set's all given contact's phone instances: land-line, mobile and
534          * faxNumber
535          * <p>
536          * @param contact Contact to set phone instances for
537          */
538         private void setPhoneInstances (final Contact contact) {
539                 // The contact must be valid
540                 if (null == contact) {
541                         // Throw NPE
542                         throw new NullPointerException("contact is null"); //NOI18N
543                 } else if (contact.getContactId() == null) {
544                         // Throw again ...
545                         throw new NullPointerException("contact.contactId is null"); //NOI18N
546                 } else if (contact.getContactId() < 1) {
547                         // Not valid
548                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
549                 }
550
551                 // Is mobile set?
552                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
553                         // Yes, then set it in admin controller
554                         this.setMobileNumber(contact.getContactMobileNumber());
555                 }
556
557                 // Is land-line set?
558                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
559                         // Yes, then set it in admin controller
560                         this.setLandLineNumber(contact.getContactLandLineNumber());
561                 }
562
563                 // Is faxNumber set?
564                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
565                         // Yes, then set it in admin controller
566                         this.setFaxNumber(contact.getContactFaxNumber());
567                 }
568         }
569
570 }