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