📄 productservices.java
字号:
/* * $Id: ProductServices.java 6249 2005-12-06 00:28:19Z jaz $ * * Copyright (c) 2002-2004 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.sql.Timestamp;import java.util.*;import org.apache.commons.collections.map.LinkedMap;import org.apache.commons.collections.set.ListOrderedSet;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;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.product.catalog.CatalogWorker;import org.ofbiz.product.category.CategoryWorker;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * Product Services * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version $Rev: 6249 $ * @since 2.0 */public class ProductServices { public static final String module = ProductServices.class.getName(); public static final String resource = "ProductUiLabels"; /** * Creates a Collection of product entities which are variant products from the specified product ID. */ public static Map prodFindAllVariants(DispatchContext dctx, Map context) { // * String productId -- Parent (virtual) product ID context.put("type", "PRODUCT_VARIANT"); return prodFindAssociatedByType(dctx, context); } /** * Finds a specific product or products which contain the selected features. */ public static Map prodFindSelectedVariant(DispatchContext dctx, Map context) { // * String productId -- Parent (virtual) product ID // * Map selectedFeatures -- Selected features GenericDelegator delegator = dctx.getDelegator(); Locale locale = (Locale) context.get("locale"); String productId = (String) context.get("productId"); Map selectedFeatures = (Map) context.get("selectedFeatures"); ArrayList products = new ArrayList(); // All the variants for this products are retrieved Map resVariants = prodFindAllVariants(dctx, context); List variants = (List)resVariants.get("assocProducts"); GenericValue oneVariant = null; Iterator variantsIt = variants.iterator(); while (variantsIt.hasNext()) { // For every variant, all the standard features are retrieved oneVariant = (GenericValue)variantsIt.next(); Map feaContext = new HashMap(); feaContext.put("productId", oneVariant.get("productIdTo")); feaContext.put("type", "STANDARD_FEATURE"); Map resFeatures = prodGetFeatures(dctx, feaContext); List features = (List)resFeatures.get("productFeatures"); Iterator featuresIt = features.iterator(); GenericValue oneFeature = null; boolean variantFound = true; // The variant is discarded if at least one of its standard features // has the same type of one of the selected features but a different feature id. // Example: // Input: (COLOR, Black), (SIZE, Small) // Variant1: (COLOR, Black), (SIZE, Large) --> nok // Variant2: (COLOR, Black), (SIZE, Small) --> ok // Variant3: (COLOR, Black), (SIZE, Small), (IMAGE, SkyLine) --> ok // Variant4: (COLOR, Black), (IMAGE, SkyLine) --> ok while (featuresIt.hasNext()) { oneFeature = (GenericValue)featuresIt.next(); if (selectedFeatures.containsKey(oneFeature.getString("productFeatureTypeId"))) { if (!selectedFeatures.containsValue(oneFeature.getString("productFeatureId"))) { variantFound = false; break; } } } if (variantFound) { try { products.add(delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", oneVariant.getString("productIdTo")))); } catch (GenericEntityException e) { Map messageMap = UtilMisc.toMap("errProductFeatures", e.toString()); String errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_features_errors", messageMap, locale); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } } } Map result = ServiceUtil.returnSuccess(); result.put("products", products); return result; } /** * Finds product variants based on a product ID and a distinct feature. */ public static Map prodFindDistinctVariants(DispatchContext dctx, Map context) { // * String productId -- Parent (virtual) product ID // * String feature -- Distinct feature name GenericDelegator delegator = dctx.getDelegator(); String productId = (String) context.get("productId"); String feature = (String) context.get("feature"); return ServiceUtil.returnError("This service has not yet been implemented."); } /** * Finds a Set of feature types in sequence. */ public static Map prodFindFeatureTypes(DispatchContext dctx, Map context) { // * String productId -- Product ID to look up feature types GenericDelegator delegator = dctx.getDelegator(); String productId = (String) context.get("productId"); Locale locale = (Locale) context.get("locale"); String errMsg=null; Set featureSet = new ListOrderedSet(); try { Map fields = UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "SELECTABLE_FEATURE"); List order = UtilMisc.toList("sequenceNum", "productFeatureTypeId"); List features = delegator.findByAndCache("ProductFeatureAndAppl", fields, order); Iterator i = features.iterator(); while (i.hasNext()) { featureSet.add(((GenericValue) i.next()).getString("productFeatureTypeId")); } //if (Debug.infoOn()) Debug.logInfo("" + featureSet, module); } catch (GenericEntityException e) { Map messageMap = UtilMisc.toMap("errProductFeatures", e.toString()); errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_features_errors", messageMap, locale); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } if (featureSet.size() == 0) { errMsg = UtilProperties.getMessage(resource,"productservices.problem_reading_product_features", locale); // ToDo DO 2004-02-23 Where should the errMsg go? Debug.logWarning(errMsg + " for product " + productId, module); //return ServiceUtil.returnError(errMsg); } Map result = ServiceUtil.returnSuccess(); result.put("featureSet", featureSet); return result; } /** * Builds a variant feature tree. */ public static Map prodMakeFeatureTree(DispatchContext dctx, Map context) { // * String productId -- Parent (virtual) product ID // * List featureOrder -- Order of features // * String productStoreId -- Product Store ID for Inventory String productStoreId = (String) context.get("productStoreId"); Locale locale = (Locale) context.get("locale"); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Map result = new HashMap(); List featureOrder = new LinkedList((Collection) context.get("featureOrder")); if (featureOrder == null || featureOrder.size() == 0) { return ServiceUtil.returnError("Empty list of features passed"); } Collection variants = (Collection) prodFindAllVariants(dctx, context).get("assocProducts"); List virtualVariant = new ArrayList(); if (variants == null || variants.size() == 0) { return ServiceUtil.returnSuccess(); } List items = new ArrayList(); Iterator i = variants.iterator(); while (i.hasNext()) { String productIdTo = (String) ((GenericValue) i.next()).get("productIdTo"); // first check to see if intro and discontinue dates are within range GenericValue productTo = null; try { productTo = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productIdTo)); } catch (GenericEntityException e) { Debug.logError(e, module); Map messageMap = UtilMisc.toMap("productIdTo", productIdTo, "errMessage", e.toString()); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "productservices.error_finding_associated_variant_with_ID_error", messageMap, locale)); } if (productTo == null) { Debug.logWarning("Could not find associated variant with ID " + productIdTo + ", not showing in list", module); continue; } java.sql.Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); // check to see if introductionDate hasn't passed yet if (productTo.get("introductionDate") != null && nowTimestamp.before(productTo.getTimestamp("introductionDate"))) { if (Debug.verboseOn()) { String excMsg = "Tried to view the Product " + productTo.getString("productName") + " (productId: " + productTo.getString("productId") + ") as a variant. This product has not yet been made available for sale, so not adding for view."; Debug.logVerbose(excMsg, module); } continue; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -