]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelperBean.java
Please cherry-pick:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / helper / AddressbookWebRequestHelperBean.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.addressbook.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.addressbook.beans.contact.AddressbookAdminContactWebRequestController;
26 import org.mxchange.addressbook.beans.phone.AddressbookAdminPhoneWebRequestController;
27 import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController;
28 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
29 import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent;
30 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
31 import org.mxchange.jcontacts.model.contact.Contact;
32 import org.mxchange.jphone.events.fax.created.CreatedFaxNumberEvent;
33 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
34 import org.mxchange.jphone.events.landline.created.CreatedLandLineNumberEvent;
35 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
36 import org.mxchange.jphone.events.mobile.created.CreatedMobileNumberEvent;
37 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
38 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
41 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
42 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
43 import org.mxchange.jusercore.model.user.User;
44
45 /**
46  * A general helper for beans
47  * <p>
48  * @author Roland Häder<roland@mxchange.org>
49  */
50 @Named ("beanHelper")
51 @RequestScoped
52 public class AddressbookWebRequestHelperBean implements AddressbookWebRequestHelperController {
53
54         /**
55          * Call-stack instance (5 may show BeanELResolver.getValue as caller)
56          */
57         private static final short THREAD_STACK = 5;
58
59         /**
60          * Serial number
61          */
62         private static final long serialVersionUID = 17_258_793_567_145_701L;
63
64         /**
65          * Administrative contact controller
66          */
67         @Inject
68         private AddressbookAdminContactWebRequestController adminContactController;
69
70         /**
71          * Administrative phone controller
72          */
73         @Inject
74         private AddressbookAdminPhoneWebRequestController adminPhoneController;
75
76         /**
77          * Administrative user controller
78          */
79         @Inject
80         private AddressbookAdminUserWebRequestController adminUserController;
81
82         /**
83          * Contact instance
84          */
85         private Contact contact;
86
87         /**
88          * Event for when a contact instance was created
89          */
90         @Any
91         @Inject
92         private Event<ObservableCreatedContactEvent> contactCreatedEvent;
93
94         /**
95          * Fax number
96          */
97         private DialableFaxNumber faxNumber;
98
99         /**
100          * Event for when a fax number instance was created
101          */
102         @Any
103         @Inject
104         private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
105
106         /**
107          * Land-line number
108          */
109         private DialableLandLineNumber landLineNumber;
110
111         /**
112          * Event for when a land-line number instance was created
113          */
114         @Any
115         @Inject
116         private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
117
118         /**
119          * Mobile number
120          */
121         private DialableMobileNumber mobileNumber;
122
123         /**
124          * Event for when a mobile number instance was created
125          */
126         @Any
127         @Inject
128         private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
129
130         /**
131          * User instance
132          */
133         private User user;
134
135         /**
136          * Regular user controller
137          */
138         @Inject
139         private AddressbookUserWebSessionController userController;
140
141         /**
142          * Event for when a user instance was created
143          */
144         @Any
145         @Inject
146         private Event<ObservableCreatedUserEvent> userCreatedEvent;
147
148         /**
149          * Default constructor
150          */
151         public AddressbookWebRequestHelperBean () {
152                 // Call super constructor
153                 super();
154
155                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
156                 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
157         }
158
159         /**
160          * Getter for contact instance
161          * <p>
162          * @return Contact instance
163          */
164         public Contact getContact () {
165                 return this.contact;
166         }
167
168         /**
169          * Setter for contact instance
170          * <p>
171          * @param contact Contact instance
172          */
173         public void setContact (final Contact contact) {
174                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
175                 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
176                 this.contact = contact;
177         }
178
179         /**
180          * Returns a message key depending on if this contact is a user and/or a
181          * contact. If this contact is unused, a default key is returned.
182          * <p>
183          * @param contact Contact instance to check
184          * <p>
185          * @return Message key
186          */
187         public String getContactUsageMessageKey (final Contact contact) {
188                 // The contact must be valid
189                 if (null == contact) {
190                         // Throw NPE
191                         throw new NullPointerException("contact is null"); //NOI18N
192                 } else if (contact.getContactId() == null) {
193                         // Throw again ...
194                         throw new NullPointerException("contact.contactId is null"); //NOI18N
195                 } else if (contact.getContactId() < 1) {
196                         // Not valid
197                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
198                 }
199
200                 // Default key is "unused"
201                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
202
203                 // Check user contact
204                 boolean isUserContact = this.userController.isContactFound(contact);
205
206                 // Check user first
207                 if (isUserContact) {
208                         // Only user
209                         messageKey = "CONTACT_IS_USER"; //NOI18N
210                 }
211
212                 // Return message key
213                 return messageKey;
214         }
215
216         /**
217          * Getter for dialable fax number
218          * <p>
219          * @return Dialable fax number
220          */
221         public DialableFaxNumber getFaxNumber () {
222                 return this.faxNumber;
223         }
224
225         /**
226          * Setter for dialable fax number
227          * <p>
228          * @param faxNumber Dialable fax number
229          */
230         public void setFaxNumber (final DialableFaxNumber faxNumber) {
231                 this.faxNumber = faxNumber;
232         }
233
234         /**
235          * Getter for dialable land-line number
236          * <p>
237          * @return Dialable land-line number
238          */
239         public DialableLandLineNumber getLandLineNumber () {
240                 return this.landLineNumber;
241         }
242
243         /**
244          * Setter for dialable land-line number
245          * <p>
246          * @param landLineNumber Dialable land-line number
247          */
248         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
249                 this.landLineNumber = landLineNumber;
250         }
251
252         /**
253          * Getter for dialable mobile number
254          * <p>
255          * @return Dialable mobile number
256          */
257         public DialableMobileNumber getMobileNumber () {
258                 return this.mobileNumber;
259         }
260
261         /**
262          * Setter for dialable mobile number
263          * <p>
264          * @param mobileNumber Dialable mobile number
265          */
266         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
267                 this.mobileNumber = mobileNumber;
268         }
269
270         /**
271          * Getter for user instance
272          * <p>
273          * @return User instance
274          */
275         public User getUser () {
276                 return this.user;
277         }
278
279         /**
280          * Setter for user instance
281          * <p>
282          * @param user User instance
283          */
284         public void setUser (final User user) {
285                 this.user = user;
286         }
287
288         /**
289          * Notifies other controllers (backing beans) if a contact id has been
290          * successfully converted to a Contact instance.
291          */
292         public void notifyControllerContactConverted () {
293                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
294                 // System.out.println(MessageFormat.format("{0}.notifyControllerContactConverted: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
295
296                 // Validate contact instance
297                 if (this.getContact() == null) {
298                         // Throw NPE
299                         throw new NullPointerException("this.contact is null"); //NOI18N
300                 } else if (this.getContact().getContactId() == null) {
301                         // Throw NPE again
302                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
303                 } else if (this.getContact().getContactId() < 1) {
304                         // Not valid
305                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
306                 }
307
308                 // Set all phone instances
309                 this.setPhoneInstances(this.getContact());
310
311                 // Set all fields: user
312                 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
313         }
314
315         /**
316          * Notifies other controllers (backing beans) if a phone id has been
317          * successfully converted to a DialableFaxNumber instance.
318          */
319         public void notifyControllerFaxNumberConverted () {
320                 // Validate fax instance
321                 if (this.getFaxNumber() == null) {
322                         // Throw NPE
323                         throw new NullPointerException("this.faxNumber is null"); //NOI18N
324                 } else if (this.getFaxNumber().getPhoneId() == null) {
325                         // Throw again
326                         throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
327                 } else if (this.getFaxNumber().getPhoneId() < 1) {
328                         // Invalid id number
329                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
330                 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
331                         // Throw again
332                         throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
333                 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
334                         // Invalid id number
335                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
336                 } else if (this.getFaxNumber().getPhoneCountry() == null) {
337                         // Throw NPE again
338                         throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
339                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
340                         // ... throw again
341                         throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
342                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
343                         // Invalid id
344                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
345                 } else if (this.getFaxNumber().getPhoneNumber() == null) {
346                         // Throw NPE again ...
347                         throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
348                 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
349                         // Invalid id number
350                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
351                 }
352
353                 // Fire event
354                 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
355         }
356
357         /**
358          * Notifies other controllers (backing beans) if a phone id has been
359          * successfully converted to a DialableLandLineNumber instance.
360          */
361         public void notifyControllerLandLineNumberConverted () {
362                 // Validate land-line instance
363                 if (this.getLandLineNumber() == null) {
364                         // Throw NPE
365                         throw new NullPointerException("this.landLineNumber is null"); //NOI18N
366                 } else if (this.getLandLineNumber().getPhoneId() == null) {
367                         // Throw again
368                         throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
369                 } else if (this.getLandLineNumber().getPhoneId() < 1) {
370                         // Invalid id number
371                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
372                 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
373                         // Throw again
374                         throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
375                 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
376                         // Invalid id number
377                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
378                 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
379                         // Throw NPE again
380                         throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
381                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
382                         // ... throw again
383                         throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
384                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
385                         // Invalid id
386                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
387                 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
388                         // Throw NPE again ...
389                         throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
390                 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
391                         // Invalid id number
392                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
393                 }
394
395                 // Fire event
396                 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
397         }
398
399         /**
400          * Notifies other controllers (backing beans) if a phone id has been
401          * successfully converted to a DialableMobileNumber instance.
402          */
403         public void notifyControllerMobileNumberConverted () {
404                 // Validate mobile instance
405                 if (this.getMobileNumber() == null) {
406                         // Throw NPE
407                         throw new NullPointerException("this.mobileNumber is null"); //NOI18N
408                 } else if (this.getMobileNumber().getPhoneId() == null) {
409                         // Throw again
410                         throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
411                 } else if (this.getMobileNumber().getPhoneId() < 1) {
412                         // Invalid id number
413                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
414                 } else if (this.getMobileNumber().getMobileProvider() == null) {
415                         // Throw NPE again
416                         throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
417                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
418                         // ... throw again
419                         throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
420                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
421                         // Invalid id
422                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
423                 } else if (this.getMobileNumber().getPhoneNumber() == null) {
424                         // Throw NPE again ...
425                         throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
426                 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
427                         // Invalid id number
428                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
429                 }
430
431                 // Fire event
432                 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
433         }
434
435         /**
436          * Notifies other controllers (backing beans) if a user id has been
437          * successfully converted to a User instance.
438          */
439         public void notifyControllerUserConverted () {
440                 // Log message
441                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::notifyControllerUserConverted - CALLED!"); //NOI18N
442
443                 // Validate user instance
444                 if (this.getUser() == null) {
445                         // Throw NPE
446                         throw new NullPointerException("this.user is null"); //NOI18N
447                 } else if (this.getUser().getUserId() == null) {
448                         // Throw NPE again
449                         throw new NullPointerException("this.user.userId is null"); //NOI18N
450                 } else if (this.getUser().getUserId() < 1) {
451                         // Not valid
452                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
453                 }
454
455                 // Get contact
456                 Contact userContact = this.getUser().getUserContact();
457
458                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
459                 this.setContact(userContact);
460
461                 // Set all phone instances
462                 this.setPhoneInstances(userContact);
463
464                 // Fire event
465                 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
466         }
467
468         /**
469          * Set's all given contact's phone instances: land-line, mobile and
470          * faxNumber
471          * <p>
472          * @param contact Contact to set phone instances for
473          */
474         private void setPhoneInstances (final Contact contact) {
475                 // The contact must be valid
476                 if (null == contact) {
477                         // Throw NPE
478                         throw new NullPointerException("contact is null"); //NOI18N
479                 } else if (contact.getContactId() == null) {
480                         // Throw again ...
481                         throw new NullPointerException("contact.contactId is null"); //NOI18N
482                 } else if (contact.getContactId() < 1) {
483                         // Not valid
484                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
485                 }
486
487                 // Is mobile set?
488                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
489                         // Yes, then set it in admin controller
490                         this.setMobileNumber(contact.getContactMobileNumber());
491                 }
492
493                 // Is land-line set?
494                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
495                         // Yes, then set it in admin controller
496                         this.setLandLineNumber(contact.getContactLandLineNumber());
497                 }
498
499                 // Is faxNumber set?
500                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
501                         // Yes, then set it in admin controller
502                         this.setFaxNumber(contact.getContactFaxNumber());
503                 }
504         }
505
506 }