📄 productworker.java
字号:
/*
* $Id: ProductWorker.java,v 1.13 2004/02/26 09:10:50 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.product.product;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.PageContext;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.OrderedMap;
import org.ofbiz.base.util.UtilFormatOut;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
/**
* Product Worker class to reduce code in JSPs.
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.13 $
* @since 2.0
*/
public class ProductWorker {
public static final String module = ProductWorker.class.getName();
public static final String resource = "ProductUiLabels";
public static void getProduct(PageContext pageContext, String attributeName) {
getProduct(pageContext, attributeName, null);
}
public static boolean shippingApplies(GenericValue product) {
String errMsg = null;
if (product != null) {
String productTypeId = product.getString("productTypeId");
if ("SERVICE".equals(productTypeId) || "DIGITAL_GOOD".equals(productTypeId)) {
// don't charge shipping on services or digital goods
return false;
}
Boolean chargeShipping = product.getBoolean("chargeShipping");
if (chargeShipping == null) {
return true;
} else {
return chargeShipping.booleanValue();
}
} else {
// todo: Hier noch Uebersetzungen einfuegen?
//errMsg = UtilProperties.getMessage(resource,"productworker.null_product_entity_not_valid", UtilHttp.getLocale(request));
throw new IllegalArgumentException(errMsg);
}
}
public static boolean taxApplies(GenericValue product) {
String errMsg = null;
if (product != null) {
Boolean taxable = product.getBoolean("taxable");
if (taxable == null) {
return true;
} else {
return taxable.booleanValue();
}
} else {
// todo: Hier noch Uebersetzungen einfuegen?
//errMsg = UtilProperties.getMessage(resource,"productworker.null_product_entity_not_valid", UtilHttp.getLocale(request));
throw new IllegalArgumentException(errMsg);
}
}
public static void getProduct(PageContext pageContext, String attributeName, String productId) {
GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
ServletRequest request = pageContext.getRequest();
if (productId == null)
productId = UtilFormatOut.checkNull(request.getParameter("product_id"), request.getParameter("PRODUCT_ID"));
if (productId.equals(""))
return;
GenericValue product = null;
try {
product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId));
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
product = null;
}
if (product != null)
pageContext.setAttribute(attributeName, product);
}
public static String getVariantVirtualId(GenericValue variantProduct) throws GenericEntityException {
List productAssocs = getVariantVirtualAssocs(variantProduct);
GenericValue productAssoc = EntityUtil.getFirst(productAssocs);
if (productAssoc != null) {
return productAssoc.getString("productId");
} else {
return null;
}
}
public static List getVariantVirtualAssocs(GenericValue variantProduct) throws GenericEntityException {
if (variantProduct != null && "Y".equals(variantProduct.getString("isVariant"))) {
List productAssocs = EntityUtil.filterByDate(variantProduct.getRelatedByAndCache("AssocProductAssoc",
UtilMisc.toMap("productAssocTypeId", "PRODUCT_VARIANT")), true);
return productAssocs;
}
return null;
}
/** invokes the getInventoryAvailableByFacility service, returns true if specified quantity is available, else false **/
public static boolean isProductInventoryAvailableByFacility(String productId, String inventoryFacilityId, double quantity, LocalDispatcher dispatcher) throws GenericServiceException {
Double availableToPromise = null;
try {
Map result = dispatcher.runSync("getInventoryAvailableByFacility",
UtilMisc.toMap("productId", productId, "facilityId", inventoryFacilityId));
availableToPromise = (Double) result.get("availableToPromise");
if (availableToPromise == null) {
Debug.logWarning("The getInventoryAvailableByFacility service returned a null availableToPromise, the error message was:\n" + result.get(ModelService.ERROR_MESSAGE), module);
return false;
}
} catch (GenericServiceException e) {
Debug.logWarning(e, "Error invoking getInventoryAvailableByFacility service in isCatalogInventoryAvailable", module);
return false;
}
// check to see if we got enough back...
if (availableToPromise.doubleValue() >= quantity) {
if (Debug.infoOn()) Debug.logInfo("Inventory IS available in facility with id " + inventoryFacilityId + " for product id " + productId + "; desired quantity is " + quantity + ", available quantity is " + availableToPromise, module);
return true;
} else {
if (Debug.infoOn()) Debug.logInfo("Returning false because there is insufficient inventory available in facility with id " + inventoryFacilityId + " for product id " + productId + "; desired quantity is " + quantity + ", available quantity is " + availableToPromise, module);
return false;
}
}
/** invokes the reserveProductInventoryByFacility service, returns quantity not reserved, 0 on error, null on success **/
public static Double reserveProductInventoryByFacility(String productId, Double quantity, String inventoryFacilityId, String orderId, String reserveOrderEnumId, String orderItemSeqId, boolean requireInventory, GenericValue userLogin, LocalDispatcher dispatcher) throws GenericServiceException {
Double quantityNotReserved = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -