1 23 package org.ofbiz.product.feature; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedHashMap ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.UtilMisc; 35 import org.ofbiz.entity.GenericDelegator; 36 import org.ofbiz.entity.GenericEntity; 37 import org.ofbiz.entity.GenericEntityException; 38 import org.ofbiz.entity.GenericValue; 39 import org.ofbiz.entity.util.EntityUtil; 40 import org.ofbiz.service.DispatchContext; 41 import org.ofbiz.service.GenericServiceException; 42 import org.ofbiz.service.LocalDispatcher; 43 import org.ofbiz.service.ModelService; 44 import org.ofbiz.service.ServiceUtil; 45 46 47 54 55 public class ProductFeatureServices { 56 57 public static final String module = ProductFeatureServices.class.getName(); 58 public static final String resource = "ProductUiLabels"; 59 60 67 public static Map getProductFeaturesByType(DispatchContext dctx, Map context) { 68 Map results = new HashMap (); 69 GenericDelegator delegator = dctx.getDelegator(); 70 71 74 String valueToSearch = (String ) context.get("productFeatureCategoryId"); 75 String productFeatureApplTypeId = (String ) context.get("productFeatureApplTypeId"); 76 77 String entityToSearch = "ProductFeature"; 78 String fieldToSearch = "productFeatureCategoryId"; 79 List orderBy = UtilMisc.toList("productFeatureTypeId", "description"); 80 81 if (valueToSearch == null && context.get("productFeatureGroupId") != null) { 82 entityToSearch = "ProductFeatureGroupAndAppl"; 83 fieldToSearch = "productFeatureGroupId"; 84 valueToSearch = (String ) context.get("productFeatureGroupId"); 85 } else if (valueToSearch == null && context.get("productId") != null){ 87 entityToSearch = "ProductFeatureAndAppl"; 88 fieldToSearch = "productId"; 89 valueToSearch = (String ) context.get("productId"); 90 orderBy = UtilMisc.toList("sequenceNum", "productFeatureApplTypeId", "productFeatureTypeId", "description"); 91 } 92 93 if (valueToSearch == null) { 94 return ServiceUtil.returnError("This service requires a productId, a productFeatureGroupId, or a productFeatureCategoryId to run."); 95 } 96 97 try { 98 List allFeatures = delegator.findByAnd(entityToSearch, UtilMisc.toMap(fieldToSearch, valueToSearch), orderBy); 100 101 if (entityToSearch.equals("ProductFeatureAndAppl") && productFeatureApplTypeId != null) 102 allFeatures = EntityUtil.filterByAnd(allFeatures, UtilMisc.toMap("productFeatureApplTypeId", productFeatureApplTypeId)); 103 104 List featureTypes = new ArrayList (); Map featuresByType = new LinkedHashMap (); 106 GenericValue feature = null; 107 for (Iterator featuresIter = allFeatures.iterator(); featuresIter.hasNext(); ) { 108 feature = (GenericValue) featuresIter.next(); 109 String featureType = feature.getString("productFeatureTypeId"); 110 if (!featureTypes.contains(featureType)) { 111 featureTypes.add(featureType); 112 } 113 if (!featuresByType.containsKey(featureType)) { 114 featuresByType.put(featureType, new ArrayList ()); 115 } 116 List features = (List )featuresByType.get(featureType); 117 features.add(feature); 118 } 119 120 results = ServiceUtil.returnSuccess(); 121 results.put("productFeatureTypes", featureTypes); 122 results.put("productFeaturesByType", featuresByType); 123 } catch (GenericEntityException ex) { 124 Debug.logError(ex, ex.getMessage(), module); 125 return ServiceUtil.returnError(ex.getMessage()); 126 } 127 return results; 128 } 129 130 134 public static Map getAllExistingVariants(DispatchContext dctx, Map context) { 135 Map results = new HashMap (); 136 Map featuresByType = new HashMap (); 137 GenericDelegator delegator = dctx.getDelegator(); 138 139 String productId = (String ) context.get("productId"); 140 List curProductFeatureAndAppls = (List ) context.get("productFeatureAppls"); 141 List existingVariantProductIds = new ArrayList (); 142 143 try { 144 149 List productAssocs = EntityUtil.filterByDate(delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT")), true); 150 if (productAssocs != null && productAssocs.size() > 0) { 151 Iterator productAssocIter = productAssocs.iterator(); 152 while (productAssocIter.hasNext()) { 153 GenericEntity productAssoc = (GenericEntity) productAssocIter.next(); 154 155 boolean hasAllFeatures = true; 157 Iterator curProductFeatureAndApplIter = curProductFeatureAndAppls.iterator(); 158 while (curProductFeatureAndApplIter.hasNext()) { 159 String productFeatureAndAppl = (String ) curProductFeatureAndApplIter.next(); 160 Map findByMap = UtilMisc.toMap("productId", productAssoc.getString("productIdTo"), 161 "productFeatureId", productFeatureAndAppl, 162 "productFeatureApplTypeId", "STANDARD_FEATURE"); 163 164 166 List standardProductFeatureAndAppls = EntityUtil.filterByDate(delegator.findByAnd("ProductFeatureAppl", findByMap), true); 167 if (standardProductFeatureAndAppls == null || standardProductFeatureAndAppls.size() == 0) { 168 hasAllFeatures = false; 170 break; 171 } else { 172 } 174 } 175 176 if (hasAllFeatures) { 177 existingVariantProductIds.add(productAssoc.get("productIdTo")); 179 } 180 } 181 } 182 results = ServiceUtil.returnSuccess(); 183 results.put("variantProductIds", existingVariantProductIds); 184 } catch (GenericEntityException ex) { 185 Debug.logError(ex, ex.getMessage(), module); 186 return ServiceUtil.returnError(ex.getMessage()); 187 } 188 return results; 189 } 190 191 196 public static Map getVariantCombinations(DispatchContext dctx, Map context) { 197 Map results = new HashMap (); 198 Map featuresByType = new HashMap (); 199 GenericDelegator delegator = dctx.getDelegator(); 200 LocalDispatcher dispatcher = dctx.getDispatcher(); 201 202 String productId = (String ) context.get("productId"); 203 204 try { 205 Map featuresResults = dispatcher.runSync("getProductFeaturesByType", UtilMisc.toMap("productId", productId)); 206 Map features = new HashMap (); 207 208 if (featuresResults.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) 209 features = (Map ) featuresResults.get("productFeaturesByType"); 210 else 211 return ServiceUtil.returnError((String ) featuresResults.get(ModelService.ERROR_MESSAGE_LIST)); 212 213 List oldCombinations = new LinkedList (); 216 217 for (Iterator fi = features.keySet().iterator(); fi.hasNext(); ) { 219 String currentFeatureType = (String ) fi.next(); 220 List currentFeatures = (List ) features.get(currentFeatureType); 221 222 List newCombinations = new LinkedList (); 223 List combinations; 224 225 if (oldCombinations.size() > 0) 227 combinations = oldCombinations; 228 else 229 combinations = new LinkedList (); 230 231 if (combinations.size()==0) { 236 for (Iterator cFi = currentFeatures.iterator(); cFi.hasNext(); ) { 237 GenericEntity currentFeature = (GenericEntity) cFi.next(); 238 if (currentFeature.getString("productFeatureApplTypeId").equals("SELECTABLE_FEATURE")) { 239 Map newCombination = new HashMap (); 240 List newFeatures = new LinkedList (); 241 List newFeatureIds = new LinkedList (); 242 if (currentFeature.getString("idCode") != null) 243 newCombination.put("defaultVariantProductId", productId + currentFeature.getString("idCode")); 244 else 245 newCombination.put("defaultVariantProductId", productId); 246 newFeatures.add(currentFeature); 247 newFeatureIds.add(currentFeature.getString("productFeatureId")); 248 newCombination.put("curProductFeatureAndAppls", newFeatures); 249 newCombination.put("curProductFeatureIds", newFeatureIds); 250 newCombinations.add(newCombination); 251 } 252 } 253 } else { 254 for (Iterator comboIt = combinations.iterator(); comboIt.hasNext(); ) { 255 Map combination = (Map ) comboIt.next(); 256 for (Iterator cFi = currentFeatures.iterator(); cFi.hasNext(); ) { 257 GenericEntity currentFeature = (GenericEntity) cFi.next(); 258 String defaultVariantProductId = null; 259 if (currentFeature.getString("productFeatureApplTypeId").equals("SELECTABLE_FEATURE")) { 260 Map newCombination = new HashMap (); 261 List newFeatures = ((List ) ((LinkedList ) combination.get("curProductFeatureAndAppls")).clone()); 264 List newFeatureIds = ((List ) ((LinkedList ) combination.get("curProductFeatureIds")).clone()); 265 if (currentFeature.getString("idCode") != null) 266 newCombination.put("defaultVariantProductId", combination.get("defaultVariantProductId") + currentFeature.getString("idCode")); 267 else 268 newCombination.put("defaultVariantProductId", combination.get("defaultVariantProductId")); 269 newFeatures.add(currentFeature); 270 newFeatureIds.add(currentFeature.getString("productFeatureId")); 271 newCombination.put("curProductFeatureAndAppls", newFeatures); 272 newCombination.put("curProductFeatureIds", newFeatureIds); 273 newCombinations.add(newCombination); 274 } 275 } 276 } 277 } 278 if (newCombinations.size() >= oldCombinations.size()) 279 oldCombinations = newCombinations; } 281 282 int defaultCodeCounter = 1; 283 HashMap defaultVariantProductIds = new HashMap (); defaultVariantProductIds.put(productId, null); 285 286 for (Iterator fCi = oldCombinations.iterator(); fCi.hasNext(); ) { 288 Map combination = (Map ) fCi.next(); 289 if (defaultVariantProductIds.containsKey(combination.get("defaultVariantProductId"))) { 291 combination.put("defaultVariantProductId", combination.get("defaultVariantProductId") + (defaultCodeCounter < 10? "0" + defaultCodeCounter: "" + defaultCodeCounter)); 292 defaultCodeCounter++; 293 } 294 defaultVariantProductIds.put(combination.get("defaultVariantProductId"), null); 295 results = dispatcher.runSync("getAllExistingVariants", UtilMisc.toMap("productId", productId, 296 "productFeatureAppls", combination.get("curProductFeatureIds"))); 297 combination.put("existingVariantProductIds", results.get("variantProductIds")); 298 } 299 results = ServiceUtil.returnSuccess(); 300 results.put("featureCombinations", oldCombinations); 301 } catch (GenericServiceException ex) { 302 Debug.logError(ex, ex.getMessage(), module); 303 return ServiceUtil.returnError(ex.getMessage()); 304 } 305 306 return results; 307 } 308 309 313 public static Map getCategoryVariantProducts(DispatchContext dctx, Map context) { 314 Map results = new HashMap (); 315 GenericDelegator delegator = dctx.getDelegator(); 316 LocalDispatcher dispatcher = dctx.getDispatcher(); 317 318 List productFeatures = (List ) context.get("productFeatures"); 319 String productCategoryId = (String ) context.get("productCategoryId"); 320 321 Map result = new HashMap (); 323 try { 324 result = dispatcher.runSync("getProductCategoryMembers", UtilMisc.toMap("categoryId", productCategoryId)); 325 } catch (GenericServiceException ex) { 326 Debug.logError("Cannot get category memebers for " + productCategoryId + " due to error: " + ex.getMessage(), module); 327 return ServiceUtil.returnError(ex.getMessage()); 328 } 329 330 List memberProducts = (List ) result.get("categoryMembers"); 331 if ((memberProducts != null) && (memberProducts.size() > 0)) { 332 Map featuresByType = new HashMap (); 334 for (Iterator pFi = productFeatures.iterator(); pFi.hasNext(); ) { 335 GenericValue nextFeature = (GenericValue) pFi.next(); 336 featuresByType.put(nextFeature.getString("productFeatureTypeId"), nextFeature.getString("productFeatureId")); 337 } 338 339 List products = new ArrayList (); for (Iterator mPi = memberProducts.iterator(); mPi.hasNext(); ) { 341 GenericValue memberProduct = (GenericValue) mPi.next(); 343 344 try { 345 result = dispatcher.runSync("getProductVariant", UtilMisc.toMap("productId", memberProduct.getString("productId"), "selectedFeatures", featuresByType)); 346 } catch (GenericServiceException ex) { 347 Debug.logError("Cannot get product variants for " + memberProduct.getString("productId") + " due to error: " + ex.getMessage(), module); 348 return ServiceUtil.returnError(ex.getMessage()); 349 } 350 351 List variantProducts = (List ) result.get("products"); 352 if ((variantProducts != null) && (variantProducts.size() > 0)) { 353 products.addAll(variantProducts); 354 } else { 355 Debug.logWarning("Product " + memberProduct.getString("productId") + " did not have any variants for the given features", module); 356 } 357 } 358 359 if (products.size() == 0) { 360 return ServiceUtil.returnError("No products which fit your requirements were found."); 361 } else { 362 results = ServiceUtil.returnSuccess(); 363 results.put("products", products); 364 } 365 366 } else { 367 Debug.logWarning("No products found in " + productCategoryId, module); 368 } 369 370 return results; 371 } 372 } 373 | Popular Tags |