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.inject.Inject;
24 import javax.inject.Named;
25 import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent;
26 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
29 import org.mxchange.jcontactsbusiness.model.department.Department;
30 import org.mxchange.jcontactsbusiness.model.employee.Employee;
31 import org.mxchange.jcontactsbusiness.model.headquarters.HeadquartersData;
32 import org.mxchange.jjobs.beans.BaseJobsBean;
33 import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
34 import org.mxchange.jphone.events.fax.created.CreatedFaxNumberEvent;
35 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
36 import org.mxchange.jphone.events.landline.created.CreatedLandLineNumberEvent;
37 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
38 import org.mxchange.jphone.events.mobile.created.CreatedMobileNumberEvent;
39 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
40 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
41 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
42 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
43 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
44 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
45 import org.mxchange.jusercore.model.user.User;
48 * A general helper for beans
50 * @author Roland Häder<roland@mxchange.org>
54 public class JobsWebRequestHelperBean extends BaseJobsBean implements JobsWebRequestHelperController {
57 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
59 private static final short THREAD_STACK = 5;
64 private static final long serialVersionUID = 17_258_793_567_145_701L;
69 private Contact contact;
72 * Event for when a contact instance was created
76 private Event<ObservableCreatedContactEvent> contactCreatedEvent;
81 private DialableFaxNumber faxNumber;
84 * Event for when a fax number instance was created
88 private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
93 private DialableLandLineNumber landLineNumber;
96 * Event for when a land-line number instance was created
100 private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
105 private DialableMobileNumber mobileNumber;
108 * Event for when a mobile number instance was created
112 private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
120 * Regular user controller
123 private JobsUserWebRequestController userController;
126 * Event for when a user instance was created
130 private Event<ObservableCreatedUserEvent> userCreatedEvent;
133 * Default constructor
135 public JobsWebRequestHelperBean () {
136 // Call super constructor
141 * Getter for contact instance
143 * @return Contact instance
145 public Contact getContact () {
150 * Setter for contact instance
152 * @param contact Contact instance
154 public void setContact (final Contact contact) {
155 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
156 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
157 this.contact = contact;
161 * Returns a message key depending on if this contact is a user and/or a
162 * contact. If this contact is unused, a default key is returned.
164 * @param contact Contact instance to check
166 * @return Message key
168 public String getContactUsageMessageKey (final Contact contact) {
169 // The contact must be valid
170 if (null == contact) {
172 throw new NullPointerException("contact is null"); //NOI18N
173 } else if (contact.getContactId() == null) {
175 throw new NullPointerException("contact.contactId is null"); //NOI18N
176 } else if (contact.getContactId() < 1) {
178 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
181 // Default key is "unused"
182 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
185 if (this.userController.isContactFound(contact)) {
187 messageKey = "CONTACT_IS_USER"; //NOI18N
190 // Return message key
195 * Getter for dialable fax number
197 * @return Dialable fax number
199 public DialableFaxNumber getFaxNumber () {
200 return this.faxNumber;
204 * Setter for dialable fax number
206 * @param faxNumber Dialable fax number
208 public void setFaxNumber (final DialableFaxNumber faxNumber) {
209 this.faxNumber = faxNumber;
213 * Getter for dialable land-line number
215 * @return Dialable land-line number
217 public DialableLandLineNumber getLandLineNumber () {
218 return this.landLineNumber;
222 * Setter for dialable land-line number
224 * @param landLineNumber Dialable land-line number
226 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
227 this.landLineNumber = landLineNumber;
231 * Getter for dialable mobile number
233 * @return Dialable mobile number
235 public DialableMobileNumber getMobileNumber () {
236 return this.mobileNumber;
240 * Setter for dialable mobile number
242 * @param mobileNumber Dialable mobile number
244 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
245 this.mobileNumber = mobileNumber;
249 * Getter for user instance
251 * @return User instance
253 public User getUser () {
258 * Setter for user instance
260 * @param user User instance
262 public void setUser (final User user) {
267 * Notifies other controllers (backing beans) if a contact id has been
268 * successfully converted to a Contact instance.
270 public void notifyControllerContactConverted () {
271 // Validate contact instance
272 if (this.getContact() == null) {
274 throw new NullPointerException("this.contact is null"); //NOI18N
275 } else if (this.getContact().getContactId() == null) {
277 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
278 } else if (this.getContact().getContactId() < 1) {
280 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
283 // Set all phone instances
284 this.setPhoneInstances(this.getContact());
286 // Set all fields: user
287 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
291 * Notifies other controllers (backing beans) if a phone id has been
292 * successfully converted to a DialableFaxNumber instance.
294 public void notifyControllerFaxNumberConverted () {
295 // Validate fax instance
296 if (this.getFaxNumber() == null) {
298 throw new NullPointerException("this.faxNumber is null"); //NOI18N
299 } else if (this.getFaxNumber().getPhoneId() == null) {
301 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
302 } else if (this.getFaxNumber().getPhoneId() < 1) {
304 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
305 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
307 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
308 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
310 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
311 } else if (this.getFaxNumber().getPhoneCountry() == null) {
313 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
314 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
316 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
317 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
319 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
320 } else if (this.getFaxNumber().getPhoneNumber() == null) {
321 // Throw NPE again ...
322 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
323 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
325 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
329 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
333 * Notifies other controllers (backing beans) if a phone id has been
334 * successfully converted to a DialableLandLineNumber instance.
336 public void notifyControllerLandLineNumberConverted () {
337 // Validate land-line instance
338 if (this.getLandLineNumber() == null) {
340 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
341 } else if (this.getLandLineNumber().getPhoneId() == null) {
343 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
344 } else if (this.getLandLineNumber().getPhoneId() < 1) {
346 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
347 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
349 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
350 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
352 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
353 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
355 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
356 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
358 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
359 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
361 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
362 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
363 // Throw NPE again ...
364 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
365 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
367 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
371 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
375 * Notifies other controllers (backing beans) if a phone id has been
376 * successfully converted to a DialableMobileNumber instance.
378 public void notifyControllerMobileNumberConverted () {
379 // Validate mobile instance
380 if (this.getMobileNumber() == null) {
382 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
383 } else if (this.getMobileNumber().getPhoneId() == null) {
385 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
386 } else if (this.getMobileNumber().getPhoneId() < 1) {
388 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
389 } else if (this.getMobileNumber().getMobileProvider() == null) {
391 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
392 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
394 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
395 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
397 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
398 } else if (this.getMobileNumber().getPhoneNumber() == null) {
399 // Throw NPE again ...
400 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
401 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
403 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
407 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
411 * Notifies other controllers (backing beans) if a user id has been
412 * successfully converted to a User instance.
414 public void notifyControllerUserConverted () {
415 // Validate user instance
416 if (this.getUser() == null) {
418 throw new NullPointerException("this.user is null"); //NOI18N
419 } else if (this.getUser().getUserId() == null) {
421 throw new NullPointerException("this.user.userId is null"); //NOI18N
422 } else if (this.getUser().getUserId() < 1) {
424 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
428 final Contact userContact = this.getUser().getUserContact();
430 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
431 this.setContact(userContact);
433 // Set all phone instances
434 this.setPhoneInstances(userContact);
437 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
441 * Returns the branch office's full address. If null is provided, an empty
442 * string is returned.
444 * @param branchOffice Branch office instance
446 * @return Branch office's address
448 public String renderBranchOffice (final BranchOffice branchOffice) {
449 // Default is empty string, so let's get started
450 final StringBuilder sb = new StringBuilder(30);
452 // Is a branch office instance given?
453 if (branchOffice instanceof BranchOffice) {
454 // Yes, then append all data
455 sb.append(branchOffice.getBranchCompany().getCompanyName());
456 sb.append(", "); //NOI18N
457 sb.append(branchOffice.getBranchStreet());
458 sb.append(" "); //NOI18N
459 sb.append(branchOffice.getBranchHouseNumber());
460 sb.append(", "); //NOI18N
461 sb.append(branchOffice.getBranchCountry().getCountryCode());
462 sb.append(" "); //NOI18N
463 sb.append(branchOffice.getBranchZipCode());
464 sb.append(" "); //NOI18N
465 sb.append(branchOffice.getBranchCity());
469 return sb.toString();
473 * Returns the contact's personal title, family name and name. If null is
474 * provided, an empty string is returned.
476 * @param contact Contact instance
478 * @return Contact's full name
480 public String renderContact (final Contact contact) {
481 // Default is empty string, so let's get started
482 final StringBuilder sb = new StringBuilder(20);
485 if (contact instanceof Contact) {
487 sb.append(this.getMessageFromBundle(contact.getContactPersonalTitle().getMessageKey()));
488 sb.append(" "); //NOI18N
489 sb.append(contact.getContactFamilyName());
490 sb.append(" "); //NOI18N
491 sb.append(contact.getContactFirstName());
495 return sb.toString();
499 * Returns the department's name and name of assigned company. If null is
500 * provided, an empty string is returned.
502 * @param department Department instance
504 * @return Department's full name
506 public String renderDepartment (final Department department) {
507 // Default is empty string, so let's get started
508 final StringBuilder sb = new StringBuilder(10);
510 // Is a department set?
511 if (department instanceof Department) {
513 sb.append(department.getDepartmentName());
514 sb.append(" ("); //NOI18N
515 sb.append(department.getDepartmentCompany().getCompanyName());
516 sb.append(")"); //NOI18N
520 return sb.toString();
524 * Returns the employee's number, personal title, family name and name if
525 * available. If null is provided, an empty string is returned.
527 * @param employee Employee instance
529 * @return Contact's full name
531 public String renderEmployee (final Employee employee) {
532 // Default is empty string, so let's get started
533 final StringBuilder sb = new StringBuilder(20);
536 if (employee instanceof Employee) {
538 sb.append(employee.getEmployeeNumber());
540 // Is contact data found?
541 if (employee.getEmployeePersonalData() instanceof Contact) {
542 // Yes, then render it
543 final String contactName = this.renderContact(employee.getEmployeePersonalData());
546 if (contactName != null) {
548 sb.append(" ("); //NOI18N
549 sb.append(contactName);
550 sb.append(")"); //NOI18N
556 return sb.toString();
560 * Returns the headquarters' address. If null is provided, an empty string
563 * @param headquarters Headquarters instance
565 * @return Headquarters' address
567 public String renderHeadquarters (final HeadquartersData headquarters) {
568 // Default is empty string, so let's get started
569 final StringBuilder sb = new StringBuilder(10);
571 // Is a headquarters set?
572 if (headquarters instanceof HeadquartersData) {
574 sb.append(headquarters.getHeadquartersStreet());
575 sb.append(" "); //NOI18N
576 sb.append(headquarters.getHeadquartersHouseNumber());
578 // Is store/suite number set?
579 if (headquarters.getHeadquartersStore() instanceof Short) {
580 sb.append(" ("); //NOI18N
581 sb.append(this.getMessageFromBundle("DATA_STORE")); //NOI18N
582 sb.append(" "); //NOI18N
583 sb.append(headquarters.getHeadquartersStore());
584 sb.append(", "); //NOI18N
585 sb.append(this.getMessageFromBundle("DATA_SUITE_NUMBER")); //NOI18N
586 sb.append(" "); //NOI18N
587 sb.append(headquarters.getHeadquartersSuiteNumber());
588 sb.append(")"); //NOI18N
591 // Continue with country, ZIP code and city
592 sb.append(", "); //NOI18N
593 sb.append(headquarters.getHeadquartersCountry().getCountryCode());
594 sb.append(" "); //NOI18N
595 sb.append(headquarters.getHeadquartersZipCode());
596 sb.append(" "); //NOI18N
597 sb.append(headquarters.getHeadquartersCity());
601 return sb.toString();
605 * Set's all given contact's phone instances: land-line, mobile and
608 * @param contact Contact to set phone instances for
610 private void setPhoneInstances (final Contact contact) {
611 // The contact must be valid
612 if (null == contact) {
614 throw new NullPointerException("contact is null"); //NOI18N
615 } else if (contact.getContactId() == null) {
617 throw new NullPointerException("contact.contactId is null"); //NOI18N
618 } else if (contact.getContactId() < 1) {
620 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
624 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
625 // Yes, then set it in admin controller
626 this.setMobileNumber(contact.getContactMobileNumber());
630 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
631 // Yes, then set it in admin controller
632 this.setLandLineNumber(contact.getContactLandLineNumber());
636 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
637 // Yes, then set it in admin controller
638 this.setFaxNumber(contact.getContactFaxNumber());