From: Roland Häder Date: Wed, 27 Apr 2016 09:49:36 +0000 (+0200) Subject: implemented business method linkCustomer() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8ae3f2388d322e1443cfa58858131da477524ca5;p=pizzaservice-ejb.git implemented business method linkCustomer() --- diff --git a/src/java/org/mxchange/pizzaapplication/model/customer/PizzaAdminCustomerSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/customer/PizzaAdminCustomerSessionBean.java index ed9e578..21feee5 100644 --- a/src/java/org/mxchange/pizzaapplication/model/customer/PizzaAdminCustomerSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/customer/PizzaAdminCustomerSessionBean.java @@ -178,4 +178,55 @@ public class PizzaAdminCustomerSessionBean extends BasePizzaDatabaseBean impleme return customerNumber; } + @Override + public Customer linkCustomer (final Customer customer) throws CustomerAlreadyRegisteredException { + // Parameters must be valid + if (null == customer) { + // Throw NPE + throw new NullPointerException("customer is null"); + } else if (customer.getCustomerContact() == null) { + // Throw NPE again + throw new NullPointerException("customer.customerContact is null"); //NOI18N + } else if (customer.getCustomerContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N + } else if (customer.getCustomerContact().getContactId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //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 + } else if (this.customerBean.isReqistered(customer)) { + // Throw exception + throw new CustomerAlreadyRegisteredException(customer); + } + + // Set created timestamp + customer.setCustomerCreated(new GregorianCalendar()); + + // Try to find the contact instance + Contact foundContact = this.getEntityManager().find(customer.getCustomerContact().getClass(), customer.getCustomerContact().getContactId()); + + // Try to merge it + Contact detachedContact = this.getEntityManager().merge(foundContact); + + // Set it in customer + customer.setCustomerContact(detachedContact); + + // Persist customer + this.getEntityManager().persist(customer); + + // Flush it to get id + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N + + // Return updated instance + return customer; + } + }