]> git.mxchange.org Git - jjobs-war.git/commitdiff
Continued with contacts:
authorRoland Häder <roland@mxchange.org>
Wed, 27 Apr 2016 15:01:22 +0000 (17:01 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 29 Apr 2016 19:42:07 +0000 (21:42 +0200)
- added method addContact()
- introduced isSameContactFound() which checks if the given contact can be found in a list.
- this method can later be moved to ContactUtils to become generic.
- commented out noisy System.out messages
- removed no longer used logger messages

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestBean.java
src/java/org/mxchange/jjobs/beans/contact/JobsAdminContactWebRequestController.java

index 2362342448ad25887aedaaf83475d5eecc25489c..37e798b3800a10c1d224e1c815c9b4f0d6ae5e0f 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.jjobs.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;
@@ -31,13 +32,21 @@ import javax.naming.InitialContext;
 import javax.naming.NamingException;
 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.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.jjobs.beans.helper.JobsAdminWebRequestController;
+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;
 
@@ -232,6 +241,24 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques
 
        @Override
        public String addContact () {
+               // Are all minimum fields set?
+               if (this.getGender() == null) {
+                       // Throw NPE
+                       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
+               }
+
                // Create new contact instance
                Contact contact = this.createContactInstance();
 
@@ -256,9 +283,6 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques
                // Fire event
                this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
 
-               // Clear this bean
-               this.clear();
-
                // Return outcome
                return "admin_list_contact"; //NOI18N
        }
@@ -268,6 +292,39 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques
                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
@@ -319,6 +376,29 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques
                // 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
+               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
@@ -343,8 +423,8 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques
                // Clear bean
                this.clear();
 
-               // Return to contact list (for now)
-               return "admin_list_contact"; //NOI18N
+               // Return it
+               return contact;
        }
 
        @Override
@@ -586,6 +666,68 @@ public class JobsAdminContactWebRequestBean implements JobsAdminContactWebReques
        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>
index aa33a93eeccc0c299d1119939ef957de3564729e..8c973157e68f3d407c218a811a4285d4dea770d9 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.jjobs.beans.contact;
 
 import java.io.Serializable;
 import java.util.Date;
+import java.util.List;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.gender.Gender;
 import org.mxchange.jcountry.data.Country;
@@ -30,6 +31,35 @@ import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
  */
 public interface JobsAdminContactWebRequestController extends Serializable {
 
+       /**
+        * Adds contact data to database and redirects on success. If the contact is
+        * already found, a proper exception is thrown.
+        * <p>
+        * @return Redirect outcome
+        */
+       String addContact ();
+
+       /**
+        * Returns a list of all found contacts
+        * <p>
+        * @return A list of all contacts.
+        */
+       List<Contact> allContacts ();
+
+       /**
+        * Checks whether there are contacts.
+        * <p>
+        * @return Whether contacts are there
+        */
+       boolean hasContacts ();
+
+       /**
+        * Creates an instance from contact data
+        * <p>
+        * @return New contact instance
+        */
+       Contact createContactInstance ();
+
        /**
         * Copies given contact's data to this controller
         * <p>