public String execute() {
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();

    try {
      KKAppEng kkAppEng = this.getKKAppEng(request, response);

      // Set the cookie so that we don't display the message again
      kkAppEng.setAgreedCookies(true);
      setKKCookie(AGREED_COOKIES, "1", request, response, kkAppEng);

      return SUCCESS;

    } catch (Exception e) {
      return super.handleException(request, e);
    }
  }
Esempio n. 2
0
  /**
   * Method used to create a browser cookie when a customer first accesses the application. If the
   * cookie already exists then we retrieve the guest customer id from the cookie which will be used
   * to retrieve and cart items that the customer added to the cart on his last visit.
   *
   * @param request
   * @param response
   * @param kkAppEng
   * @return Returns the Customer UUID
   * @throws KKException
   * @throws KKAppException
   */
  private String manageCookies(
      HttpServletRequest request, HttpServletResponse response, KKAppEng kkAppEng)
      throws KKException, KKAppException {
    if (!kkAppEng.isKkCookieEnabled()) {
      return null;
    }

    /*
     * The current customer should at this point be a guest customer with a negative customer id
     */
    CustomerIf currentCustomer = kkAppEng.getCustomerMgr().getCurrentCustomer();
    if (currentCustomer == null) {
      log.warn(
          "Current customer is set to null in the manageCookies method. This should never happen");
      return null;
    }

    /*
     * Get the customerUuid from the browser cookie. A new cookie is created if it doesn't exist
     */
    String customerUuid = getCustomerUuidFromBrowserCookie(request, response, kkAppEng);

    /*
     * Get the guestCustomerId from the KK database.
     */
    String guestCustomerIdStr = getKKCookie(customerUuid, GUEST_CUSTOMER_ID, kkAppEng);

    if (guestCustomerIdStr == null) {
      /*
       * If it doesn't exist, then we create it
       */
      setKKCookie(
          customerUuid, GUEST_CUSTOMER_ID, Integer.toString(currentCustomer.getId()), kkAppEng);

    } else {
      /*
       * Set the current customer id with the one retrieved from the cookie and fetch any cart
       * items that he may have.
       */
      currentCustomer.setId(Integer.parseInt(guestCustomerIdStr));
      kkAppEng.getBasketMgr().getBasketItemsPerCustomer();
      if (kkAppEng.getWishListMgr().allowWishListWhenNotLoggedIn()) {
        kkAppEng.getWishListMgr().fetchCustomersWishLists();
      }

      // Get the product page size
      String prodPageSizeStr = getKKCookie(customerUuid, TAG_PROD_PAGE_SIZE, kkAppEng);
      if (prodPageSizeStr != null && prodPageSizeStr.length() > 0) {
        try {
          int prodPageSize = Integer.parseInt(prodPageSizeStr);
          kkAppEng.getProductMgr().setMaxDisplaySearchResults(prodPageSize);
        } catch (NumberFormatException e) {
          log.warn(
              "The product page size value stored in the cookie for customer with guest id "
                  + guestCustomerIdStr
                  + " is not a numeric value: "
                  + prodPageSizeStr);
        }
      }

      // Get the order page size
      String orderPageSizeStr = getKKCookie(customerUuid, TAG_ORDER_PAGE_SIZE, kkAppEng);
      if (orderPageSizeStr != null && orderPageSizeStr.length() > 0) {
        try {
          int orderPageSize = Integer.parseInt(orderPageSizeStr);
          kkAppEng.getOrderMgr().setPageSize(orderPageSize);
        } catch (NumberFormatException e) {
          log.warn(
              "The order page size value stored in the cookie for customer with guest id "
                  + guestCustomerIdStr
                  + " is not a numeric value: "
                  + orderPageSizeStr);
        }
      }

      // Get the review page size
      String reviewPageSizeStr = getKKCookie(customerUuid, TAG_REVIEW_PAGE_SIZE, kkAppEng);
      if (reviewPageSizeStr != null && reviewPageSizeStr.length() > 0) {
        try {
          int reviewPageSize = Integer.parseInt(reviewPageSizeStr);
          kkAppEng.getReviewMgr().setPageSize(reviewPageSize);
        } catch (NumberFormatException e) {
          log.warn(
              "The review page size value stored in the cookie for customer with guest id "
                  + guestCustomerIdStr
                  + " is not a numeric value: "
                  + reviewPageSizeStr);
        }
      }

      // Figure out whether the customer has agreed to cookies
      String agreedCookies = getKKCookie(customerUuid, AGREED_COOKIES, kkAppEng);
      if (agreedCookies != null) {
        kkAppEng.setAgreedCookies(true);
      }
    }

    if (log.isDebugEnabled()) {
      log.debug(
          "GUEST_CUSTOMER_ID cookie value = "
              + getKKCookie(customerUuid, GUEST_CUSTOMER_ID, kkAppEng));
      log.debug(
          "CUSTOMER_NAME cookie value = " + getKKCookie(customerUuid, CUSTOMER_NAME, kkAppEng));
      log.debug(
          "AGREED_COOKIES cookie value = " + getKKCookie(customerUuid, AGREED_COOKIES, kkAppEng));
      log.debug(
          "CUSTOMER_LOCALE cookie value = " + getKKCookie(customerUuid, CUSTOMER_LOCALE, kkAppEng));
      log.debug(
          "PROD_PAGE_SIZE cookie value = "
              + getKKCookie(customerUuid, TAG_PROD_PAGE_SIZE, kkAppEng));
      log.debug(
          "ORDER_PAGE_SIZE cookie value = "
              + getKKCookie(customerUuid, TAG_ORDER_PAGE_SIZE, kkAppEng));
      log.debug(
          "REVIEW_PAGE_SIZE cookie value = "
              + getKKCookie(customerUuid, TAG_REVIEW_PAGE_SIZE, kkAppEng));
    }

    /*
     * Call class where you can place custom code
     */
    CustomCookieAction cca = new CustomCookieAction();
    cca.manageCookiesOnEntry(request, response, kkAppEng);

    return customerUuid;
  }