/*
- * Copyright (C) 2016 - 2020 Free Software Foundation
+ * 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
import java.util.Objects;
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.FacesException;
import javax.faces.application.FacesMessage;
-import javax.faces.view.facelets.FaceletException;
import javax.inject.Inject;
import javax.inject.Named;
+import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
+import org.mxchange.jcontacts.events.contact.update.UpdatedContactEvent;
+import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
import org.mxchange.jcontacts.model.contact.Contact;
import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
import org.mxchange.jcontacts.model.contact.Contacts;
import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
+import org.mxchange.jusercore.events.user.update.pre.ObservablePreUserPersonalDataUpdatedEvent;
import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
*/
private FinancialsContactListWebViewController contactListController;
+ /**
+ * Event being fired when a user has updated his contact data
+ */
+ @Any
+ @Inject
+ private Event<ObservableUpdatedContactEvent> contactUpdatedEvent;
+
/**
* Country instance
*/
*/
private String houseNumberExtension;
- /**
- * Whether a fax entry has been unlinked
- */
- private boolean isFaxUnlinked;
-
- /**
- * Whether a land-line number has been unlinked
- */
- private boolean isLandLineUnlinked;
-
- /**
- * Whether a mobile entry has been unlinked
- */
- private boolean isMobileUnlinked;
-
/**
* Phone number area code
*/
* @param event Event being fired
*/
public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
- // event should not be null
+ // Event and contained entity instance should not be null
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
* @param event Event being fired
*/
public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
- // event should not be null
+ // Event and contained entity instance should not be null
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
* @param event Event instance
*/
public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
- // event should not be null
+ // Event and contained entity instance should not be null
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
}
// Copy all data to this bean
- this.copyContact(event.getLoggedInUser().getUserContact());
+ this.copyFromContact(event.getLoggedInUser().getUserContact());
+ }
+
+ /**
+ * Observes events being fired before an updated of personal data done by a
+ * user has started.
+ * <p>
+ * @param event Event being observed
+ */
+ public void beforeUserUpdatedPersonalDataEvent (@Observes final ObservablePreUserPersonalDataUpdatedEvent event) {
+ // Is the instance valid?
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedUser() == null) {
+ // Throw NPE
+ throw new NullPointerException("event.updatedUser is null"); //NOI18N
+ }
+
+ // Set all
+ this.copyToContact(event.getUpdatedUser().getUserContact());
}
@Override
// Is all required data set?
if (!this.isRequiredPersonalDataSet()) {
// No, then abort here
- throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
+ throw new FacesException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
}
// Required personal data must be set
assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
- // Generate phone number
- DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
- DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
- DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
-
// Create new contact
- Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
- contact.setContactStreet(this.getStreet());
- contact.setContactHouseNumber(this.getHouseNumber());
- contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
- contact.setContactZipCode(this.getZipCode());
- contact.setContactCity(this.getCity());
- contact.setContactCountry(this.getCountry());
- contact.setContactEmailAddress(this.getEmailAddress());
- contact.setContactBirthday(this.getBirthday());
- contact.setContactComment(this.getComment());
-
- // Don't set null or wrong references
- if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
- // Now the number must be given
- if (phone.getPhoneAreaCode() == null) {
- // Is null
- throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
- } else if (phone.getPhoneAreaCode() < 1) {
- // Abort here
- throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
- } else if (phone.getPhoneNumber() == null) {
- // Is null
- throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
- } else if (phone.getPhoneNumber() < 1) {
- // Abort here
- throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
- }
-
- // Set phone number
- contact.setContactLandLineNumber(phone);
- }
+ final Contact contact = new UserContact(
+ this.getPersonalTitle(),
+ this.getFirstName(),
+ this.getFamilyName()
+ );
- // Don't set null or wrong references
- if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
- // Now the number must be given
- if (fax.getPhoneAreaCode() == null) {
- // Is null
- throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
- } else if (fax.getPhoneAreaCode() < 1) {
- // Abort here
- throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
- } else if (fax.getPhoneNumber() == null) {
- // Is null
- throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
- } else if (fax.getPhoneNumber() < 1) {
- // Abort here
- throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
- }
-
- // Set fax number
- contact.setContactFaxNumber(fax);
- }
-
- // Is the provider set?
- if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
- // Is the number set?
- if (mobile.getPhoneNumber() == null) {
- // Is null
- throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
- } else if (mobile.getPhoneNumber() < 1) {
- // Abort here
- throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
- }
-
- // Set mobile number
- contact.setContactMobileNumber(mobile);
- }
-
- // Return it
+ // Return instance
return contact;
}
throw new IllegalStateException("User is not logged-in"); //NOI18N
} else if (!this.isRequiredChangePersonalDataSet()) {
// Not all required fields are set
- throw new FaceletException("Not all required fields are set."); //NOI18N
+ throw new FacesException("Not all required fields are set."); //NOI18N
} else if (!this.userLoginController.ifCurrentPasswordMatches()) {
// Password not matching
this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()), FacesMessage.SEVERITY_ERROR); //NOI18N
}
// Get contact instance
- Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
+ final Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
// It should be there, so run some tests on it
assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
contact.setContactCountry(this.getCountry());
// Update contact's mobile number
- this.isMobileUnlinked = Contacts.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
+ final boolean isMobileUnlinked = Contacts.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
// Update contact's land-line number
- this.isLandLineUnlinked = Contacts.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+ final boolean isLandLineUnlinked = Contacts.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
// Update contact's fax number
- this.isFaxUnlinked = Contacts.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+ final boolean isFaxUnlinked = Contacts.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+ // Init updated contact instance
+ final Contact updatedContact;
- // Send it to the EJB
- this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+ try {
+ // Send it to the EJB
+ updatedContact = this.contactBean.updateContactData(contact, isMobileUnlinked, isLandLineUnlinked, isFaxUnlinked);
+ } catch (final ContactNotFoundException ex) {
+ // Throw as cause
+ throw new FacesException(ex);
+ }
+
+ // Fire event
+ this.contactUpdatedEvent.fire(new UpdatedContactEvent(updatedContact));
// All fine
return "contact_data_saved"; //NOI18N
this.comment = comment;
}
- @Override
- public String getControllerType () {
- return "general"; //NOI18N
- }
-
- @Override
- @Deprecated
- public void setControllerType (final String controllerType) {
- throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
- }
-
/**
* Getter for country instance
* <p>
return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
}
- @Override
- public void updateContactDataFromController (final Contact contact) {
- // Is the instance valid?
- if (null == contact) {
- // Throw NPE
- throw new NullPointerException("contact is null"); //NOI18N
- } else if (contact.getContactId() == null) {
- // Throw NPE
- throw new NullPointerException("contact.contactId is null"); //NOI18N
- } else if (contact.getContactId() < 1) {
- // Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
- }
-
- // Set all
- this.copyContact(contact);
- }
-
/**
* Clears this bean
*/
}
/**
- * Copies given contact into the controller
+ * Copies given contact data into the controller
* <p>
* @param contact Contact instance
*/
- private void copyContact (final Contact contact) {
+ private void copyFromContact (final Contact contact) {
// Copy all fields:
// - base data
this.setPersonalTitle(contact.getContactPersonalTitle());
this.setComment(contact.getContactComment());
// Get mobile, phone and fax instance
- DialableMobileNumber mobile = contact.getContactMobileNumber();
- DialableFaxNumber fax = contact.getContactFaxNumber();
- DialableLandLineNumber phone = contact.getContactLandLineNumber();
+ final DialableFaxNumber fax = contact.getContactFaxNumber();
+ final DialableLandLineNumber landLine = contact.getContactLandLineNumber();
+ final DialableMobileNumber mobile = contact.getContactMobileNumber();
- // - contact data
- if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
- this.setLandLineCountry(phone.getPhoneCountry());
- this.setLandLineAreaCode(phone.getPhoneAreaCode());
- this.setLandLineNumber(phone.getPhoneNumber());
+ // Is a fax number set?
+ if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
+ // Copy elements
+ this.setFaxCountry(fax.getPhoneCountry());
+ this.setFaxAreaCode(fax.getPhoneAreaCode());
+ this.setFaxNumber(fax.getPhoneNumber());
+ }
+
+ // Is a land-line number set?
+ if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneAreaCode() > 0)) {
+ // Copy elements
+ this.setLandLineCountry(landLine.getPhoneCountry());
+ this.setLandLineAreaCode(landLine.getPhoneAreaCode());
+ this.setLandLineNumber(landLine.getPhoneNumber());
}
+ // Is a mobile number set?
if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
+ // Copy elements
this.setMobileProvider(mobile.getMobileProvider());
- this.setMobileNumber(mobile.getPhoneNumber());
+ this.setMobileNumber(mobile.getMobileNumber());
}
+ }
- if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
- this.setFaxCountry(fax.getPhoneCountry());
- this.setFaxAreaCode(fax.getPhoneAreaCode());
- this.setFaxNumber(fax.getPhoneNumber());
+ /**
+ * Copies all fields from this backing bean into given instance.
+ * <p>
+ * @param contact An instance of a Contact class
+ */
+ private void copyToContact (final Contact contact) {
+ // Set other elements
+ contact.setContactStreet(this.getStreet());
+ contact.setContactHouseNumber(this.getHouseNumber());
+ contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
+ contact.setContactZipCode(this.getZipCode());
+ contact.setContactCity(this.getCity());
+ contact.setContactCountry(this.getCountry());
+ contact.setContactEmailAddress(this.getEmailAddress());
+ contact.setContactBirthday(this.getBirthday());
+ contact.setContactComment(this.getComment());
+
+ // Generate phone number
+ final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+ final DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
+ final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+ // Don't set null or wrong references
+ if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
+ // Now the number must be given
+ if (landLine.getPhoneAreaCode() == null) {
+ // Is null
+ throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
+ } else if (landLine.getPhoneAreaCode() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
+ } else if (landLine.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
+ } else if (landLine.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set phone number
+ contact.setContactLandLineNumber(landLine);
+ }
+
+ // Don't set null or wrong references
+ if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
+ // Now the number must be given
+ if (fax.getPhoneAreaCode() == null) {
+ // Is null
+ throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
+ } else if (fax.getPhoneAreaCode() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
+ } else if (fax.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
+ } else if (fax.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set fax number
+ contact.setContactFaxNumber(fax);
+ }
+
+ // Is the provider set?
+ if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
+ // Is the number set?
+ if (mobile.getMobileNumber() == null) {
+ // Is null
+ throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
+ } else if (mobile.getMobileNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set mobile number
+ contact.setContactMobileNumber(mobile);
}
}