📄 shoppingcarthelper.java
字号:
/*
* $Id: ShoppingCartHelper.java,v 1.11 2004/03/02 20:02:17 ajzeneski 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.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilFormatOut;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.security.Security;
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 $Revision: 1.11 $
* @since 2.0
*/
public class ShoppingCartHelper {
public static final String resource = "OrderUiLabels";
public static String module = ShoppingCartHelper.class.getName();
// 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, Map context) {
Map result;
Map attributes = null;
String errMsg = null;
// price sanity check
if (productId == null && price < 0) {
errMsg = UtilProperties.getMessage(resource,"cart.price_not_positive_number", this.cart.getLocale());
result = ServiceUtil.returnError(errMsg);
return result;
}
// quantity sanity check
if (quantity < 1) {
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;
}
// 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;
}
}
// Retrieve the catalog ID
try {
int itemId = -1;
if (productId != null) {
itemId = cart.addOrIncreaseItem(productId, amount, quantity, null, attributes, catalogId, 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 cartException) {
result = ServiceUtil.returnError(cartException.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;
}
if (itemIter != null && itemIter.hasNext()) {
while (itemIter.hasNext()) {
GenericValue orderItem = (GenericValue) itemIter.next();
// never read: int itemId = -1;
if (orderItem.get("productId") != null && orderItem.get("quantity") != null) {
double amount = 0.00;
if (orderItem.get("selectedAmount") != null) {
amount = orderItem.getDouble("selectedAmount").doubleValue();
}
try {
this.cart.addOrIncreaseItem(orderItem.getString("productId"),
amount, orderItem.getDouble("quantity").doubleValue(), null, null, catalogId, dispatcher);
noItems = false;
} catch (CartItemModifyException e) {
errorMsgs.add(e.getMessage());
}
}
}
if (errorMsgs.size() > 0) {
result = ServiceUtil.returnError(errorMsgs);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result; // don't return error because this is a non-critical error and should go back to the same page
}
} else {
noItems = true;
}
} else {
noItems = true;
if (itemIds != null) {
for (int i = 0; i < itemIds.length; i++) {
String orderItemSeqId = itemIds[i];
GenericValue orderItem = null;
try {
orderItem = delegator.findByPrimaryKey("OrderItem", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId));
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
errorMsgs.add("Order line \"" + orderItemSeqId + "\" not found, so not added.");
continue;
}
if (orderItem != null) {
if (orderItem.get("productId") != null && orderItem.get("quantity") != null) {
double amount = 0.00;
if (orderItem.get("selectedAmount") != null) {
amount = orderItem.getDouble("selectedAmount").doubleValue();
}
try {
this.cart.addOrIncreaseItem(orderItem.getString("productId"), amount,
orderItem.getDouble("quantity").doubleValue(), null, null, catalogId, dispatcher);
noItems = false;
} catch (CartItemModifyException e) {
errorMsgs.add(e.getMessage());
}
}
}
}
if (errorMsgs.size() > 0) {
result = ServiceUtil.returnError(errorMsgs);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result; // don't return error because this is a non-critical error and should go back to the same page
}
} // else no items
}
if (noItems) {
result = ServiceUtil.returnSuccess();
result.put("_ERROR_MESSAGE_", "No items found to add.");
return result; // don't return error because this is a non-critical error and should go back to the same page
}
result = ServiceUtil.returnSuccess();
return result;
}
/**
* Adds all products in a category according to quantity request parameter
* for each; if no parameter for a certain product in the category, or if
* quantity is 0, do not add
*/
public Map addToCartBulk(String catalogId, String categoryId, Map context) {
Map result = null;
String errMsg = null;
if (categoryId == null || categoryId.length() <= 0) {
errMsg = UtilProperties.getMessage(resource,"cart.category_not_specified_to_add_from", this.cart.getLocale());
result = ServiceUtil.returnError(errMsg);
return result;
}
Collection prodCatMemberCol = null;
try {
prodCatMemberCol = delegator.findByAndCache("ProductCategoryMember", UtilMisc.toMap("productCategoryId", categoryId));
} catch (GenericEntityException e) {
Map messageMap = UtilMisc.toMap("categoryId", categoryId);
messageMap.put("message", e.getMessage());
Debug.logWarning(e.getMessage(), module);
errMsg = UtilProperties.getMessage(resource,"cart.could_not_get_products_in_category_cart", messageMap, this.cart.getLocale());
result = ServiceUtil.returnError(errMsg);
return result;
}
if (prodCatMemberCol == null) {
Map messageMap = UtilMisc.toMap("categoryId", categoryId);
errMsg = UtilProperties.getMessage(resource,"cart.could_not_get_products_in_category", messageMap, this.cart.getLocale());
result = ServiceUtil.returnError(errMsg);
return result;
}
// never read: String errMsg = "";
Iterator pcmIter = prodCatMemberCol.iterator();
while (pcmIter.hasNext()) {
GenericValue productCategoryMember = (GenericValue) pcmIter.next();
String quantStr = (String) context.get("quantity_" + productCategoryMember.getString("productId"));
if (quantStr != null && quantStr.length() > 0) {
double quantity = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -