コード例 #1
0
  /** 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;
  }