]> git.mxchange.org Git - addressbook-war.git/blobdiff - src/java/org/mxchange/addressbook/beans/contact/AddressbookAdminContactWebRequestBean.java
it is this way ...
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / AddressbookAdminContactWebRequestBean.java
index fadc833a31dd42243623c767686dd557c9f18159..6b77e0c161e1c86c8756322c81a9f60cb7da119c 100644 (file)
@@ -36,6 +36,7 @@ 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.add.AdminContactAddedEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
@@ -48,7 +49,6 @@ import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
-import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
 
 /**
  * An administrative user bean (controller)
@@ -65,11 +65,11 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
        private static final long serialVersionUID = 542_145_347_916L;
 
        /**
-        * An event fired when the administrator has added a new user
+        * An event fired when the administrator has added a new contact
         */
        @Inject
        @Any
-       private Event<AdminAddedUserEvent> addedUserEvent;
+       private Event<AdminAddedContactEvent> addedContactEvent;
 
        /**
         * Admin helper instance
@@ -112,6 +112,12 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
         */
        private final ContactSessionBeanRemote contactBean;
 
+       /**
+        * General contact controller
+        */
+       @Inject
+       private AddressbookContactWebSessionController contactController;
+
        /**
         * Contact id
         */
@@ -292,39 +298,6 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
                return this.contactController.allContacts();
        }
 
-       @Override
-       public String changeContactData () {
-               // Get contact instance
-               Contact contact = this.adminHelper.getContact();
-
-               // Default is not same contact
-               if (this.isSameContactFound(contact)) {
-                       // Already registered
-                       throw new FaceletException(new ContactAlreadyAddedException(contact));
-               }
-
-               // Init contact
-               Contact updatedContact;
-
-               // Try to call EJB
-               try {
-                       // Call EJB
-                       updatedContact = this.contactBean.addContact(contact);
-               } catch (final ContactAlreadyAddedException ex) {
-                       // Throw again
-                       throw new FaceletException(ex);
-               }
-
-               // Fire event
-               this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
-
-               // Clear this bean
-               this.clear();
-
-               // Return outcome
-               return "admin_list_contact"; //NOI18N
-       }
-
        @Override
        public void copyContactToController (final Contact contact) {
                // The contact instance must be valid
@@ -371,24 +344,6 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
                this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
        }
 
-       @Override
-       public String editContactData () {
-               // Get contact instance
-               Contact contact = this.adminHelper.getContact();
-
-               // Is the land-line number set?
-               if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
-                       // .. land-line data
-                       this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
-                       this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
-                       this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
-                       this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
-               }
-
-               // Log message
-               //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
-       }
-
        @Override
        public Contact createContactInstance () {
                // Generate phone number
@@ -427,6 +382,39 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
                return contact;
        }
 
+       @Override
+       public String editContactData () {
+               // Get contact instance
+               Contact contact = this.adminHelper.getContact();
+
+               // Check if contact instance is in helper and valid
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("adminHelper.contact is null"); //NOI18N
+               } else if (contact.getContactId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("adminHelper.contact.contactId is null"); //NOI18N //NOI18N
+               } else if (contact.getContactId() < 1) {
+                       // Invalid id
+                       throw new IllegalStateException(MessageFormat.format("adminHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
+               }
+
+               // Update all data in contact
+               this.updateContactData(contact);
+
+               // Call EJB for updating contact data
+               Contact updatedContact = this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+
+               // Fire event
+               this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
+
+               // Clear bean
+               this.clear();
+
+               // Return to contact list (for now)
+               return "admin_list_contact"; //NOI18N
+       }
+
        @Override
        @SuppressWarnings ("ReturnOfDateField")
        public Date getBirthday () {
@@ -659,6 +647,11 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
                this.zipCode = zipCode;
        }
 
+       @Override
+       public boolean hasContacts () {
+               return (!this.allContacts().isEmpty());
+       }
+
        /**
         * Post-initialization of this class
         */