📄 shoppingcartitem.java
字号:
/*
* $Id: ShoppingCartItem.java,v 1.27 2004/01/12 17:18:19 jonesde Exp $
*
* Copyright (c) 2001, 2002 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.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 org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilFormatOut;
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.GenericPK;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.order.order.OrderReadHelper;
import org.ofbiz.product.catalog.CatalogWorker;
import org.ofbiz.product.category.CategoryWorker;
import org.ofbiz.product.product.ProductContentWrapper;
import org.ofbiz.product.product.ProductWorker;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
/**
* <p><b>Title:</b> ShoppingCartItem.java
* <p><b>Description:</b> Shopping cart item object.
*
* @author <a href="mailto:jaz@ofbiz.org.com">Andy Zeneski</a>
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.27 $
* @since 2.0
*/
public class ShoppingCartItem implements java.io.Serializable {
public static String module = ShoppingCartItem.class.getName();
public static final String resource = "OrderUiLabels";
public static String[] attributeNames = { "shoppingListId", "shoppingListItemSeqId", "surveyResponses" };
private transient GenericDelegator delegator = null;
private transient GenericValue _product = null; // the actual product
private transient GenericValue _parentProduct = null; // the virtual product
private String delegatorName = null;
private String prodCatalogId = null;
private String productId = null;
private String itemType = null;
private String itemComment = null;
private String productCategoryId = null;
private String itemDescription = null; // special field for non-product items
private double quantity = 0.0;
private double basePrice = 0.0;
private double listPrice = 0.0;
private double selectedAmount = 0.0;
private Map attributes = null;
private String orderItemSeqId = null;
private GenericValue orderShipmentPreference = null;
private Locale locale = null;
private Map contactMechIdsMap = new HashMap();
private List orderItemPriceInfos = null;
private List itemAdjustments = new LinkedList();
private boolean isPromo = false;
private double promoQuantityUsed = 0;
private Map quantityUsedPerPromoCandidate = new HashMap();
private Map quantityUsedPerPromoFailed = new HashMap();
private Map quantityUsedPerPromoActual = new HashMap();
private Map additionalProductFeatureAndAppls = new HashMap();
/**
* Makes a ShoppingCartItem and adds it to the cart.
* NOTE: This method will get the product entity and check to make sure it can be purchased.
*
* @param cartLocation The location to place this item; null will place at the end
* @param productId The primary key of the product being added
* @param quantity The quantity to add
* @param additionalProductFeatureAndAppls Product feature/appls map
* @param attributes All unique attributes for this item (NOT features)
* @param prodCatalogId The catalog this item was added from
* @param dispatcher LocalDispatcher object for doing promotions, etc
* @param cart The parent shopping cart object this item will belong to
* @return a new ShoppingCartItem object
* @throws CartItemModifyException
*/
public static ShoppingCartItem makeItem(Integer cartLocation, String productId, double selectedAmount, double quantity, Map additionalProductFeatureAndAppls, Map attributes, String prodCatalogId, LocalDispatcher dispatcher, ShoppingCart cart) throws CartItemModifyException {
GenericDelegator delegator = cart.getDelegator();
GenericValue product = null;
try {
product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId));
// first see if there is a purchase allow category and if this product is in it or not
String purchaseProductCategoryId = CatalogWorker.getCatalogPurchaseAllowCategoryId(delegator, prodCatalogId);
if (purchaseProductCategoryId != null) {
if (!CategoryWorker.isProductInCategory(delegator, product.getString("productId"), purchaseProductCategoryId)) {
// a Purchase allow productCategoryId was found, but the product is not in the category, axe it...
product = null;
}
}
} catch (GenericEntityException e) {
Debug.logWarning(e.toString(), module);
product = null;
}
if (product == null) {
Map messageMap = UtilMisc.toMap("productId", productId );
String excMsg = UtilProperties.getMessage(resource, "item.product_not_found",
messageMap , cart.getLocale() );
Debug.logWarning(excMsg, module);
throw new CartItemModifyException(excMsg);
}
return makeItem(cartLocation, product, selectedAmount, quantity, additionalProductFeatureAndAppls, attributes, prodCatalogId, dispatcher, cart, true);
}
/**
* Makes a ShoppingCartItem and adds it to the cart.
* WARNING: This method does not check if the product is in a purchase category.
*
* @param cartLocation The location to place this item; null will place at the end
* @param product The product entity relating to the product being added
* @param quantity The quantity to add
* @param additionalProductFeatureAndAppls Product feature/appls map
* @param attributes All unique attributes for this item (NOT features)
* @param prodCatalogId The catalog this item was added from
* @param dispatcher LocalDispatcher object for doing promotions, etc
* @param cart The parent shopping cart object this item will belong to
* @param doPromotions Indicates if we should run promotions
* @return a new ShoppingCartItem object
* @throws CartItemModifyException
*/
public static ShoppingCartItem makeItem(Integer cartLocation, GenericValue product, double selectedAmount, double quantity, Map additionalProductFeatureAndAppls, Map attributes, String prodCatalogId, LocalDispatcher dispatcher, ShoppingCart cart, boolean doPromotions) throws CartItemModifyException {
ShoppingCartItem newItem = new ShoppingCartItem(product, additionalProductFeatureAndAppls, attributes, prodCatalogId, cart.getLocale());
// check to see if product is virtual
if ("Y".equals(product.getString("isVirtual"))) {
String excMsg = "Tried to add the Virtual Product " + product.getString("productName") +
" (productId: " + product.getString("productId") + ") to the cart, not adding.";
Debug.logWarning(excMsg, module);
throw new CartItemModifyException(excMsg);
}
java.sql.Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
// check to see if introductionDate hasn't passed yet
if (product.get("introductionDate") != null && nowTimestamp.before(product.getTimestamp("introductionDate"))) {
String excMsg = "Tried to add the Product " + product.getString("productName") +
" (productId: " + product.getString("productId") + ") to the cart. This product has not yet been made available for sale, so not adding.";
Debug.logWarning(excMsg, module);
throw new CartItemModifyException(excMsg);
}
// check to see if salesDiscontinuationDate has passed
if (product.get("salesDiscontinuationDate") != null && nowTimestamp.after(product.getTimestamp("salesDiscontinuationDate"))) {
String excMsg = "Tried to add the Product " + product.getString("productName") +
" (productId: " + product.getString("productId") + ") to the cart. This product is no longer available for sale, so not adding.";
Debug.logWarning(excMsg, module);
throw new CartItemModifyException(excMsg);
}
// add to cart before setting quantity so that we can get order total, etc
if (cartLocation == null) {
cart.addItemToEnd(newItem);
} else {
cart.addItem(cartLocation.intValue(), newItem);
}
try {
newItem.setQuantity(quantity, dispatcher, cart, doPromotions);
} catch (CartItemModifyException e) {
cart.removeCartItem(cart.getItemIndex(newItem), dispatcher);
cart.removeEmptyCartItems();
throw e;
}
if (selectedAmount > 0) {
newItem.setSelectedAmount(selectedAmount);
}
return newItem;
}
public static ShoppingCartItem makeItem(Integer cartLocation, GenericValue product, double quantity, Map additionalProductFeatureAndAppls, Map attributes, String prodCatalogId, LocalDispatcher dispatcher, ShoppingCart cart, boolean doPromotions) throws CartItemModifyException {
return makeItem(cartLocation, product, 0.00, quantity, additionalProductFeatureAndAppls, attributes, prodCatalogId, dispatcher, cart, doPromotions);
}
/**
* Makes a non-product ShoppingCartItem and adds it to the cart.
* NOTE: This is only for non-product items; items without a product entity (work items, bulk items, etc)
*
* @param cartLocation The location to place this item; null will place at the end
* @param itemType The OrderItemTypeId for the item being added
* @param itemDescription The optional description of the item
* @param productCategoryId The optional category the product *will* go in
* @param basePrice The price for this item
* @param quantity The quantity to add
* @param attributes All unique attributes for this item (NOT features)
* @param prodCatalogId The catalog this item was added from
* @param dispatcher LocalDispatcher object for doing promotions, etc
* @param cart The parent shopping cart object this item will belong to
* @param doPromotions Indicates if we should run promotions
* @return a new ShoppingCartItem object
* @throws CartItemModifyException
*/
public static ShoppingCartItem makeItem(Integer cartLocation, String itemType, String itemDescription, String productCategoryId, double basePrice, double selectedAmount, double quantity, Map attributes, String prodCatalogId, LocalDispatcher dispatcher, ShoppingCart cart, boolean doPromotions) throws CartItemModifyException {
GenericDelegator delegator = cart.getDelegator();
ShoppingCartItem newItem = new ShoppingCartItem(delegator, itemType, itemDescription, productCategoryId, basePrice, attributes, prodCatalogId, cart.getLocale());
// add to cart before setting quantity so that we can get order total, etc
if (cartLocation == null) {
cart.addItemToEnd(newItem);
} else {
cart.addItem(cartLocation.intValue(), newItem);
}
try {
newItem.setQuantity(quantity, dispatcher, cart, doPromotions);
} catch (CartItemModifyException e) {
cart.removeEmptyCartItems();
throw e;
}
if (selectedAmount > 0) {
newItem.setSelectedAmount(selectedAmount);
}
return newItem;
}
/** Clone an item. */
public ShoppingCartItem(ShoppingCartItem item) {
try {
this._product = item.getProduct();
} catch (IllegalStateException e) {
this._product = null;
}
this.delegator = item.getDelegator();
this.delegatorName = item.delegatorName;
this.prodCatalogId = item.getProdCatalogId();
this.productId = item.getProductId();
this.itemType = item.getItemType();
this.itemComment = item.getItemComment();
this.productCategoryId = item.getProductCategoryId();
this.quantity = item.getQuantity();
this.selectedAmount = item.getSelectedAmount();
this.basePrice = item.getBasePrice();
this.listPrice = item.getListPrice();
this.isPromo = item.getIsPromo();
this.promoQuantityUsed = item.promoQuantityUsed;
this.locale = item.locale;
this.quantityUsedPerPromoCandidate = new HashMap(item.quantityUsedPerPromoCandidate);
this.quantityUsedPerPromoFailed = new HashMap(item.quantityUsedPerPromoFailed);
this.quantityUsedPerPromoActual = new HashMap(item.quantityUsedPerPromoActual);
this.orderItemSeqId = item.getOrderItemSeqId();
this.orderShipmentPreference = new GenericValue(item.getOrderShipmentPreference());
this.additionalProductFeatureAndAppls = item.getAdditionalProductFeatureAndAppls() == null ?
null : new HashMap(item.getAdditionalProductFeatureAndAppls());
this.attributes = item.getAttributes() == null ? null : new HashMap(item.getAttributes());
this.contactMechIdsMap = item.getOrderItemContactMechIds() == null ? null : new HashMap(item.getOrderItemContactMechIds());
this.orderItemPriceInfos = item.getOrderItemPriceInfos() == null ? null : new LinkedList(item.getOrderItemPriceInfos());
this.itemAdjustments = item.getAdjustments() == null ? null : new LinkedList(item.getAdjustments());
if (this._product == null) {
this.itemDescription = item.getName();
}
}
/** Cannot create shopping cart item with no parameters */
protected ShoppingCartItem() {}
/** Creates new ShoppingCartItem object. */
protected ShoppingCartItem(GenericValue product, Map additionalProductFeatureAndAppls, Map attributes, String prodCatalogId, Locale locale) {
this._product = product;
this.productId = _product.getString("productId");
this.itemType = "PRODUCT_ORDER_ITEM";
this.prodCatalogId = prodCatalogId;
this.itemComment = null;
this.attributes = attributes;
this.delegator = _product.getDelegator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -