]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaContactWebSessionBean.java
fixed imports to new location
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / PizzaContactWebSessionBean.java
index 2ca7817a8ea0d4f9be9d3a059c622a00a17e6d37..66e252ba4c04a3267b400ec7aeb17cd7424a4918 100644 (file)
@@ -34,9 +34,12 @@ import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
 import org.mxchange.jcontacts.contact.UserContact;
 import org.mxchange.jcontacts.contact.gender.Gender;
+import org.mxchange.jcontacts.contact.utils.ContactUtils;
+import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
 import org.mxchange.jcountry.data.Country;
+import org.mxchange.jcustomercore.events.customer.added.AdminAddedCustomerEvent;
 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
@@ -46,6 +49,7 @@ import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
+import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
 
@@ -153,11 +157,26 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
         */
        private Short houseNumber;
 
+       /**
+        * Whether a cellphone entry has been unlinked
+        */
+       private boolean isCellphoneUnlinked;
+
+       /**
+        * Whether a fax entry has been unlinked
+        */
+       private boolean isFaxUnlinked;
+
+       /**
+        * Whether a land-line number has been unlinked
+        */
+       private boolean isLandLineUnlinked;
+
        /**
         * Login bean (controller)
         */
        @Inject
-       private PizzaUserLoginWebSessionController loginController;
+       private PizzaUserLoginWebSessionController userLoginController;
 
        /**
         * Phone number area code
@@ -210,10 +229,88 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                this.emailAddressList.add(contactEmailAddress);
        }
 
+       @Override
+       public void afterAdminAddedContact (@Observes final AdminAddedContactEvent 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.customerId is null"); //NOI18N
+               } else if (event.getAddedContact().getContactId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
+               }
+
+               // Clear this bean
+               this.clear();
+
+               // Call other method
+               this.contactList.add(event.getAddedContact());
+       }
+
+       @Override
+       public void afterAdminAddedCustomer (@Observes final AdminAddedCustomerEvent event) {
+               // The event must be valid
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getAddedCustomer() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("event.addedCustomer is null"); //NOI18N
+               } else if (event.getAddedCustomer().getCustomerId() == null) {
+                       // ... and again
+                       throw new NullPointerException("event.addedCustomer.customerId is null"); //NOI18N
+               } else if (event.getAddedCustomer().getCustomerId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerId={0} is not valid", event.getAddedCustomer().getCustomerId())); //NOI18N //NOI18N
+               }
+
+               // Clear this bean
+               this.clear();
+
+               // Call other method
+               this.contactList.add(event.getAddedCustomer().getCustomerContact());
+       }
+
+       @Override
+       public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
+               // The event must be valid
+               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
+               } else if (event.getAddedUser().getUserContact() == null) {
+                       // userId is null
+                       throw new NullPointerException("event.addedUser.userContact is null"); //NOI18N
+               } else if (event.getAddedUser().getUserContact().getContactId() == null) {
+                       // userId is null
+                       throw new NullPointerException("event.addedUser.userContact.contactId is null"); //NOI18N
+               } else if (event.getAddedUser().getUserContact().getContactId() < 1) {
+                       // userId is null
+                       throw new IllegalArgumentException(MessageFormat.format("event.addedUser.userContact.contactId={0} is not valid", event.getAddedUser().getUserContact().getContactId())); //NOI18N
+               }
+
+               // Add user to local list
+               this.contactList.add(event.getAddedUser().getUserContact());
+       }
+
        @Override
        public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
                // Trace message
-               System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
 
                // event should not be null
                if (null == event) {
@@ -221,13 +318,13 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                        throw new NullPointerException("event is null"); //NOI18N
                } else if (event.getUpdatedContact()== null) {
                        // Throw NPE again
-                       throw new NullPointerException("event.user is null"); //NOI18N
+                       throw new NullPointerException("event.updatedUser is null"); //NOI18N
                } else if (event.getUpdatedContact().getContactId() == null) {
                        // userId is null
-                       throw new NullPointerException("event.user.userId is null"); //NOI18N
+                       throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
                } else if (event.getUpdatedContact().getContactId() < 1) {
                        // Not avalid id
-                       throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
+                       throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
                }
 
                // Get iterator from list
@@ -257,9 +354,9 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
        }
 
        @Override
-       public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
+       public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
                // Trace message
-               System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
 
                // event should not be null
                if (null == event) {
@@ -280,8 +377,7 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                Contact registeredContact = event.getRegisteredUser().getUserContact();
 
                // Debug message
-               System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
-
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
                // Copy all data from registered->user
                this.copyContact(registeredContact);
 
@@ -292,13 +388,13 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                this.clear();
 
                // Trace message
-               System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
        }
 
        @Override
-       public void afterUserLogin (final @Observes UserLoggedInEvent event) {
+       public void afterUserLogin (@Observes final UserLoggedInEvent event) {
                // Trace message
-               System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
 
                // event should not be null
                if (null == event) {
@@ -319,48 +415,20 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                this.copyContact(event.getLoggedInUser().getUserContact());
 
                // Trace message
-               System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
        }
 
-       /**
-        * Clears this bean
-        */
        @Override
-       public void clear () {
-               // Clear all data
-               // - personal data
-               this.setGender(Gender.UNKNOWN);
-               this.setFirstName(null);
-               this.setFamilyName(null);
-               this.setStreet(null);
-               this.setHouseNumber(null);
-               this.setZipCode(null);
-               this.setCity(null);
-               this.setCountry(null);
-
-               // - contact data
-               this.setEmailAddress(null);
-               this.setEmailAddressRepeat(null);
-               this.setPhoneAreaCode(null);
-               this.setCellphoneCarrier(null);
-               this.setFaxAreaCode(null);
-
-               // - other data
-               this.setBirthday(null);
-               this.setComment(null);
+       @SuppressWarnings ("ReturnOfCollectionOrArrayField")
+       public List<Contact> allContacts () {
+               return this.contactList;
        }
 
        @Override
        public Contact createContactInstance () {
-               // User message
-               //this.getLogger().logTrace("createContactInstance: CALLED!");
-
                // Required personal data must be set
                assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
 
-               // Create new contact instance
-               Contact localContact = new UserContact();
-
                // Generate phone number
                DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
                DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
@@ -374,6 +442,11 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                contact.setContactCity(this.getCity());
                contact.setContactCountry(this.getCountry());
                contact.setContactEmailAddress(this.getEmailAddress());
+               contact.setContactBirthday(this.getBirthday());
+               contact.setContactComment(this.getComment());
+
+               // Set ownContact
+               contact.setContactOwnContact(Boolean.TRUE);
 
                // Don't set null or wrong references
                if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
@@ -432,40 +505,31 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                        contact.setContactCellphoneNumber(cellphone);
                }
 
-               contact.setContactBirthday(this.getBirthday());
-               contact.setContactComment(this.getComment());
-
-               // Created timestamp and ownContact
-               contact.setContactOwnContact(Boolean.TRUE);
-
-               // Trace message
-               //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
-
                // Return it
-               return localContact;
+               return contact;
        }
 
        @Override
        public String doChangePersonalContactData () {
                // This method shall only be called if the user is logged-in
-               if (!this.loginController.isUserLoggedIn()) {
+               if (!this.userLoginController.isUserLoggedIn()) {
                        // Not logged-in
                        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
-               } else if (!this.loginController.ifCurrentPasswordMatches()) {
+               } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
                        // Password not matching
-                       throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
+                       throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
                }
 
                // Get contact instance
-               Contact contact = this.loginController.getLoggedInUser().getUserContact();
+               Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
 
                // It should be there, so run some tests on it
-               assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
-               assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
-               assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
+               assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
+               assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
+               assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
 
                // Update all fields
                contact.setContactGender(this.getGender());
@@ -477,49 +541,30 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                contact.setContactCity(this.getCity());
                contact.setContactCountry(this.getCountry());
 
-               // Is there a phone number?
-               if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
-                       // Debug message
-                       System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
+               // Update contact's cellphone number
+               this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
 
-                       // Yes, then update as well
-                       contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
-                       contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
-               }
-
-               // Is there a fax number?
-               if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
-                       // Debug message
-                       System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
+               // Update contact's land-line number
+               this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
 
-                       // Yes, then update as well
-                       contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
-                       contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
-               }
-
-               // Is there a cellphone number?
-               if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
-                       // Debug message
-                       System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: cellPhoneId={0}", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N
-
-                       // Yes, then update as well
-                       contact.getContactCellphoneNumber().setCellphoneProvider(this.getCellphoneCarrier());
-                       contact.getContactCellphoneNumber().setPhoneNumber(this.getCellphoneNumber());
-               }
+               // Update contact's fax number
+               this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
 
                // Send it to the EJB
-               this.contactBean.updateContactPersonalData(contact);
+               this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
 
                // All fine
                return "contact_data_saved"; //NOI18N
        }
 
        @Override
+       @SuppressWarnings ("ReturnOfDateField")
        public Date getBirthday () {
                return this.birthday;
        }
 
        @Override
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
        public void setBirthday (final Date birthday) {
                this.birthday = birthday;
        }
@@ -828,6 +873,38 @@ public class PizzaContactWebSessionBean implements PizzaContactWebSessionControl
                this.emailAddressList.add(contact.getContactEmailAddress());
        }
 
+       /**
+        * Clears this bean
+        */
+       private void clear () {
+               // Clear all data
+               // - personal data
+               this.setGender(Gender.UNKNOWN);
+               this.setFirstName(null);
+               this.setFamilyName(null);
+               this.setStreet(null);
+               this.setHouseNumber(null);
+               this.setZipCode(null);
+               this.setCity(null);
+               this.setCountry(null);
+
+               // - contact data
+               this.setEmailAddress(null);
+               this.setEmailAddressRepeat(null);
+               this.setPhoneCountry(null);
+               this.setPhoneAreaCode(null);
+               this.setPhoneNumber(null);
+               this.setCellphoneCarrier(null);
+               this.setCellphoneNumber(null);
+               this.setFaxCountry(null);
+               this.setFaxAreaCode(null);
+               this.setFaxNumber(null);
+
+               // - other data
+               this.setBirthday(null);
+               this.setComment(null);
+       }
+
        /**
         * Copies given contact into the controller
         * <p>