Example #1
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";
  }