]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
implemented business method findCustomerById()
authorRoland Häder <roland@mxchange.org>
Thu, 28 Apr 2016 08:30:54 +0000 (10:30 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 28 Apr 2016 17:23:32 +0000 (19:23 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java

index 0229a31ffdc40948ed7accf9a0c0224717a09fcd..1bcbb02edc551999c9c39458490b8fe08cfcf66e 100644 (file)
@@ -21,6 +21,7 @@ import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
+import org.mxchange.jcustomercore.exceptions.CustomerNotFoundException;
 import org.mxchange.jcustomercore.model.customer.Customer;
 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
 
@@ -42,6 +43,48 @@ public class PizzaCustomerSessionBean extends BasePizzaDatabaseBean implements P
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //NOI18N
        }
 
+       @Override
+       public Customer findCustomerById (final Long customerId) throws CustomerNotFoundException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findCustomerById: customerId={0} - CALLED!", customerId)); //NOI18N
+
+               // The parameter must be valid
+               if (null == customerId) {
+                       // Throw NPE
+                       throw new NullPointerException("customerId is null"); //NOI18N
+               } else if (customerId < 1) {
+                       // Invalid id number
+                       throw new IllegalArgumentException(MessageFormat.format("customerId={0} - is not valid", customerId)); //NOI18N
+               }
+
+               // Init instance
+               Customer customer;
+
+               // Get named query
+               Query query = this.getEntityManager().createNamedQuery("SearchCustomerById", Customer.class); //NOI18N
+
+               // Set customer id
+               query.setParameter("customerId", customerId); //NOI18N
+
+               // Try to find it
+               try {
+                       // Get single result
+                       customer = (Customer) query.getSingleResult();
+
+                       // Debug message (success)
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("findCustomerById: Found customer={0} with customerId={1}", customer, customerId)); //NOI18N
+               } catch (final NoResultException ex) {
+                       // Not found, throw again
+                       throw new CustomerNotFoundException(customerId, ex);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findCustomerById; customer={0} - EXIT!", customer)); //NOI18N
+
+               // Return found instance
+               return customer;
+       }
+
        @Override
        public boolean isReqistered (final Customer customer) {
                // Trace message