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