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