2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.helper;
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;
50 * A general helper for beans
52 * @author Roland Häder<roland@mxchange.org>
56 public class JobsWebRequestHelperBean extends BaseJobsBean implements JobsWebRequestHelperController {
59 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
61 private static final short THREAD_STACK = 5;
66 private static final long serialVersionUID = 17_258_793_567_145_701L;
69 * Administrative contact controller
72 private JobsAdminContactWebRequestController adminContactController;
75 * Administrative phone controller
78 private JobsAdminPhoneWebRequestController adminPhoneController;
81 * Administrative user controller
84 private JobsAdminUserWebRequestController adminUserController;
89 private Contact contact;
92 * Event for when a contact instance was created
96 private Event<ObservableCreatedContactEvent> contactCreatedEvent;
101 private DialableFaxNumber faxNumber;
104 * Event for when a fax number instance was created
108 private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
113 private DialableLandLineNumber landLineNumber;
116 * Event for when a land-line number instance was created
120 private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
125 private DialableMobileNumber mobileNumber;
128 * Event for when a mobile number instance was created
132 private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
140 * Regular user controller
143 private JobsUserWebRequestController userController;
146 * Event for when a user instance was created
150 private Event<ObservableCreatedUserEvent> userCreatedEvent;
153 * Default constructor
155 public JobsWebRequestHelperBean () {
156 // Call super constructor
161 * Getter for contact instance
163 * @return Contact instance
165 public Contact getContact () {
170 * Setter for contact instance
172 * @param contact Contact instance
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;
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.
184 * @param contact Contact instance to check
186 * @return Message key
188 public String getContactUsageMessageKey (final Contact contact) {
189 // The contact must be valid
190 if (null == contact) {
192 throw new NullPointerException("contact is null"); //NOI18N
193 } else if (contact.getContactId() == null) {
195 throw new NullPointerException("contact.contactId is null"); //NOI18N
196 } else if (contact.getContactId() < 1) {
198 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
201 // Default key is "unused"
202 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
205 if (this.userController.isContactFound(contact)) {
207 messageKey = "CONTACT_IS_USER"; //NOI18N
210 // Return message key
215 * Getter for dialable fax number
217 * @return Dialable fax number
219 public DialableFaxNumber getFaxNumber () {
220 return this.faxNumber;
224 * Setter for dialable fax number
226 * @param faxNumber Dialable fax number
228 public void setFaxNumber (final DialableFaxNumber faxNumber) {
229 this.faxNumber = faxNumber;
233 * Getter for dialable land-line number
235 * @return Dialable land-line number
237 public DialableLandLineNumber getLandLineNumber () {
238 return this.landLineNumber;
242 * Setter for dialable land-line number
244 * @param landLineNumber Dialable land-line number
246 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
247 this.landLineNumber = landLineNumber;
251 * Getter for dialable mobile number
253 * @return Dialable mobile number
255 public DialableMobileNumber getMobileNumber () {
256 return this.mobileNumber;
260 * Setter for dialable mobile number
262 * @param mobileNumber Dialable mobile number
264 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
265 this.mobileNumber = mobileNumber;
269 * Getter for user instance
271 * @return User instance
273 public User getUser () {
278 * Setter for user instance
280 * @param user User instance
282 public void setUser (final User user) {
287 * Notifies other controllers (backing beans) if a contact id has been
288 * successfully converted to a Contact instance.
290 public void notifyControllerContactConverted () {
291 // Validate contact instance
292 if (this.getContact() == null) {
294 throw new NullPointerException("this.contact is null"); //NOI18N
295 } else if (this.getContact().getContactId() == null) {
297 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
298 } else if (this.getContact().getContactId() < 1) {
300 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
303 // Set all phone instances
304 this.setPhoneInstances(this.getContact());
306 // Set all fields: user
307 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
311 * Notifies other controllers (backing beans) if a phone id has been
312 * successfully converted to a DialableFaxNumber instance.
314 public void notifyControllerFaxNumberConverted () {
315 // Validate fax instance
316 if (this.getFaxNumber() == null) {
318 throw new NullPointerException("this.faxNumber is null"); //NOI18N
319 } else if (this.getFaxNumber().getPhoneId() == null) {
321 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
322 } else if (this.getFaxNumber().getPhoneId() < 1) {
324 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
325 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
327 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
328 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
330 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
331 } else if (this.getFaxNumber().getPhoneCountry() == null) {
333 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
334 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
336 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
337 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
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) {
345 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
349 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
353 * Notifies other controllers (backing beans) if a phone id has been
354 * successfully converted to a DialableLandLineNumber instance.
356 public void notifyControllerLandLineNumberConverted () {
357 // Validate land-line instance
358 if (this.getLandLineNumber() == null) {
360 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
361 } else if (this.getLandLineNumber().getPhoneId() == null) {
363 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
364 } else if (this.getLandLineNumber().getPhoneId() < 1) {
366 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
367 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
369 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
370 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
372 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
373 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
375 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
376 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
378 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
379 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
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) {
387 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
391 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
395 * Notifies other controllers (backing beans) if a phone id has been
396 * successfully converted to a DialableMobileNumber instance.
398 public void notifyControllerMobileNumberConverted () {
399 // Validate mobile instance
400 if (this.getMobileNumber() == null) {
402 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
403 } else if (this.getMobileNumber().getPhoneId() == null) {
405 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
406 } else if (this.getMobileNumber().getPhoneId() < 1) {
408 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
409 } else if (this.getMobileNumber().getMobileProvider() == null) {
411 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
412 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
414 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
415 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
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) {
423 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
427 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
431 * Notifies other controllers (backing beans) if a user id has been
432 * successfully converted to a User instance.
434 public void notifyControllerUserConverted () {
435 // Validate user instance
436 if (this.getUser() == null) {
438 throw new NullPointerException("this.user is null"); //NOI18N
439 } else if (this.getUser().getUserId() == null) {
441 throw new NullPointerException("this.user.userId is null"); //NOI18N
442 } else if (this.getUser().getUserId() < 1) {
444 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
448 final Contact userContact = this.getUser().getUserContact();
450 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
451 this.setContact(userContact);
453 // Set all phone instances
454 this.setPhoneInstances(userContact);
457 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
461 * Returns the branch office's full address. If null is provided, an empty
462 * string is returned.
464 * @param branchOffice Branch office instance
466 * @return Branch office's address
468 public String renderBranchOffice (final BranchOffice branchOffice) {
469 // Default is empty string, so let's get started
470 final StringBuilder sb = new StringBuilder(30);
472 // Is a branch office instance given?
473 if (branchOffice instanceof BranchOffice) {
474 // Yes, then append all data
476 sb.append(branchOffice.getBranchStreet());
478 sb.append(branchOffice.getBranchHouseNumber());
480 sb.append(branchOffice.getBranchCountry().getCountryCode());
482 sb.append(branchOffice.getBranchZipCode());
483 sb.append(branchOffice.getBranchCity());
487 return sb.toString();
491 * Returns the contact's personal title, family name and name. If null is
492 * provided, an empty string is returned.
494 * @param contact Contact instance
496 * @return Contact's name
498 public String renderContact (final Contact contact) {
499 // Default is empty string, so let's get started
500 final StringBuilder sb = new StringBuilder(20);
503 if (contact instanceof Contact) {
505 sb.append(String.format(FacesContext.getCurrentInstance().getViewRoot().getLocale(), "{0} {1}, {2}", this.getMessageFromBundle(contact.getContactPersonalTitle().getMessageKey()), contact.getContactFamilyName(), contact.getContactFirstName())); //NOI18N
509 return sb.toString();
513 * Set's all given contact's phone instances: land-line, mobile and
516 * @param contact Contact to set phone instances for
518 private void setPhoneInstances (final Contact contact) {
519 // The contact must be valid
520 if (null == contact) {
522 throw new NullPointerException("contact is null"); //NOI18N
523 } else if (contact.getContactId() == null) {
525 throw new NullPointerException("contact.contactId is null"); //NOI18N
526 } else if (contact.getContactId() < 1) {
528 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
532 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
533 // Yes, then set it in admin controller
534 this.setMobileNumber(contact.getContactMobileNumber());
538 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
539 // Yes, then set it in admin controller
540 this.setLandLineNumber(contact.getContactLandLineNumber());
544 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
545 // Yes, then set it in admin controller
546 this.setFaxNumber(contact.getContactFaxNumber());