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.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.AddressbookUserWebSessionController;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jphone.events.helper.fax.created.HelperCreatedFaxNumberEvent;
31 import org.mxchange.jphone.events.helper.fax.created.ObservableHelperCreatedFaxNumberEvent;
32 import org.mxchange.jphone.events.helper.landline.created.HelperCreatedLandLineNumberEvent;
33 import org.mxchange.jphone.events.helper.landline.created.ObservableHelperCreatedLandLineNumberEvent;
34 import org.mxchange.jphone.events.helper.mobile.created.HelperCreatedMobileNumberEvent;
35 import org.mxchange.jphone.events.helper.mobile.created.ObservableHelperCreatedMobileNumberEvent;
36 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
37 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
38 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
39 import org.mxchange.jusercore.events.user.helper.created.HelperCreatedUserEvent;
40 import org.mxchange.jusercore.events.user.helper.created.ObservableHelperCreatedUserEvent;
41 import org.mxchange.jusercore.model.user.User;
44 * A general helper for beans
46 * @author Roland Häder<roland@mxchange.org>
50 public class AddressbookWebRequestHelperBean implements AddressbookWebRequestHelperController {
53 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
55 private static final short THREAD_STACK = 5;
60 private static final long serialVersionUID = 17_258_793_567_145_701L;
63 * Administrative contact controller
66 private AddressbookAdminContactWebRequestController adminContactController;
69 * Administrative phone controller
72 private AddressbookAdminPhoneWebRequestController adminPhoneController;
75 * Administrative user controller
78 private AddressbookAdminUserWebRequestController adminUserController;
83 private Contact contact;
88 private DialableFaxNumber faxNumber;
93 private DialableLandLineNumber landLineNumber;
98 private DialableMobileNumber mobileNumber;
106 * Regular user controller
109 private AddressbookUserWebSessionController userController;
112 * Event for when a user instance was created
116 private Event<ObservableHelperCreatedUserEvent> userCreatedEvent;
119 * Event for when a fax number instance was created
123 private Event<ObservableHelperCreatedFaxNumberEvent> faxNumberCreatedEvent;
126 * Event for when a land-line number instance was created
130 private Event<ObservableHelperCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
133 * Event for when a mobile number instance was created
137 private Event<ObservableHelperCreatedMobileNumberEvent> mobileNumberCreatedEvent;
140 * Default constructor
142 public AddressbookWebRequestHelperBean () {
143 // Call super constructor
146 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
147 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
151 public void copyContactToController () {
152 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
153 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
155 // Validate contact instance
156 if (this.getContact() == null) {
158 throw new NullPointerException("this.contact is null"); //NOI18N
159 } else if (this.getContact().getContactId() == null) {
161 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
162 } else if (this.getContact().getContactId() < 1) {
164 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
167 // Set all phone instances
168 this.setPhoneInstances(this.getContact());
170 // Set all fields: user
171 this.adminContactController.copyContactToController(this.getContact());
175 public void copyFaxNumberToController () {
176 // Validate fax instance
177 if (this.getFaxNumber() == null) {
179 throw new NullPointerException("this.faxNumber is null"); //NOI18N
180 } else if (this.getFaxNumber().getPhoneId() == null) {
182 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
183 } else if (this.getFaxNumber().getPhoneId() < 1) {
185 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
186 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
188 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
189 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
191 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
192 } else if (this.getFaxNumber().getPhoneCountry() == null) {
194 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
195 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
197 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
198 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
200 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
201 } else if (this.getFaxNumber().getPhoneNumber() == null) {
202 // Throw NPE again ...
203 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
204 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
206 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
210 this.faxNumberCreatedEvent.fire(new HelperCreatedFaxNumberEvent(this.getFaxNumber()));
214 public void copyLandLineNumberToController () {
215 // Validate land-line instance
216 if (this.getLandLineNumber() == null) {
218 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
219 } else if (this.getLandLineNumber().getPhoneId() == null) {
221 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
222 } else if (this.getLandLineNumber().getPhoneId() < 1) {
224 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
225 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
227 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
228 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
230 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
231 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
233 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
234 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
236 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
237 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
239 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
240 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
241 // Throw NPE again ...
242 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
243 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
245 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
249 this.landLineNumberCreatedEvent.fire(new HelperCreatedLandLineNumberEvent(this.getLandLineNumber()));
253 public void copyMobileNumberToController () {
254 // Validate mobile instance
255 if (this.getMobileNumber() == null) {
257 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
258 } else if (this.getMobileNumber().getPhoneId() == null) {
260 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
261 } else if (this.getMobileNumber().getPhoneId() < 1) {
263 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
264 } else if (this.getMobileNumber().getMobileProvider() == null) {
266 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
267 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
269 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
270 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
272 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
273 } else if (this.getMobileNumber().getPhoneNumber() == null) {
274 // Throw NPE again ...
275 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
276 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
278 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
282 this.mobileNumberCreatedEvent.fire(new HelperCreatedMobileNumberEvent(this.getMobileNumber()));
286 public void copyUserToController () {
288 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
290 // Validate user instance
291 if (this.getUser() == null) {
293 throw new NullPointerException("this.user is null"); //NOI18N
294 } else if (this.getUser().getUserId() == null) {
296 throw new NullPointerException("this.user.userId is null"); //NOI18N
297 } else if (this.getUser().getUserId() < 1) {
299 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
303 Contact userContact = this.getUser().getUserContact();
305 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
306 this.setContact(userContact);
308 // Set all phone instances
309 this.setPhoneInstances(userContact);
312 this.userCreatedEvent.fire(new HelperCreatedUserEvent(this.getUser()));
316 public Contact getContact () {
317 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
318 // System.out.println(MessageFormat.format("{0}: Returning this.contact={1}, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
323 public void setContact (final Contact contact) {
324 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
325 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
326 this.contact = contact;
330 public String getContactUsageMessageKey (final Contact contact) {
331 // The contact must be valid
332 if (null == contact) {
334 throw new NullPointerException("contact is null"); //NOI18N
335 } else if (contact.getContactId() == null) {
337 throw new NullPointerException("contact.contactId is null"); //NOI18N
338 } else if (contact.getContactId() < 1) {
340 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
343 // Default key is "unused"
344 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
346 // Check user contact
347 boolean isUserContact = this.userController.isContactFound(contact);
352 messageKey = "CONTACT_IS_USER"; //NOI18N
355 // Return message key
360 public DialableFaxNumber getFaxNumber () {
361 return this.faxNumber;
365 public void setFaxNumber (final DialableFaxNumber faxNumber) {
366 this.faxNumber = faxNumber;
370 public DialableLandLineNumber getLandLineNumber () {
371 return this.landLineNumber;
375 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
376 this.landLineNumber = landLineNumber;
380 public DialableMobileNumber getMobileNumber () {
381 return this.mobileNumber;
385 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
386 this.mobileNumber = mobileNumber;
390 public User getUser () {
395 public void setUser (final User user) {
400 * Set's all given contact's phone instances: land-line, mobile and
403 * @param contact Contact to set phone instances for
405 private void setPhoneInstances (final Contact contact) {
406 // The contact must be valid
407 if (null == contact) {
409 throw new NullPointerException("contact is null"); //NOI18N
410 } else if (contact.getContactId() == null) {
412 throw new NullPointerException("contact.contactId is null"); //NOI18N
413 } else if (contact.getContactId() < 1) {
415 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
419 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
420 // Yes, then set it in admin controller
421 this.setMobileNumber(contact.getContactMobileNumber());
425 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
426 // Yes, then set it in admin controller
427 this.setLandLineNumber(contact.getContactLandLineNumber());
431 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
432 // Yes, then set it in admin controller
433 this.setFaxNumber(contact.getContactFaxNumber());