--- /dev/null
+/*
+ * Copyright (C) 2016 - 2022 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jfinancials.beans.contact.mobile;
+
+import java.text.MessageFormat;
+import java.util.Date;
+import javax.ejb.EJB;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Event;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Any;
+import javax.faces.application.FacesMessage;
+import javax.inject.Inject;
+import javax.inject.Named;
+import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
+import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
+import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
+import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
+import org.mxchange.jcontacts.events.mobile.linked.AdminLinkedMobileNumberEvent;
+import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
+import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
+import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
+import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
+import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.jcontacts.model.mobile.AdminContactsMobileSessionBeanRemote;
+import org.mxchange.jfinancials.beans.BaseFinancialsBean;
+import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
+import org.mxchange.jphone.exceptions.mobile.MobileNumberAlreadyLinkedException;
+import org.mxchange.jphone.exceptions.mobile.MobileNumberNotLinkedException;
+import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
+import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
+import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
+import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
+
+/**
+ * An administrative contact mobile controller (bean)
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Named ("adminContactMobileController")
+@RequestScoped
+public class FinancialsAdminContactMobileWebRequestBean extends BaseFinancialsBean implements FinancialsAdminContactMobileWebRequestController {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 542_145_347_919L;
+
+ /**
+ * Administrative EJB for phone number
+ */
+ @EJB (lookup = "java:global/jfinancials-ejb/adminContactMobile!org.mxchange.jcontacts.model.mobile.AdminContactsMobileSessionBeanRemote")
+ private AdminContactsMobileSessionBeanRemote adminContactMobileBean;
+
+ /**
+ * Event being fired when a mobile number has been linked
+ */
+ @Inject
+ @Any
+ private Event<ObservableAdminLinkedMobileNumberEvent> adminLinkedMobileNumberEvent;
+
+ /**
+ * Contact instance
+ */
+ private Contact contact;
+
+ /**
+ * When mobile number has been created
+ */
+ private Date mobileEntryCreated;
+
+ /**
+ * When mobile number has been updated
+ */
+ private Date mobileEntryUpdated;
+
+ /**
+ * Phone id (primary key)
+ */
+ private Long mobileId;
+
+ /**
+ * Mobile number
+ */
+ private Long mobileNumber;
+
+ /**
+ * Event being fired when administrator unlinks mobile from contact
+ */
+ @Inject
+ @Any
+ private Event<ObservableAdminUnlinkedMobileNumberEvent> mobileNumberUnlinkedEvent;
+
+ /**
+ * Mobile provider
+ */
+ private MobileProvider mobileProvider;
+
+ /**
+ * Default constructor
+ */
+ public FinancialsAdminContactMobileWebRequestBean () {
+ // Call super constructor
+ super();
+
+ // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
+ // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
+ }
+
+ /**
+ * Observes events being fired when an administrator has added a new
+ * contact.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
+ // The event must be valid
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedContact() == null) {
+ // Throw again ...
+ throw new NullPointerException("event.addedContact is null"); //NOI18N
+ } else if (event.getAddedContact().getContactId() == null) {
+ // ... and again
+ throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
+ } else if (event.getAddedContact().getContactId() < 1) {
+ // Not valid
+ throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
+ }
+
+ // Clear this bean
+ this.clear();
+ }
+
+ /**
+ * Event observer for newly added users by administrator
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
+ // Event and contained entity instance should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.addedUser is null"); //NOI18N
+ } else if (event.getAddedUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
+ } else if (event.getAddedUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
+ }
+
+ // Clear all data
+ this.clear();
+ }
+
+ /**
+ * Event observer for updated contact data by administrators
+ * <p>
+ * @param event Updated contact data event
+ */
+ public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
+ // Event and contained entity instance should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.updatedContact is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() == null) {
+ // userId is null
+ throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
+ }
+
+ // Clear all data
+ this.clear();
+ }
+
+ /**
+ * Observer for events being fired when a bean helper has successfully
+ * created a contact instance.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterCreatedContactEvent (@Observes final ObservableCreatedContactEvent event) {
+ // Log message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::afterCreatedContactEvent(): contact={0} - CALLED!", contact)); //NOI18N
+
+ // The event instance must be valid
+ if (null == event) {
+ // Throw NPE again
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getCreatedContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.createdContact is null"); //NOI18N
+ } else if (event.getCreatedContact().getContactId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N
+ } else if (event.getCreatedContact().getContactId() < 1) {
+ // Not valid
+ throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N
+ }
+
+ // Set it here
+ this.setContact(event.getCreatedContact());
+ }
+
+ /**
+ * Observes events being fired when a bean helper has successfully created a
+ * mobile number instance.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
+ // The event instance must be valid
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getMobileNumber() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.mobileNumber is null"); //NOI18N
+ } else if (event.getMobileNumber().getMobileId() == null) {
+ // Throw NPE yet again
+ throw new NullPointerException("event.mobileNumber.mobileId is null"); //NOI18N
+ } else if (event.getMobileNumber().getMobileId() < 1) {
+ // Throw NPE yet again
+ throw new NullPointerException(MessageFormat.format("event.mobileNumber.mobileId={0} is invalid", event.getMobileNumber().getMobileId())); //NOI18N
+ }
+
+ // Get fax number from event
+ final DialableMobileNumber number = event.getMobileNumber();
+
+ // Copy all data to this bean
+ this.setMobileId(number.getMobileId());
+ this.setMobileProvider(number.getMobileProvider());
+ this.setMobileNumber(number.getMobileNumber());
+ this.setMobileEntryCreated(number.getMobileEntryCreated());
+ this.setMobileEntryUpdated(number.getMobileEntryUpdated());
+ }
+
+ /**
+ * Event observer for updated contact data by the user
+ * <p>
+ * @param event Updated contact data event
+ */
+ public void afterUpdatedContactDataEvent (@Observes final ObservableUpdatedContactEvent event) {
+ // Event and contained entity instance should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.updatedContact is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() == null) {
+ // userId is null
+ throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
+ }
+
+ // Clear all data
+ this.clear();
+ }
+
+ /**
+ * Links mobile number to contact from bean helper as "main mobile number".
+ * <p>
+ * @return Redirect outcome
+ */
+ public String doLinkMainMobileNumber () {
+ // Get contact from helper
+ final Contact targetContact = this.getContact();
+
+ // Is all data properly set?
+ if (null == targetContact) {
+ // Throw NPE
+ throw new NullPointerException("targetContact is null"); //NOI18N
+ } else if (targetContact.getContactId() == null) {
+ // Throw it again
+ throw new NullPointerException("targetContact.contactId is null"); //NOI18N
+ } else if (targetContact.getContactId() < 1) {
+ // Is not valid
+ throw new IllegalArgumentException(MessageFormat.format("targetContact.contactId={0} is not valid", targetContact.getContactId())); //NOI18N
+ } else if (this.getMobileProvider() == null) {
+ // Throw NPE
+ throw new NullPointerException("this.mobileProvider is null"); //NOI18N
+ } else if (this.getMobileProvider().getProviderId() == null) {
+ // Throw NPE
+ throw new NullPointerException("this.mobileProvider.providerId is null"); //NOI18N
+ } else if (this.getMobileProvider().getProviderId() < 1) {
+ // Throw NPE
+ throw new NullPointerException(MessageFormat.format("this.mobileProvider.providerId={0} is invalid", this.getMobileProvider().getProviderId())); //NOI18N
+ } else if (this.getMobileNumber() == null) {
+ // Throw NPE again
+ throw new NullPointerException("this.mobileNumber is null"); //NOI18N
+ } else if (this.getMobileNumber() < 1) {
+ // Invalid id number
+ throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber={0} is invalid", this.getMobileNumber())); //NOI18N
+ }
+
+ // Init instance
+ final Contact updatedContact;
+ final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
+
+ // Try it again
+ try {
+ // Link it, too
+ updatedContact = this.adminContactMobileBean.linkNewMobileNumberWithContact(targetContact, number);
+ } catch (final MobileNumberAlreadyLinkedException | ContactNotFoundException ex) {
+ // Throw again as cause
+ this.showFacesException("form_add_contact_mobile:mobileNumber", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
+ return ""; //NOI18N
+ }
+
+ // Fire event
+ this.adminLinkedMobileNumberEvent.fire(new AdminLinkedMobileNumberEvent(updatedContact, number));
+
+ // Return to contact profile
+ return "admin_show_contact"; //NOI18N
+ }
+
+ /**
+ * Getter for contact instance
+ * <p>
+ * @return Contact instance
+ */
+ public Contact getContact () {
+ return this.contact;
+ }
+
+ /**
+ * Setter for contact instance
+ * <p>
+ * @param contact Contact instance
+ */
+ public void setContact (final Contact contact) {
+ this.contact = contact;
+ }
+
+ /**
+ * Setter for phone id
+ * <p>
+ * @return Phone id
+ */
+ public Long getMobileId () {
+ return this.mobileId;
+ }
+
+ /**
+ * Getter for phone id
+ * <p>
+ * @param mobileId Phone id
+ */
+ public void setMobileId (final Long mobileId) {
+ this.mobileId = mobileId;
+ }
+
+ /**
+ * Getter for mobile number
+ * <p>
+ * @return Mobile number
+ */
+ public Long getMobileNumber () {
+ return this.mobileNumber;
+ }
+
+ /**
+ * Setter for mobile number
+ * <p>
+ * @param mobileNumber Mobile number
+ */
+ public void setMobileNumber (final Long mobileNumber) {
+ this.mobileNumber = mobileNumber;
+ }
+
+ /**
+ * Getter for mobile provider
+ * <p>
+ * @return Mobile provider
+ */
+ public MobileProvider getMobileProvider () {
+ return this.mobileProvider;
+ }
+
+ /**
+ * Setter for mobile provider
+ * <p>
+ * @param mobileProvider Mobile provider
+ */
+ public void setMobileProvider (final MobileProvider mobileProvider) {
+ this.mobileProvider = mobileProvider;
+ }
+
+ /**
+ * Unlinks mobile data with current contact
+ * <p>
+ * @return Redirect outcome
+ */
+ public String unlinkMobileContactData () {
+ // Create fax number instance
+ final DialableMobileNumber number = this.createMobileNumber();
+
+ // Is all data set
+ if (number == null) {
+ // Not set, throw NPE
+ throw new NullPointerException("number is null"); //NOI18N
+ } else if (number.getMobileId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("number.phoneId is null"); //NOI18N
+ } else if (number.getMobileId() < 1) {
+ // Invalid number
+ throw new IllegalArgumentException(MessageFormat.format("number.phoneId={0} is not valid", number.getMobileId())); //NOI18N
+ } else if (number.getMobileProvider() == null) {
+ // Throw NPE
+ throw new NullPointerException("number.mobileProvider is null"); //NOI18N
+ } else if (number.getMobileProvider().getProviderId() == null) {
+ // ... throw again
+ throw new NullPointerException("number.mobileProvider.providerId is null"); //NOI18N
+ } else if (number.getMobileProvider().getProviderId() < 1) {
+ // Id not valid
+ throw new IllegalArgumentException(MessageFormat.format("number.mobileProvider.providerId={0} is not valid.", number.getMobileProvider().getProviderId())); //NOI18N
+ } else if (number.getMobileNumber() == null) {
+ // Throw NPE again
+ throw new NullPointerException("number.phoneNumber is null"); //NOI18N
+ } else if (number.getMobileNumber() < 1) {
+ // Throw it again ...
+ throw new NullPointerException(MessageFormat.format("number.phoneNumber={0} is not valid.", number.getMobileNumber())); //NOI18N
+ } else if (this.getContact() == null) {
+ // ... and throw again
+ throw new NullPointerException("this.contact is null"); //NOI18N
+ } else if (this.getContact().getContactId() == null) {
+ // ... and again ...
+ throw new NullPointerException("this.contact.contactId is null"); //NOI18N
+ } else if (this.getContact().getContactId() < 1) {
+ // Invalid id number
+ throw new IllegalArgumentException(MessageFormat.format("this.contact.contactId={0} is invalid.", this.getContact().getContactId())); //NOI18N
+ }
+
+ // Init contact instance
+ final Contact updatedContact;
+
+ try {
+ // Unlink it and return contact without mobile instance
+ updatedContact = this.adminContactMobileBean.unlinkMobileDataFromContact(this.getContact(), number);
+ } catch (final MobileNumberNotLinkedException | ContactNotFoundException ex) {
+ // Did not work
+ this.showFacesException("form_unlink_contact_mobile:mobileNumberId", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
+ return ""; //NOI18N
+ }
+
+ // Fire event
+ this.mobileNumberUnlinkedEvent.fire(new AdminUnlinkedMobileNumberEvent(updatedContact, number));
+
+ // All fine here
+ return "admin_show_contact"; //NOI18N
+ }
+
+ /**
+ * Clears this bean
+ */
+ private void clear () {
+ // Clear all data
+ }
+
+ /**
+ * Returns an instance of a DialableMobileNumber from all fields stored in
+ * this bean.
+ * <p>
+ * @return An instance of a DialableMobileNumber class
+ */
+ private DialableMobileNumber createMobileNumber () {
+ // Initialize it
+ final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
+
+ // Add all other data
+ number.setMobileEntryCreated(this.getMobileEntryCreated());
+ number.setMobileEntryUpdated(this.getMobileEntryUpdated());
+
+ // Is id number set?
+ if (this.getMobileId() instanceof Long) {
+ // Set it
+ number.setMobileId(this.getMobileId());
+ }
+
+ // Return it
+ return number;
+ }
+
+ /**
+ * Getter for mobile entry created
+ * <p>
+ * @return Mobile entry created
+ */
+ @SuppressWarnings ("ReturnOfDateField")
+ private Date getMobileEntryCreated () {
+ return this.mobileEntryCreated;
+ }
+
+ /**
+ * Setter for mobile entry created
+ * <p>
+ * @param mobileEntryCreated Mobile entry created
+ */
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+ private void setMobileEntryCreated (final Date mobileEntryCreated) {
+ this.mobileEntryCreated = mobileEntryCreated;
+ }
+
+ /**
+ * Getter for mobile entry updated
+ * <p>
+ * @return Mobile entry updated
+ */
+ @SuppressWarnings ("ReturnOfDateField")
+ private Date getMobileEntryUpdated () {
+ return this.mobileEntryUpdated;
+ }
+
+ /**
+ * Setter for mobile entry updated
+ * <p>
+ * @param mobileEntryUpdated Mobile entry updated
+ */
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+ private void setMobileEntryUpdated (final Date mobileEntryUpdated) {
+ this.mobileEntryUpdated = mobileEntryUpdated;
+ }
+
+}