From d6f4881c020196c87ba309d33a4048b5936675ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 28 Apr 2016 10:30:54 +0200 Subject: [PATCH] implemented business method findCustomerById() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../customer/PizzaCustomerSessionBean.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) 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 -- 2.39.2