📄 productfeatureservices.java
字号:
/* * $Id: ProductFeatureServices.java 5867 2005-09-30 16:34:06Z sichen $ * * Copyright (c) 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.feature;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntity;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;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;/** * Services for product features * * @author <a href="mailto:schen@graciousstyle.com">Si Chen</a> * @version $Revision: 5867 $ * @since 3.0 */public class ProductFeatureServices { public static final String module = ProductFeatureServices.class.getName(); public static final String resource = "ProductUiLabels"; /* * Parameters: productFeatureCategoryId, productFeatureGroupId, productId, productFeatureApplTypeId * Result: productFeaturesByType, a Map of all product features from productFeatureCategoryId, group by productFeatureType -> List of productFeatures * If the parameter were productFeatureCategoryId, the results are from ProductFeatures. If productFeatureCategoryId were null and there were a productFeatureGroupId, * the results are from ProductFeatureGroupAndAppl. Otherwise, if there is a productId, the results are from ProductFeatureAndAppl. * The optional productFeatureApplTypeId causes results to be filtered by this parameter--only used in conjunction with productId. */ public static Map getProductFeaturesByType(DispatchContext dctx, Map context) { Map results = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); /* because we might need to search either for product features or for product features of a product, the search code has to be generic. * we will determine which entity and field to search on based on what the user has supplied us with. */ String valueToSearch = (String) context.get("productFeatureCategoryId"); String productFeatureApplTypeId = (String) context.get("productFeatureApplTypeId"); String entityToSearch = "ProductFeature"; String fieldToSearch = "productFeatureCategoryId"; List orderBy = UtilMisc.toList("productFeatureTypeId", "description"); if (valueToSearch == null && context.get("productFeatureGroupId") != null) { entityToSearch = "ProductFeatureGroupAndAppl"; fieldToSearch = "productFeatureGroupId"; valueToSearch = (String) context.get("productFeatureGroupId"); // use same orderBy as with a productFeatureCategoryId search } else if (valueToSearch == null && context.get("productId") != null){ entityToSearch = "ProductFeatureAndAppl"; fieldToSearch = "productId"; valueToSearch = (String) context.get("productId"); orderBy = UtilMisc.toList("sequenceNum", "productFeatureApplTypeId", "productFeatureTypeId", "description"); } if (valueToSearch == null) { return ServiceUtil.returnError("This service requires a productId, a productFeatureGroupId, or a productFeatureCategoryId to run."); } try { // get all product features in this feature category List allFeatures = delegator.findByAnd(entityToSearch, UtilMisc.toMap(fieldToSearch, valueToSearch), orderBy); if (entityToSearch.equals("ProductFeatureAndAppl") && productFeatureApplTypeId != null) allFeatures = EntityUtil.filterByAnd(allFeatures, UtilMisc.toMap("productFeatureApplTypeId", productFeatureApplTypeId)); List featureTypes = new ArrayList(); // or LinkedList? Map featuresByType = new LinkedHashMap(); GenericValue feature = null; for (Iterator featuresIter = allFeatures.iterator(); featuresIter.hasNext(); ) { feature = (GenericValue) featuresIter.next(); String featureType = feature.getString("productFeatureTypeId"); if (!featureTypes.contains(featureType)) { featureTypes.add(featureType); } if (!featuresByType.containsKey(featureType)) { featuresByType.put(featureType, new ArrayList()); } List features = (List)featuresByType.get(featureType); features.add(feature); } results = ServiceUtil.returnSuccess(); results.put("productFeatureTypes", featureTypes); results.put("productFeaturesByType", featuresByType); } catch (GenericEntityException ex) { Debug.logError(ex, ex.getMessage(), module); return ServiceUtil.returnError(ex.getMessage()); } return results; } /* * Parameter: productId, productFeatureAppls (a List of ProductFeatureAndAppl entities of features applied to productId) * Result: variantProductIds: a List of productIds of variants with those features */ public static Map getAllExistingVariants(DispatchContext dctx, Map context) { Map results = new HashMap(); Map featuresByType = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); String productId = (String) context.get("productId"); List curProductFeatureAndAppls = (List) context.get("productFeatureAppls"); List existingVariantProductIds = new ArrayList(); try { /* * get a list of all products which are associated with the current one as PRODUCT_VARIANT and for each one, * see if it has every single feature in the list of productFeatureAppls as a STANDARD_FEATURE. If so, then * it qualifies and add it to the list of existingVariantProductIds. */ List productAssocs = EntityUtil.filterByDate(delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT")), true); if (productAssocs != null && productAssocs.size() > 0) { Iterator productAssocIter = productAssocs.iterator(); while (productAssocIter.hasNext()) { GenericEntity productAssoc = (GenericEntity) productAssocIter.next(); //for each associated product, if it has all standard features, display it's productId boolean hasAllFeatures = true; Iterator curProductFeatureAndApplIter = curProductFeatureAndAppls.iterator(); while (curProductFeatureAndApplIter.hasNext()) { String productFeatureAndAppl = (String) curProductFeatureAndApplIter.next(); Map findByMap = UtilMisc.toMap("productId", productAssoc.getString("productIdTo"), "productFeatureId", productFeatureAndAppl, "productFeatureApplTypeId", "STANDARD_FEATURE"); //Debug.log("Using findByMap: " + findByMap); List standardProductFeatureAndAppls = EntityUtil.filterByDate(delegator.findByAnd("ProductFeatureAppl", findByMap), true); if (standardProductFeatureAndAppls == null || standardProductFeatureAndAppls.size() == 0) { // Debug.log("Does NOT have this standard feature"); hasAllFeatures = false; break; } else { // Debug.log("DOES have this standard feature"); } } if (hasAllFeatures) { // add to list of existing variants: productId=productAssoc.productIdTo existingVariantProductIds.add(productAssoc.get("productIdTo")); } } } results = ServiceUtil.returnSuccess(); results.put("variantProductIds", existingVariantProductIds); } catch (GenericEntityException ex) { Debug.logError(ex, ex.getMessage(), module); return ServiceUtil.returnError(ex.getMessage()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -