From: Roland Häder Date: Tue, 26 Apr 2016 10:28:11 +0000 (+0200) Subject: Implemented business method isReqistered() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=05b0f47bc06f34d498516aea29d7143581fc98c6;p=pizzaservice-ejb.git Implemented business method isReqistered() Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java index 80fcb57..44edd2c 100644 --- a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java +++ b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java @@ -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); diff --git a/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java index cbf716e..82f836b 100644 --- a/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/customer/PizzaCustomerSessionBean.java @@ -16,10 +16,14 @@ */ 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) *

@@ -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