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.jfinancials.beans.contact.mobile;
19 import java.text.MessageFormat;
20 import java.util.Date;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.event.Observes;
25 import javax.enterprise.inject.Any;
26 import javax.faces.application.FacesMessage;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
30 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
32 import org.mxchange.jcontacts.events.mobile.linked.AdminLinkedMobileNumberEvent;
33 import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
34 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
35 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
36 import org.mxchange.jcontacts.model.contact.Contact;
37 import org.mxchange.jcontacts.model.phone.AdminContactsPhoneSessionBeanRemote;
38 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
39 import org.mxchange.jfinancials.beans.phone.FinancialsAdminPhoneWebRequestController;
40 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
41 import org.mxchange.jphone.exceptions.phone.PhoneNumberAlreadyLinkedException;
42 import org.mxchange.jphone.exceptions.phone.PhoneNumberNotLinkedException;
43 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
44 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
45 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
46 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
49 * An administrative contact mobile controller (bean)
51 * @author Roland Häder<roland@mxchange.org>
53 @Named ("adminContactMobileController")
55 public class FinancialsAdminContactMobileWebRequestBean extends BaseFinancialsBean implements FinancialsAdminContactMobileWebRequestController {
60 private static final long serialVersionUID = 542_145_347_919L;
63 * Administrative EJB for phone number
65 @EJB (lookup = "java:global/jfinancials-ejb/adminContactPhone!org.mxchange.jcontacts.model.phone.AdminContactsPhoneSessionBeanRemote")
66 private AdminContactsPhoneSessionBeanRemote adminContactPhoneBean;
69 * Event being fired when a mobile number has been linked
73 private Event<ObservableAdminLinkedMobileNumberEvent> adminLinkedMobileNumberEvent;
76 * Administrative phone controller
79 private FinancialsAdminPhoneWebRequestController adminPhoneController;
84 private Contact contact;
87 * When mobile number has been created
89 private Date mobileEntryCreated;
92 * When mobile number has been updated
94 private Date mobileEntryUpdated;
97 * Phone id (primary key)
99 private Long mobileId;
104 private Long mobileNumber;
107 * Event being fired when administrator unlinks mobile from contact
111 private Event<ObservableAdminUnlinkedMobileNumberEvent> mobileNumberUnlinkedEvent;
116 private MobileProvider mobileProvider;
119 * Default constructor
121 public FinancialsAdminContactMobileWebRequestBean () {
122 // Call super constructor
125 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
126 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
130 * Observes events being fired when an administrator has added a new
133 * @param event Event being fired
135 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
136 // The event must be valid
139 throw new NullPointerException("event is null"); //NOI18N
140 } else if (event.getAddedContact() == null) {
142 throw new NullPointerException("event.addedContact is null"); //NOI18N
143 } else if (event.getAddedContact().getContactId() == null) {
145 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
146 } else if (event.getAddedContact().getContactId() < 1) {
148 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
156 * Event observer for newly added users by administrator
158 * @param event Event being fired
160 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
161 // Event and contained entity instance should not be null
164 throw new NullPointerException("event is null"); //NOI18N
165 } else if (event.getAddedUser() == null) {
167 throw new NullPointerException("event.addedUser is null"); //NOI18N
168 } else if (event.getAddedUser().getUserId() == null) {
170 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
171 } else if (event.getAddedUser().getUserId() < 1) {
173 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
181 * Event observer for updated contact data by administrators
183 * @param event Updated contact data event
185 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
186 // Event and contained entity instance should not be null
189 throw new NullPointerException("event is null"); //NOI18N
190 } else if (event.getUpdatedContact() == null) {
192 throw new NullPointerException("event.updatedContact is null"); //NOI18N
193 } else if (event.getUpdatedContact().getContactId() == null) {
195 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
196 } else if (event.getUpdatedContact().getContactId() < 1) {
198 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
206 * Observer for events being fired when a bean helper has successfully
207 * created a contact instance.
209 * @param event Event being fired
211 public void afterCreatedContactEvent (@Observes final ObservableCreatedContactEvent event) {
213 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::afterCreatedContactEvent(): contact={0} - CALLED!", contact)); //NOI18N
215 // The event instance must be valid
218 throw new NullPointerException("event is null"); //NOI18N
219 } else if (event.getCreatedContact() == null) {
221 throw new NullPointerException("event.createdContact is null"); //NOI18N //NOI18N
222 } else if (event.getCreatedContact().getContactId() == null) {
224 throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N //NOI18N
225 } else if (event.getCreatedContact().getContactId() < 1) {
227 throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N
231 this.setContact(event.getCreatedContact());
235 * Observes events being fired when a bean helper has successfully created a
236 * mobile number instance.
238 * @param event Event being fired
240 public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
241 // The event instance must be valid
244 throw new NullPointerException("event is null"); //NOI18N
245 } else if (event.getMobileNumber() == null) {
247 throw new NullPointerException("event.mobileNumber is null"); //NOI18N
248 } else if (event.getMobileNumber().getMobileId() == null) {
249 // Throw NPE yet again
250 throw new NullPointerException("event.mobileNumber.mobileId is null"); //NOI18N
251 } else if (event.getMobileNumber().getMobileId() < 1) {
252 // Throw NPE yet again
253 throw new NullPointerException(MessageFormat.format("event.mobileNumber.mobileId={0} is invalid", event.getMobileNumber().getMobileId())); //NOI18N
256 // Get fax number from event
257 final DialableMobileNumber number = event.getMobileNumber();
259 // Copy all data to this bean
260 this.setMobileId(number.getMobileId());
261 this.setMobileProvider(number.getMobileProvider());
262 this.setMobileNumber(number.getMobileNumber());
263 this.setMobileEntryCreated(number.getMobileEntryCreated());
264 this.setMobileEntryUpdated(number.getMobileEntryUpdated());
268 * Links mobile number to contact from bean helper as "main mobile number".
270 * @return Redirect outcome
272 public String doLinkMainMobileNumber () {
273 // Get contact from helper
274 final Contact targetContact = this.getContact();
276 // Is all data properly set?
277 if (null == targetContact) {
279 throw new NullPointerException("targetContact is null"); //NOI18N
280 } else if (targetContact.getContactId() == null) {
282 throw new NullPointerException("targetContact.contactId is null"); //NOI18N
283 } else if (targetContact.getContactId() < 1) {
285 throw new IllegalArgumentException(MessageFormat.format("targetContact.contactId={0} is not valid", targetContact.getContactId())); //NOI18N
286 } else if (this.getMobileProvider() == null) {
288 throw new NullPointerException("this.mobileProvider is null"); //NOI18N
289 } else if (this.getMobileProvider().getProviderId() == null) {
291 throw new NullPointerException("this.mobileProvider.providerId is null"); //NOI18N
292 } else if (this.getMobileProvider().getProviderId() < 1) {
294 throw new NullPointerException(MessageFormat.format("this.mobileProvider.providerId={0} is invalid", this.getMobileProvider().getProviderId())); //NOI18N
295 } else if (this.getMobileNumber() == null) {
297 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
298 } else if (this.getMobileNumber() < 1) {
300 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber={0} is invalid", this.getMobileNumber())); //NOI18N
304 final Contact updatedContact;
305 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
310 updatedContact = this.adminContactPhoneBean.linkNewMobileNumberWithContact(targetContact, number);
311 } catch (final PhoneNumberAlreadyLinkedException ex) {
312 // Throw again as cause
313 this.showFacesMessage("form_add_contact_mobile:mobileNumber", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
318 this.adminLinkedMobileNumberEvent.fire(new AdminLinkedMobileNumberEvent(updatedContact, number));
320 // Return to contact profile
321 return "admin_show_contact"; //NOI18N
325 * Getter for contact instance
327 * @return Contact instance
329 public Contact getContact () {
334 * Setter for contact instance
336 * @param contact Contact instance
338 public void setContact (final Contact contact) {
339 this.contact = contact;
343 * Setter for phone id
347 public Long getMobileId () {
348 return this.mobileId;
352 * Getter for phone id
354 * @param mobileId Phone id
356 public void setMobileId (final Long mobileId) {
357 this.mobileId = mobileId;
361 * Getter for mobile number
363 * @return Mobile number
365 public Long getMobileNumber () {
366 return this.mobileNumber;
370 * Setter for mobile number
372 * @param mobileNumber Mobile number
374 public void setMobileNumber (final Long mobileNumber) {
375 this.mobileNumber = mobileNumber;
379 * Getter for mobile provider
381 * @return Mobile provider
383 public MobileProvider getMobileProvider () {
384 return this.mobileProvider;
388 * Setter for mobile provider
390 * @param mobileProvider Mobile provider
392 public void setMobileProvider (final MobileProvider mobileProvider) {
393 this.mobileProvider = mobileProvider;
397 * Unlinks mobile data with current contact
399 * @return Redirect outcome
401 public String unlinkMobileContactData () {
402 // Create fax number instance
403 final DialableMobileNumber number = this.createMobileNumber();
406 if (number == null) {
407 // Not set, throw NPE
408 throw new NullPointerException("number is null"); //NOI18N
409 } else if (number.getMobileId() == null) {
411 throw new NullPointerException("number.phoneId is null"); //NOI18N
412 } else if (number.getMobileId() < 1) {
414 throw new IllegalArgumentException(MessageFormat.format("number.phoneId={0} is not valid", number.getMobileId())); //NOI18N
415 } else if (number.getMobileProvider() == null) {
417 throw new NullPointerException("number.mobileProvider is null"); //NOI18N
418 } else if (number.getMobileProvider().getProviderId() == null) {
420 throw new NullPointerException("number.mobileProvider.providerId is null"); //NOI18N
421 } else if (number.getMobileProvider().getProviderId() < 1) {
423 throw new IllegalArgumentException(MessageFormat.format("number.mobileProvider.providerId={0} is not valid.", number.getMobileProvider().getProviderId())); //NOI18N
424 } else if (number.getMobileNumber() == null) {
426 throw new NullPointerException("number.phoneNumber is null"); //NOI18N
427 } else if (number.getMobileNumber() < 1) {
428 // Throw it again ...
429 throw new NullPointerException(MessageFormat.format("number.phoneNumber={0} is not valid.", number.getMobileNumber())); //NOI18N
430 } else if (this.getContact() == null) {
431 // ... and throw again
432 throw new NullPointerException("this.contact is null"); //NOI18N
433 } else if (this.getContact().getContactId() == null) {
435 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
436 } else if (this.getContact().getContactId() < 1) {
438 throw new IllegalArgumentException(MessageFormat.format("this.contact.contactId={0} is invalid.", this.getContact().getContactId())); //NOI18N
441 // Init contact instance
442 final Contact updatedContact;
445 // Unlink it and return contact without mobile instance
446 updatedContact = this.adminContactPhoneBean.unlinkMobileDataFromContact(this.getContact(), number);
447 } catch (final PhoneNumberNotLinkedException ex) {
449 this.showFacesMessage("form_unlink_contact_mobile:mobileNumberId", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
454 this.mobileNumberUnlinkedEvent.fire(new AdminUnlinkedMobileNumberEvent(updatedContact, number));
457 return "admin_show_contact"; //NOI18N
463 private void clear () {
468 * Returns an instance of a DialableMobileNumber from all fields stored in
471 * @return An instance of a DialableMobileNumber class
473 private DialableMobileNumber createMobileNumber () {
475 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
477 // Add all other data
478 number.setMobileEntryCreated(this.getMobileEntryCreated());
479 number.setMobileEntryUpdated(this.getMobileEntryUpdated());
482 if (this.getMobileId() instanceof Long) {
484 number.setMobileId(this.getMobileId());
492 * Getter for mobile entry created
494 * @return Mobile entry created
496 @SuppressWarnings ("ReturnOfDateField")
497 private Date getMobileEntryCreated () {
498 return this.mobileEntryCreated;
502 * Setter for mobile entry created
504 * @param mobileEntryCreated Mobile entry created
506 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
507 private void setMobileEntryCreated (final Date mobileEntryCreated) {
508 this.mobileEntryCreated = mobileEntryCreated;
512 * Getter for mobile entry updated
514 * @return Mobile entry updated
516 @SuppressWarnings ("ReturnOfDateField")
517 private Date getMobileEntryUpdated () {
518 return this.mobileEntryUpdated;
522 * Setter for mobile entry updated
524 * @param mobileEntryUpdated Mobile entry updated
526 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
527 private void setMobileEntryUpdated (final Date mobileEntryUpdated) {
528 this.mobileEntryUpdated = mobileEntryUpdated;