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