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.jjobs.beans.BaseJobsController;
27 import org.mxchange.jjobs.beans.contact.JobsAdminContactWebRequestController;
28 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
29 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
30 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
31 import org.mxchange.jphone.events.helper.fax.created.HelperCreatedFaxNumberEvent;
32 import org.mxchange.jphone.events.helper.fax.created.ObservableHelperCreatedFaxNumberEvent;
33 import org.mxchange.jphone.events.helper.landline.created.HelperCreatedLandLineNumberEvent;
34 import org.mxchange.jphone.events.helper.landline.created.ObservableHelperCreatedLandLineNumberEvent;
35 import org.mxchange.jphone.events.helper.mobile.created.HelperCreatedMobileNumberEvent;
36 import org.mxchange.jphone.events.helper.mobile.created.ObservableHelperCreatedMobileNumberEvent;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
40 import org.mxchange.jusercore.events.user.helper.created.HelperCreatedUserEvent;
41 import org.mxchange.jusercore.events.user.helper.created.ObservableHelperCreatedUserEvent;
42 import org.mxchange.jusercore.model.user.User;
45 * A general helper for beans
47 * @author Roland Häder<roland@mxchange.org>
51 public class JobsWebRequestHelperBean extends BaseJobsController implements JobsWebRequestHelperController {
54 * Call-stack instance (5 may show BeanELResolver.getValue as caller)
56 private static final short THREAD_STACK = 5;
61 private static final long serialVersionUID = 17_258_793_567_145_701L;
64 * Administrative contact controller
67 private JobsAdminContactWebRequestController adminContactController;
70 * Administrative phone controller
73 private JobsAdminPhoneWebRequestController adminPhoneController;
76 * Administrative user controller
79 private JobsAdminUserWebRequestController adminUserController;
84 private Contact contact;
89 private DialableFaxNumber faxNumber;
94 private DialableLandLineNumber landLineNumber;
99 private DialableMobileNumber mobileNumber;
107 * Regular user controller
110 private JobsUserWebSessionController userController;
113 * Event for when a user instance was created
117 private Event<ObservableHelperCreatedUserEvent> userCreatedEvent;
120 * Event for when a fax number instance was created
124 private Event<ObservableHelperCreatedFaxNumberEvent> faxNumberCreatedEvent;
127 * Event for when a land-line number instance was created
131 private Event<ObservableHelperCreatedLandLineNumberEvent> landLineNumberCreatedEvent;
134 * Event for when a mobile number instance was created
138 private Event<ObservableHelperCreatedMobileNumberEvent> mobileNumberCreatedEvent;
141 * Default constructor
143 public JobsWebRequestHelperBean () {
144 // Call super constructor
147 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
148 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
152 public void copyContactToController () {
153 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
154 // System.out.println(MessageFormat.format("{0}.copyContactToController: CALLED, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
156 // Validate contact instance
157 if (this.getContact() == null) {
159 throw new NullPointerException("this.contact is null"); //NOI18N
160 } else if (this.getContact().getContactId() == null) {
162 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
163 } else if (this.getContact().getContactId() < 1) {
165 throw new IllegalStateException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId())); //NOI18N
168 // Set all phone instances
169 this.setPhoneInstances(this.getContact());
171 // Set all fields: user
172 this.adminContactController.copyContactToController(this.getContact());
176 public void copyFaxNumberToController () {
177 // Validate fax instance
178 if (this.getFaxNumber() == null) {
180 throw new NullPointerException("this.faxNumber is null"); //NOI18N
181 } else if (this.getFaxNumber().getPhoneId() == null) {
183 throw new NullPointerException("this.faxNumber.phoneId is null"); //NOI18N
184 } else if (this.getFaxNumber().getPhoneId() < 1) {
186 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneId={0} is not valid", this.getFaxNumber().getPhoneId())); //NOI18N
187 } else if (this.getFaxNumber().getPhoneAreaCode() == null) {
189 throw new NullPointerException("this.faxNumber.phoneAreaCode is null"); //NOI18N
190 } else if (this.getFaxNumber().getPhoneAreaCode() < 1) {
192 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
193 } else if (this.getFaxNumber().getPhoneCountry() == null) {
195 throw new NullPointerException("this.faxNumber.phoneCountry is null"); //NOI18N
196 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() == null) {
198 throw new NullPointerException("this.faxNumber.phoneCountry.countryId is null"); //NOI18N
199 } else if (this.getFaxNumber().getPhoneCountry().getCountryId() < 1) {
201 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneCountry.countryId={0} is invalid", this.getFaxNumber().getPhoneCountry().getCountryId())); //NOI18N
202 } else if (this.getFaxNumber().getPhoneNumber() == null) {
203 // Throw NPE again ...
204 throw new NullPointerException("this.faxNumber.phoneNumber is null"); //NOI18N
205 } else if (this.getFaxNumber().getPhoneNumber() < 1) {
207 throw new IllegalArgumentException(MessageFormat.format("this.faxNumber.phoneNumber={0} is not valid", this.getFaxNumber().getPhoneNumber())); //NOI18N
211 this.faxNumberCreatedEvent.fire(new HelperCreatedFaxNumberEvent(this.getFaxNumber()));
215 public void copyLandLineNumberToController () {
216 // Validate land-line instance
217 if (this.getLandLineNumber() == null) {
219 throw new NullPointerException("this.landLineNumber is null"); //NOI18N
220 } else if (this.getLandLineNumber().getPhoneId() == null) {
222 throw new NullPointerException("this.landLineNumber.phoneId is null"); //NOI18N
223 } else if (this.getLandLineNumber().getPhoneId() < 1) {
225 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneId={0} is not valid", this.getLandLineNumber().getPhoneId())); //NOI18N
226 } else if (this.getLandLineNumber().getPhoneAreaCode() == null) {
228 throw new NullPointerException("this.landLineNumber.phoneAreaCode is null"); //NOI18N
229 } else if (this.getLandLineNumber().getPhoneAreaCode() < 1) {
231 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneAreaCode={0} is not valid", this.getFaxNumber().getPhoneAreaCode())); //NOI18N
232 } else if (this.getLandLineNumber().getPhoneCountry() == null) {
234 throw new NullPointerException("this.landLineNumber.phoneCountry is null"); //NOI18N
235 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() == null) {
237 throw new NullPointerException("this.landLineNumber.phoneCountry.countryId is null"); //NOI18N
238 } else if (this.getLandLineNumber().getPhoneCountry().getCountryId() < 1) {
240 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneCountry.countryId={0} is invalid", this.getLandLineNumber().getPhoneCountry().getCountryId())); //NOI18N
241 } else if (this.getLandLineNumber().getPhoneNumber() == null) {
242 // Throw NPE again ...
243 throw new NullPointerException("this.landLineNumber.phoneNumber is null"); //NOI18N
244 } else if (this.getLandLineNumber().getPhoneNumber() < 1) {
246 throw new IllegalArgumentException(MessageFormat.format("this.landLineNumber.phoneNumber={0} is not valid", this.getLandLineNumber().getPhoneNumber())); //NOI18N
250 this.landLineNumberCreatedEvent.fire(new HelperCreatedLandLineNumberEvent(this.getLandLineNumber()));
254 public void copyMobileNumberToController () {
255 // Validate mobile instance
256 if (this.getMobileNumber() == null) {
258 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
259 } else if (this.getMobileNumber().getPhoneId() == null) {
261 throw new NullPointerException("this.mobileNumber.phoneId is null"); //NOI18N
262 } else if (this.getMobileNumber().getPhoneId() < 1) {
264 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneId={0} is not valid", this.getMobileNumber().getPhoneId())); //NOI18N
265 } else if (this.getMobileNumber().getMobileProvider() == null) {
267 throw new NullPointerException("this.mobileNumber.mobileProvider is null"); //NOI18N
268 } else if (this.getMobileNumber().getMobileProvider().getProviderId() == null) {
270 throw new NullPointerException("this.mobileNumber.mobileProvider.providerId is null"); //NOI18N
271 } else if (this.getMobileNumber().getMobileProvider().getProviderId() < 1) {
273 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.mobileProvider.providerId={0} is invalid", this.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
274 } else if (this.getMobileNumber().getPhoneNumber() == null) {
275 // Throw NPE again ...
276 throw new NullPointerException("this.mobileNumber.phoneNumber is null"); //NOI18N
277 } else if (this.getMobileNumber().getPhoneNumber() < 1) {
279 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber.phoneNumber={0} is not valid", this.getMobileNumber().getPhoneNumber())); //NOI18N
283 this.mobileNumberCreatedEvent.fire(new HelperCreatedMobileNumberEvent(this.getMobileNumber()));
287 public void copyUserToController () {
289 //* NOISY-DEBUG: */ System.out.println("AdminHelper::copyUserToController - CALLED!"); //NOI18N
291 // Validate user instance
292 if (this.getUser() == null) {
294 throw new NullPointerException("this.user is null"); //NOI18N
295 } else if (this.getUser().getUserId() == null) {
297 throw new NullPointerException("this.user.userId is null"); //NOI18N
298 } else if (this.getUser().getUserId() < 1) {
300 throw new IllegalStateException(MessageFormat.format("this.user.userId={0} is not valid.", this.getUser().getUserId())); //NOI18N
304 Contact userContact = this.getUser().getUserContact();
306 // Set contact here, too. This avoids parameters that cannot auto-complete in IDEs.
307 this.setContact(userContact);
309 // Set all phone instances
310 this.setPhoneInstances(userContact);
313 this.userCreatedEvent.fire(new HelperCreatedUserEvent(this.getUser()));
317 public Contact getContact () {
318 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
319 // System.out.println(MessageFormat.format("{0}: Returning this.contact={1}, caller: {2}", this.getClass().getSimpleName(), this.contact, caller));
324 public void setContact (final Contact contact) {
325 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[THREAD_STACK].getClassName(), Thread.currentThread().getStackTrace()[THREAD_STACK].getMethodName());
326 // System.out.println(MessageFormat.format("{0}: Setting contact={1}, previous: {2}, caller: {3}", this.getClass().getSimpleName(), contact, this.contact, caller));
327 this.contact = contact;
331 public String getContactUsageMessageKey (final Contact contact) {
332 // The contact must be valid
333 if (null == contact) {
335 throw new NullPointerException("contact is null"); //NOI18N
336 } else if (contact.getContactId() == null) {
338 throw new NullPointerException("contact.contactId is null"); //NOI18N
339 } else if (contact.getContactId() < 1) {
341 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
344 // Default key is "unused"
345 String messageKey = "CONTACT_IS_UNUSED"; //NOI18N
347 // Check user contact
348 boolean isUserContact = this.userController.isContactFound(contact);
353 messageKey = "CONTACT_IS_USER"; //NOI18N
356 // Return message key
361 public DialableFaxNumber getFaxNumber () {
362 return this.faxNumber;
366 public void setFaxNumber (final DialableFaxNumber faxNumber) {
367 this.faxNumber = faxNumber;
371 public DialableLandLineNumber getLandLineNumber () {
372 return this.landLineNumber;
376 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
377 this.landLineNumber = landLineNumber;
381 public DialableMobileNumber getMobileNumber () {
382 return this.mobileNumber;
386 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
387 this.mobileNumber = mobileNumber;
391 public User getUser () {
396 public void setUser (final User user) {
401 * Set's all given contact's phone instances: land-line, mobile and
404 * @param contact Contact to set phone instances for
406 private void setPhoneInstances (final Contact contact) {
407 // The contact must be valid
408 if (null == contact) {
410 throw new NullPointerException("contact is null"); //NOI18N
411 } else if (contact.getContactId() == null) {
413 throw new NullPointerException("contact.contactId is null"); //NOI18N
414 } else if (contact.getContactId() < 1) {
416 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
420 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
421 // Yes, then set it in admin controller
422 this.setMobileNumber(contact.getContactMobileNumber());
426 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
427 // Yes, then set it in admin controller
428 this.setLandLineNumber(contact.getContactLandLineNumber());
432 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
433 // Yes, then set it in admin controller
434 this.setFaxNumber(contact.getContactFaxNumber());