]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/helper/PizzaWebRequestHelperBean.java
a55b9919221def217cda0ce965fdd8fac2acd8df
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / helper / PizzaWebRequestHelperBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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         @Override
210         public Contact getContact () {
211                 return this.contact;
212         }
213
214         /**
215          * Setter for contact instance
216          * <p>
217          * @param contact Contact instance
218          */
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;
223         }
224
225         /**
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.
228          * <p>
229          * @param contact Contact instance to check
230          * <p>
231          * @return Message key
232          */
233         public String getContactUsageMessageKey (final Contact contact) {
234                 // The contact must be valid
235                 if (null == contact) {
236                         // Throw NPE
237                         throw new NullPointerException("contact is null"); //NOI18N
238                 } else if (contact.getContactId() == null) {
239                         // Throw again ...
240                         throw new NullPointerException("contact.contactId is null"); //NOI18N
241                 } else if (contact.getContactId() < 1) {
242                         // Not valid
243                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
244                 }
245
246                 // Default key is "unused"
247                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
248
249                 // Check user/customer contact
250                 boolean isUserContact = this.userController.isContactFound(contact);
251                 boolean isCustomerContact = this.customerController.isContactFound(contact);
252
253                 // Check user first
254                 if (isUserContact && isCustomerContact) {
255                         // Is both
256                         messageKey = "CONTACT_IS_CUSTOMER_USER"; //NOI18N
257                 } else if (isUserContact) {
258                         // Only user
259                         messageKey = "CONTACT_IS_USER"; //NOI18N
260                 } else if (isCustomerContact) {
261                         // Only customer
262                         messageKey = "CONTACT_IS_CUSTOMER"; //NOI18N
263                 }
264
265                 // Return message key
266                 return messageKey;
267         }
268
269         /**
270          * Getter for customer instance
271          * <p>
272          * @return Customer instance
273          */
274         public Customer getCustomer () {
275                 return this.customer;
276         }
277
278         /**
279          * Setter for customer instance
280          * <p>
281          * @param customer Contact instance
282          */
283         public void setCustomer (final Customer customer) {
284                 this.customer = customer;
285         }
286
287         /**
288          * Getter for dialable fax number
289          * <p>
290          * @return Dialable fax number
291          */
292         @Override
293         public DialableFaxNumber getFaxNumber () {
294                 return this.faxNumber;
295         }
296
297         /**
298          * Setter for dialable fax number
299          * <p>
300          * @param faxNumber Dialable fax number
301          */
302         public void setFaxNumber (final DialableFaxNumber faxNumber) {
303                 this.faxNumber = faxNumber;
304         }
305
306         /**
307          * Getter for dialable land-line number
308          * <p>
309          * @return Dialable land-line number
310          */
311         @Override
312         public DialableLandLineNumber getLandLineNumber () {
313                 return this.landLineNumber;
314         }
315
316         /**
317          * Setter for dialable land-line number
318          * <p>
319          * @param landLineNumber Dialable land-line number
320          */
321         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
322                 this.landLineNumber = landLineNumber;
323         }
324
325         /**
326          * Getter for dialable mobile number
327          * <p>
328          * @return Dialable mobile number
329          */
330         @Override
331         public DialableMobileNumber getMobileNumber () {
332                 return this.mobileNumber;
333         }
334
335         /**
336          * Setter for dialable mobile number
337          * <p>
338          * @param mobileNumber Dialable mobile number
339          */
340         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
341                 this.mobileNumber = mobileNumber;
342         }
343
344         /**
345          * Getter for user instance
346          * <p>
347          * @return User instance
348          */
349         @Override
350         public User getUser () {
351                 return this.user;
352         }
353
354         /**
355          * Setter for user instance
356          * <p>
357          * @param user User instance
358          */
359         public void setUser (final User user) {
360                 this.user = user;
361         }
362
363         /**
364          * Notifies other controllers (backing beans) if a contact id has been
365          * successfully converted to a Contact instance.
366          */
367         public void notifyControllerContactConverted () {
368                 // Validate contact instance
369                 if (this.getContact() == null) {
370                         // Throw NPE
371                         throw new NullPointerException("this.contact is null"); //NOI18N
372                 } else if (this.getContact().getContactId() == null) {
373                         // Throw NPE again
374                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
375                 } else if (this.getContact().getContactId() < 1) {
376                         // Not valid
377                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
378                 }
379
380                 // Set all phone instances
381                 this.setPhoneInstances(this.getContact());
382
383                 // Set all fields: user
384                 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
385         }
386
387         /**
388          * Notifies other controllers (backing beans) if a phone id has been
389          * successfully converted to a DialableFaxNumber instance.
390          */
391         public void notifyControllerFaxNumberConverted () {
392                 // Validate fax instance
393                 if (this.getFaxNumber() == null) {
394                         // Throw NPE
395                         throw new NullPointerException("this.faxNumber is null"); //NOI18N
396                 } else if (this.getFaxNumber().getPhoneId() == null) {
397                         // Throw again
398                         throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
399                 } else if (this.getFaxNumber().getPhoneId() < 1) {
400                         // Invalid id number
401                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
402                 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
403                         // Throw again
404                         throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
405                 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
406                         // Invalid id number
407                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
408                 } else if (this.getFaxNumber().getPhoneCountry() == null) {
409                         // Throw NPE again
410                         throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
411                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
412                         // ... throw again
413                         throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
414                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
415                         // Invalid id
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) {
421                         // Invalid id number
422                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
423                 }
424
425                 // Fire event
426                 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
427         }
428
429         /**
430          * Notifies other controllers (backing beans) if a phone id has been
431          * successfully converted to a DialableLandLineNumber instance.
432          */
433         public void notifyControllerLandLineNumberConverted () {
434                 // Validate land-line instance
435                 if (this.getLandLineNumber() == null) {
436                         // Throw NPE
437                         throw new NullPointerException("this.landLineNumber is null"); //NOI18N
438                 } else if (this.getLandLineNumber().getPhoneId() == null) {
439                         // Throw again
440                         throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
441                 } else if (this.getLandLineNumber().getPhoneId() < 1) {
442                         // Invalid id number
443                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
444                 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
445                         // Throw again
446                         throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
447                 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
448                         // Invalid id number
449                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
450                 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
451                         // Throw NPE again
452                         throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
453                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
454                         // ... throw again
455                         throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
456                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
457                         // Invalid id
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) {
463                         // Invalid id number
464                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
465                 }
466
467                 // Fire event
468                 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
469         }
470
471         /**
472          * Notifies other controllers (backing beans) if a phone id has been
473          * successfully converted to a DialableMobileNumber instance.
474          */
475         public void notifyControllerMobileNumberConverted () {
476                 // Validate mobile instance
477                 if (this.getMobileNumber() == null) {
478                         // Throw NPE
479                         throw new NullPointerException("this.mobileNumber is null"); //NOI18N
480                 } else if (this.getMobileNumber().getPhoneId() == null) {
481                         // Throw again
482                         throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
483                 } else if (this.getMobileNumber().getPhoneId() < 1) {
484                         // Invalid id number
485                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
486                 } else if (this.getMobileNumber().getMobileProvider() == null) {
487                         // Throw NPE again
488                         throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
489                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
490                         // ... throw again
491                         throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
492                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
493                         // Invalid id
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) {
499                         // Invalid id number
500                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
501                 }
502
503                 // Fire event
504                 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
505         }
506
507         /**
508          * Notifies other controllers (backing beans) if a user id has been
509          * successfully converted to a User instance.
510          */
511         public void notifyControllerUserConverted () {
512                 // Validate user instance
513                 if (this.getUser() == null) {
514                         // Throw NPE
515                         throw new NullPointerException("this.user is null"); //NOI18N
516                 } else if (this.getUser().getUserId() == null) {
517                         // Throw NPE again
518                         throw new NullPointerException("this.user.userId is null"); //NOI18N
519                 } else if (this.getUser().getUserId() < 1) {
520                         // Not valid
521                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
522                 }
523
524                 // Get contact
525                 final Contact userContact = this.getUser().getUserContact();
526
527                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
528                 this.setContact(userContact);
529
530                 // Set all phone instances
531                 this.setPhoneInstances(userContact);
532
533                 // Fire event
534                 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
535         }
536
537         /**
538          * Set's all given contact's phone instances: land-line, mobile and
539          * faxNumber
540          * <p>
541          * @param contact Contact to set phone instances for
542          */
543         private void setPhoneInstances (final Contact contact) {
544                 // The contact must be valid
545                 if (null == contact) {
546                         // Throw NPE
547                         throw new NullPointerException("contact is null"); //NOI18N
548                 } else if (contact.getContactId() == null) {
549                         // Throw again ...
550                         throw new NullPointerException("contact.contactId is null"); //NOI18N
551                 } else if (contact.getContactId() < 1) {
552                         // Not valid
553                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
554                 }
555
556                 // Is mobile set?
557                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
558                         // Yes, then set it in admin controller
559                         this.setMobileNumber(contact.getContactMobileNumber());
560                 }
561
562                 // Is land-line set?
563                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
564                         // Yes, then set it in admin controller
565                         this.setLandLineNumber(contact.getContactLandLineNumber());
566                 }
567
568                 // Is faxNumber set?
569                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
570                         // Yes, then set it in admin controller
571                         this.setFaxNumber(contact.getContactFaxNumber());
572                 }
573         }
574
575 }