📄 productworker.java
字号:
/* * $Id: ProductWorker.java 6313 2005-12-13 18:48:25Z jaz $ * * 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.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Set;import javax.servlet.ServletRequest;import javax.servlet.jsp.PageContext;import org.apache.commons.collections.map.LinkedMap;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilMisc;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.product.config.ProductConfigWrapper;import org.ofbiz.product.config.ProductConfigWrapper.ConfigOption;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 $Rev: 6313 $ * @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) || (ProductWorker.isDigital(product) && !ProductWorker.isPhysical(product))) { // 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); if (productAssocs == null) { return null; } 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 * this is only used in the related method that uses a ProductConfigWrapper, until that is refactored into a service as well... */ private 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("availableToPromiseTotal"); 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 getInventoryAvailableByFacility service, returns true if specified quantity is available for all the selected parts, else false. * Also, set the available flag for all the product configuration's options. **/ public static boolean isProductInventoryAvailableByFacility(ProductConfigWrapper productConfig, String inventoryFacilityId, double quantity, LocalDispatcher dispatcher) throws GenericServiceException { boolean available = true; List options = productConfig.getSelectedOptions(); Iterator optionsIt = options.iterator(); while (optionsIt.hasNext()) { ConfigOption ci = (ConfigOption)optionsIt.next(); List products = ci.getComponents(); Iterator productsIt = products.iterator(); while (productsIt.hasNext()) { GenericValue product = (GenericValue)productsIt.next(); String productId = product.getString("productId"); Double cmpQuantity = product.getDouble("quantity"); double neededQty = 1.0; if (cmpQuantity != null) { neededQty = quantity * cmpQuantity.doubleValue(); } if (!isProductInventoryAvailableByFacility(productId, inventoryFacilityId, neededQty, dispatcher)) { ci.setAvailable(false); } } if (!ci.isAvailable()) { available = false; } } return available; } public static void getAssociatedProducts(PageContext pageContext, String productAttributeName, String assocPrefix) { GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator"); GenericValue product = (GenericValue) pageContext.getAttribute(productAttributeName); if (product == null) return; try { List upgradeProducts = product.getRelatedByAndCache("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", "PRODUCT_UPGRADE")); List complementProducts = product.getRelatedByAndCache("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", "PRODUCT_COMPLEMENT")); List obsolescenceProducts = product.getRelatedByAndCache("AssocProductAssoc", UtilMisc.toMap("productAssocTypeId", "PRODUCT_OBSOLESCENCE")); List obsoleteByProducts = product.getRelatedByAndCache("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", "PRODUCT_OBSOLESCENCE")); // since ProductAssoc records have a fromDate and thruDate, we can filter by now so that only assocs in the date range are included upgradeProducts = EntityUtil.filterByDate(upgradeProducts, true); complementProducts = EntityUtil.filterByDate(complementProducts, true); obsolescenceProducts = EntityUtil.filterByDate(obsolescenceProducts, true); obsoleteByProducts = EntityUtil.filterByDate(obsoleteByProducts, true); if (upgradeProducts != null && upgradeProducts.size() > 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -