]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/helper/AddressbookWebRequestHelperBean.java
WIP: 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.contact.Contact;
30 import org.mxchange.jcontacts.events.contact.helper.created.HelperCreatedContactEvent;
31 import org.mxchange.jcontacts.events.contact.helper.created.ObservableHelperCreatedContactEvent;
32 import org.mxchange.jphone.events.helper.fax.created.HelperCreatedFaxNumberEvent;
33 import org.mxchange.jphone.events.helper.fax.created.ObservableHelperCreatedFaxNumberEvent;
34 import org.mxchange.jphone.events.helper.landline.created.HelperCreatedLandLineNumberEvent;
35 import org.mxchange.jphone.events.helper.landline.created.ObservableHelperCreatedLandLineNumberEvent;
36 import org.mxchange.jphone.events.helper.mobile.created.HelperCreatedMobileNumberEvent;
37 import org.mxchange.jphone.events.helper.mobile.created.ObservableHelperCreatedMobileNumberEvent;
38 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
41 import org.mxchange.jusercore.events.user.helper.created.HelperCreatedUserEvent;
42 import org.mxchange.jusercore.events.user.helper.created.ObservableHelperCreatedUserEvent;
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<ObservableHelperCreatedContactEvent> 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<ObservableHelperCreatedFaxNumberEvent> 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<ObservableHelperCreatedLandLineNumberEvent> 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<ObservableHelperCreatedMobileNumberEvent> 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<ObservableHelperCreatedUserEvent> 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          * Copies currently set contact instances data to adminContactController
161          */
162         public void copyContactToController () {
163                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
164                 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
165
166                 // Validate contact instance
167                 if (this.getContact() == null) {
168                         // Throw NPE
169                         throw new NullPointerException("this.contact is null"); //NOI18N
170                 } else if (this.getContact().getContactId() == null) {
171                         // Throw NPE again
172                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
173                 } else if (this.getContact().getContactId() < 1) {
174                         // Not valid
175                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
176                 }
177
178                 // Set all phone instances
179                 this.setPhoneInstances(this.getContact());
180
181                 // Set all fields: user
182                 this.contactCreatedEvent.fire(new HelperCreatedContactEvent(this.getContact()));
183         }
184
185         /**
186          * Copies currently set fax number's data to admin phone controller
187          */
188         public void copyFaxNumberToController () {
189                 // Validate fax instance
190                 if (this.getFaxNumber() == null) {
191                         // Throw NPE
192                         throw new NullPointerException("this.faxNumber is null"); //NOI18N
193                 } else if (this.getFaxNumber().getPhoneId() == null) {
194                         // Throw again
195                         throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
196                 } else if (this.getFaxNumber().getPhoneId() < 1) {
197                         // Invalid id number
198                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
199                 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
200                         // Throw again
201                         throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
202                 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
203                         // Invalid id number
204                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
205                 } else if (this.getFaxNumber().getPhoneCountry() == null) {
206                         // Throw NPE again
207                         throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
208                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
209                         // ... throw again
210                         throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
211                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
212                         // Invalid id
213                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
214                 } else if (this.getFaxNumber().getPhoneNumber() == null) {
215                         // Throw NPE again ...
216                         throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
217                 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
218                         // Invalid id number
219                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
220                 }
221
222                 // Fire event
223                 this.faxNumberCreatedEvent.fire(new HelperCreatedFaxNumberEvent(this.getFaxNumber()));
224         }
225
226         /**
227          * Copies currently set land-line number's data to admin phone controller
228          */
229         public void copyLandLineNumberToController () {
230                 // Validate land-line instance
231                 if (this.getLandLineNumber() == null) {
232                         // Throw NPE
233                         throw new NullPointerException("this.landLineNumber is null"); //NOI18N
234                 } else if (this.getLandLineNumber().getPhoneId() == null) {
235                         // Throw again
236                         throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
237                 } else if (this.getLandLineNumber().getPhoneId() < 1) {
238                         // Invalid id number
239                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
240                 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
241                         // Throw again
242                         throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
243                 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
244                         // Invalid id number
245                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
246                 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
247                         // Throw NPE again
248                         throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
249                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
250                         // ... throw again
251                         throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
252                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
253                         // Invalid id
254                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
255                 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
256                         // Throw NPE again ...
257                         throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
258                 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
259                         // Invalid id number
260                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
261                 }
262
263                 // Fire event
264                 this.landLineNumberCreatedEvent.fire(new HelperCreatedLandLineNumberEvent(this.getLandLineNumber()));
265         }
266
267         /**
268          * Copies currently set mobile number's data to admin phone controller
269          */
270         public void copyMobileNumberToController () {
271                 // Validate mobile instance
272                 if (this.getMobileNumber() == null) {
273                         // Throw NPE
274                         throw new NullPointerException("this.mobileNumber is null"); //NOI18N
275                 } else if (this.getMobileNumber().getPhoneId() == null) {
276                         // Throw again
277                         throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
278                 } else if (this.getMobileNumber().getPhoneId() < 1) {
279                         // Invalid id number
280                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
281                 } else if (this.getMobileNumber().getMobileProvider() == null) {
282                         // Throw NPE again
283                         throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
284                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
285                         // ... throw again
286                         throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
287                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
288                         // Invalid id
289                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
290                 } else if (this.getMobileNumber().getPhoneNumber() == null) {
291                         // Throw NPE again ...
292                         throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
293                 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
294                         // Invalid id number
295                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
296                 }
297
298                 // Fire event
299                 this.mobileNumberCreatedEvent.fire(new HelperCreatedMobileNumberEvent(this.getMobileNumber()));
300         }
301
302         /**
303          * Copies currently set user instances data to adminUserController
304          */
305         public void copyUserToController () {
306                 // Log message
307                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
308
309                 // Validate user instance
310                 if (this.getUser() == null) {
311                         // Throw NPE
312                         throw new NullPointerException("this.user is null"); //NOI18N
313                 } else if (this.getUser().getUserId() == null) {
314                         // Throw NPE again
315                         throw new NullPointerException("this.user.userId is null"); //NOI18N
316                 } else if (this.getUser().getUserId() < 1) {
317                         // Not valid
318                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
319                 }
320
321                 // Get contact
322                 Contact userContact = this.getUser().getUserContact();
323
324                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
325                 this.setContact(userContact);
326
327                 // Set all phone instances
328                 this.setPhoneInstances(userContact);
329
330                 // Fire event
331                 this.userCreatedEvent.fire(new HelperCreatedUserEvent(this.getUser()));
332         }
333
334         /**
335          * Getter for contact instance
336          * <p>
337          * @return Contact instance
338          */
339         public Contact getContact () {
340                 return this.contact;
341         }
342
343         /**
344          * Setter for contact instance
345          * <p>
346          * @param contact Contact instance
347          */
348         public void setContact (final Contact contact) {
349                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
350                 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
351                 this.contact = contact;
352         }
353
354         /**
355          * Returns a message key depending on if this contact is a user and/or a
356          * contact. If this contact is unused, a default key is returned.
357          * <p>
358          * @param contact Contact instance to check
359          * <p>
360          * @return Message key
361          */
362         public String getContactUsageMessageKey (final Contact contact) {
363                 // The contact must be valid
364                 if (null == contact) {
365                         // Throw NPE
366                         throw new NullPointerException("contact is null"); //NOI18N
367                 } else if (contact.getContactId() == null) {
368                         // Throw again ...
369                         throw new NullPointerException("contact.contactId is null"); //NOI18N
370                 } else if (contact.getContactId() < 1) {
371                         // Not valid
372                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
373                 }
374
375                 // Default key is "unused"
376                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
377
378                 // Check user contact
379                 boolean isUserContact = this.userController.isContactFound(contact);
380
381                 // Check user first
382                 if (isUserContact) {
383                         // Only user
384                         messageKey = "CONTACT_IS_USER"; //NOI18N
385                 }
386
387                 // Return message key
388                 return messageKey;
389         }
390
391         /**
392          * Getter for dialable fax number
393          * <p>
394          * @return Dialable fax number
395          */
396         public DialableFaxNumber getFaxNumber () {
397                 return this.faxNumber;
398         }
399
400         /**
401          * Setter for dialable fax number
402          * <p>
403          * @param faxNumber Dialable fax number
404          */
405         public void setFaxNumber (final DialableFaxNumber faxNumber) {
406                 this.faxNumber = faxNumber;
407         }
408
409         /**
410          * Getter for dialable land-line number
411          * <p>
412          * @return Dialable land-line number
413          */
414         public DialableLandLineNumber getLandLineNumber () {
415                 return this.landLineNumber;
416         }
417
418         /**
419          * Setter for dialable land-line number
420          * <p>
421          * @param landLineNumber Dialable land-line number
422          */
423         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
424                 this.landLineNumber = landLineNumber;
425         }
426
427         /**
428          * Getter for dialable mobile number
429          * <p>
430          * @return Dialable mobile number
431          */
432         public DialableMobileNumber getMobileNumber () {
433                 return this.mobileNumber;
434         }
435
436         /**
437          * Setter for dialable mobile number
438          * <p>
439          * @param mobileNumber Dialable mobile number
440          */
441         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
442                 this.mobileNumber = mobileNumber;
443         }
444
445         /**
446          * Getter for user instance
447          * <p>
448          * @return User instance
449          */
450         public User getUser () {
451                 return this.user;
452         }
453
454         /**
455          * Setter for user instance
456          * <p>
457          * @param user User instance
458          */
459         public void setUser (final User user) {
460                 this.user = user;
461         }
462
463         /**
464          * Set's all given contact's phone instances: land-line, mobile and
465          * faxNumber
466          * <p>
467          * @param contact Contact to set phone instances for
468          */
469         private void setPhoneInstances (final Contact contact) {
470                 // The contact must be valid
471                 if (null == contact) {
472                         // Throw NPE
473                         throw new NullPointerException("contact is null"); //NOI18N
474                 } else if (contact.getContactId() == null) {
475                         // Throw again ...
476                         throw new NullPointerException("contact.contactId is null"); //NOI18N
477                 } else if (contact.getContactId() < 1) {
478                         // Not valid
479                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
480                 }
481
482                 // Is mobile set?
483                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
484                         // Yes, then set it in admin controller
485                         this.setMobileNumber(contact.getContactMobileNumber());
486                 }
487
488                 // Is land-line set?
489                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
490                         // Yes, then set it in admin controller
491                         this.setLandLineNumber(contact.getContactLandLineNumber());
492                 }
493
494                 // Is faxNumber set?
495                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
496                         // Yes, then set it in admin controller
497                         this.setFaxNumber(contact.getContactFaxNumber());
498                 }
499         }
500
501 }