2 * Copyright (C) 2016, 2020 Free Software Foundation
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.addressbook.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.addressbook.beans.contact.AddressbookAdminContactWebRequestController;
26 import org.mxchange.addressbook.beans.phone.AddressbookAdminPhoneWebRequestController;
27 import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController;
28 import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController;
29 import org.mxchange.jcontacts.events.contact.created.CreatedContactEvent;
30 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
31 import org.mxchange.jcontacts.model.contact.Contact;
32 import org.mxchange.jphone.events.fax.created.CreatedFaxNumberEvent;
33 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
34 import org.mxchange.jphone.events.landline.created.CreatedLandLineNumberEvent;
35 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
36 import org.mxchange.jphone.events.mobile.created.CreatedMobileNumberEvent;
37 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
38 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
41 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
42 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
43 import org.mxchange.jusercore.model.user.User;
46 * A general helper for beans
48 * @author Roland Häder<roland@mxchange.org>
52 public class AddressbookWebRequestHelperBean implements AddressbookWebRequestHelperController {
55 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
57 private static final short THREAD_STACK = 5;
62 private static final long serialVersionUID = 17_258_793_567_145_701L;
65 * Administrative contact controller
68 private AddressbookAdminContactWebRequestController adminContactController;
71 * Administrative phone controller
74 private AddressbookAdminPhoneWebRequestController adminPhoneController;
77 * Administrative user controller
80 private AddressbookAdminUserWebRequestController adminUserController;
85 private Contact contact;
88 * Event for when a contact instance was created
92 private Event<ObservableCreatedContactEvent> contactCreatedEvent;
97 private DialableFaxNumber faxNumber;
100 * Event for when a fax number instance was created
104 private Event<ObservableCreatedFaxNumberEvent> faxNumberCreatedEvent;
109 private DialableLandLineNumber landLineNumber;
112 * Event for when a land-line number instance was created
116 private Event<ObservableCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
121 private DialableMobileNumber mobileNumber;
124 * Event for when a mobile number instance was created
128 private Event<ObservableCreatedMobileNumberEvent> mobileNumberCreatedEvent;
136 * Regular user controller
139 private AddressbookUserWebRequestController userController;
142 * Event for when a user instance was created
146 private Event<ObservableCreatedUserEvent> userCreatedEvent;
149 * Default constructor
151 public AddressbookWebRequestHelperBean () {
152 // Call super constructor
155 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
156 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
160 * Getter for contact instance
162 * @return Contact instance
164 public Contact getContact () {
169 * Setter for contact instance
171 * @param contact Contact instance
173 public void setContact (final Contact contact) {
174 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
175 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
176 this.contact = contact;
180 * Returns a message key depending on if this contact is a user and/or a
181 * contact. If this contact is unused, a default key is returned.
183 * @param contact Contact instance to check
185 * @return Message key
187 public String getContactUsageMessageKey (final Contact contact) {
188 // The contact must be valid
189 if (null == contact) {
191 throw new NullPointerException("contact is null"); //NOI18N
192 } else if (contact.getContactId() == null) {
194 throw new NullPointerException("contact.contactId is null"); //NOI18N
195 } else if (contact.getContactId() < 1) {
197 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
200 // Default key is "unused"
201 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
204 if (this.userController.isContactFound(contact)) {
206 messageKey = "CONTACT_IS_USER"; //NOI18N
209 // Return message key
214 * Getter for dialable fax number
216 * @return Dialable fax number
218 public DialableFaxNumber getFaxNumber () {
219 return this.faxNumber;
223 * Setter for dialable fax number
225 * @param faxNumber Dialable fax number
227 public void setFaxNumber (final DialableFaxNumber faxNumber) {
228 this.faxNumber = faxNumber;
232 * Getter for dialable land-line number
234 * @return Dialable land-line number
236 public DialableLandLineNumber getLandLineNumber () {
237 return this.landLineNumber;
241 * Setter for dialable land-line number
243 * @param landLineNumber Dialable land-line number
245 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
246 this.landLineNumber = landLineNumber;
250 * Getter for dialable mobile number
252 * @return Dialable mobile number
254 public DialableMobileNumber getMobileNumber () {
255 return this.mobileNumber;
259 * Setter for dialable mobile number
261 * @param mobileNumber Dialable mobile number
263 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
264 this.mobileNumber = mobileNumber;
268 * Getter for user instance
270 * @return User instance
272 public User getUser () {
277 * Setter for user instance
279 * @param user User instance
281 public void setUser (final User user) {
286 * Notifies other controllers (backing beans) if a contact id has been
287 * successfully converted to a Contact instance.
289 public void notifyControllerContactConverted () {
290 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
291 // System.out.println(MessageFormat.format("{0}.notifyControllerContactConverted: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
293 // Validate contact instance
294 if (this.getContact() == null) {
296 throw new NullPointerException("this.contact is null"); //NOI18N
297 } else if (this.getContact().getContactId() == null) {
299 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
300 } else if (this.getContact().getContactId() < 1) {
302 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
305 // Set all phone instances
306 this.setPhoneInstances(this.getContact());
308 // Set all fields: user
309 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
313 * Notifies other controllers (backing beans) if a phone id has been
314 * successfully converted to a DialableFaxNumber instance.
316 public void notifyControllerFaxNumberConverted () {
317 // Validate fax instance
318 if (this.getFaxNumber() == null) {
320 throw new NullPointerException("this.faxNumber is null"); //NOI18N
321 } else if (this.getFaxNumber().getPhoneId() == null) {
323 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
324 } else if (this.getFaxNumber().getPhoneId() < 1) {
326 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
327 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
329 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
330 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
332 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
333 } else if (this.getFaxNumber().getPhoneCountry() == null) {
335 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
336 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
338 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
339 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
341 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
342 } else if (this.getFaxNumber().getPhoneNumber() == null) {
343 // Throw NPE again ...
344 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
345 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
347 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
351 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
355 * Notifies other controllers (backing beans) if a phone id has been
356 * successfully converted to a DialableLandLineNumber instance.
358 public void notifyControllerLandLineNumberConverted () {
359 // Validate land-line instance
360 if (this.getLandLineNumber() == null) {
362 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
363 } else if (this.getLandLineNumber().getPhoneId() == null) {
365 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
366 } else if (this.getLandLineNumber().getPhoneId() < 1) {
368 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
369 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
371 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
372 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
374 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
375 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
377 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
378 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
380 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
381 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
383 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
384 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
385 // Throw NPE again ...
386 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
387 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
389 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
393 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
397 * Notifies other controllers (backing beans) if a phone id has been
398 * successfully converted to a DialableMobileNumber instance.
400 public void notifyControllerMobileNumberConverted () {
401 // Validate mobile instance
402 if (this.getMobileNumber() == null) {
404 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
405 } else if (this.getMobileNumber().getPhoneId() == null) {
407 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
408 } else if (this.getMobileNumber().getPhoneId() < 1) {
410 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
411 } else if (this.getMobileNumber().getMobileProvider() == null) {
413 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
414 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
416 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
417 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
419 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
420 } else if (this.getMobileNumber().getPhoneNumber() == null) {
421 // Throw NPE again ...
422 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
423 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
425 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
429 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
433 * Notifies other controllers (backing beans) if a user id has been
434 * successfully converted to a User instance.
436 public void notifyControllerUserConverted () {
438 //* NOISY-DEBUG: */ System.out.println("AdminHelper::notifyControllerUserConverted - CALLED!"); //NOI18N
440 // Validate user instance
441 if (this.getUser() == null) {
443 throw new NullPointerException("this.user is null"); //NOI18N
444 } else if (this.getUser().getUserId() == null) {
446 throw new NullPointerException("this.user.userId is null"); //NOI18N
447 } else if (this.getUser().getUserId() < 1) {
449 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
453 final Contact userContact = this.getUser().getUserContact();
455 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
456 this.setContact(userContact);
458 // Set all phone instances
459 this.setPhoneInstances(userContact);
462 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
466 * Set's all given contact's phone instances: land-line, mobile and
469 * @param contact Contact to set phone instances for
471 private void setPhoneInstances (final Contact contact) {
472 // The contact must be valid
473 if (null == contact) {
475 throw new NullPointerException("contact is null"); //NOI18N
476 } else if (contact.getContactId() == null) {
478 throw new NullPointerException("contact.contactId is null"); //NOI18N
479 } else if (contact.getContactId() < 1) {
481 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
485 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
486 // Yes, then set it in admin controller
487 this.setMobileNumber(contact.getContactMobileNumber());
491 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
492 // Yes, then set it in admin controller
493 this.setLandLineNumber(contact.getContactLandLineNumber());
497 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
498 // Yes, then set it in admin controller
499 this.setFaxNumber(contact.getContactFaxNumber());