*/
package org.mxchange.pizzaapplication.model.customer;
+import java.text.MessageFormat;
import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
import org.mxchange.jcustomercore.model.customer.Customer;
import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
+
/**
* A stateless customer session bean (EJB)
* <p>
@Override
public Customer fillCustomerData (final Customer customer) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //NOI18N
}
@Override
public boolean isReqistered (final Customer customer) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isReqistered: 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
+ }
+
+ // Init query instance
+ Query query = this.getEntityManager().createNamedQuery("SearchCustomerByNumber", Customer.class); //NOI18N
+
+ // Set parameter
+ query.setParameter("customerNumber", customer.getCustomerNumber()); //NOI18N
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Try to find it
+ try {
+ Customer dummy = (Customer) query.getSingleResult();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("isReqistered: Customer {0} has number{1} and contact {2}", dummy.getCustomerId(), customer.getCustomerNumber(), customer.getCustomerContact().getContactId())); //NOI18N
+
+ // Mark as found
+ isFound = true;
+ } catch (final NoResultException ex) {
+ // Not found
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("isReqistered: Not found: {0}", ex.getMessage())); //NOI18N
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("isReqistered: isFound={0} - EXIT!", isFound)); //NOI18N
+
+ // Return it
+ return isFound;
}
@Override