*/
package org.mxchange.pizzaapplication.beans.customer;
-import java.util.Collections;
-import java.util.List;
+import java.text.MessageFormat;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Any;
import javax.faces.view.facelets.FaceletException;
+import javax.inject.Inject;
import javax.inject.Named;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
+import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.gender.Gender;
+import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
+import org.mxchange.jcustomercore.events.customer.added.AdminAddedCustomerEvent;
+import org.mxchange.jcustomercore.events.customer.added.CustomerAdminAddedEvent;
+import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
import org.mxchange.jcustomercore.model.customer.Customer;
+import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
+import org.mxchange.pizzaapplication.beans.BasePizzaController;
+import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
+import org.mxchange.pizzaapplication.model.customer.PizzaCustomer;
+import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
/**
* Administrative customer bean (controller)
*/
@Named ("adminCustomerController")
@RequestScoped
-public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
+public class PizzaAdminCustomerWebRequestBean extends BasePizzaController implements PizzaAdminCustomerWebRequestController {
/**
* Serial number
*/
private static final long serialVersionUID = 12_487_062_487_527_913L;
+ /**
+ * Administrative contact controller (for personal data)
+ */
+ @Inject
+ private PizzaAdminContactWebRequestController adminContactController;
+
/**
* Administrative customer EJB
*/
private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
/**
- * A list of all customers
+ * Admin helper instance
+ */
+ @Inject
+ private PizzaWebRequestController adminHelper;
+
+ /**
+ * An event being fired when an administrator has added a new customer
*/
- private List<Customer> customerList;
+ @Inject
+ @Any
+ private Event<AdminAddedCustomerEvent> customerAddedEvent;
+
+ /**
+ * General customer controller
+ */
+ @Inject
+ private PizzaCustomerWebSessionController customerController;
/**
* Default constructor
Context context = new InitialContext();
// Try to lookup
- this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
+ this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
} catch (final NamingException e) {
// Throw again
throw new FaceletException(e);
}
@Override
- public List<Customer> allCustomers () {
- // Return it
- return Collections.unmodifiableList(this.customerList);
+ public String addCustomer () {
+ // Are at least some fields added?
+ if ((this.adminHelper.getContact() == null) && (!this.isCustomerDataSet())) {
+ // Not all customer data is set
+ throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
+ }
+
+ // Init contact
+ Contact contact;
+
+ // Is the contact set in helper?
+ if (this.adminHelper.getContact() instanceof Contact) {
+ // Get from helper
+ contact = this.adminHelper.getContact();
+ } else {
+ // Get new contact instance
+ contact = this.adminContactController.createContactInstance();
+ }
+
+ // Ask the EJB for a free customer number
+ String customerNumber = this.adminCustomerBean.createCustomerNumber();
+
+ // Create new customer instance
+ Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
+
+ // Init instance
+ Customer updatedCustomer;
+
+ try {
+ // Add/link customer and return updated
+ if (this.adminHelper.getContact() instanceof Contact) {
+ // Link customer
+ updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
+
+ // Remove contact instance
+ this.adminHelper.setContact(null);
+ } else {
+ // Add new customer instance
+ updatedCustomer = this.adminCustomerBean.addCustomer(customer);
+ }
+ } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
+ // Throw again
+ throw new FaceletException(ex);
+ }
+
+ // Fire event
+ this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
+
+ // Redirect
+ return "admin_list_customer"; //NOI18N
}
@Override
- public boolean hasCustomers () {
- return (!this.allCustomers().isEmpty());
+ public void copyCustomerToController (final Customer customer) {
+ // Parameters 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) {
+ // Invalid id number
+ throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
+ } else if (customer.getCustomerContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("customer.customerContact is null"); //NOI18N
+ } else if (customer.getCustomerContact().getContactId() == null) {
+ // .. and again
+ throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
+ } else if (customer.getCustomerContact().getContactId() < 1) {
+ // Invalid id
+ throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
+ }
+
+ // @TODO Set all data
}
/**
*/
@PostConstruct
public void init () {
- // Initialize customer list
- this.customerList = this.adminCustomerBean.allCustomers();
+ }
+
+ /**
+ * Checks whether ther minimum customer data is set
+ * <p>
+ * @return Whether minimum data is set
+ */
+ private boolean isCustomerDataSet () {
+ // Check all
+ return ((this.adminContactController.getGender() instanceof Gender) &&
+ (this.adminContactController.getFirstName() != null) &&
+ (!this.adminContactController.getFirstName().isEmpty()) &&
+ (this.adminContactController.getFamilyName() != null) &&
+ (!this.adminContactController.getFamilyName().isEmpty()));
}
}