]> git.mxchange.org Git - jfinancials-war.git/blobdiff - src/java/org/mxchange/jfinancials/beans/contact/FinancialsContactWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / contact / FinancialsContactWebRequestBean.java
index 8e9d7ecd3397dcfc10d0780637db30e5a51a0263..29316ec548ef7a859163a976abc54389c4268bf3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -21,11 +21,16 @@ import java.util.Date;
 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;
@@ -44,6 +49,7 @@ import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
 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;
 
@@ -92,6 +98,13 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
         */
        private FinancialsContactListWebViewController contactListController;
 
+       /**
+        * Event being fired when a user has updated his contact data
+        */
+       @Any
+       @Inject
+       private Event<ObservableUpdatedContactEvent> contactUpdatedEvent;
+
        /**
         * Country instance
         */
@@ -142,21 +155,6 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
         */
        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
         */
@@ -217,7 +215,7 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
         * @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
@@ -243,7 +241,7 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
         * @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
@@ -271,7 +269,7 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
         * @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
@@ -287,7 +285,27 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                }
 
                // 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
@@ -302,87 +320,20 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                // 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;
        }
 
@@ -394,7 +345,7 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                        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
@@ -402,7 +353,7 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                }
 
                // 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
@@ -421,16 +372,27 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                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
@@ -510,17 +472,6 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                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>
@@ -909,24 +860,6 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                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
         */
@@ -961,11 +894,11 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
        }
 
        /**
-        * 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());
@@ -983,26 +916,111 @@ public class FinancialsContactWebRequestBean extends BaseFinancialsBean implemen
                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);
                }
        }