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