📄 productfeatureservices.java
字号:
return results; } /* * Parameter: productId (of the parent product which has SELECTABLE features) * Result: featureCombinations, a List of Maps containing, for each possible variant of the productid: * {defaultVariantProductId: id of this variant; curProductFeatureAndAppls: features applied to this variant; existingVariantProductIds: List of productIds which are already variants with these features } */ public static Map getVariantCombinations(DispatchContext dctx, Map context) { Map results = new HashMap(); Map featuresByType = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String productId = (String) context.get("productId"); try { Map featuresResults = dispatcher.runSync("getProductFeaturesByType", UtilMisc.toMap("productId", productId)); Map features = new HashMap(); if (featuresResults.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) features = (Map) featuresResults.get("productFeaturesByType"); else return ServiceUtil.returnError((String) featuresResults.get(ModelService.ERROR_MESSAGE_LIST)); // need to keep 2 lists, oldCombinations and newCombinations, and keep swapping them after each looping. Otherwise, you'll get a // concurrent modification exception List oldCombinations = new LinkedList(); // loop through each feature type for (Iterator fi = features.keySet().iterator(); fi.hasNext(); ) { String currentFeatureType = (String) fi.next(); List currentFeatures = (List) features.get(currentFeatureType); List newCombinations = new LinkedList(); List combinations; // start with either existing combinations or from scratch if (oldCombinations.size() > 0) combinations = oldCombinations; else combinations = new LinkedList(); // in both cases, use each feature of current feature type's idCode and // product feature and add it to the id code and product feature applications // of the next variant. just a matter of whether we're starting with an // existing list of features and id code or from scratch. if (combinations.size()==0) { for (Iterator cFi = currentFeatures.iterator(); cFi.hasNext(); ) { GenericEntity currentFeature = (GenericEntity) cFi.next(); if (currentFeature.getString("productFeatureApplTypeId").equals("SELECTABLE_FEATURE")) { Map newCombination = new HashMap(); List newFeatures = new LinkedList(); List newFeatureIds = new LinkedList(); if (currentFeature.getString("idCode") != null) newCombination.put("defaultVariantProductId", productId + currentFeature.getString("idCode")); else newCombination.put("defaultVariantProductId", productId); newFeatures.add(currentFeature); newFeatureIds.add(currentFeature.getString("productFeatureId")); newCombination.put("curProductFeatureAndAppls", newFeatures); newCombination.put("curProductFeatureIds", newFeatureIds); newCombinations.add(newCombination); } } } else { for (Iterator comboIt = combinations.iterator(); comboIt.hasNext(); ) { Map combination = (Map) comboIt.next(); for (Iterator cFi = currentFeatures.iterator(); cFi.hasNext(); ) { GenericEntity currentFeature = (GenericEntity) cFi.next(); String defaultVariantProductId = null; if (currentFeature.getString("productFeatureApplTypeId").equals("SELECTABLE_FEATURE")) { Map newCombination = new HashMap(); // .clone() is important, or you'll keep adding to the same List for all the variants // have to cast twice: once from get() and once from clone() List newFeatures = ((List) ((LinkedList) combination.get("curProductFeatureAndAppls")).clone()); List newFeatureIds = ((List) ((LinkedList) combination.get("curProductFeatureIds")).clone()); if (currentFeature.getString("idCode") != null) newCombination.put("defaultVariantProductId", combination.get("defaultVariantProductId") + currentFeature.getString("idCode")); else newCombination.put("defaultVariantProductId", combination.get("defaultVariantProductId")); newFeatures.add(currentFeature); newFeatureIds.add(currentFeature.getString("productFeatureId")); newCombination.put("curProductFeatureAndAppls", newFeatures); newCombination.put("curProductFeatureIds", newFeatureIds); newCombinations.add(newCombination); } } } } if (newCombinations.size() >= oldCombinations.size()) oldCombinations = newCombinations; // save the newly expanded list as oldCombinations } int defaultCodeCounter = 1; HashMap defaultVariantProductIds = new HashMap(); // this map will contain the codes already used (as keys) defaultVariantProductIds.put(productId, null); // now figure out which of these combinations already have productIds associated with them for (Iterator fCi = oldCombinations.iterator(); fCi.hasNext(); ) { Map combination = (Map) fCi.next(); // Verify if the default code is already used, if so add a numeric suffix if (defaultVariantProductIds.containsKey(combination.get("defaultVariantProductId"))) { combination.put("defaultVariantProductId", combination.get("defaultVariantProductId") + (defaultCodeCounter < 10? "0" + defaultCodeCounter: "" + defaultCodeCounter)); defaultCodeCounter++; } defaultVariantProductIds.put(combination.get("defaultVariantProductId"), null); results = dispatcher.runSync("getAllExistingVariants", UtilMisc.toMap("productId", productId, "productFeatureAppls", combination.get("curProductFeatureIds"))); combination.put("existingVariantProductIds", results.get("variantProductIds")); } results = ServiceUtil.returnSuccess(); results.put("featureCombinations", oldCombinations); } catch (GenericServiceException ex) { Debug.logError(ex, ex.getMessage(), module); return ServiceUtil.returnError(ex.getMessage()); } return results; } /* * Parameters: productCategoryId (String) and productFeatures (a List of ProductFeature GenericValues) * Result: products (a List of Product GenericValues) */ public static Map getCategoryVariantProducts(DispatchContext dctx, Map context) { Map results = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); List productFeatures = (List) context.get("productFeatures"); String productCategoryId = (String) context.get("productCategoryId"); // get all the product members of the product category Map result = new HashMap(); try { result = dispatcher.runSync("getProductCategoryMembers", UtilMisc.toMap("categoryId", productCategoryId)); } catch (GenericServiceException ex) { Debug.logError("Cannot get category memebers for " + productCategoryId + " due to error: " + ex.getMessage(), module); return ServiceUtil.returnError(ex.getMessage()); } List memberProducts = (List) result.get("categoryMembers"); if ((memberProducts != null) && (memberProducts.size() > 0)) { // construct a Map of productFeatureTypeId -> productFeatureId from the productFeatures List Map featuresByType = new HashMap(); for (Iterator pFi = productFeatures.iterator(); pFi.hasNext(); ) { GenericValue nextFeature = (GenericValue) pFi.next(); featuresByType.put(nextFeature.getString("productFeatureTypeId"), nextFeature.getString("productFeatureId")); } List products = new ArrayList(); // final list of variant products for (Iterator mPi = memberProducts.iterator(); mPi.hasNext(); ) { // find variants for each member product of the category GenericValue memberProduct = (GenericValue) mPi.next(); try { result = dispatcher.runSync("getProductVariant", UtilMisc.toMap("productId", memberProduct.getString("productId"), "selectedFeatures", featuresByType)); } catch (GenericServiceException ex) { Debug.logError("Cannot get product variants for " + memberProduct.getString("productId") + " due to error: " + ex.getMessage(), module); return ServiceUtil.returnError(ex.getMessage()); } List variantProducts = (List) result.get("products"); if ((variantProducts != null) && (variantProducts.size() > 0)) { products.addAll(variantProducts); } else { Debug.logWarning("Product " + memberProduct.getString("productId") + " did not have any variants for the given features", module); } } if (products.size() == 0) { return ServiceUtil.returnError("No products which fit your requirements were found."); } else { results = ServiceUtil.returnSuccess(); results.put("products", products); } } else { Debug.logWarning("No products found in " + productCategoryId, module); } return results; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -