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;
+ }
+
}