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.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;
47 * A general helper for beans
49 * @author Roland Häder<roland@mxchange.org>
53 public class JobsWebRequestHelperBean extends BaseJobsController implements JobsWebRequestHelperController {
56 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
58 private static final short THREAD_STACK = 5;
63 private static final long serialVersionUID = 17_258_793_567_145_701L;
66 * Administrative contact controller
69 private JobsAdminContactWebRequestController adminContactController;
72 * Administrative phone controller
75 private JobsAdminPhoneWebRequestController adminPhoneController;
78 * Administrative user controller
81 private JobsAdminUserWebRequestController adminUserController;
86 private Contact contact;
89 * Event for when a contact instance was created
93 private Event<ObservableCreatedContactEvent> contactCreatedEvent;
98 private DialableFaxNumber faxNumber;
101 * Event for when a fax number instance was created
105 private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
110 private DialableLandLineNumber landLineNumber;
113 * Event for when a land-line number instance was created
117 private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
122 private DialableMobileNumber mobileNumber;
125 * Event for when a mobile number instance was created
129 private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
137 * Regular user controller
140 private JobsUserWebRequestController userController;
143 * Event for when a user instance was created
147 private Event<ObservableCreatedUserEvent> userCreatedEvent;
150 * Default constructor
152 public JobsWebRequestHelperBean () {
153 // Call super constructor
158 * Getter for contact instance
160 * @return Contact instance
162 public Contact getContact () {
167 * Setter for contact instance
169 * @param contact Contact instance
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;
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.
181 * @param contact Contact instance to check
183 * @return Message key
185 public String getContactUsageMessageKey (final Contact contact) {
186 // The contact must be valid
187 if (null == contact) {
189 throw new NullPointerException("contact is null"); //NOI18N
190 } else if (contact.getContactId() == null) {
192 throw new NullPointerException("contact.contactId is null"); //NOI18N
193 } else if (contact.getContactId() < 1) {
195 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
198 // Default key is "unused"
199 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
202 if (this.userController.isContactFound(contact)) {
204 messageKey = "CONTACT_IS_USER"; //NOI18N
207 // Return message key
212 * Getter for dialable fax number
214 * @return Dialable fax number
216 public DialableFaxNumber getFaxNumber () {
217 return this.faxNumber;
221 * Setter for dialable fax number
223 * @param faxNumber Dialable fax number
225 public void setFaxNumber (final DialableFaxNumber faxNumber) {
226 this.faxNumber = faxNumber;
230 * Getter for dialable land-line number
232 * @return Dialable land-line number
234 public DialableLandLineNumber getLandLineNumber () {
235 return this.landLineNumber;
239 * Setter for dialable land-line number
241 * @param landLineNumber Dialable land-line number
243 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
244 this.landLineNumber = landLineNumber;
248 * Getter for dialable mobile number
250 * @return Dialable mobile number
252 public DialableMobileNumber getMobileNumber () {
253 return this.mobileNumber;
257 * Setter for dialable mobile number
259 * @param mobileNumber Dialable mobile number
261 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
262 this.mobileNumber = mobileNumber;
266 * Getter for user instance
268 * @return User instance
270 public User getUser () {
275 * Setter for user instance
277 * @param user User instance
279 public void setUser (final User user) {
284 * Notifies other controllers (backing beans) if a contact id has been
285 * successfully converted to a Contact instance.
287 public void notifyControllerContactConverted () {
288 // Validate contact instance
289 if (this.getContact() == null) {
291 throw new NullPointerException("this.contact is null"); //NOI18N
292 } else if (this.getContact().getContactId() == null) {
294 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
295 } else if (this.getContact().getContactId() < 1) {
297 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
300 // Set all phone instances
301 this.setPhoneInstances(this.getContact());
303 // Set all fields: user
304 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
308 * Notifies other controllers (backing beans) if a phone id has been
309 * successfully converted to a DialableFaxNumber instance.
311 public void notifyControllerFaxNumberConverted () {
312 // Validate fax instance
313 if (this.getFaxNumber() == null) {
315 throw new NullPointerException("this.faxNumber is null"); //NOI18N
316 } else if (this.getFaxNumber().getPhoneId() == null) {
318 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
319 } else if (this.getFaxNumber().getPhoneId() < 1) {
321 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
322 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
324 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
325 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
327 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
328 } else if (this.getFaxNumber().getPhoneCountry() == null) {
330 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
331 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
333 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
334 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
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) {
342 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
346 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
350 * Notifies other controllers (backing beans) if a phone id has been
351 * successfully converted to a DialableLandLineNumber instance.
353 public void notifyControllerLandLineNumberConverted () {
354 // Validate land-line instance
355 if (this.getLandLineNumber() == null) {
357 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
358 } else if (this.getLandLineNumber().getPhoneId() == null) {
360 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
361 } else if (this.getLandLineNumber().getPhoneId() < 1) {
363 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
364 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
366 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
367 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
369 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
370 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
372 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
373 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
375 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
376 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
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) {
384 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
388 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
392 * Notifies other controllers (backing beans) if a phone id has been
393 * successfully converted to a DialableMobileNumber instance.
395 public void notifyControllerMobileNumberConverted () {
396 // Validate mobile instance
397 if (this.getMobileNumber() == null) {
399 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
400 } else if (this.getMobileNumber().getPhoneId() == null) {
402 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
403 } else if (this.getMobileNumber().getPhoneId() < 1) {
405 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
406 } else if (this.getMobileNumber().getMobileProvider() == null) {
408 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
409 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
411 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
412 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
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) {
420 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
424 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
428 * Notifies other controllers (backing beans) if a user id has been
429 * successfully converted to a User instance.
431 public void notifyControllerUserConverted () {
432 // Validate user instance
433 if (this.getUser() == null) {
435 throw new NullPointerException("this.user is null"); //NOI18N
436 } else if (this.getUser().getUserId() == null) {
438 throw new NullPointerException("this.user.userId is null"); //NOI18N
439 } else if (this.getUser().getUserId() < 1) {
441 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
445 final Contact userContact = this.getUser().getUserContact();
447 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
448 this.setContact(userContact);
450 // Set all phone instances
451 this.setPhoneInstances(userContact);
454 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
458 * Set's all given contact's phone instances: land-line, mobile and
461 * @param contact Contact to set phone instances for
463 private void setPhoneInstances (final Contact contact) {
464 // The contact must be valid
465 if (null == contact) {
467 throw new NullPointerException("contact is null"); //NOI18N
468 } else if (contact.getContactId() == null) {
470 throw new NullPointerException("contact.contactId is null"); //NOI18N
471 } else if (contact.getContactId() < 1) {
473 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
477 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
478 // Yes, then set it in admin controller
479 this.setMobileNumber(contact.getContactMobileNumber());
483 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
484 // Yes, then set it in admin controller
485 this.setLandLineNumber(contact.getContactLandLineNumber());
489 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
490 // Yes, then set it in admin controller
491 this.setFaxNumber(contact.getContactFaxNumber());