1 23 package org.ofbiz.product.feature; 24 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.http.HttpServletRequest ; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilHttp; 38 import org.ofbiz.base.util.UtilMisc; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.entity.util.EntityListIterator; 43 import org.ofbiz.entity.util.EntityUtil; 44 45 52 public class ParametricSearch { 53 54 public static final String module = ParametricSearch.class.getName(); 55 56 public static final int DEFAULT_PER_TYPE_MAX_SIZE = 1000; 57 58 62 66 public static Map makeCategoryFeatureLists(String productCategoryId, GenericDelegator delegator) { 67 return makeCategoryFeatureLists(productCategoryId, delegator, DEFAULT_PER_TYPE_MAX_SIZE); 68 } 69 70 public static Map makeCategoryFeatureLists(String productCategoryId, GenericDelegator delegator, int perTypeMaxSize) { 71 Map productFeaturesByTypeMap = new HashMap (); 72 try { 73 List productFeatureCategoryAppls = delegator.findByAndCache("ProductFeatureCategoryAppl", UtilMisc.toMap("productCategoryId", productCategoryId)); 74 productFeatureCategoryAppls = EntityUtil.filterByDate(productFeatureCategoryAppls, true); 75 if (productFeatureCategoryAppls != null) { 76 Iterator pfcasIter = productFeatureCategoryAppls.iterator(); 77 while (pfcasIter.hasNext()) { 78 GenericValue productFeatureCategoryAppl = (GenericValue) pfcasIter.next(); 79 List productFeatures = delegator.findByAndCache("ProductFeature", UtilMisc.toMap("productFeatureCategoryId", productFeatureCategoryAppl.get("productFeatureCategoryId"))); 80 Iterator pfsIter = productFeatures.iterator(); 81 while (pfsIter.hasNext()) { 82 GenericValue productFeature = (GenericValue) pfsIter.next(); 83 String productFeatureTypeId = productFeature.getString("productFeatureTypeId"); 84 Map featuresByType = (Map ) productFeaturesByTypeMap.get(productFeatureTypeId); 85 if (featuresByType == null) { 86 featuresByType = new HashMap (); 87 productFeaturesByTypeMap.put(productFeatureTypeId, featuresByType); 88 } 89 if (featuresByType.size() < perTypeMaxSize) { 90 featuresByType.put(productFeature.get("productFeatureId"), productFeature); 91 } 92 } 93 } 94 } 95 } catch (GenericEntityException e) { 96 Debug.logError(e, "Error getting feature categories associated with the category with ID: " + productCategoryId, module); 97 } 98 99 try { 100 List productFeatureCatGrpAppls = delegator.findByAndCache("ProductFeatureCatGrpAppl", UtilMisc.toMap("productCategoryId", productCategoryId)); 101 productFeatureCatGrpAppls = EntityUtil.filterByDate(productFeatureCatGrpAppls, true); 102 if (productFeatureCatGrpAppls != null) { 103 Iterator pfcgasIter = productFeatureCatGrpAppls.iterator(); 104 while (pfcgasIter.hasNext()) { 105 GenericValue productFeatureCatGrpAppl = (GenericValue) pfcgasIter.next(); 106 List productFeatureGroupAppls = delegator.findByAndCache("ProductFeatureGroupAppl", UtilMisc.toMap("productFeatureGroupId", productFeatureCatGrpAppl.get("productFeatureGroupId"))); 107 Iterator pfgaasIter = productFeatureGroupAppls.iterator(); 108 while (pfgaasIter.hasNext()) { 109 GenericValue productFeatureGroupAppl = (GenericValue) pfgaasIter.next(); 110 GenericValue productFeature = delegator.findByPrimaryKeyCache("ProductFeature", UtilMisc.toMap("productFeatureId", productFeatureGroupAppl.get("productFeatureId"))); 111 112 String productFeatureTypeId = productFeature.getString("productFeatureTypeId"); 113 Map featuresByType = (Map ) productFeaturesByTypeMap.get(productFeatureTypeId); 114 if (featuresByType == null) { 115 featuresByType = new HashMap (); 116 productFeaturesByTypeMap.put(productFeatureTypeId, featuresByType); 117 } 118 if (featuresByType.size() < perTypeMaxSize) { 119 featuresByType.put(productFeature.get("productFeatureId"), productFeature); 120 } 121 } 122 } 123 } 124 } catch (GenericEntityException e) { 125 Debug.logError(e, "Error getting feature groups associated with the category with ID: " + productCategoryId, module); 126 } 127 128 Iterator productFeatureTypeEntries = productFeaturesByTypeMap.entrySet().iterator(); 130 while (productFeatureTypeEntries.hasNext()) { 131 Map.Entry entry = (Map.Entry ) productFeatureTypeEntries.next(); 132 List sortedFeatures = EntityUtil.orderBy(((Map ) entry.getValue()).values(), UtilMisc.toList("description")); 133 productFeaturesByTypeMap.put(entry.getKey(), sortedFeatures); 134 } 135 136 return productFeaturesByTypeMap; 137 } 138 139 public static Map getAllFeaturesByType(GenericDelegator delegator) { 140 return getAllFeaturesByType(delegator, DEFAULT_PER_TYPE_MAX_SIZE); 141 } 142 public static Map getAllFeaturesByType(GenericDelegator delegator, int perTypeMaxSize) { 143 Map productFeaturesByTypeMap = new HashMap (); 144 try { 145 Set typesWithOverflowMessages = new HashSet (); 146 EntityListIterator productFeatureEli = delegator.findListIteratorByCondition("ProductFeature", null, null, UtilMisc.toList("description")); 147 GenericValue productFeature = null; 148 while ((productFeature = (GenericValue) productFeatureEli.next()) != null) { 149 String productFeatureTypeId = productFeature.getString("productFeatureTypeId"); 150 List featuresByType = (List ) productFeaturesByTypeMap.get(productFeatureTypeId); 151 if (featuresByType == null) { 152 featuresByType = new LinkedList (); 153 productFeaturesByTypeMap.put(productFeatureTypeId, featuresByType); 154 } 155 if (featuresByType.size() > perTypeMaxSize) { 156 if (!typesWithOverflowMessages.contains(productFeatureTypeId)) { 157 typesWithOverflowMessages.add(productFeatureTypeId); 158 } 160 } else { 161 featuresByType.add(productFeature); 162 } 163 } 164 productFeatureEli.close(); 165 } catch (GenericEntityException e) { 166 Debug.logError(e, "Error getting all features", module); 167 } 168 return productFeaturesByTypeMap; 169 } 170 171 public static Map makeFeatureIdByTypeMap(ServletRequest request) { 172 Map parameters = UtilHttp.getParameterMap((HttpServletRequest ) request); 173 return makeFeatureIdByTypeMap(parameters); 174 } 175 176 public static Map makeFeatureIdByTypeMap(Map parameters) { 177 Map featureIdByType = new HashMap (); 178 if (parameters == null) return featureIdByType; 179 180 Iterator parameterNameIter = parameters.keySet().iterator(); 181 while (parameterNameIter.hasNext()) { 182 String parameterName = (String ) parameterNameIter.next(); 183 if (parameterName.startsWith("pft_")) { 184 String productFeatureTypeId = parameterName.substring(4); 185 String productFeatureId = (String ) parameters.get(parameterName); 186 if (productFeatureId != null && productFeatureId.length() > 0) { 187 featureIdByType.put(productFeatureTypeId, productFeatureId); 188 } 189 } 190 } 191 192 return featureIdByType; 193 } 194 195 public static String makeFeatureIdByTypeString(Map featureIdByType) { 196 if (featureIdByType == null || featureIdByType.size() == 0) { 197 return ""; 198 } 199 200 StringBuffer outSb = new StringBuffer (); 201 Iterator fbtIter = featureIdByType.entrySet().iterator(); 202 while (fbtIter.hasNext()) { 203 Map.Entry entry = (Map.Entry ) fbtIter.next(); 204 String productFeatureTypeId = (String ) entry.getKey(); 205 String productFeatureId = (String ) entry.getValue(); 206 outSb.append(productFeatureTypeId); 207 outSb.append('='); 208 outSb.append(productFeatureId); 209 if (fbtIter.hasNext()) { 210 outSb.append('&'); 211 } 212 } 213 214 return outSb.toString(); 215 } 216 217 259 } 260 | Popular Tags |