📄 shoppingcartevents.java
字号:
boolean removeSelected = ("true".equals(removeSelectedFlag) && selectedItems != null && selectedItems.length > 0); result = cartHelper.modifyCart(security, userLogin, paramMap, removeSelected, selectedItems, locale); controlDirective = processResult(result, request); //Determine where to send the browser if (controlDirective.equals(ERROR)) { return "error"; } else { return "success"; } } /** Empty the shopping cart. */ public static String clearCart(HttpServletRequest request, HttpServletResponse response) { ShoppingCart cart = getCartObject(request); cart.clear(); return "success"; } /** Totally wipe out the cart, removes all stored info. */ public static String destroyCart(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); clearCart(request, response); session.removeAttribute("shoppingCart"); session.removeAttribute("orderPartyId"); session.removeAttribute("orderMode"); session.removeAttribute("productStoreId"); session.removeAttribute("CURRENT_CATALOG_ID"); return "success"; } /** Gets or creates the shopping cart object */ public static ShoppingCart getCartObject(HttpServletRequest request, Locale locale, String currencyUom) { LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); ShoppingCart cart = (ShoppingCart) request.getAttribute("shoppingCart"); HttpSession session = request.getSession(true); if (cart == null) { cart = (ShoppingCart) session.getAttribute("shoppingCart"); } else { session.setAttribute("shoppingCart", cart); } if (cart == null) { if (locale == null) { locale = UtilHttp.getLocale(request); } if (currencyUom == null) { currencyUom = UtilHttp.getCurrencyUom(request); } cart = new WebShoppingCart(request, locale, currencyUom); session.setAttribute("shoppingCart", cart); } else { if (locale != null && !locale.equals(cart.getLocale())) { cart.setLocale(locale); } if (currencyUom != null && !currencyUom.equals(cart.getCurrency())) { try { cart.setCurrency(dispatcher, currencyUom); } catch (CartItemModifyException e) { Debug.logError(e, "Unable to modify currency in cart", module); } } } return cart; } /** Main get cart method; uses the locale & currency from the session */ public static ShoppingCart getCartObject(HttpServletRequest request) { return getCartObject(request, null, null); } /** Update the cart's UserLogin object if it isn't already set. */ public static String keepCartUpdated(HttpServletRequest request, HttpServletResponse response) { LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); HttpSession session = request.getSession(); ShoppingCart cart = getCartObject(request); // if we just logged in set the UL if (cart.getUserLogin() == null) { GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); if (userLogin != null) { try { cart.setUserLogin(userLogin, dispatcher); } catch (CartItemModifyException e) { Debug.logWarning(e, module); } } } // same for autoUserLogin if (cart.getAutoUserLogin() == null) { GenericValue autoUserLogin = (GenericValue) session.getAttribute("autoUserLogin"); if (autoUserLogin != null) { if (cart.getUserLogin() == null) { try { cart.setAutoUserLogin(autoUserLogin, dispatcher); } catch (CartItemModifyException e) { Debug.logWarning(e, module); } } else { cart.setAutoUserLogin(autoUserLogin); } } } // update the locale Locale locale = UtilHttp.getLocale(request); if (cart.getLocale() == null || !locale.equals(cart.getLocale())) { cart.setLocale(locale); } return "success"; } /** For GWP Promotions with multiple alternatives, selects an alternative to the current GWP */ public static String setDesiredAlternateGwpProductId(HttpServletRequest request, HttpServletResponse response) { ShoppingCart cart = getCartObject(request); GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); String alternateGwpProductId = request.getParameter("alternateGwpProductId"); String alternateGwpLineStr = request.getParameter("alternateGwpLine"); Locale locale = UtilHttp.getLocale(request); if (UtilValidate.isEmpty(alternateGwpProductId)) { request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotSelectAlternateGiftNoAlternateGwpProductIdPassed", locale)); return "error"; } if (UtilValidate.isEmpty(alternateGwpLineStr)) { request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotSelectAlternateGiftNoAlternateGwpLinePassed", locale)); return "error"; } int alternateGwpLine = 0; try { alternateGwpLine = Integer.parseInt(alternateGwpLineStr); } catch (Exception e) { request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotSelectAlternateGiftAlternateGwpLineIsNotAValidNumber", locale)); return "error"; } ShoppingCartItem cartLine = cart.findCartItem(alternateGwpLine); if (cartLine == null) { request.setAttribute("_ERROR_MESSAGE_", "Could not select alternate gift, no cart line item found for #" + alternateGwpLine + "."); return "error"; } if (cartLine.getIsPromo()) { // note that there should just be one promo adjustment, the reversal of the GWP, so use that to get the promo action key Iterator checkOrderAdjustments = UtilMisc.toIterator(cartLine.getAdjustments()); while (checkOrderAdjustments != null && checkOrderAdjustments.hasNext()) { GenericValue checkOrderAdjustment = (GenericValue) checkOrderAdjustments.next(); if (UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoId")) && UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoRuleId")) && UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoActionSeqId"))) { GenericPK productPromoActionPk = delegator.makeValidValue("ProductPromoAction", checkOrderAdjustment).getPrimaryKey(); cart.setDesiredAlternateGiftByAction(productPromoActionPk, alternateGwpProductId); if (cart.getOrderType().equals("SALES_ORDER")) { org.ofbiz.order.shoppingcart.product.ProductPromoWorker.doPromotions(cart, dispatcher); } return "success"; } } } request.setAttribute("_ERROR_MESSAGE_", "Could not select alternate gift, cart line item found for #" + alternateGwpLine + " does not appear to be a valid promotional gift."); return "error"; } /** Associates a party to order */ public static String addAdditionalParty(HttpServletRequest request, HttpServletResponse response) { ShoppingCart cart = getCartObject(request); String partyId = request.getParameter("additionalPartyId"); String roleTypeId[] = request.getParameterValues("additionalRoleTypeId"); List eventList = new LinkedList(); Locale locale = UtilHttp.getLocale(request); int i; if (UtilValidate.isEmpty(partyId) || roleTypeId.length < 1) { request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderPartyIdAndOrRoleTypeIdNotDefined", locale)); return "error"; } if (request.getAttribute("_EVENT_MESSAGE_LIST_") != null) { eventList.addAll((List) request.getAttribute("_EVENT_MESSAGE_LIST_")); } for (i = 0; i < roleTypeId.length; i++) { try { cart.addAdditionalPartyRole(partyId, roleTypeId[i]); } catch (Exception e) { eventList.add(e.getLocalizedMessage()); } } request.removeAttribute("_EVENT_MESSAGE_LIST_"); request.setAttribute("_EVENT_MESSAGE_LIST_", eventList); return "success"; } /** Removes a previously associated party to order */ public static String removeAdditionalParty(HttpServletRequest request, HttpServletResponse response) { ShoppingCart cart = getCartObject(request); String partyId = request.getParameter("additionalPartyId"); String roleTypeId[] = request.getParameterValues("additionalRoleTypeId"); List eventList = new LinkedList(); Locale locale = UtilHttp.getLocale(request); int i; if (UtilValidate.isEmpty(partyId) || roleTypeId.length < 1) { request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderPartyIdAndOrRoleTypeIdNotDefined", locale)); return "error"; } if (request.getAttribute("_EVENT_MESSAGE_LIST_") != null) { eventList.addAll((List) request.getAttribute("_EVENT_MESSAGE_LIST_")); } for (i = 0; i < roleTypeId.length; i++) { try { cart.removeAdditionalPartyRole(partyId, roleTypeId[i]); } catch (Exception e) { Debug.logInfo(e.getLocalizedMessage(), module); eventList.add(e.getLocalizedMessage()); } } request.removeAttribute("_EVENT_MESSAGE_LIST_"); request.setAttribute("_EVENT_MESSAGE_LIST_", eventList); return "success"; } /** * This should be called to translate the error messages of the * <code>ShoppingCartHelper</code> to an appropriately formatted * <code>String</code> in the request object and indicate whether * the result was an error or not and whether the errors were * critical or not * * @param result The result returned from the * <code>ShoppingCartHelper</code> * @param request The servlet request instance to set the error messages * in * @return one of NON_CRITICAL_ERROR, ERROR or NO_ERROR. */ private static String processResult(Map result, HttpServletRequest request) { //Check for errors StringBuffer errMsg = new StringBuffer(); if (result.containsKey(ModelService.ERROR_MESSAGE_LIST)) { List errorMsgs = (List)result.get(ModelService.ERROR_MESSAGE_LIST); Iterator iterator = errorMsgs.iterator(); errMsg.append("<ul>"); while (iterator.hasNext()) { errMsg.append("<li>"); errMsg.append(iterator.next()); errMsg.append("</li>"); } errMsg.append("</ul>"); } else if (result.containsKey(ModelService.ERROR_MESSAGE)) { errMsg.append(result.get(ModelService.ERROR_MESSAGE)); request.setAttribute("_ERROR_MESSAGE_", errMsg.toString()); } //See whether there was an error if (errMsg.length() > 0) { request.setAttribute("_ERROR_MESSAGE_", errMsg.toString()); if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) { return NON_CRITICAL_ERROR; } else { return ERROR; } } else { return NO_ERROR;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -