]> git.mxchange.org Git - addressbook-war.git/blobdiff - src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java
Don't cherry-pick:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / AddressbookAdminContactWebRequestBean.java
index 9f3ed0cf6ad5c26251d3310985b286f8e2605e01..11396494db84dc780b5229bd812ec6dd442fab3f 100644 (file)
@@ -18,22 +18,25 @@ package org.mxchange.addressbook.beans.contact;
 
 import java.text.MessageFormat;
 import java.util.Date;
-import java.util.Iterator;
 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.view.facelets.FaceletException;
+import javax.faces.FacesException;
 import javax.inject.Inject;
 import javax.inject.Named;
 import org.mxchange.addressbook.beans.BaseAddressbookBean;
+import org.mxchange.addressbook.beans.contact.list.AddressbookContactListWebViewController;
 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
+import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent;
+import org.mxchange.jcontacts.events.contact.deleted.ObservableAdminDeletedContactEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
+import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
 import org.mxchange.jcontacts.model.contact.AdminContactSessionBeanRemote;
 import org.mxchange.jcontacts.model.contact.Contact;
 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
@@ -98,16 +101,15 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
        private String comment;
 
        /**
-        * EJB for general contact purposes
+        * Current contact instance
         */
-       @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
-       private ContactSessionBeanRemote contactBean;
+       private Contact contact;
 
        /**
-        * General contact controller
+        * EJB for general contact purposes
         */
-       @Inject
-       private AddressbookContactWebRequestController contactController;
+       @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
+       private ContactSessionBeanRemote contactBean;
 
        /**
         * Country instance
@@ -119,6 +121,19 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
         */
        private Long contactId;
 
+       /**
+        * An instance of a contact list controller
+        */
+       @Inject
+       private AddressbookContactListWebViewController contactListController;
+
+       /**
+        * Event being fired when an administrator has deleted a contact
+        */
+       @Any
+       @Inject
+       private Event<ObservableAdminDeletedContactEvent> deletedContactEvent;
+
        /**
         * Email address
         */
@@ -247,10 +262,8 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
        /**
         * Adds contact data to database and redirects on success. If the contact is
         * already found, a proper exception is thrown.
-        * <p>
-        * @return Redirect outcome
         */
