private static String[] validate(Customer customer, Locale locale, MessageSource messageSource) { List<String> validationErrorList = new ArrayList<String>(); validate( customer.getCustomerFirstname(), "messages.required.firstname", validationErrorList, locale, messageSource); validate( customer.getCustomerLastname(), "messages.required.lastname", validationErrorList, locale, messageSource); validate( customer.getCustomerEmailAddress(), "messages.required.email", validationErrorList, locale, messageSource); validate( customer.getCustomerTelephone(), "messages.required.phone", validationErrorList, locale, messageSource); validate( customer.getCustomerCity(), "messages.required.city", validationErrorList, locale, messageSource); validate( customer.getCustomerPostalCode(), "messages.required.postalcode", validationErrorList, locale, messageSource); validate( customer.getCustomerStreetAddress(), "messages.required.streetaddress", validationErrorList, locale, messageSource); validate( customer.getCustomerLang(), "messages.required.language", validationErrorList, locale, messageSource); // validate country if (customer.getCustomerCountryId() == 0) { validationErrorList.add( messageSource.getMessage("messages.required.customercountrycode", null, locale)); } else { java.util.Map countries = RefCache.getAllcountriesmap(1); Country c = (Country) countries.get(customer.getCustomerCountryId()); if (c == null) { validationErrorList.add( messageSource.getMessage("messages.required.customercountrycode", null, locale)); } } // validate zone if (customer.getCustomerZoneId() == 0) { if (StringUtils.isBlank(customer.getZoneName())) { validationErrorList.add( messageSource.getMessage("messages.required.customerzonecode", null, locale)); } } else { java.util.Map zones = RefCache.getAllZonesmap(1); Zone z = (Zone) zones.get(customer.getCustomerZoneId()); if (z == null) { validationErrorList.add( messageSource.getMessage("messages.required.customerzonecode", null, locale)); } } if (validationErrorList.size() > 0) { String[] messages = (String[]) validationErrorList.toArray(); return messages; } else { return null; } }
/** Creates a new Customer */ @WebMethod public @WebResult CreateCustomerWebServiceResponse createCustomer( @WebParam(name = "credentials") WebServiceCredentials credentials, @WebParam(name = "customer") Customer customer) { MessageSource messageSource = (MessageSource) SpringUtil.getBean("messageSource"); Locale locale = LocaleUtil.getDefaultLocale(); if (StringUtils.isNotBlank(customer.getCustomerLang())) { locale = LocaleUtil.getLocale(customer.getCustomerLang()); } CreateCustomerWebServiceResponse response = new CreateCustomerWebServiceResponse(); try { // check credentials validateCredentials(locale, credentials); String[] validationErrorList = validate(customer, locale, messageSource); if (validationErrorList != null && validationErrorList.length > 0) { response.setMessages(validationErrorList); response.setStatus(2); return response; } CustomerService cservice = (CustomerService) ServiceFactory.getService(ServiceFactory.CustomerService); // if customer has customer id >0 check that it belongs to this merchant id com.salesmanager.core.entity.customer.Customer tmpCustomer = null; if (customer.getCustomerId() > 0) { tmpCustomer = cservice.getCustomer(customer.getCustomerId()); if (tmpCustomer != null) { if (tmpCustomer.getMerchantId() != credentials.getMerchantId()) { response.setMessages( new String[] {messageSource.getMessage("messages.authorization", null, locale)}); response.setStatus(0); } } } com.salesmanager.core.entity.customer.Customer newCustomer = new com.salesmanager.core.entity.customer.Customer(); if (tmpCustomer != null) { // modify existing customer newCustomer = tmpCustomer; } BeanUtils.copyProperties(newCustomer, customer); // copy properties to billing newCustomer.setCustomerBillingCity(customer.getCustomerCity()); newCustomer.setCustomerBillingCountryId(customer.getCustomerCountryId()); newCustomer.setCustomerBillingCountryName(newCustomer.getBillingCountry()); newCustomer.setCustomerBillingFirstName(customer.getCustomerFirstname()); newCustomer.setCustomerBillingLastName(customer.getCustomerLastname()); newCustomer.setCustomerBillingPostalCode(customer.getCustomerPostalCode()); newCustomer.setCustomerBillingState(newCustomer.getStateProvinceName()); newCustomer.setCustomerBillingStreetAddress(customer.getCustomerStreetAddress()); newCustomer.setCustomerBillingZoneId(customer.getCustomerZoneId()); newCustomer.setLocale(locale); newCustomer.setMerchantId(credentials.getMerchantId()); if (StringUtils.isBlank(customer.getZoneName()) && customer.getCustomerZoneId() > 0) { java.util.Map zones = (java.util.Map) RefCache.getAllZonesmap(LanguageUtil.getLanguageNumberCode(locale.getLanguage())); if (zones != null) { Zone z = (Zone) zones.get(customer.getCustomerZoneId()); if (z != null) { newCustomer.setCustomerState(z.getZoneName()); } } } if (StringUtils.isBlank(newCustomer.getBillingState()) && newCustomer.getCustomerZoneId() > 0) { java.util.Map zones = (java.util.Map) RefCache.getAllZonesmap(LanguageUtil.getLanguageNumberCode(locale.getLanguage())); if (zones != null) { Zone z = (Zone) zones.get(newCustomer.getCustomerZoneId()); if (z != null) { newCustomer.setCustomerBillingState(z.getZoneName()); } } } cservice.saveOrUpdateCustomer(newCustomer, SystemUrlEntryType.WEB, locale); response.setMessages( new String[] { messageSource.getMessage("messages.customer.customerregistered", null, locale) }); response.setStatus(1); response.setCustomerId(newCustomer.getCustomerId()); } catch (Exception e) { if (e instanceof ServiceException) { String msg[] = {((ServiceException) e).getMessage()}; response.setMessages(msg); response.setStatus(0); } else { log.error("Exception occurred while creating Customer", e); response.setMessages( new String[] {messageSource.getMessage("errors.technical", null, locale)}); response.setStatus(0); } } return response; }