📄 shoppinglistevents.java
字号:
} catch (ItemNotFoundException e) { Debug.logWarning(e, UtilProperties.getMessage(resource_error,"OrderProductNotFound", cart.getLocale())); Map messageMap = UtilMisc.toMap("productId", productId); errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.problem_adding_product_to_cart", messageMap, cart.getLocale()); eventMessage.append(errMsg + "\n"); } } if (eventMessage.length() > 0) { return eventMessage.toString(); } // all done return ""; // no message to return; will simply reply as success } public static String replaceShoppingListItem(HttpServletRequest request, HttpServletResponse response) { String quantityStr = request.getParameter("quantity"); // just call the updateShoppingListItem service LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); Locale locale = UtilHttp.getLocale(request); Double quantity = null; try { quantity = Double.valueOf(quantityStr); } catch (Exception e) { // do nothing, just won't pass to service if it is null } Map serviceInMap = new HashMap(); serviceInMap.put("shoppingListId", request.getParameter("shoppingListId")); serviceInMap.put("shoppingListItemSeqId", request.getParameter("shoppingListItemSeqId")); serviceInMap.put("productId", request.getParameter("add_product_id")); serviceInMap.put("userLogin", userLogin); if (quantity != null) serviceInMap.put("quantity", quantity); Map result = null; try { result = dispatcher.runSync("updateShoppingListItem", serviceInMap); } catch (GenericServiceException e) { String errMsg = UtilProperties.getMessage(ShoppingListEvents.err_resource,"shoppingListEvents.error_calling_update", locale) + ": " + e.toString(); request.setAttribute("_ERROR_MESSAGE_", errMsg); String errorMsg = "Error calling the updateShoppingListItem in handleShoppingListItemVariant: " + e.toString(); Debug.logError(e, errorMsg, module); return "error"; } ServiceUtil.getMessages(request, result, "", "", "", "", "", "", ""); if ("error".equals(result.get(ModelService.RESPONSE_MESSAGE))) { return "error"; } else { return "success"; } } /** * Finds or creates a specialized (auto-save) shopping list used to record shopping bag contents between user visits. */ public static String getAutoSaveListId(GenericDelegator delegator, LocalDispatcher dispatcher, String partyId, GenericValue userLogin, String productStoreId) throws GenericEntityException, GenericServiceException { if (partyId == null && userLogin != null) { partyId = userLogin.getString("partyId"); } String autoSaveListId = null; // TODO: add sorting, just in case there are multiple... Map findMap = UtilMisc.toMap("partyId", partyId, "productStoreId", productStoreId, "shoppingListTypeId", "SLT_SPEC_PURP", "listName", PERSISTANT_LIST_NAME); List existingLists = delegator.findByAnd("ShoppingList", findMap); Debug.logInfo("Finding existing auto-save shopping list with: \nfindMap: " + findMap + "\nlists: " + existingLists, module); GenericValue list = null; if (existingLists != null && !existingLists.isEmpty()) { list = EntityUtil.getFirst(existingLists); autoSaveListId = list.getString("shoppingListId"); } if (list == null && dispatcher != null && userLogin != null) { Map listFields = UtilMisc.toMap("userLogin", userLogin, "productStoreId", productStoreId, "shoppingListTypeId", "SLT_SPEC_PURP", "listName", PERSISTANT_LIST_NAME); Map newListResult = dispatcher.runSync("createShoppingList", listFields); if (newListResult != null) { autoSaveListId = (String) newListResult.get("shoppingListId"); } } return autoSaveListId; } /** * Fills the specialized shopping list with the current shopping cart if one exists (if not leaves it alone) */ public static void fillAutoSaveList(ShoppingCart cart, LocalDispatcher dispatcher) throws GeneralException { if (cart != null && dispatcher != null) { GenericValue userLogin = ShoppingListEvents.getCartUserLogin(cart); if (userLogin == null) return; //only save carts when a user is logged in.... GenericDelegator delegator = cart.getDelegator(); String autoSaveListId = getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId()); try { String[] itemsArray = makeCartItemsArray(cart); if (itemsArray != null && itemsArray.length != 0) { addBulkFromCart(delegator, dispatcher, cart, userLogin, autoSaveListId, itemsArray, false, false); } } catch (IllegalArgumentException e) { throw new GeneralException(e.getMessage(), e); } } } /** * Saves the shopping cart to the specialized (auto-save) shopping list */ public static String saveCartToAutoSaveList(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 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"; } /** * Restores the specialized (auto-save) shopping list back into the shopping cart */ public static String restoreAutoSaveList(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) 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(CatalogWorker.getWebSiteId(request)); } // locate the user's identity GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); if (userLogin == null) { userLogin = (GenericValue) session.getAttribute("autoUserLogin"); } if (userLogin == null) { // not logged in; cannot identify the user return "success"; } // 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); } // 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 = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", autoSaveListId)); } 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, false); cart.setLastListRestore(UtilDateTime.nowTimestamp()); } catch (IllegalArgumentException e) { Debug.logError(e, module); } } return "success"; } /** * Remove all items from the given list. */ public static int clearListInfo(GenericDelegator delegator, String shoppingListId) throws GenericEntityException { // remove the survey responses first delegator.removeByAnd("ShoppingListItemSurvey", UtilMisc.toMap("shoppingListId", shoppingListId)); // next remove the items return delegator.removeByAnd("ShoppingListItem", UtilMisc.toMap("shoppingListId", shoppingListId)); } /** * Creates records for survey responses on survey items */ public static int makeListItemSurveyResp(GenericDelegator delegator, GenericValue item, List surveyResps) throws GenericEntityException { if (surveyResps != null && surveyResps.size() > 0) { Iterator i = surveyResps.iterator(); int count = 0; while (i.hasNext()) { String responseId = (String) i.next(); GenericValue listResp = delegator.makeValue("ShoppingListItemSurvey", null); listResp.set("shoppingListId", item.getString("shoppingListId")); listResp.set("shoppingListItemSeqId", item.getString("shoppingListItemSeqId")); listResp.set("surveyResponseId", responseId); delegator.create(listResp); count++; } return count; } return -1; } /** * Returns Map keyed on item sequence ID containing a list of survey response IDs */ public static Map getItemSurveyInfos(List items) { Map surveyInfos = new HashMap(); if (items != null && items.size() > 0) { Iterator itemIt = items.iterator(); while (itemIt.hasNext()) { GenericValue item = (GenericValue) itemIt.next(); String listId = item.getString("shoppingListId"); String itemId = item.getString("shoppingListItemSeqId"); surveyInfos.put(listId + "." + itemId, getItemSurveyInfo(item)); } } return surveyInfos; } /** * Returns a list of survey response IDs for a shopping list item */ public static List getItemSurveyInfo(GenericValue item) { List responseIds = new ArrayList(); List surveyResp = null; try { surveyResp = item.getRelated("ShoppingListItemSurvey"); } catch (GenericEntityException e) { Debug.logError(e, module); } if (surveyResp != null || surveyResp.size() > 0) { Iterator respIt = surveyResp.iterator(); while (respIt.hasNext()) { GenericValue resp = (GenericValue) respIt.next(); responseIds.add(resp.getString("surveyResponseId")); } } return responseIds; } private static GenericValue getCartUserLogin(ShoppingCart cart) { GenericValue ul = cart.getUserLogin(); if (ul == null) { ul = cart.getAutoUserLogin(); } return ul; } private static String[] makeCartItemsArray(ShoppingCart cart) { int len = cart.size(); String[] arr = new String[len]; for (int i = 0; i < len; i++) { arr[i] = new Integer(i).toString(); } return arr; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -