From: Roland Häder Date: Wed, 27 Apr 2016 09:13:37 +0000 (+0200) Subject: Continued with customer: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a530e1ff81cefb6f55bfaec532528030501632c0;p=pizzaservice-war.git Continued with customer: - opps, tpyo fixed in class name - added observing method if an administrator has added a customer Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java index a82e358c..b0a3a01d 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java @@ -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 customerList; + /** + * A list of all selectable contacts + */ + private List 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 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 allContacts = this.contactBean.getAllContacts(); + + // Get iterator + Iterator iterator = allContacts.iterator(); + + // Loop through it + while (iterator.hasNext()) { + // Get next element + Contact next = iterator.next(); + + // Get iterator + Iterator 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 diff --git a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionController.java index 098f529b..be6035f0 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionController.java @@ -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 + *

+ * @param event Event being fired + */ + void afterAdminAddedCustomer (final AdminAddedCustomerEvent event); + /** * Checks whether the given contact is a customer *