package org.mxchange.pizzaapplication.model.customer;
import java.text.MessageFormat;
+import java.util.GregorianCalendar;
import java.util.List;
+import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;
+import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
+import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
import org.mxchange.jcustomercore.model.customer.Customer;
import org.mxchange.jcustomercore.utils.CustomerUtils;
import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
*/
private static final long serialVersionUID = 19_845_893_648_175_427L;
+ /**
+ * Contact instance
+ */
+ @EJB
+ private ContactSessionBeanRemote contactBean;
+
+ /**
+ * General customer bean
+ */
+ @EJB
+ private RateCalcCustomerSessionBeanRemote customerBean;
+
+ @Override
+ public Customer addCustomer (final Customer customer) throws CustomerAlreadyRegisteredException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer={0} - CALLED!", customer)); //NOI18N
+
+ // Paramerter customer should be valid
+ if (null == customer) {
+ // Throw NPE
+ throw new NullPointerException("customer is null"); //NOI18N
+ } else if ((customer.getCustomerId() instanceof Long) && (customer.getCustomerId() > 0)) {
+ // Not allowed
+ throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not allowed here.", customer.getCustomerId())); //NOI18N
+ } else if (customer.getCustomerContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("customer.customerContact is null"); //NOI18N
+ } else if (customer.getCustomerNumber() == null) {
+ // Customer numbers should be set, at least generate one with CustomerUtils
+ throw new NullPointerException("customer.customerNumber is null"); //NOI18N
+ } else if (customer.getCustomerNumber().length() < RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH) {
+ // To short number
+ throw new IllegalArgumentException(MessageFormat.format("customer.customerNumber.length={0} is shorter than expected: {1}", customer.getCustomerNumber().length(), RateCalcAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH)); //NOI18N
+ } else if (this.customerBean.isReqistered(customer)) {
+ // Throw exception
+ throw new CustomerAlreadyRegisteredException(customer);
+ }
+
+ // Get contact instance
+ Contact contact = customer.getCustomerContact();
+ Contact updatedContact = this.contactBean.lookupContact(contact);
+
+ // Is a customer found?
+ if (updatedContact instanceof Contact) {
+ // Yes, then get updated version
+ updatedContact = this.contactBean.updateContactData(contact);
+
+ // Remove it from customer so it won't get persisted again
+ customer.setCustomerContact(null);
+ }
+
+ // Set created timestamp(s)
+ customer.setCustomerCreated(new GregorianCalendar());
+ this.setAllContactPhoneEntriesCreated(contact);
+
+ // Persist the customer
+ this.getEntityManager().persist(customer);
+
+ // Flush it to get id number set
+ this.getEntityManager().flush();
+
+ // Has the contact being updated?
+ if (updatedContact instanceof Contact) {
+ // Set it again
+ customer.setCustomerContact(updatedContact);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N
+
+ // Return updated instance
+ return customer;
+ }
+
@Override
@SuppressWarnings ("unchecked")
public List<Customer> allCustomers () {
@Override
public String createCustomerNumber () {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("createCustomerNumber: CALLED!"); //NOI18N
+
// Init named query
Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
// Create new number
String cn = CustomerUtils.generateCustomerNumber(PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_LENGTH, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_BLOCK_SIZE, PizzaAdminCustomerSessionBeanRemote.CUSTOMER_NUMBER_SEPARATOR);
+ // Debug message#
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: cn={0}", cn)); //NOI18N
+
// Set the generated number as param
query.setParameter("customerNumber", cn); //NOI18N
try {
// Get a single result
Customer customer = (Customer) query.getSingleResult();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found customer={0} with customerNumber={1} - continuing ...", customer, customerNumber)); //NOI18N
} catch (final NoResultException ex) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("createCustomerNumber: Found free number {0}, exception message:{1}", cn, ex.getMessage())); //NOI18N
+
// Not found, okay
customerNumber = cn;
}
}
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("createCustomerNumber: customerNumber={0} - EXIT!", customerNumber)); //NOI18N
+
// Return generated number
return customerNumber;
}