]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java
Rewritten a lot:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / customer / PizzaCustomerWebSessionBean.java
index 1d910236fce8b71e38bc0917f5bfc6e6babab7c2..632083a2ed9f01e8d706325d6ee7d0ab8ea4c188 100644 (file)
@@ -23,9 +23,7 @@ import java.util.List;
 import java.util.Objects;
 import javax.annotation.PostConstruct;
 import javax.enterprise.context.SessionScoped;
-import javax.enterprise.event.Event;
 import javax.enterprise.event.Observes;
-import javax.enterprise.inject.Any;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -34,8 +32,10 @@ import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
-import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
+import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
+import org.mxchange.jcustomercore.events.customer.added.AdminAddedCustomerEvent;
 import org.mxchange.jcustomercore.model.customer.Customer;
+import org.mxchange.pizzaapplication.beans.BasePizzaController;
 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
 
@@ -46,7 +46,7 @@ import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBea
  */
 @Named ("customerController")
 @SessionScoped
-public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionController {
+public class PizzaCustomerWebSessionBean extends BasePizzaController implements PizzaCustomerWebSessionController {
 
        /**
         * Serial number
@@ -69,13 +69,6 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
         */
        private final ContactSessionBeanRemote contactBean;
 
-       /**
-        * An event being fired when an administrator has added a new customer
-        */
-       @Inject
-       @Any
-       private Event<AdminAddedCustomerEvent> customerAddedEvent;
-
        /**
         * A list of all customers
         */
@@ -99,7 +92,7 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
                        this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/admincustomer!de.chotime.jratecalc.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
 
                        // 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/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
                } catch (final NamingException e) {
                        // Throw again
                        throw new FaceletException(e);
@@ -107,30 +100,24 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
        }
 
        @Override
-       public void addCustomer (final Customer customer) {
-               // The contact must be valid
-               if (null == customer) {
-                       // Throw NPE
-                       throw new NullPointerException("customer is null"); //NOI18N
-               } else if (customer.getCustomerId() == null) {
-                       // Throw again ...
-                       throw new NullPointerException("customer.customerId is null"); //NOI18N
-               } else if (customer.getCustomerId() < 1) {
-                       // Not valid
-                       throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
-               } else if (customer.getCustomerContact() == null) {
+       public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
+               // The event must be valid
+               if (null == event) {
                        // Throw NPE
-                       throw new NullPointerException("customer.customerContact is null"); //NOI18N
-               } else if (customer.getCustomerContact().getContactId() == null) {
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getAddedContact() == null) {
                        // Throw again ...
-                       throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
-               } else if (customer.getCustomerContact().getContactId() < 1) {
+                       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("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
+                       throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
                }
 
-               // Add to list
-               this.customerList.add(customer);
+               // Call other method
+               this.selectableContacts.add(event.getAddedContact());
        }
 
        @Override
@@ -148,8 +135,23 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
                } else if (event.getAddedCustomer().getCustomerId() < 1) {
                        // Not valid
                        throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerId={0} is not valid", event.getAddedCustomer().getCustomerId())); //NOI18N //NOI18N
+               } else if (event.getAddedCustomer().getCustomerContact() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("event.addedCustomer.customerContact is null"); //NOI18N
+               } else if (event.getAddedCustomer().getCustomerContact().getContactId() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("event.addedCustomer.customerContact.contactId is null"); //NOI18N
+               } else if (event.getAddedCustomer().getCustomerContact().getContactId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerContact.contactId={0} is not valid", event.getAddedCustomer().getCustomerContact().getContactId())); //NOI18N
+               } else if (!this.selectableContacts.contains(event.getAddedCustomer().getCustomerContact())) {
+                       // Contact not in selectable list found
+                       throw new IllegalStateException(MessageFormat.format("event.addedCustomer.customerId={0} is not in selectableContacts list.", event.getAddedCustomer().getCustomerContact().getContactId()));
                }
 
+               // Remove this contact from selectable list
+               assert (this.selectableContacts.remove(event.getAddedCustomer().getCustomerContact())) : "contact was not removed"; //NOI18N
+
                // Call other method
                this.addCustomer(event.getAddedCustomer());
        }
@@ -247,4 +249,36 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
                return Collections.unmodifiableList(this.selectableContacts);
        }
 
+       /**
+        * Adds given customer instance to this controller. The customer should be
+        * an updated instance (with id number).
+        * <p>
+        * @param customer Customer instance
+        */
+       private void addCustomer (final Customer customer) {
+               // The contact must be valid
+               if (null == customer) {
+                       // Throw NPE
+                       throw new NullPointerException("customer is null"); //NOI18N
+               } else if (customer.getCustomerId() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("customer.customerId is null"); //NOI18N
+               } else if (customer.getCustomerId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
+               } else if (customer.getCustomerContact() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("customer.customerContact is null"); //NOI18N
+               } else if (customer.getCustomerContact().getContactId() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
+               } else if (customer.getCustomerContact().getContactId() < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
+               }
+
+               // Add to list
+               this.customerList.add(customer);
+       }
+
 }