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