]> git.mxchange.org Git - pizzaservice-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>
Wed, 27 Apr 2016 20:22:55 +0000 (22:22 +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

src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java
src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestController.java

index bbbc007a48f38336132a9029e77473aa4348da82..5a53d610d676f8753786b93b222d4c2c80a6df70 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.pizzaapplication.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;
@@ -35,8 +36,10 @@ 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;
@@ -242,6 +245,54 @@ public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequ
                }
        }
 
+       @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();
+
+               // 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));
+
+               // Return outcome
+               return "admin_list_contact"; //NOI18N
+       }
+
        @Override
        public List<Contact> allContacts () {
                return this.contactController.allContacts();
@@ -280,7 +331,7 @@ public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequ
        @Override
        public void copyContactToController (final Contact contact) {
                // Log message
-               System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
 
                // The contact instance must be valid
                if (null == contact) {
@@ -335,14 +386,11 @@ public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequ
                }
 
                // Log message
-               System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
        }
 
        @Override
        public Contact createContactInstance () {
-               // User message
-               //this.getLogger().logTrace("createContactInstance: CALLED!");
-
                // Generate phone number
                DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
                DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
@@ -422,9 +470,6 @@ public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequ
                        contact.setContactCellphoneNumber(cellphone);
                }
 
-               // Trace message
-               //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
-
                // Return it
                return contact;
        }
@@ -702,6 +747,37 @@ public class PizzaAdminContactWebRequestBean implements PizzaAdminContactWebRequ
                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 e22b92de9935daf937139af8ddb6d7103c5ac3f0..8fb5577f2fa312b96c064d73cef73129e975736d 100644 (file)
@@ -31,12 +31,20 @@ import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
  */
 public interface PizzaAdminContactWebRequestController 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();
+       List<Contact> allContacts ();
 
        /**
         * Checks whether there are contacts.