⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcarthelper.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * $Id: ShoppingCartHelper.java 7151 2006-03-31 13:17:41Z jacopo $ * *  Copyright (c) 2001-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.shoppingcart;import java.sql.Timestamp;import java.text.NumberFormat;import java.text.ParseException;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Vector;import java.util.Locale;import org.ofbiz.base.util.Debug;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.product.ProductPromoWorker;import org.ofbiz.product.config.ProductConfigWrapper;import org.ofbiz.security.Security;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * A facade over the  * {@link org.ofbiz.order.shoppingcart.ShoppingCart ShoppingCart} * providing catalog and product services to simplify the interaction * with the cart directly.  * * @author     <a href="mailto:tristana@twibble.org">Tristan Austin</a> * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version    $Rev: 7151 $ * @since      2.0 */public class ShoppingCartHelper {    public static final String resource = "OrderUiLabels";    public static String module = ShoppingCartHelper.class.getName();    public static final String resource_error = "OrderErrorUiLabels";    // The shopping cart to manipulate    private ShoppingCart cart = null;    // The entity engine delegator    private GenericDelegator delegator = null;    // The service invoker    private LocalDispatcher dispatcher = null;    /**     * Changes will be made to the cart directly, as opposed     * to a copy of the cart provided.     *      * @param cart The cart to manipulate     */    public ShoppingCartHelper(GenericDelegator delegator, LocalDispatcher dispatcher, ShoppingCart cart) {        this.dispatcher = dispatcher;        this.delegator = delegator;        this.cart = cart;        if (delegator == null) {            this.delegator = dispatcher.getDelegator();        }        if (dispatcher == null) {            throw new IllegalArgumentException("Dispatcher argument is null");        }        if (cart == null) {            throw new IllegalArgumentException("ShoppingCart argument is null");        }    }    /** Event to add an item to the shopping cart. */    public Map addToCart(String catalogId, String shoppingListId, String shoppingListItemSeqId, String productId,            String productCategoryId, String itemType, String itemDescription,             double price, double amount, double quantity,             java.sql.Timestamp reservStart, double reservLength, double reservPersons,             java.sql.Timestamp shipBeforeDate, java.sql.Timestamp shipAfterDate,            ProductConfigWrapper configWrapper, Map context) {        Map result = null;        Map attributes = null;        // price sanity check        if (productId == null && price < 0) {            String errMsg = UtilProperties.getMessage(resource, "cart.price_not_positive_number", this.cart.getLocale());            result = ServiceUtil.returnError(errMsg);            return result;        }        // quantity sanity check        if (quantity < 1) {            String errMsg = UtilProperties.getMessage(resource, "cart.quantity_not_positive_number", this.cart.getLocale());            result = ServiceUtil.returnError(errMsg);            return result;        }        // amount sanity check        if (amount < 0) {            amount = 0;        }        // check desiredDeliveryDate syntax and remove if empty        String ddDate = (String) context.get("itemDesiredDeliveryDate");        if (!UtilValidate.isEmpty(ddDate)) {            try {                java.sql.Timestamp.valueOf((String) context.get("itemDesiredDeliveryDate"));            } catch (IllegalArgumentException e) {            	return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderInvalidDesiredDeliveryDateSyntaxError",this.cart.getLocale()));            }        } else {            context.remove("itemDesiredDeliveryDate");        }        // remove an empty comment        String comment = (String) context.get("itemComment");        if (UtilValidate.isEmpty(comment)) {            context.remove("itemComment");        }        // stores the default desired delivery date in the cart if need        if (!UtilValidate.isEmpty((String) context.get("useAsDefaultDesiredDeliveryDate"))) {            cart.setDefaultItemDeliveryDate((String) context.get("itemDesiredDeliveryDate"));        } else {            // do we really want to clear this if it isn't checked?            cart.setDefaultItemDeliveryDate(null);        }        // stores the default comment in session if need        if (!UtilValidate.isEmpty((String) context.get("useAsDefaultComment"))) {            cart.setDefaultItemComment((String) context.get("itemComment"));        } else {            // do we really want to clear this if it isn't checked?            cart.setDefaultItemComment(null);        }        // Create a HashMap of product attributes - From ShoppingCartItem.attributeNames[]        for (int namesIdx = 0; namesIdx < ShoppingCartItem.attributeNames.length; namesIdx++) {            if (attributes == null)                attributes = new HashMap();            if (context.containsKey(ShoppingCartItem.attributeNames[namesIdx])) {                attributes.put(ShoppingCartItem.attributeNames[namesIdx], context.get(ShoppingCartItem.attributeNames[namesIdx]));            }        }        // check for required amount flag; if amount and no flag set to 0        GenericValue product = null;        if (productId != null) {            try {                product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId));            } catch (GenericEntityException e) {                Debug.logError(e, "Unable to lookup product : " + productId, module);            }            if (product == null || product.get("requireAmount") == null || "N".equals(product.getString("requireAmount"))) {                amount = 0;            }        }        // add or increase the item to the cart                try {            int itemId = -1;            if (productId != null) {                itemId = cart.addOrIncreaseItem(productId, amount, quantity, reservStart, reservLength, reservPersons, shipBeforeDate, shipAfterDate,                        null, attributes, catalogId, configWrapper, dispatcher);            } else {                itemId = cart.addNonProductItem(itemType, itemDescription, productCategoryId, price, quantity, attributes, catalogId, dispatcher);            }            // set the shopping list info            if (itemId > -1 && shoppingListId != null && shoppingListItemSeqId != null) {                ShoppingCartItem item = cart.findCartItem(itemId);                item.setShoppingList(shoppingListId, shoppingListItemSeqId);            }        } catch (CartItemModifyException e) {            if (cart.getOrderType().equals("PURCHASE_ORDER")) {                String errMsg = UtilProperties.getMessage(resource, "cart.product_not_valid_for_supplier", this.cart.getLocale());                errMsg = errMsg + " (" + e.getMessage() + ")";                result = ServiceUtil.returnError(errMsg);            } else {                result = ServiceUtil.returnError(e.getMessage());            }            return result;        } catch (ItemNotFoundException e) {            result = ServiceUtil.returnError(e.getMessage());            return result;        }                // Indicate there were no critical errors        result = ServiceUtil.returnSuccess();        return result;    }    public Map addToCartFromOrder(String catalogId, String orderId, String[] itemIds, boolean addAll) {        ArrayList errorMsgs = new ArrayList();        Map result;        String errMsg = null;        if (orderId == null || orderId.length() <= 0) {            errMsg = UtilProperties.getMessage(resource,"cart.order_not_specified_to_add_from", this.cart.getLocale());            result = ServiceUtil.returnError(errMsg);            return result;        }        boolean noItems = true;        if (addAll) {            Iterator itemIter = null;            try {                itemIter = UtilMisc.toIterator(delegator.findByAnd("OrderItem", UtilMisc.toMap("orderId", orderId), null));            } catch (GenericEntityException e) {                Debug.logWarning(e.getMessage(), module);                itemIter = null;            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -