]> git.mxchange.org Git - pizzaservice-ejb.git/blobdiff - src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java
Implemented business method isReqistered()
[pizzaservice-ejb.git] / src / java / org / mxchange / pizzaapplication / model / customer / PizzaCustomerSessionBean.java
index 8a9a6e3fbe837f071696348bda60426e239f6ebd..82f836b11307a2cd51ba50e7d5a461722632280b 100644 (file)
  */
 package org.mxchange.pizzaapplication.model.customer;
 
-import de.chotime.jratecalc.model.customer.RateCalcCustomerSessionBeanRemote;
+import java.text.MessageFormat;
 import javax.ejb.Stateless;
-import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
-;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
-import org.mxchange.jcustomercore.model.customer.Customer;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)
@@ -30,7 +30,7 @@ import org.mxchange.jcustomercore.model.customer.Customer;
  * @author Roland Haeder<rhaeder@cho-time.de>
  */
 @Stateless (name = "customer", description = "A bean handling customer data")
-public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements RateCalcCustomerSessionBeanRemote {
+public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements PizzaCustomerSessionBeanRemote {
 
        /**
         * Serial number
@@ -39,17 +39,65 @@ public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements R
 
        @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
        public Customer registerCustomer (final Customer customer) throws CustomerAlreadyRegisteredException {
-               throw new UnsupportedOperationException("Registration by guests is not supported with RateCalc application."); //NOI18N
+               throw new UnsupportedOperationException("Registration by guests is not supported with Pizza application."); //NOI18N
        }
 
 }