]> git.mxchange.org Git - addressbook-war.git/blobdiff - src/java/org/mxchange/addressbook/beans/contact/AddressbookContactWebSessionBean.java
Continued with fixes/rewrites:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / contact / AddressbookContactWebSessionBean.java
index 5e2010d63b38c72a637f255e6aba8b0a6224a908..6dcff57207fa1feb016b8c36839955df802f6989 100644 (file)
@@ -17,6 +17,7 @@
 package org.mxchange.addressbook.beans.contact;
 
 import java.text.MessageFormat;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
@@ -36,6 +37,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.update.AdminUpdatedContactEvent;
 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
 import org.mxchange.jcountry.data.Country;
@@ -211,7 +213,7 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
                        Context context = new InitialContext();
 
                        // Try to lookup
-                       this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
+                       this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
                } catch (final NamingException e) {
                        // Throw again
                        throw new FaceletException(e);
@@ -219,9 +221,27 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
        }
 
        @Override
-       public void addEmailAddress (final String contactEmailAddress) {
-               // Add it
-               this.emailAddressList.add(contactEmailAddress);
+       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
@@ -364,9 +384,12 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
        }
 
        @Override
-       @SuppressWarnings ("ReturnOfCollectionOrArrayField")
        public List<Contact> allContacts () {
-               return this.contactList;
+               // Debug message
+               /* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
+
+               // Return un-modified list
+               return Collections.unmodifiableList(this.contactList);
        }
 
        @Override
@@ -463,11 +486,7 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
 
                // Trace message
                //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
-               // Created timestamp and ownContact
-               contact.setContactOwnContact(Boolean.TRUE);
 
-               // Trace message
-               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
                // Return it
                return localContact;
        }
@@ -724,7 +743,7 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
 
        @Override
        public boolean hasContacts () {
-               return (!this.allContacts().isEmpty());
+               return (!this.contactList.isEmpty());
        }
 
        /**
@@ -741,6 +760,19 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
 
        @Override
        public boolean isEmailAddressRegistered (final Contact contact) {
+               // Cherck parameter
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("contact is null"); //NOI18N
+               } else if (contact.getContactEmailAddress() == null) {
+                       // Throw again
+                       throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
+               } else if (contact.getContactEmailAddress().isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
+               }
+
+               // Determine it
                return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
        }
 
@@ -808,21 +840,21 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
        }
 
        @Override
-       public void updateContactDataFromController (final Contact userContact) {
+       public void updateContactDataFromController (final Contact contact) {
                // Is the instance valid?
-               if (null == userContact) {
+               if (null == contact) {
                        // Throw NPE
-                       throw new NullPointerException("userContact is null"); //NOI18N
-               } else if (userContact.getContactId() == null) {
+                       throw new NullPointerException("contact is null"); //NOI18N
+               } else if (contact.getContactId() == null) {
                        // Throw NPE
-                       throw new NullPointerException("userContact.contactId is null"); //NOI18N
-               } else if (userContact.getContactId() < 1) {
+                       throw new NullPointerException("contact.contactId is null"); //NOI18N
+               } else if (contact.getContactId() < 1) {
                        // Not valid id number
-                       throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
+                       throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
                }
 
                // Set all
-               this.copyContact(userContact);
+               this.copyContact(contact);
        }
 
        /**
@@ -874,6 +906,18 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
         * @param contact Contact instance
         */
        private void copyContact (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
+               }
+
                // Copy all fields:
                // - base data
                this.setGender(contact.getContactGender());
@@ -884,6 +928,9 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
                this.setZipCode(contact.getContactZipCode());
                this.setCity(contact.getContactCity());
                this.setCountry(contact.getContactCountry());
+               this.setEmailAddress(contact.getContactEmailAddress());
+               this.setBirthday(contact.getContactBirthday());
+               this.setComment(contact.getContactComment());
 
                // Get cellphone, phone and fax instance
                DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
@@ -896,20 +943,45 @@ public class AddressbookContactWebSessionBean implements AddressbookContactWebSe
                        this.setPhoneAreaCode(phone.getPhoneAreaCode());
                        this.setPhoneNumber(phone.getPhoneNumber());
                }
+
                if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
                        this.setCellphoneCarrier(cellphone.getCellphoneProvider());
                        this.setCellphoneNumber(cellphone.getPhoneNumber());
                }
+
                if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
                        this.setFaxCountry(fax.getPhoneCountry());
                        this.setFaxAreaCode(fax.getPhoneAreaCode());
                        this.setFaxNumber(fax.getPhoneNumber());
                }
-               this.setEmailAddress(contact.getContactEmailAddress());
+       }
 
-               // -- other data
-               this.setBirthday(contact.getContactBirthday());
-               this.setComment(contact.getContactComment());
+       /**
+        * Removes given contact from all lists
+        * <p>
+        * @param contact Contact instance to remove
+        */
+       private void removeContact (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
+               }
+
+               // Remove from general list
+               if (!this.contactList.remove(contact)) {
+                       // Did not remove contact
+                       throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
+               }
+
+               // Remove from other lists
+               this.emailAddressList.remove(contact.getContactEmailAddress());
        }
 
 }