/**
   * Anonymous checkout process.
   *
   * <p>Creates a new guest customer and updates the session cart with this user. The session user
   * will be anonymous and it's never updated with this guest user.
   *
   * <p>If email is required, grab the email from the form and set it as uid with "guid|email"
   * format.
   *
   * @throws de.hybris.platform.cms2.exceptions.CMSItemNotFoundException
   */
  protected String processAnonymousCheckoutUserRequest(
      final GuestForm form,
      final BindingResult bindingResult,
      final Model model,
      final HttpServletRequest request,
      final HttpServletResponse response)
      throws CMSItemNotFoundException {
    try {
      if (bindingResult.hasErrors()) {
        model.addAttribute(form);
        model.addAttribute(new LoginForm());
        model.addAttribute(new RegisterForm());
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
      }

      getCustomerFacade()
          .createGuestUserForAnonymousCheckout(
              form.getEmail(),
              getMessageSource()
                  .getMessage("text.guest.customer", null, getI18nService().getCurrentLocale()));
      getGuidCookieStrategy().setCookie(request, response);
      getSessionService().setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE);
    } catch (final DuplicateUidException e) {
      LOG.warn("guest registration failed: " + e);
      GlobalMessages.addErrorMessage(model, "form.global.error");
      return handleRegistrationError(model);
    }

    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
  }
  @RequestMapping(
      value = "/cart/update",
      method = RequestMethod.POST,
      produces = "application/json")
  public String updateCartQuantities(
      @RequestParam("storeNamePost") final String storeId,
      @RequestParam("entryNumber") final long entryNumber,
      @RequestParam("hiddenPickupQty") final long quantity,
      final RedirectAttributes redirectModel)
      throws CommerceCartModificationException {
    final CartModificationData cartModificationData =
        cartFacade.updateCartEntry(entryNumber, storeId);

    if (entryNumber == cartModificationData.getEntry().getEntryNumber().intValue()) {
      final CartModificationData cartModification =
          cartFacade.updateCartEntry(entryNumber, quantity);
      if (cartModification.getQuantity() == quantity) {
        // Success
        if (cartModification.getQuantity() == 0) {
          // Success in removing entry
          GlobalMessages.addFlashMessage(
              redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "basket.page.message.remove");
        } else {
          // Success in update quantity
          GlobalMessages.addFlashMessage(
              redirectModel,
              GlobalMessages.CONF_MESSAGES_HOLDER,
              "basket.page.message.update.pickupinstoreitem");
        }
      } else {
        // Less than successful
        GlobalMessages.addFlashMessage(
            redirectModel,
            GlobalMessages.ERROR_MESSAGES_HOLDER,
            "basket.information.quantity.reducedNumberOfItemsAdded."
                + cartModification.getStatusCode());
      }
    } else if (!CommerceCartModificationStatus.SUCCESS.equals(
        cartModificationData.getStatusCode())) {
      // When update pickupInStore happens to be same as existing entry with POS and SKU and that
      // merged POS has lower stock
      GlobalMessages.addFlashMessage(
          redirectModel,
          GlobalMessages.ERROR_MESSAGES_HOLDER,
          "basket.information.quantity.reducedNumberOfItemsAdded."
              + cartModificationData.getStatusCode());
    }

    return REDIRECT_PREFIX + "/cart";
  }
  /**
   * This method takes data from the registration form and create a new customer account and
   * attempts to log in using the credentials of this new user.
   *
   * @param referer
   * @param form
   * @param bindingResult
   * @param model
   * @param request
   * @param response
   * @return true if there are no binding errors or the account does not already exists.
   * @throws CMSItemNotFoundException
   */
  protected String processRegisterUserRequest(
      final String referer,
      final RegisterForm form,
      final BindingResult bindingResult,
      final Model model,
      final HttpServletRequest request,
      final HttpServletResponse response,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
      model.addAttribute(form);
      model.addAttribute(new LoginForm());
      model.addAttribute(new GuestForm());
      GlobalMessages.addErrorMessage(model, "form.global.error");
      return handleRegistrationError(model);
    }

    final RegisterData data = new RegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
      getCustomerFacade().register(data);
      getAutoLoginStrategy().login(form.getEmail().toLowerCase(), form.getPwd(), request, response);

      GlobalMessages.addFlashMessage(
          redirectModel,
          GlobalMessages.CONF_MESSAGES_HOLDER,
          "registration.confirmation.message.title");
    } catch (final DuplicateUidException e) {
      LOG.warn("registration failed: " + e);
      model.addAttribute(form);
      model.addAttribute(new LoginForm());
      model.addAttribute(new GuestForm());
      bindingResult.rejectValue("email", "registration.error.account.exists.title");
      GlobalMessages.addErrorMessage(model, "form.global.error");
      return handleRegistrationError(model);
    }

    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
  }
  @RequestMapping(
      value = "/cart/update/delivery",
      method = {RequestMethod.GET, RequestMethod.POST})
  public String updateToDelivery(
      @RequestParam("entryNumber") final long entryNumber, final RedirectAttributes redirectModel)
      throws CommerceCartModificationException {
    final CartModificationData cartModificationData = cartFacade.updateCartEntry(entryNumber, null);
    if (CommerceCartModificationStatus.SUCCESS.equals(cartModificationData.getStatusCode())) {
      // Success in update quantity
      GlobalMessages.addFlashMessage(
          redirectModel,
          GlobalMessages.CONF_MESSAGES_HOLDER,
          "basket.page.message.update.pickupinstoreitem.toship");
    } else {
      // Less than successful
      GlobalMessages.addFlashMessage(
          redirectModel,
          GlobalMessages.ERROR_MESSAGES_HOLDER,
          "basket.information.quantity.reducedNumberOfItemsAdded."
              + cartModificationData.getStatusCode());
    }

    return REDIRECT_PREFIX + "/cart";
  }