]> 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, 2017 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.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import org.mxchange.jcontacts.contact.Contact;
26 import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent;
27 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
28 import org.mxchange.jjobs.beans.BaseJobsController;
29 import org.mxchange.jjobs.beans.contact.JobsAdminContactWebRequestController;
30 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
31 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
32 import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
33 import org.mxchange.jphone.events.fax.created.CreatedFaxNumberEvent;
34 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
35 import org.mxchange.jphone.events.landline.created.CreatedLandLineNumberEvent;
36 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
37 import org.mxchange.jphone.events.mobile.created.CreatedMobileNumberEvent;
38 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
39 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
42 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
43 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
44 import org.mxchange.jusercore.model.user.User;
45
46 /**
47  * A general helper for beans
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Named ("beanHelper")
52 @RequestScoped
53 public class JobsWebRequestHelperBean extends BaseJobsController implements JobsWebRequestHelperController {
54
55         /**
56          * Call-stack instance (5 may show BeanELResolver.getValue as caller)
57          */
58         private static final short THREAD_STACK = 5;
59
60         /**
61          * Serial number
62          */
63         private static final long serialVersionUID = 17_258_793_567_145_701L;
64
65         /**
66          * Administrative contact controller
67          */
68         @Inject
69         private JobsAdminContactWebRequestController adminContactController;
70
71         /**
72          * Administrative phone controller
73          */
74         @Inject
75         private JobsAdminPhoneWebRequestController adminPhoneController;
76
77         /**
78          * Administrative user controller
79          */
80         @Inject
81         private JobsAdminUserWebRequestController adminUserController;
82
83         /**
84          * Contact instance
85          */
86         private Contact contact;
87
88         /**
89          * Event for when a contact instance was created
90          */
91         @Any
92         @Inject
93         private Event<ObservableCreatedContactEvent> contactCreatedEvent;
94
95         /**
96          * Fax number
97          */
98         private DialableFaxNumber faxNumber;
99
100         /**
101          * Event for when a fax number instance was created
102          */
103         @Any
104         @Inject
105         private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
106
107         /**
108          * Land-line number
109          */
110         private DialableLandLineNumber landLineNumber;
111
112         /**
113          * Event for when a land-line number instance was created
114          */
115         @Any
116         @Inject
117         private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
118
119         /**
120          * Mobile number
121          */
122         private DialableMobileNumber mobileNumber;
123
124         /**
125          * Event for when a mobile number instance was created
126          */
127         @Any
128         @Inject
129         private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
130
131         /**
132          * User instance
133          */
134         private User user;
135
136         /**
137          * Regular user controller
138          */
139         @Inject
140         private JobsUserWebRequestController userController;
141
142         /**
143          * Event for when a user instance was created
144          */
145         @Any
146         @Inject
147         private Event<ObservableCreatedUserEvent> userCreatedEvent;
148
149         /**
150          * Default constructor
151          */
152         public JobsWebRequestHelperBean () {
153                 // Call super constructor
154                 super();
155         }
156
157         /**
158          * Getter for contact instance
159          * <p>
160          * @return Contact instance
161          */
162         public Contact getContact () {
163                 return this.contact;
164         }
165
166         /**
167          * Setter for contact instance
168          * <p>
169          * @param contact Contact instance
170          */
171         public void setContact (final Contact contact) {
172                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
173                 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
174                 this.contact = contact;
175         }
176
177         /**
178          * Returns a message key depending on if this contact is a user and/or a
179          * contact. If this contact is unused, a default key is returned.
180          * <p>
181          * @param contact Contact instance to check
182          * <p>
183          * @return Message key
184          */
185         public String getContactUsageMessageKey (final Contact contact) {
186                 // The contact must be valid
187                 if (null == contact) {
188                         // Throw NPE
189                         throw new NullPointerException("contact is null"); //NOI18N
190                 } else if (contact.getContactId() == null) {
191                         // Throw again ...
192                         throw new NullPointerException("contact.contactId is null"); //NOI18N
193                 } else if (contact.getContactId() < 1) {
194                         // Not valid
195                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
196                 }
197
198                 // Default key is "unused"
199                 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
200
201                 // Check user first
202                 if (this.userController.isContactFound(contact)) {
203                         // Only user
204                         messageKey = "CONTACT_IS_USER"; //NOI18N
205                 }
206
207                 // Return message key
208                 return messageKey;
209         }
210
211         /**
212          * Getter for dialable fax number
213          * <p>
214          * @return Dialable fax number
215          */
216         public DialableFaxNumber getFaxNumber () {
217                 return this.faxNumber;
218         }
219
220         /**
221          * Setter for dialable fax number
222          * <p>
223          * @param faxNumber Dialable fax number
224          */
225         public void setFaxNumber (final DialableFaxNumber faxNumber) {
226                 this.faxNumber = faxNumber;
227         }
228
229         /**
230          * Getter for dialable land-line number
231          * <p>
232          * @return Dialable land-line number
233          */
234         public DialableLandLineNumber getLandLineNumber () {
235                 return this.landLineNumber;
236         }
237
238         /**
239          * Setter for dialable land-line number
240          * <p>
241          * @param landLineNumber Dialable land-line number
242          */
243         public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
244                 this.landLineNumber = landLineNumber;
245         }
246
247         /**
248          * Getter for dialable mobile number
249          * <p>
250          * @return Dialable mobile number
251          */
252         public DialableMobileNumber getMobileNumber () {
253                 return this.mobileNumber;
254         }
255
256         /**
257          * Setter for dialable mobile number
258          * <p>
259          * @param mobileNumber Dialable mobile number
260          */
261         public void setMobileNumber (final DialableMobileNumber mobileNumber) {
262                 this.mobileNumber = mobileNumber;
263         }
264
265         /**
266          * Getter for user instance
267          * <p>
268          * @return User instance
269          */
270         public User getUser () {
271                 return this.user;
272         }
273
274         /**
275          * Setter for user instance
276          * <p>
277          * @param user User instance
278          */
279         public void setUser (final User user) {
280                 this.user = user;
281         }
282
283         /**
284          * Notifies other controllers (backing beans) if a contact id has been
285          * successfully converted to a Contact instance.
286          */
287         public void notifyControllerContactConverted () {
288                 // Validate contact instance
289                 if (this.getContact() == null) {
290                         // Throw NPE
291                         throw new NullPointerException("this.contact is null"); //NOI18N
292                 } else if (this.getContact().getContactId() == null) {
293                         // Throw NPE again
294                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
295                 } else if (this.getContact().getContactId() < 1) {
296                         // Not valid
297                         throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
298                 }
299
300                 // Set all phone instances
301                 this.setPhoneInstances(this.getContact());
302
303                 // Set all fields: user
304                 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
305         }
306
307         /**
308          * Notifies other controllers (backing beans) if a phone id has been
309          * successfully converted to a DialableFaxNumber instance.
310          */
311         public void notifyControllerFaxNumberConverted () {
312                 // Validate fax instance
313                 if (this.getFaxNumber() == null) {
314                         // Throw NPE
315                         throw new NullPointerException("this.faxNumber is null"); //NOI18N
316                 } else if (this.getFaxNumber().getPhoneId() == null) {
317                         // Throw again
318                         throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
319                 } else if (this.getFaxNumber().getPhoneId() < 1) {
320                         // Invalid id number
321                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
322                 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
323                         // Throw again
324                         throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
325                 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
326                         // Invalid id number
327                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
328                 } else if (this.getFaxNumber().getPhoneCountry() == null) {
329                         // Throw NPE again
330                         throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
331                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
332                         // ... throw again
333                         throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
334                 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
335                         // Invalid id
336                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
337                 } else if (this.getFaxNumber().getPhoneNumber() == null) {
338                         // Throw NPE again ...
339                         throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
340                 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
341                         // Invalid id number
342                         throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
343                 }
344
345                 // Fire event
346                 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
347         }
348
349         /**
350          * Notifies other controllers (backing beans) if a phone id has been
351          * successfully converted to a DialableLandLineNumber instance.
352          */
353         public void notifyControllerLandLineNumberConverted () {
354                 // Validate land-line instance
355                 if (this.getLandLineNumber() == null) {
356                         // Throw NPE
357                         throw new NullPointerException("this.landLineNumber is null"); //NOI18N
358                 } else if (this.getLandLineNumber().getPhoneId() == null) {
359                         // Throw again
360                         throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
361                 } else if (this.getLandLineNumber().getPhoneId() < 1) {
362                         // Invalid id number
363                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
364                 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
365                         // Throw again
366                         throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
367                 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
368                         // Invalid id number
369                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
370                 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
371                         // Throw NPE again
372                         throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
373                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
374                         // ... throw again
375                         throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
376                 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
377                         // Invalid id
378                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
379                 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
380                         // Throw NPE again ...
381                         throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
382                 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
383                         // Invalid id number
384                         throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
385                 }
386
387                 // Fire event
388                 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
389         }
390
391         /**
392          * Notifies other controllers (backing beans) if a phone id has been
393          * successfully converted to a DialableMobileNumber instance.
394          */
395         public void notifyControllerMobileNumberConverted () {
396                 // Validate mobile instance
397                 if (this.getMobileNumber() == null) {
398                         // Throw NPE
399                         throw new NullPointerException("this.mobileNumber is null"); //NOI18N
400                 } else if (this.getMobileNumber().getPhoneId() == null) {
401                         // Throw again
402                         throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
403                 } else if (this.getMobileNumber().getPhoneId() < 1) {
404                         // Invalid id number
405                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
406                 } else if (this.getMobileNumber().getMobileProvider() == null) {
407                         // Throw NPE again
408                         throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
409                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
410                         // ... throw again
411                         throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
412                 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
413                         // Invalid id
414                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
415                 } else if (this.getMobileNumber().getPhoneNumber() == null) {
416                         // Throw NPE again ...
417                         throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
418                 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
419                         // Invalid id number
420                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
421                 }
422
423                 // Fire event
424                 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
425         }
426
427         /**
428          * Notifies other controllers (backing beans) if a user id has been
429          * successfully converted to a User instance.
430          */
431         public void notifyControllerUserConverted () {
432                 // Validate user instance
433                 if (this.getUser() == null) {
434                         // Throw NPE
435                         throw new NullPointerException("this.user is null"); //NOI18N
436                 } else if (this.getUser().getUserId() == null) {
437                         // Throw NPE again
438                         throw new NullPointerException("this.user.userId is null"); //NOI18N
439                 } else if (this.getUser().getUserId() < 1) {
440                         // Not valid
441                         throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
442                 }
443
444                 // Get contact
445                 final Contact userContact = this.getUser().getUserContact();
446
447                 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
448                 this.setContact(userContact);
449
450                 // Set all phone instances
451                 this.setPhoneInstances(userContact);
452
453                 // Fire event
454                 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
455         }
456
457         /**
458          * Set's all given contact's phone instances: land-line, mobile and
459          * faxNumber
460          * <p>
461          * @param contact Contact to set phone instances for
462          */
463         private void setPhoneInstances (final Contact contact) {
464                 // The contact must be valid
465                 if (null == contact) {
466                         // Throw NPE
467                         throw new NullPointerException("contact is null"); //NOI18N
468                 } else if (contact.getContactId() == null) {
469                         // Throw again ...
470                         throw new NullPointerException("contact.contactId is null"); //NOI18N
471                 } else if (contact.getContactId() < 1) {
472                         // Not valid
473                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
474                 }
475
476                 // Is mobile set?
477                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
478                         // Yes, then set it in admin controller
479                         this.setMobileNumber(contact.getContactMobileNumber());
480                 }
481
482                 // Is land-line set?
483                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
484                         // Yes, then set it in admin controller
485                         this.setLandLineNumber(contact.getContactLandLineNumber());
486                 }
487
488                 // Is faxNumber set?
489                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
490                         // Yes, then set it in admin controller
491                         this.setFaxNumber(contact.getContactFaxNumber());
492                 }
493         }
494
495 }