/** Get customer for a customerId and merchantId */
  @WebMethod
  public @WebResult GetCustomerWebServiceResponse getCustomer(
      @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());
    }

    GetCustomerWebServiceResponse response = new GetCustomerWebServiceResponse();
    try {

      if (customer.getCustomerId() == 0) {
        setStatusMsg(messageSource, locale, response, "messages.authorization", 0);
        return response;
      }
      // check credentials
      validateCredentials(locale, credentials);

      CustomerService cservice =
          (CustomerService) ServiceFactory.getService(ServiceFactory.CustomerService);
      com.salesmanager.core.entity.customer.Customer entityCustomer =
          cservice.getCustomer(customer.getCustomerId());
      if (entityCustomer == null) {
        setStatusMsg(messageSource, locale, response, "messages.customer.doesnotexist", 0);
        return response;
      }

      if (entityCustomer.getMerchantId() != credentials.getMerchantId()) {
        setStatusMsg(messageSource, locale, response, "messages.authorization", 0);
        return response;
      }

      Customer webCustomer = new Customer();
      BeanUtils.copyProperties(webCustomer, entityCustomer);
      response.setCustomer(webCustomer);
      response.setStatus(1);

    } 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;
  }
  @RequestMapping(
      value = {"/password/reset.html"},
      method = RequestMethod.POST)
  public String resetPassword(HttpServletRequest request) throws Exception {

    String userName = request.getParameter("resetpasswordusername");
    CustomerService cservice =
        (CustomerService) ServiceFactory.getService(ServiceFactory.CustomerService);

    MerchantStore store = SessionUtil.getMerchantStore(request);
    Customer customer = cservice.findCustomerByUserName(userName, store.getMerchantId());

    if (customer != null) {
      cservice.resetCustomerPassword(customer);
    }

    super.setMessage("label.customer.passwordreset", request);

    return "redirect:/home.html";
  }
  /** 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;
  }
  public String execute(HttpServletRequest request) throws Exception {

    Customer customer = null;

    if (!validateCustomerLogon(request)) {

      LabelUtil l = LabelUtil.getInstance();
      Locale locale = LocaleUtil.getLocale(request);
      l.setLocale(locale);
      MessageUtil.addErrorMessage(request, l.getText(locale, "login.invalid"));
      return "redirect:/home.html";
    }
    try {

      CustomerLogonModule logon =
          (CustomerLogonModule) com.salesmanager.core.util.SpringUtil.getBean("customerLogon");

      // get merchantId
      int merchantId = 1;
      HttpSession session = request.getSession();
      MerchantStore store = (MerchantStore) session.getAttribute("STORE");
      if (store != null) {
        merchantId = store.getMerchantId();
      }

      customer = logon.logon(request, merchantId);

      if (customer == null) {

        MessageUtil.addErrorMessage(request, super.getText(request, "login.invalid"));
        return "redirect:/home.html";
      }

      Locale locale = LocaleUtil.getLocale(request);
      customer.setLocale(locale);

      // get CustomerInfo
      CustomerService cservice =
          (CustomerService) ServiceFactory.getService(ServiceFactory.CustomerService);
      CustomerInfo customerInfo = cservice.findCustomerInfoById(customer.getCustomerId());

      if (customerInfo == null) {
        customerInfo = new CustomerInfo();
        customerInfo.setCustomerInfoId(customer.getCustomerId());
      }

      Integer login = customerInfo.getCustomerInfoNumberOfLogon();
      login = login + 1;
      customerInfo.setCustomerInfoNumberOfLogon(login);
      cservice.saveOrUpdateCustomerInfo(customerInfo);

      SessionUtil.setCustomer(customer, request);
      request.setAttribute("CUSTOMER", customer);

      return "redirect:/profile/profile.html";

    } catch (ServiceException e) {

      MessageUtil.addErrorMessage(request, super.getText(request, "login.invalid"));
      return "redirect:/home.html";

    } catch (Exception ex) {
      log.error(ex);
      throw ex;
    }
  }