]> 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 51a4c04727dfc82382bd7445755f480fb0f58183..6b77e0c161e1c86c8756322c81a9f60cb7da119c 100644 (file)
@@ -18,6 +18,8 @@ package org.mxchange.addressbook.beans.contact;
 
 import java.text.MessageFormat;
 import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 import javax.annotation.PostConstruct;
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.event.Event;
@@ -31,18 +33,25 @@ import javax.naming.NamingException;
 import org.mxchange.addressbook.beans.helper.AddressbookAdminWebRequestController;
 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.add.AdminContactAddedEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
+import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
 import org.mxchange.jcountry.data.Country;
+import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
+import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+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;
 
 /**
- * A user bean (controller)
+ * An administrative user bean (controller)
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
@@ -56,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
@@ -103,6 +112,12 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
         */
        private final ContactSessionBeanRemote contactBean;
 
+       /**
+        * General contact controller
+        */
+       @Inject
+       private AddressbookContactWebSessionController contactController;
+
        /**
         * Contact id
         */
@@ -158,6 +173,21 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
         */
        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;
+
        /**
         * Land-line id number
         */
@@ -216,44 +246,67 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
        }
 
        @Override
-       public String changeContactData () {
-               // Get contact instance
-               Contact contact = this.adminHelper.getContact();
-
-               // Check if contact instance is in helper and valid
-               if (null == contact) {
+       public String addContact () {
+               // Are all minimum fields set?
+               if (this.getGender() == null) {
                        // 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
+                       throw new NullPointerException("gender is null"); //NOI18N
+               } else if (this.getFirstName() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("firstName is null"); //NOI18N
+               } else if (this.getFirstName().isEmpty()) {
+                       // Empty string
+                       throw new IllegalStateException("firstName is empty"); //NOI18N
+               } else if (this.getFamilyName() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("familyName is null"); //NOI18N
+               } else if (this.getFamilyName().isEmpty()) {
+                       // Empty string
+                       throw new IllegalStateException("familyName is empty"); //NOI18N
                }
 
-               // Update all data in contact
-               this.updateContactData(contact);
+               // Create new contact instance
+               Contact contact = this.createContactInstance();
 
-               // Call EJB for updating contact data
-               Contact updatedContact = this.contactBean.updateContactPersonalData(contact);
+               // Default is not same contact
+               if (this.isSameContactFound(contact)) {
+                       // Already registered
+                       throw new FaceletException(new ContactAlreadyAddedException(contact));
+               }
 
-               // Update list
-               this.updateList(updatedContact);
+               // 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.updatedContactDataEvent.fire(new AdminContactUpdatedEvent(updatedContact));
+               this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
 
-               // Return to contact list (for now)
+               // Return outcome
                return "admin_list_contact"; //NOI18N
        }
 
+       @Override
+       public List<Contact> allContacts () {
+               return this.contactController.allContacts();
+       }
+
        @Override
        public void copyContactToController (final Contact contact) {
                // The contact instance must be valid
                if (null == contact) {
                        // Throw NPE again
                        throw new NullPointerException("contact is null"); //NOI18N
+               } else if (contact.getContactId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
                } else if (contact.getContactId() < 1) {
                        // Not valid
                        throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
@@ -292,11 +345,84 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
        }
 
        @Override
+       public Contact createContactInstance () {
+               // Generate phone number
+               DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
+               DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
+               DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+               // Create new instance
+               Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
+
+               // 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 it
+               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 () {
                return this.birthday;
        }
 
        @Override
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
        public void setBirthday (final Date birthday) {
                this.birthday = birthday;
        }
@@ -521,6 +647,11 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
                this.zipCode = zipCode;
        }
 
+       @Override
+       public boolean hasContacts () {
+               return (!this.allContacts().isEmpty());
+       }
+
        /**
         * Post-initialization of this class
         */
@@ -528,6 +659,68 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
        public void init () {
        }
 
+       /**
+        * 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.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);
+       }
+
+       /**
+        * Checks whether the given contact is found
+        * <p>
+        * @param contact Contact inastance
+        *
+        * @return Wether contact has been found
+        */
+       private boolean isSameContactFound (final Contact contact) {
+               // Default is not found
+               boolean IsFound = false;
+
+               // Get iterator
+               Iterator<Contact> iterator = this.allContacts().iterator();
+
+               // Loop through all
+               while (iterator.hasNext()) {
+                       // Get next contact
+                       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>
@@ -557,27 +750,13 @@ public class AddressbookAdminContactWebRequestBean implements AddressbookAdminCo
                contact.setContactCountry(this.getCountry());
 
                // Update contact's cellphone number
-               ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
+               this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
 
-               // Is there a phone number?
-               if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
-                       // Debug message
-                       System.out.println(MessageFormat.format("updateContactData: phoneId={0}", contact.getContactLandLineNumber().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.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("updateContactData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
-
-                       // Yes, then update as well
-                       contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
-                       contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
-               }
+               // Update contact's fax number
+               this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
        }
 
 }