]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
Implemented business method isReqistered()
authorRoland Häder <roland@mxchange.org>
Tue, 26 Apr 2016 10:28:11 +0000 (12:28 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 27 Apr 2016 19:05:48 +0000 (21:05 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java

index 80fcb578773832f686934514c1ac6122d559b030..44edd2cb7f5169f0f63fb57b7f1366c63ea48b9d 100644 (file)
@@ -134,7 +134,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
                if (null == contact) {
                        // Throw NPE
                        throw new NullPointerException("contact is null"); //NOI18N
-               } else if (contact.getContactId() > 0) {
+               } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
                        try {
                                // Id set, ask other method
                                return (this.findContactById(contact.getContactId()) instanceof Contact);
index cbf716e2b037eabcdfa4085a2ab722c6c18dca76..82f836b11307a2cd51ba50e7d5a461722632280b 100644 (file)
  */
 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>
@@ -35,12 +39,60 @@ public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements 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