Exemplo n.º 1
1
  public static String addBulkFromCart(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
    GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");

    String shoppingListId = request.getParameter("shoppingListId");
    String shoppingListTypeId = request.getParameter("shoppingListTypeId");
    String selectedCartItems[] = request.getParameterValues("selectedItem");
    if (UtilValidate.isEmpty(selectedCartItems)) {
      selectedCartItems = makeCartItemsArray(cart);
    }

    try {
      shoppingListId =
          addBulkFromCart(
              delegator,
              dispatcher,
              cart,
              userLogin,
              shoppingListId,
              shoppingListTypeId,
              selectedCartItems,
              true,
              true);
    } catch (IllegalArgumentException e) {
      request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
      return "error";
    }

    request.setAttribute("shoppingListId", shoppingListId);
    return "success";
  }
Exemplo n.º 2
0
  public static String addListToCart(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);

    String shoppingListId = request.getParameter("shoppingListId");
    String includeChild = request.getParameter("includeChild");
    String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);

    String eventMessage = null;
    try {
      addListToCart(
          delegator,
          dispatcher,
          cart,
          prodCatalogId,
          shoppingListId,
          (includeChild != null),
          true,
          true);
    } catch (IllegalArgumentException e) {
      request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
      return "error";
    }

    if (UtilValidate.isNotEmpty(eventMessage)) {
      request.setAttribute("_EVENT_MESSAGE_", eventMessage);
    }

    return "success";
  }
Exemplo n.º 3
0
  /** Saves the shopping cart to the specialized (auto-save) shopping list */
  public static String saveCartToAutoSaveList(
      HttpServletRequest request, HttpServletResponse response) {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);

    try {
      fillAutoSaveList(cart, dispatcher);
    } catch (GeneralException e) {
      Debug.logError(e, "Error saving the cart to the auto-save list: " + e.toString(), module);
    }

    return "success";
  }
Exemplo n.º 4
0
  /** Create the guest cookies for a shopping list */
  public static String createGuestShoppingListCookies(
      HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    HttpSession session = request.getSession(true);
    ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart");
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    Properties systemProps = System.getProperties();
    String guestShoppingUserName =
        "******" + systemProps.getProperty("user.name").replace(" ", "_");
    String productStoreId = ProductStoreWorker.getProductStoreId(request);
    int cookieAge = (60 * 60 * 24 * 30);
    String autoSaveListId = null;
    Cookie[] cookies = request.getCookies();

    // check userLogin
    if (UtilValidate.isNotEmpty(userLogin)) {
      String partyId = userLogin.getString("partyId");
      if (UtilValidate.isEmpty(partyId)) {
        return "success";
      }
    }

    // find shopping list ID
    if (cookies != null) {
      for (Cookie cookie : cookies) {
        if (cookie.getName().equals(guestShoppingUserName)) {
          autoSaveListId = cookie.getValue();
          break;
        }
      }
    }

    // clear the auto-save info
    if (ProductStoreWorker.autoSaveCart(delegator, productStoreId)) {
      if (UtilValidate.isEmpty(autoSaveListId)) {
        try {
          Map<String, Object> listFields =
              UtilMisc.<String, Object>toMap(
                  "userLogin",
                  userLogin,
                  "productStoreId",
                  productStoreId,
                  "shoppingListTypeId",
                  "SLT_SPEC_PURP",
                  "listName",
                  PERSISTANT_LIST_NAME);
          Map<String, Object> newListResult = dispatcher.runSync("createShoppingList", listFields);
          if (newListResult != null) {
            autoSaveListId = (String) newListResult.get("shoppingListId");
          }
        } catch (GeneralException e) {
          Debug.logError(e, module);
        }
        Cookie guestShoppingListCookie = new Cookie(guestShoppingUserName, autoSaveListId);
        guestShoppingListCookie.setMaxAge(cookieAge);
        guestShoppingListCookie.setPath("/");
        response.addCookie(guestShoppingListCookie);
      }
    }
    if (UtilValidate.isNotEmpty(autoSaveListId)) {
      if (UtilValidate.isNotEmpty(cart)) {
        cart.setAutoSaveListId(autoSaveListId);
      } else {
        cart = ShoppingCartEvents.getCartObject(request);
        cart.setAutoSaveListId(autoSaveListId);
      }
    }
    return "success";
  }
Exemplo n.º 5
0
  /** Restores the specialized (auto-save) shopping list back into the shopping cart */
  public static String restoreAutoSaveList(
      HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    GenericValue productStore = ProductStoreWorker.getProductStore(request);

    if (!ProductStoreWorker.autoSaveCart(productStore)) {
      // if auto-save is disabled just return here
      return "success";
    }

    HttpSession session = request.getSession();
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);

    // safety check for missing required parameter.
    if (cart.getWebSiteId() == null) {
      cart.setWebSiteId(WebSiteWorker.getWebSiteId(request));
    }

    // locate the user's identity
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    if (userLogin == null) {
      userLogin = (GenericValue) session.getAttribute("autoUserLogin");
    }

    // find the list ID
    String autoSaveListId = cart.getAutoSaveListId();
    if (autoSaveListId == null) {
      try {
        autoSaveListId =
            getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId());
      } catch (GeneralException e) {
        Debug.logError(e, module);
      }
      cart.setAutoSaveListId(autoSaveListId);
    } else if (userLogin != null) {
      String existingAutoSaveListId = null;
      try {
        existingAutoSaveListId =
            getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId());
      } catch (GeneralException e) {
        Debug.logError(e, module);
      }
      if (existingAutoSaveListId != null) {
        if (!existingAutoSaveListId.equals(autoSaveListId)) {
          // Replace with existing shopping list
          cart.setAutoSaveListId(existingAutoSaveListId);
          autoSaveListId = existingAutoSaveListId;
          cart.setLastListRestore(null);
        } else {
          // CASE: User first login and logout and then re-login again. This condition does not
          // require a restore at all
          // because at this point items in the cart and the items in the shopping list are same so
          // just return.
          return "success";
        }
      }
    }

    // check to see if we are okay to load this list
    java.sql.Timestamp lastLoad = cart.getLastListRestore();
    boolean okayToLoad = autoSaveListId == null ? false : (lastLoad == null ? true : false);
    if (!okayToLoad && lastLoad != null) {
      GenericValue shoppingList = null;
      try {
        shoppingList =
            EntityQuery.use(delegator)
                .from("ShoppingList")
                .where("shoppingListId", autoSaveListId)
                .queryOne();
      } catch (GenericEntityException e) {
        Debug.logError(e, module);
      }
      if (shoppingList != null) {
        java.sql.Timestamp lastModified = shoppingList.getTimestamp("lastAdminModified");
        if (lastModified != null) {
          if (lastModified.after(lastLoad)) {
            okayToLoad = true;
          }
          if (cart.size() == 0 && lastModified.after(cart.getCartCreatedTime())) {
            okayToLoad = true;
          }
        }
      }
    }

    // load (restore) the list of we have determined it is okay to load
    if (okayToLoad) {
      String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);
      try {
        addListToCart(
            delegator,
            dispatcher,
            cart,
            prodCatalogId,
            autoSaveListId,
            false,
            false,
            userLogin != null ? true : false);
        cart.setLastListRestore(UtilDateTime.nowTimestamp());
      } catch (IllegalArgumentException e) {
        Debug.logError(e, module);
      }
    }

    return "success";
  }