-       public String addContact () {
+       public void addContact () {
                // Are all minimum fields set?
                if (this.getPersonalTitle() == null) {
                        // Throw NPE
@@ -270,12 +283,12 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                }
 
                // Create new contact instance
-               final Contact contact = this.createContactInstance();
+               final Contact createdContact = this.createContactInstance();
 
                // Default is not same contact
-               if (this.isSameContactFound(contact)) {
+               if (this.contactListController.isContactFound(createdContact)) {
                        // Already registered
-                       throw new FaceletException(new ContactAlreadyAddedException(contact));
+                       throw new FacesException(new ContactAlreadyAddedException(createdContact));
                }
 
                // Init contact
@@ -284,10 +297,10 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                // Try to call EJB
                try {
                        // Call EJB
-                       updatedContact = this.adminContactBean.addContact(contact);
+                       updatedContact = this.adminContactBean.addContact(createdContact);
                } catch (final ContactAlreadyAddedException ex) {
                        // Throw again
-                       throw new FaceletException(ex);
+                       throw new FacesException(ex);
                }
 
                // Fire event
@@ -295,9 +308,6 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
 
                // Clear this bean
                this.clear();
-
-               // Return outcome
-               return "admin_list_contact"; //NOI18N
        }
 
        /**
@@ -316,58 +326,61 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                        throw new NullPointerException("event is null"); //NOI18N
                } else if (event.getCreatedContact() == null) {
                        // Throw NPE again
-                       throw new NullPointerException("event.createdContact is null"); //NOI18N //NOI18N
+                       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 //NOI18N
+                       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 contact for e.g. delete method
+               this.setContact(event.getCreatedContact());
+
                // Get contact instance from event
-               final Contact contact = event.getCreatedContact();
+               final Contact createdContact = event.getCreatedContact();
 
                // Set all fields: contact
-               this.setContactId(contact.getContactId());
-               this.setAcademicTitle(contact.getContactTitle());
-               this.setBirthday(contact.getContactBirthday());
-               this.setCity(contact.getContactCity());
-               this.setComment(contact.getContactComment());
-               this.setContactCountry(contact.getContactCountry());
-               this.setEmailAddress(contact.getContactEmailAddress());
-               this.setFamilyName(contact.getContactFamilyName());
-               this.setFirstName(contact.getContactFirstName());
-               this.setPersonalTitle(contact.getContactPersonalTitle());
-               this.setHouseNumber(contact.getContactHouseNumber());
-               this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
-               this.setStreet(contact.getContactStreet());
-               this.setZipCode(contact.getContactZipCode());
+               this.setContactId(createdContact.getContactId());
+               this.setAcademicTitle(createdContact.getContactTitle());
+               this.setBirthday(createdContact.getContactBirthday());
+               this.setCity(createdContact.getContactCity());
+               this.setComment(createdContact.getContactComment());
+               this.setContactCountry(createdContact.getContactCountry());
+               this.setEmailAddress(createdContact.getContactEmailAddress());
+               this.setFamilyName(createdContact.getContactFamilyName());
+               this.setFirstName(createdContact.getContactFirstName());
+               this.setPersonalTitle(createdContact.getContactPersonalTitle());
+               this.setHouseNumber(createdContact.getContactHouseNumber());
+               this.setHouseNumberExtension(createdContact.getContactHouseNumberExtension());
+               this.setStreet(createdContact.getContactStreet());
+               this.setZipCode(createdContact.getContactZipCode());
 
                // Is the cell phone set?
-               if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
+               if (createdContact.getContactMobileNumber() instanceof DialableMobileNumber) {
                        // ... cmobile data
-                       this.setMobileId(contact.getContactMobileNumber().getPhoneId());
-                       this.setMobileProvider(contact.getContactMobileNumber().getMobileProvider());
-                       this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
+                       this.setMobileId(createdContact.getContactMobileNumber().getMobileId());
+                       this.setMobileProvider(createdContact.getContactMobileNumber().getMobileProvider());
+                       this.setMobileNumber(createdContact.getContactMobileNumber().getMobileNumber());
                }
 
                // Is the fax set?
-               if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
+               if (createdContact.getContactFaxNumber() instanceof DialableFaxNumber) {
                        // ... fax data
-                       this.setFaxId(contact.getContactFaxNumber().getPhoneId());
-                       this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
-                       this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
-                       this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
+                       this.setFaxId(createdContact.getContactFaxNumber().getPhoneId());
+                       this.setFaxAreaCode(createdContact.getContactFaxNumber().getPhoneAreaCode());
+                       this.setFaxCountry(createdContact.getContactFaxNumber().getPhoneCountry());
+                       this.setFaxNumber(createdContact.getContactFaxNumber().getPhoneNumber());
                }
 
                // Is the land-line number set?
-               if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+               if (createdContact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
                        // .. land-line data
-                       this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
-                       this.setLandLineAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
-                       this.setLandLineCountry(contact.getContactLandLineNumber().getPhoneCountry());
-                       this.setLandLineNumber(contact.getContactLandLineNumber().getPhoneNumber());
+                       this.setLandLineId(createdContact.getContactLandLineNumber().getPhoneId());
+                       this.setLandLineAreaCode(createdContact.getContactLandLineNumber().getPhoneAreaCode());
+                       this.setLandLineCountry(createdContact.getContactLandLineNumber().getPhoneCountry());
+                       this.setLandLineNumber(createdContact.getContactLandLineNumber().getPhoneNumber());
                }
 
                // Log message
@@ -404,17 +417,26 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                }
 
                // 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());
+               DialableLandLineNumber landLine = null;
+               DialableMobileNumber mobile = null;
+               DialableFaxNumber fax = null;
+
+               // Are all fields set?
+               if (this.getLandLineAreaCode() != null && this.getLandLineCountry() instanceof Country && this.getLandLineNumber() != null) {
+                       // Init instance
+                       landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+               }
 
-               // Create new instance
-               final Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
+               // Are all fields set?
+               if (this.getMobileProvider() instanceof MobileProvider && this.getMobileNumber() != null) {
+                       // Initialize instance
+                       mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
+               }
 
-               // Is contact id set?
-               if (this.getContactId() instanceof Long) {
-                       // Set it, too
-                       contact.setContactId(this.getContactId());
+               // Are all fields set?
+               if (this.getFaxAreaCode() != null && this.getFaxCountry() instanceof Country && this.getFaxNumber() != null) {
+                       // Initialize instance
+                       fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
                }
 
                // Create new instance
@@ -422,19 +444,21 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                                          this.getPersonalTitle(),
                                          this.getFirstName(),
                                          this.getFamilyName(),
-                                         this.getContactCountry()
+                                         this.getContactCountry(),
+                                         Boolean.FALSE,
+                                         this.getStreet(),
+                                         this.getHouseNumber(),
+                                         this.getHouseNumberExtension(),
+                                         this.getZipCode(),
+                                         this.getCity(),
+                                         this.getEmailAddress(),
+                                         this.getAcademicTitle(),
+                                         this.getBirthday(),
+                                         this.getComment()
                          );
 
-               // Add all others
-               localContact.setContactBirthday(this.getBirthday());
-               localContact.setContactCity(this.getCity());
-               localContact.setContactComment(this.getComment());
-               localContact.setContactEmailAddress(this.getEmailAddress());
-               localContact.setContactHouseNumber(this.getHouseNumber());
+               // Add missing fields
                localContact.setContactId(this.getContactId());
-               localContact.setContactStreet(this.getStreet());
-               localContact.setContactTitle(this.getAcademicTitle());
-               localContact.setContactZipCode(this.getZipCode());
 
                // 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)) {
@@ -444,17 +468,17 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                                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
+                               throw new IllegalStateException("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
+                               throw new IllegalStateException("phone.phoneNumber is zero or below."); //NOI18N
                        }
 
                        // Set phone number
-                       contact.setContactLandLineNumber(landLine);
+                       localContact.setContactLandLineNumber(landLine);
                }
 
                // Don't set null or wrong references
@@ -465,73 +489,109 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                                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
+                               throw new IllegalStateException("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
+                               throw new IllegalStateException("fax.phoneNumber is zero or below."); //NOI18N
                        }
 
                        // Set fax number
-                       contact.setContactFaxNumber(fax);
+                       localContact.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) {
+                       if (mobile.getMobileNumber() == null) {
                                // Is null
                                throw new NullPointerException("cmobile.phoneNumber is null"); //NOI18N
-                       } else if (mobile.getPhoneNumber() < 1) {
+                       } else if (mobile.getMobileNumber() < 1) {
                                // Abort here
-                               throw new IllegalArgumentException("cmobile.phoneNumber is zero or below."); //NOI18N
+                               throw new IllegalStateException("cmobile.phoneNumber is zero or below."); //NOI18N
                        }
 
                        // Set cmobile number
-                       contact.setContactMobileNumber(mobile);
+                       localContact.setContactMobileNumber(mobile);
                }
 
                // Return it
-               return contact;
+               return localContact;
+       }
+
+       /**
+        * Deletes currently chosen contact and returns to list view
+        *
+        * @return
+        */
+       public String deleteContactData () {
+               // Is contact set?
+               if (this.getContact() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("this.contact is null");
+               } else if (this.getContact().getContactId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("this.contact.contactId is null");
+               } else if (this.getContact().getContactId() < 1) {
+                       // Throw IAE
+                       throw new NullPointerException(MessageFormat.format("this.contact.contactId={0} is not valid.", this.getContact().getContactId()));
+               }
+
+               try {
+                       // Invoke EJB
+                       this.adminContactBean.deleteContactData(this.getContact());
+               } catch (final ContactNotFoundException ex) {
+                       // Throw it again
+                       throw new FacesException(ex);
+               }
+
+               // Fire event
+               this.deletedContactEvent.fire(new AdminDeletedContactEvent(this.getContact()));
+
+               // Return to list view
+               return "admin_list_contacts";
        }
 
        /**
         * Edits currently loaded contact's data in database.
-        * <p>
-        * @return Redirect outcome
         */
-       public String editContactData () {
+       public void editContactData () {
                // Get contact instance
-               final Contact contact = this.createContactInstance();
+               final Contact createdContact = this.createContactInstance();
 
                // Check if contact instance is in helper and valid
-               if (null == contact) {
+               if (null == createdContact) {
                        // Throw NPE
                        throw new NullPointerException("beanHelper.contact is null"); //NOI18N
-               } else if (contact.getContactId() == null) {
+               } else if (createdContact.getContactId() == null) {
                        // Throw NPE again
-                       throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
-               } else if (contact.getContactId() < 1) {
+                       throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N
+               } else if (createdContact.getContactId() < 1) {
                        // Invalid id
-                       throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
+                       throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", createdContact.getContactId())); //NOI18N
                }
 
                // Update all data in contact
-               this.updateContactData(contact);
+               this.updateContactData(createdContact);
+
+               // Init updated contact instance
+               final Contact updatedContact;
 
-               // Call EJB for updating contact data
-               final Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+               try {
+                       // Call EJB for updating contact data
+                       updatedContact = this.contactBean.updateContactData(createdContact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+               } catch (final ContactNotFoundException ex) {
+                       // Throw as a cause
+                       throw new FacesException(ex);
+               }
 
                // Fire event
                this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
 
                // Clear bean
                this.clear();
-
-               // Return to contact list (for now)
-               return "admin_list_contact"; //NOI18N
        }
 
        /**
@@ -553,7 +613,7 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                                         "%s%d%d", //NOI18N
                                         mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
                                         mobileNumber.getMobileProvider().getProviderDialPrefix(),
-                                        mobileNumber.getPhoneNumber()
+                                        mobileNumber.getMobileNumber()
                         );
 
                // Return it
@@ -661,6 +721,24 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                this.comment = comment;
        }
 
+       /**
+        * 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;
+       }
+
        /**
         * Getter for contactCountry instance
         * <p>
@@ -702,12 +780,6 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                return "admin"; //NOI18N
        }
 
-       @Override
-       @Deprecated
-       public void setControllerType (final String controllerType) {
-               throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
-       }
-
        /**
         * Getter for email address
         * <p>
@@ -1082,6 +1154,8 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
         */
        private void clear () {
                // Clear all data
+               this.setContact(null);
+
                // - personal data
                this.setAcademicTitle(null);
                this.setFirstName(null);
@@ -1109,37 +1183,6 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                this.setComment(null);
        }
 
-       /**
-        * Checks whether the given contact is found
-        * <p>
-        * @param contact Contact instance
-        *
-        * @return Whether contact has been found
-        */
-       private boolean isSameContactFound (final Contact contact) {
-               // Default is not found
-               boolean IsFound = false;
-
-               // Get iterator
-               final Iterator<Contact> iterator = this.contactController.allContacts().iterator();
-
-               // Loop through all
-               while (iterator.hasNext()) {
-                       // Get next contact
-                       final Contact next = iterator.next();
-
-                       // Is the same?
-                       if (ContactUtils.isSameContact(contact, next)) {
-                               // Yes, then abort loop
-                               IsFound = false;
-                               break;
-                       }
-               }
-
-               // Return status
-               return IsFound;
-       }
-
        /**
         * Updates all data in contact instance.
         * <p>
@@ -1152,7 +1195,7 @@ public class AddressbookAdminContactWebRequestBean extends BaseAddressbookBean i
                        throw new NullPointerException("contact is null"); //NOI18N
                } else if (contact.getContactId() == null) {
                        // Throw NPE again
-                       throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
+                       throw new NullPointerException("contact.contactId is null"); //NOI18N
                } else if (contact.getContactId() < 1) {
                        // Invalid id
                        throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N