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