EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
CustomerMaintActionForm form = (CustomerMaintActionForm) actionForm;
AdminBean adminBean = getAdminBean(request);
Site site = adminBean.getSite();
Customer customer = CustomerDAO.load(site.getSiteId(), Format.getLong(form.getCustId()));
ActionMessages errors = validate(form, site.getSiteId());
if (errors.size() != 0) {
saveMessages(request, errors);
initSearchInfo(request, form, site.getSiteId(), customer);
return mapping.findForward("error");
}
customer.setCustEmail(form.getCustEmail());
customer.setCustPublicName(form.getCustPublicName());
customer.setCustComments(form.getCustComments());
customer.setActive(Constants.VALUE_NO);
if (!Format.isNullOrEmpty(form.getActive())) {
customer.setActive(form.getActive().equals("Y") ? Constants.VALUE_YES : Constants.VALUE_NO);
}
CustomerAddress address = customer.getCustAddress();
if (address == null) {
address = new CustomerAddress();
customer.setCustAddress(address);
customer.getCustAddresses().add(address);
address.setRecCreateBy(adminBean.getUserId());
address.setRecCreateDatetime(new Date(System.currentTimeMillis()));
}
address.setCustUseAddress(Constants.CUST_ADDRESS_USE_OWN);
address.setCustAddressType(Constants.CUSTOMER_ADDRESS_CUST);
address.setCustPrefix(form.getCustPrefix());
address.setCustLastName(form.getCustLastName());
address.setCustMiddleName(form.getCustMiddleName());
address.setCustFirstName(form.getCustFirstName());
address.setCustSuffix(form.getCustSuffix());
address.setCustPhoneNum(form.getCustPhoneNum());
address.setCustFaxNum(form.getCustFaxNum());
address.setCustAddressLine1(form.getCustAddressLine1());
address.setCustAddressLine2(form.getCustAddressLine2());
address.setCustStateCode(form.getCustStateCode());
address.setCustStateName("");
if (Format.isNullOrEmpty(form.getCustStateCode())) {
address.setCustStateName(Utility.getStateName(adminBean.getSiteId(), form.getCustStateCode()));
}
address.setCustCountryCode(form.getCustCountryCode());
address.setCustCountryName("");
if (Format.isNullOrEmpty(form.getCustCountryCode())) {
address.setCustCountryName(Utility.getCountryName(adminBean.getSiteId(), form.getCustCountryCode()));
}
address.setCustZipCode(form.getCustZipCode());
address.setRecUpdateBy(adminBean.getUserId());
address.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(address);
address = null;
for (CustomerAddress a : customer.getCustAddresses()) {
if (a.getCustAddressType().equals(Constants.CUSTOMER_ADDRESS_BILLING)) {
address = a;
}
}
if (address == null) {
address = new CustomerAddress();
customer.getCustAddresses().add(address);
address.setRecCreateBy(adminBean.getUserId());
address.setRecCreateDatetime(new Date(System.currentTimeMillis()));
}
address.setCustUseAddress(form.getBillingCustUseAddress());
address.setCustAddressType(Constants.CUSTOMER_ADDRESS_BILLING);
address.setCustPrefix(form.getBillingCustPrefix());
address.setCustLastName(form.getBillingCustLastName());
address.setCustMiddleName(form.getBillingCustMiddleName());
address.setCustFirstName(form.getBillingCustFirstName());
address.setCustSuffix(form.getBillingCustSuffix());
address.setCustPhoneNum(form.getBillingCustPhoneNum());
address.setCustFaxNum(form.getBillingCustFaxNum());
address.setCustAddressLine1(form.getBillingCustAddressLine1());
address.setCustAddressLine2(form.getBillingCustAddressLine2());
address.setCustStateCode(form.getBillingCustStateCode());
address.setCustStateName("");
if (!Format.isNullOrEmpty(form.getBillingCustStateCode())) {
address.setCustStateName(Utility.getStateName(adminBean.getSiteId(), form.getBillingCustStateCode()));
}
address.setCustCountryCode(form.getBillingCustCountryCode());
address.setCustCountryName("");
if (!Format.isNullOrEmpty(form.getBillingCustCountryCode())) {
address.setCustCountryName(Utility.getCountryName(adminBean.getSiteId(), form.getBillingCustCountryCode()));
}
address.setCustZipCode(form.getBillingCustZipCode());
address.setRecUpdateBy(adminBean.getUserId());
address.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(address);
address = null;
for (CustomerAddress a : customer.getCustAddresses()) {
if (a.getCustAddressType().equals(Constants.CUSTOMER_ADDRESS_SHIPPING)) {
address = a;
}
}
if (address == null) {
address = new CustomerAddress();
customer.getCustAddresses().add(address);
address.setRecCreateBy(adminBean.getUserId());
address.setRecCreateDatetime(new Date(System.currentTimeMillis()));
}
address.setCustUseAddress(form.getShippingCustUseAddress());
address.setCustAddressType(Constants.CUSTOMER_ADDRESS_SHIPPING);
address.setCustPrefix(form.getShippingCustPrefix());
address.setCustLastName(form.getShippingCustLastName());
address.setCustMiddleName(form.getShippingCustMiddleName());
address.setCustFirstName(form.getShippingCustFirstName());
address.setCustSuffix(form.getShippingCustSuffix());
address.setCustPhoneNum(form.getShippingCustPhoneNum());
address.setCustFaxNum(form.getShippingCustFaxNum());
address.setCustAddressLine1(form.getShippingCustAddressLine1());
address.setCustAddressLine2(form.getShippingCustAddressLine2());
address.setCustStateCode(form.getShippingCustStateCode());
address.setCustStateName("");
if (!Format.isNullOrEmpty(form.getShippingCustStateCode())) {
address.setCustStateName(Utility.getStateName(adminBean.getSiteId(), form.getShippingCustStateCode()));
}
address.setCustCountryCode(form.getShippingCustCountryCode());
address.setCustCountryName("");
if (!Format.isNullOrEmpty(form.getShippingCustCountryCode())) {
address.setCustCountryName(Utility.getCountryName(adminBean.getSiteId(), form.getShippingCustCountryCode()));
}
address.setCustZipCode(form.getShippingCustZipCode());
address.setRecUpdateBy(adminBean.getUserId());
address.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(address);
customer.setRecUpdateBy(adminBean.getUser().getUserId());
customer.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (!Format.isNullOrEmpty(form.getCustPassword())) {
customer.setCustPassword(AESEncoder.getInstance().encode(form.getCustPassword()));
}
if (!Format.isNullOrEmpty(form.getCustClassId())) {
CustomerClass customerClass = (CustomerClass) em.find(CustomerClass.class, Format.getLong(form.getCustClassId()));
if (customerClass != null) {
customer.setCustomerClass(customerClass);
}
}
else {
customer.setCustomerClass(null);
}
em.persist(customer);
CustomerCreditCard customerCreditCard = null;
Iterator<?> iterator = customer.getCustCreditCards().iterator();
if (iterator.hasNext()) {
customerCreditCard = (CustomerCreditCard) iterator.next();
}
if (customerCreditCard == null) {
customerCreditCard = new CustomerCreditCard();
customerCreditCard.setRecCreateBy(adminBean.getUser().getUserId());
customerCreditCard.setRecCreateDatetime(new Date(System.currentTimeMillis()));
}
customerCreditCard.setCustCreditCardFullName(form.getCustCreditCardFullName());
customerCreditCard.setCustCreditCardNum(AESEncoder.getInstance().encode(form.getCustCreditCardNum()));
customerCreditCard.setCustCreditCardExpiryMonth(form.getCustCreditCardExpiryMonth());
customerCreditCard.setCustCreditCardExpiryYear(form.getCustCreditCardExpiryYear());
customerCreditCard.setCustCreditCardVerNum(form.getCustCreditCardVerNum());
customerCreditCard.setRecUpdateBy(adminBean.getUser().getUserId());
customerCreditCard.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
CreditCard creditCard = CreditCardDAO.load(site.getSiteId(), Long.valueOf(form.getCreditCardId()));
customerCreditCard.setCreditCard(creditCard);
customer.getCustCreditCards().add(customerCreditCard);
em.persist(customerCreditCard);
initSearchInfo(request, form, site.getSiteId(), customer);
FormUtils.setFormDisplayMode(request, form, FormUtils.EDIT_MODE);