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;
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;
*/
@Named ("customerController")
@SessionScoped
-public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionController {
+public class PizzaCustomerWebSessionBean extends BasePizzaController implements PizzaCustomerWebSessionController {
/**
* Serial number
*/
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
*/
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);
}
@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
} 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());
}
return isFound;
}
+ @Override
+ public List<Contact> selectableContacts () {
+ 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);
+ }
+
}