📄 shoppinglistevents.java
字号:
/* * $Id: ShoppingListEvents.java 5847 2005-09-28 05:59:20Z jacopo $ * * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.order.shoppinglist;import java.sql.Timestamp;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Locale;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.order.shoppingcart.CartItemModifyException;import org.ofbiz.order.shoppingcart.ItemNotFoundException;import org.ofbiz.order.shoppingcart.ShoppingCart;import org.ofbiz.order.shoppingcart.ShoppingCartEvents;import org.ofbiz.order.shoppingcart.ShoppingCartItem;import org.ofbiz.product.catalog.CatalogWorker;import org.ofbiz.product.store.ProductStoreWorker;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * Shopping cart events. * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 5847 $ * @since 2.2 */public class ShoppingListEvents { public static final String module = ShoppingListEvents.class.getName(); public static final String resource = "OrderUiLabels"; public static final String err_resource = "OrderErrorUiLabel"; public static final String resource_error = "OrderErrorUiLabels"; public static final String PERSISTANT_LIST_NAME = "auto-save"; public static String addBulkFromCart(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); ShoppingCart cart = ShoppingCartEvents.getCartObject(request); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); Locale locale = UtilHttp.getLocale(request); String shoppingListId = request.getParameter("shoppingListId"); String selectedCartItems[] = request.getParameterValues("selectedItem"); try { shoppingListId = addBulkFromCart(delegator, dispatcher, cart, userLogin, shoppingListId, selectedCartItems, true, true); } catch (IllegalArgumentException e) { request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); return "error"; } request.setAttribute("shoppingListId", shoppingListId); return "success"; } public static String addBulkFromCart(GenericDelegator delegator, LocalDispatcher dispatcher, ShoppingCart cart, GenericValue userLogin, String shoppingListId, String[] items, boolean allowPromo, boolean append) throws IllegalArgumentException { String errMsg = null; if (items == null || items.length == 0) { errMsg = UtilProperties.getMessage(resource, "shoppinglistevents.select_items_to_add_to_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } if (UtilValidate.isEmpty(shoppingListId)) { // create a new shopping list Map newListResult = null; try { newListResult = dispatcher.runSync("createShoppingList", UtilMisc.toMap("userLogin", userLogin, "productStoreId", cart.getProductStoreId(), "partyId", cart.getOrderPartyId(), "currencyUom", cart.getCurrency())); } catch (GenericServiceException e) { Debug.logError(e, "Problems creating new ShoppingList", module); errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.cannot_create_new_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // check for errors if (ServiceUtil.isError(newListResult)) { throw new IllegalArgumentException(ServiceUtil.getErrorMessage(newListResult)); } // get the new list id if (newListResult != null) { shoppingListId = (String) newListResult.get("shoppingListId"); } // if no list was created throw an error if (shoppingListId == null || shoppingListId.equals("")) { errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.shoppingListId_is_required_parameter", cart.getLocale()); throw new IllegalArgumentException(errMsg); } } else if (!append) { try { clearListInfo(delegator, shoppingListId); } catch (GenericEntityException e) { Debug.logError(e, module); throw new IllegalArgumentException("Could not clear current shopping list: " + e.toString()); } } for (int i = 0; i < items.length; i++) { Integer cartIdInt = null; try { cartIdInt = new Integer(items[i]); } catch (Exception e) { Debug.logWarning(e, UtilProperties.getMessage(resource_error,"OrderIllegalCharacterInSelectedItemField", cart.getLocale()), module); } if (cartIdInt != null) { ShoppingCartItem item = cart.findCartItem(cartIdInt.intValue()); if (allowPromo || !item.getIsPromo()) { Debug.logInfo("Adding cart item to shopping list [" + shoppingListId + "], allowPromo=" + allowPromo + ", item.getIsPromo()=" + item.getIsPromo() + ", item.getProductId()=" + item.getProductId() + ", item.getQuantity()=" + item.getQuantity(), module); Map serviceResult = null; try { Map ctx = UtilMisc.toMap("userLogin", userLogin, "shoppingListId", shoppingListId, "productId", item.getProductId(), "quantity", new Double(item.getQuantity())); ctx.put("reservStart", item.getReservStart()); ctx.put("reservLength", new Double(item.getReservLength())); ctx.put("reservPersons", new Double(item.getReservPersons())); serviceResult = dispatcher.runSync("createShoppingListItem", ctx); } catch (GenericServiceException e) { Debug.logError(e, "Problems creating ShoppingList item entity", module); errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.error_adding_item_to_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // check for errors if (ServiceUtil.isError(serviceResult)) { throw new IllegalArgumentException(ServiceUtil.getErrorMessage(serviceResult)); } } } } // return the shoppinglist id return shoppingListId; } public static String addListToCart(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); ShoppingCart cart = ShoppingCartEvents.getCartObject(request); Locale locale = UtilHttp.getLocale(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 (eventMessage != null && eventMessage.length() > 0) { request.setAttribute("_EVENT_MESSAGE_", eventMessage); } return "success"; } public static String addListToCart(GenericDelegator delegator, LocalDispatcher dispatcher, ShoppingCart cart, String prodCatalogId, String shoppingListId, boolean includeChild, boolean setAsListItem, boolean append) throws java.lang.IllegalArgumentException { String errMsg = null; // no list; no add if (shoppingListId == null) { errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.choose_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // get the shopping list GenericValue shoppingList = null; List shoppingListItems = null; try { shoppingList = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", shoppingListId)); if (shoppingList == null) { errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.error_getting_shopping_list_and_items", cart.getLocale()); throw new IllegalArgumentException(errMsg); } shoppingListItems = shoppingList.getRelated("ShoppingListItem"); if (shoppingListItems == null) { shoppingListItems = new LinkedList(); } // include all items of child lists if flagged to do so if (includeChild) { List childShoppingLists = shoppingList.getRelated("ChildShoppingList"); Iterator ci = childShoppingLists.iterator(); while (ci.hasNext()) { GenericValue v = (GenericValue) ci.next(); List items = v.getRelated("ShoppingListItem"); shoppingListItems.addAll(items); } } } catch (GenericEntityException e) { Debug.logError(e, "Problems getting ShoppingList and ShoppingListItem records", module); errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.error_getting_shopping_list_and_items", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // no items; not an error; just mention that nothing was added if (shoppingListItems == null || shoppingListItems.size() == 0) { errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.no_items_added", cart.getLocale()); return errMsg; } // check if we are to clear the cart first if (!append) { cart.clear(); } // get the survey info for all the items Map shoppingListSurveyInfo = getItemSurveyInfos(shoppingListItems); // add the items StringBuffer eventMessage = new StringBuffer(); Iterator i = shoppingListItems.iterator(); while (i.hasNext()) { GenericValue shoppingListItem = (GenericValue) i.next(); String productId = shoppingListItem.getString("productId"); Double quantity = shoppingListItem.getDouble("quantity"); Timestamp reservStart = shoppingListItem.getTimestamp("reservStart"); Double reservLength = shoppingListItem.getDouble("reservLength"); Double reservPersons = shoppingListItem.getDouble("reservPersons"); try { String listId = shoppingListItem.getString("shoppingListId"); String itemId = shoppingListItem.getString("shoppingListItemSeqId"); Map attributes = new HashMap(); // list items are noted in the shopping cart if (setAsListItem) { attributes.put("shoppingListId", listId); attributes.put("shoppingListItemSeqId", itemId); } // check if we have existing survey responses to append if (shoppingListSurveyInfo.containsKey(listId + "." + itemId)) { attributes.put("surveyResponses", shoppingListSurveyInfo.get(listId + "." + itemId)); } // TODO: add code to check for survey response requirement // i cannot get the addOrDecrease function to accept a null reservStart field: i get a null pointer exception a null constant works.... if (reservStart == null) cart.addOrIncreaseItem(productId, quantity.doubleValue(), null,0,0, null, attributes, prodCatalogId, dispatcher); else cart.addOrIncreaseItem(productId, quantity.doubleValue(), reservStart, reservLength.doubleValue(), reservPersons.doubleValue(), null, attributes, prodCatalogId, dispatcher); Map messageMap = UtilMisc.toMap("productId", productId); errMsg = UtilProperties.getMessage(resource,"shoppinglistevents.added_product_to_cart", messageMap, cart.getLocale()); eventMessage.append(errMsg + "\n"); } catch (CartItemModifyException e) { Debug.logWarning(e, UtilProperties.getMessage(resource_error,"OrderProblemsAddingItemFromListToCart", 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");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -