]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
implemented business method linkCustomer()
authorRoland Häder <roland@mxchange.org>
Wed, 27 Apr 2016 09:49:36 +0000 (11:49 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 27 Apr 2016 19:10:02 +0000 (21:10 +0200)
src/java/org/mxchange/pizzaapplication/model/customer/PizzaAdminCustomerSessionBean.java

index ed9e57834de05a8380dc23109ed1930dfc9fc227..21feee5f445791529f7bde1534bc510a2c1c5845 100644 (file)
@@ -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;
+       }
+
 }