+ /**
+ * Contact instance
+ */
+ @EJB
+ private ContactSessionBeanRemote customerBean;
+
+ @Override
+ public Customer addCustomer (final Customer customer) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: 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() > 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
+ }
+
+ // Get contact instance
+ Contact contact = customer.getCustomerContact();
+ Contact updatedContact = null;
+
+ // Is a customer found?
+ if (this.customerBean.isContactFound(contact)) {
+ // Yes, then get updated version
+ updatedContact = this.customerBean.updateContactData(contact);
+
+ // Remove it from customer so it won't get persisted again
+ customer.setCustomerContact(null);
+ }
+
+ // Set created timestamp
+ customer.setCustomerCreated(new GregorianCalendar());
+
+ // Persist the customer
+ this.getEntityManager().persist(customer);
+
+ // Flush it to get id number set
+ this.getEntityManager().flush();
+
+ // Has the contact being updated?
+ if (updatedContact instanceof Contact) {
+ // Set it again
+ customer.setCustomerContact(updatedContact);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addCustomer: customer.customerId={0} - EXIT!", customer.getCustomerId())); //NOI18N
+
+ // Return updated instance
+ return customer;
+ }
+