]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with customer:
authorRoland Häder <roland@mxchange.org>
Wed, 27 Apr 2016 09:13:37 +0000 (11:13 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 27 Apr 2016 20:17:13 +0000 (22:17 +0200)
- opps, tpyo fixed in class name
- added observing method if an administrator has added a customer

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java
src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionController.java

index a82e358cc8b5077d5c475a365527ecdb6a2b0c74..b0a3a01db41872a11b15549fd241696519358b12 100644 (file)
@@ -24,6 +24,7 @@ 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;
@@ -32,6 +33,7 @@ import javax.naming.Context;
 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.jcustomercore.model.customer.Customer;
 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
@@ -62,6 +64,11 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
         */
        private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
 
+       /**
+        * Remote user bean
+        */
+       private final ContactSessionBeanRemote contactBean;
+
        /**
         * An event being fired when an administrator has added a new customer
         */
@@ -74,6 +81,11 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
         */
        private List<Customer> customerList;
 
+       /**
+        * A list of all selectable contacts
+        */
+       private List<Contact> selectableContacts;
+
        /**
         * Default constructor
         */
@@ -85,6 +97,9 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
 
                        // Try to lookup
                        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
                } catch (final NamingException e) {
                        // Throw again
                        throw new FaceletException(e);
@@ -112,12 +127,33 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
                } 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);
        }
 
+       @Override
+       public void afterAdminAddedCustomer (@Observes final AdminAddedCustomerEvent event) {
+               // The event must be valid
+               if (null == event) {
+                       // Throw NPE
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getAddedCustomer() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("event.addedCustomer is null"); //NOI18N
+               } else if (event.getAddedCustomer().getCustomerId() == null) {
+                       // ... and again
+                       throw new NullPointerException("event.addedCustomer.customerId is null"); //NOI18N
+               } 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
+               }
+
+               // Call other method
+               this.addCustomer(event.getAddedCustomer());
+       }
+
        @Override
        public List<Customer> allCustomers () {
                // Return it
@@ -136,6 +172,37 @@ public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionContr
        public void init () {
                // Initialize customer list
                this.customerList = this.adminCustomerBean.allCustomers();
+
+               // Get all contacts
+               List<Contact> allContacts = this.contactBean.getAllContacts();
+
+               // Get iterator
+               Iterator<Contact> iterator = allContacts.iterator();
+
+               // Loop through it
+               while (iterator.hasNext()) {
+                       // Get next element
+                       Contact next = iterator.next();
+
+                       // Get iterator
+                       Iterator<Customer> userIterator = this.customerList.iterator();
+
+                       // Loop through all users
+                       while (userIterator.hasNext()) {
+                               // Get user instance
+                               Customer nextCustomer = userIterator.next();
+
+                               // Is contact same?
+                               if (Objects.equals(next, nextCustomer.getCustomerContact())) {
+                                       // Found same
+                                       iterator.remove();
+                                       break;
+                               }
+                       }
+               }
+
+               // Set contact list
+               this.selectableContacts = allContacts;
        }
 
        @Override
index 098f529bdcef38d1f0b573083d15249962507649..be6035f079dbf5c927b1926e2199b7dca7009e24 100644 (file)
@@ -19,6 +19,7 @@ package org.mxchange.pizzaapplication.beans.customer;
 import java.io.Serializable;
 import java.util.List;
 import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
 import org.mxchange.jcustomercore.model.customer.Customer;
 
 /**
@@ -36,6 +37,14 @@ public interface PizzaCustomerWebSessionController extends Serializable {
         */
        void addCustomer (final Customer customer);
 
+       /**
+        * Observes events being fired when an administrator has added a new
+        * customer
+        * <p>
+        * @param event Event being fired
+        */
+       void afterAdminAddedCustomer (final AdminAddedCustomerEvent event);
+
        /**
         * Checks whether the given contact is a customer
         * <p>