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
156 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
157 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
161 * Copies currently set contact instances data to adminContactController
163 public void copyContactToController () {
164 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
165 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
167 // Validate contact instance
168 if (this.getContact() == null) {
170 throw new NullPointerException("this.contact is null"); //NOI18N
171 } else if (this.getContact().getContactId() == null) {
173 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
174 } else if (this.getContact().getContactId() < 1) {
176 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
179 // Set all phone instances
180 this.setPhoneInstances(this.getContact());
182 // Set all fields: user
183 this.contactCreatedEvent.fire(new CreatedContactEvent(this.getContact()));
187 * Copies currently set fax number's data to admin phone controller
189 public void copyFaxNumberToController () {
190 // Validate fax instance
191 if (this.getFaxNumber() == null) {
193 throw new NullPointerException("this.faxNumber is null"); //NOI18N
194 } else if (this.getFaxNumber().getPhoneId() == null) {
196 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
197 } else if (this.getFaxNumber().getPhoneId() < 1) {
199 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
200 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
202 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
203 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
206 } else if (this.getFaxNumber().getPhoneCountry() == null) {
208 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
209 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
211 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
212 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
214 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
215 } else if (this.getFaxNumber().getPhoneNumber() == null) {
216 // Throw NPE again ...
217 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
218 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
220 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
224 this.faxNumberCreatedEvent.fire(new CreatedFaxNumberEvent(this.getFaxNumber()));
228 * Copies currently set land-line number's data to admin phone controller
230 public void copyLandLineNumberToController () {
231 // Validate land-line instance
232 if (this.getLandLineNumber() == null) {
234 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
235 } else if (this.getLandLineNumber().getPhoneId() == null) {
237 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
238 } else if (this.getLandLineNumber().getPhoneId() < 1) {
240 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
241 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
243 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
244 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
246 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
247 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
249 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
250 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
252 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
253 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
255 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
256 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
257 // Throw NPE again ...
258 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
259 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
261 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
265 this.landLineNumberCreatedEvent.fire(new CreatedLandLineNumberEvent(this.getLandLineNumber()));
269 * Copies currently set mobile number's data to admin phone controller
271 public void copyMobileNumberToController () {
272 // Validate mobile instance
273 if (this.getMobileNumber() == null) {
275 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
276 } else if (this.getMobileNumber().getPhoneId() == null) {
278 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
279 } else if (this.getMobileNumber().getPhoneId() < 1) {
281 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
282 } else if (this.getMobileNumber().getMobileProvider() == null) {
284 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
285 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
287 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
288 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
290 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
291 } else if (this.getMobileNumber().getPhoneNumber() == null) {
292 // Throw NPE again ...
293 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
294 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
296 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
300 this.mobileNumberCreatedEvent.fire(new CreatedMobileNumberEvent(this.getMobileNumber()));
304 * Copies currently set user instances data to adminUserController
306 public void copyUserToController () {
308 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
310 // Validate user instance
311 if (this.getUser() == null) {
313 throw new NullPointerException("this.user is null"); //NOI18N
314 } else if (this.getUser().getUserId() == null) {
316 throw new NullPointerException("this.user.userId is null"); //NOI18N
317 } else if (this.getUser().getUserId() < 1) {
319 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
323 Contact userContact = this.getUser().getUserContact();
325 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
326 this.setContact(userContact);
328 // Set all phone instances
329 this.setPhoneInstances(userContact);
332 this.userCreatedEvent.fire(new CreatedUserEvent(this.getUser()));
336 * Getter for contact instance
338 * @return Contact instance
340 public Contact getContact () {
345 * Setter for contact instance
347 * @param contact Contact instance
349 public void setContact (final Contact contact) {
350 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
351 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
352 this.contact = contact;
356 * Returns a message key depending on if this contact is a user and/or a
357 * contact. If this contact is unused, a default key is returned.
359 * @param contact Contact instance to check
361 * @return Message key
363 public String getContactUsageMessageKey (final Contact contact) {
364 // The contact must be valid
365 if (null == contact) {
367 throw new NullPointerException("contact is null"); //NOI18N
368 } else if (contact.getContactId() == null) {
370 throw new NullPointerException("contact.contactId is null"); //NOI18N
371 } else if (contact.getContactId() < 1) {
373 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
376 // Default key is "unused"
377 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
379 // Check user contact
380 boolean isUserContact = this.userController.isContactFound(contact);
385 messageKey = "CONTACT_IS_USER"; //NOI18N
388 // Return message key
393 * Getter for dialable fax number
395 * @return Dialable fax number
397 public DialableFaxNumber getFaxNumber () {
398 return this.faxNumber;
402 * Setter for dialable fax number
404 * @param faxNumber Dialable fax number
406 public void setFaxNumber (final DialableFaxNumber faxNumber) {
407 this.faxNumber = faxNumber;
411 * Getter for dialable land-line number
413 * @return Dialable land-line number
415 public DialableLandLineNumber getLandLineNumber () {
416 return this.landLineNumber;
420 * Setter for dialable land-line number
422 * @param landLineNumber Dialable land-line number
424 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
425 this.landLineNumber = landLineNumber;
429 * Getter for dialable mobile number
431 * @return Dialable mobile number
433 public DialableMobileNumber getMobileNumber () {
434 return this.mobileNumber;
438 * Setter for dialable mobile number
440 * @param mobileNumber Dialable mobile number
442 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
443 this.mobileNumber = mobileNumber;
447 * Getter for user instance
449 * @return User instance
451 public User getUser () {
456 * Setter for user instance
458 * @param user User instance
460 public void setUser (final User user) {
465 * Set's all given contact's phone instances: land-line, mobile and
468 * @param contact Contact to set phone instances for
470 private void setPhoneInstances (final Contact contact) {
471 // The contact must be valid
472 if (null == contact) {
474 throw new NullPointerException("contact is null"); //NOI18N
475 } else if (contact.getContactId() == null) {
477 throw new NullPointerException("contact.contactId is null"); //NOI18N
478 } else if (contact.getContactId() < 1) {
480 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
484 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
485 // Yes, then set it in admin controller
486 this.setMobileNumber(contact.getContactMobileNumber());
490 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
491 // Yes, then set it in admin controller
492 this.setLandLineNumber(contact.getContactLandLineNumber());
496 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
497 // Yes, then set it in admin controller
498 this.setFaxNumber(contact.getContactFaxNumber());