]> 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.setPhoneCountry(this.getFaxNumber().getPhoneCountry());
153                 this.adminPhoneController.setPhoneNumber(this.getFaxNumber().getPhoneNumber());
154         }
155
156         @Override
157         public void copyLandLineNumberToController () {
158                 // Validate land-line instance
159                 if (this.getLandLineNumber() == null) {
160                         // Throw NPE
161                         throw new NullPointerException("this.landLineNumber is null");
162                 } else if (this.getLandLineNumber().getPhoneId() == null) {
163                         // Throw again
164                         throw new NullPointerException("this.landLineNumber.phoneId is null");
165                 } else if (this.getLandLineNumber().getPhoneId() < 1) {
166                         // Invalid id number
167                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId()));
168                 } else if (this.getFaxNumber().getPhoneAreaCode()== null) {
169                         // Throw again
170                         throw new NullPointerException("this.landLineNumber.phoneAreaCode is null");
171                 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
172                         // Invalid id number
173                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode()));
174                 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
175                         // Throw NPE again
176                         throw new NullPointerException("this.landLineNumber.phoneCountry is null");
177                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
178                         // ... throw again
179                         throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null");
180                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
181                         // Invalid id
182                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId()));
183                 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
184                         // Throw NPE again ...
185                         throw new NullPointerException("this.landLineNumber.phoneNumber is null");
186                 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
187                         // Invalid id number
188                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber()));
189                 }
190
191                 // Copy all (changeable) data fields to admin controller
192                 this.adminPhoneController.setPhoneCountry(this.getLandLineNumber().getPhoneCountry());
193                 this.adminPhoneController.setPhoneNumber(this.getLandLineNumber().getPhoneNumber());
194         }
195
196         @Override
197         public void copyMobileNumberToController () {
198                 // Validate mobile instance
199                 if (this.getMobileNumber() == null) {
200                         // Throw NPE
201                         throw new NullPointerException("this.mobileNumber is null");
202                 } else if (this.getMobileNumber().getPhoneId() == null) {
203                         // Throw again
204                         throw new NullPointerException("this.mobileNumber.phoneId is null");
205                 } else if (this.getMobileNumber().getPhoneId() < 1) {
206                         // Invalid id number
207                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId()));
208                 } else if (this.getMobileNumber().getMobileProvider() == null) {
209                         // Throw NPE again
210                         throw new NullPointerException("this.mobileNumber.mobileProvider is null");
211                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
212                         // ... throw again
213                         throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null");
214                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
215                         // Invalid id
216                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId()));
217                 } else if (this.getMobileNumber().getPhoneNumber() == null) {
218                         // Throw NPE again ...
219                         throw new NullPointerException("this.mobileNumber.phoneNumber is null");
220                 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
221                         // Invalid id number
222                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber()));
223                 }
224
225                 // Copy all (changeable) data fields to admin controller
226                 this.adminPhoneController.setMobileProvider(this.getMobileNumber().getMobileProvider());
227                 this.adminPhoneController.setPhoneNumber(this.getMobileNumber().getPhoneNumber());
228         }
229
230         @Override
231         public void copyUserToController () {
232                 // Log message
233                 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
234
235                 // Validate user instance
236                 if (this.getUser() == null) {
237                         // Throw NPE
238                         throw new NullPointerException("this.user is null");
239                 } else if (this.getUser().getUserId() == null) {
240                         // Throw NPE again
241                         throw new NullPointerException("this.user.userId is null");
242                 } else if (this.getUser().getUserId() < 1) {
243                         // Not valid
244                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId()));
245                 }
246
247                 // Get contact
248                 Contact userContact = this.getUser().getUserContact();
249
250                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
251                 this.setContact(userContact);
252
253                 // Set all phone instances
254                 this.setPhoneInstances(userContact);
255
256                 // Set all fields: user
257                 this.userController.setUserName(this.getUser().getUserName());
258         }
259
260         @Override
261         public Contact getContact () {
262                 return this.contact;
263         }
264
265         @Override
266         public void setContact (final Contact contact) {
267                 this.contact = contact;
268         }
269
270         @Override
271         public String getContactUsageMessageKey (final Contact contact) {
272                 // The contact must be valid
273                 if (null == contact) {
274                         // Throw NPE
275                         throw new NullPointerException("contact is null"); //NOI18N
276                 } else if (contact.getContactId() == null) {
277                         // Throw again ...
278                         throw new NullPointerException("contact.contactId is null"); //NOI18N
279                 } else if (contact.getContactId() < 1) {
280                         // Not valid
281                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
282                 }
283
284                 // Default key is "unused"
285                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
286
287                 // Check user
288                 boolean isUserContact = this.userController.isContactFound(contact);
289
290                 // Check user first
291                 if (isUserContact) {
292                         // Only user
293                         messageKey = "CONTACT_IS_USER"; //NOI18N
294                 }
295
296                 // Return message key
297                 return messageKey;
298         }
299
300         @Override
301         public DialableFaxNumber getFaxNumber () {
302                 return this.faxNumber;
303         }
304
305         @Override
306         public void setFaxNumber (final DialableFaxNumber faxNumber) {
307                 this.faxNumber = faxNumber;
308         }
309
310         @Override
311         public DialableLandLineNumber getLandLineNumber () {
312                 return this.landLineNumber;
313         }
314
315         @Override
316         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
317                 this.landLineNumber = landLineNumber;
318         }
319
320         @Override
321         public DialableMobileNumber getMobileNumber () {
322                 return this.mobileNumber;
323         }
324
325         @Override
326         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
327                 this.mobileNumber = mobileNumber;
328         }
329
330         @Override
331         public User getUser () {
332                 return this.user;
333         }
334
335         @Override
336         public void setUser (final User user) {
337                 this.user = user;
338         }
339
340         /**
341          * Set's all given contact's phone instances: land-line, mobile and
342          * faxNumber
343          * <p>
344          * @param contact Contact to set phone instances for
345          */
346         private void setPhoneInstances (final Contact contact) {
347                 // The contact must be valid
348                 if (null == contact) {
349                         // Throw NPE
350                         throw new NullPointerException("contact is null"); //NOI18N
351                 } else if (contact.getContactId() == null) {
352                         // Throw again ...
353                         throw new NullPointerException("contact.contactId is null"); //NOI18N
354                 } else if (contact.getContactId() < 1) {
355                         // Not valid
356                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
357                 }
358
359                 // Is mobile set?
360                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
361                         // Yes, then set it in admin controller
362                         this.setMobileNumber(contact.getContactMobileNumber());
363                 }
364
365                 // Is land-line set?
366                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
367                         // Yes, then set it in admin controller
368                         this.setLandLineNumber(contact.getContactLandLineNumber());
369                 }
370
371                 // Is faxNumber set?
372                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
373                         // Yes, then set it in admin controller
374                         this.setFaxNumber(contact.getContactFaxNumber());
375                 }
376         }
377
378 }