From: Roland Häder Date: Thu, 28 Apr 2016 08:30:54 +0000 (+0200) Subject: implemented business method findCustomerById() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d6f4881c020196c87ba309d33a4048b5936675ee;p=pizzaservice-ejb.git implemented business method findCustomerById() Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java index 0229a31..1bcbb02 100644 --- a/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java @@ -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