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