KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > feature > ParametricSearch


1 /*
2  * $Id: ParametricSearch.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001 The Open For Business Project (www.ofbiz.org)
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
20  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */

23 package org.ofbiz.product.feature;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
46  * Utilities for parametric search based on features.
47  *
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version $Rev: 5462 $
50  * @since 2.1
51  */

52 public class ParametricSearch {
53     
54     public static final String JavaDoc module = ParametricSearch.class.getName();
55     
56     public static final int DEFAULT_PER_TYPE_MAX_SIZE = 1000;
57     
58     // caches expire after 10 minutes, a reasonable value hopefully...
59
//public static UtilCache featureAllCache = new UtilCache("custom.FeaturePerTypeAll", 0, 600000, true);
60
//public static UtilCache featureByCategoryCache = new UtilCache("custom.FeaturePerTypeByCategory", 0, 600000, true);
61

62     /** Gets all features associated with the specified category through:
63      * ProductCategory -> ProductFeatureCategoryAppl -> ProductFeatureCategory -> ProductFeature.
64      * Returns a Map of Lists of ProductFeature GenericValue objects organized by productFeatureTypeId.
65      */

66     public static Map JavaDoc makeCategoryFeatureLists(String JavaDoc productCategoryId, GenericDelegator delegator) {
67         return makeCategoryFeatureLists(productCategoryId, delegator, DEFAULT_PER_TYPE_MAX_SIZE);
68     }
69     
70     public static Map JavaDoc makeCategoryFeatureLists(String JavaDoc productCategoryId, GenericDelegator delegator, int perTypeMaxSize) {
71         Map JavaDoc productFeaturesByTypeMap = new HashMap JavaDoc();
72         try {
73             List JavaDoc productFeatureCategoryAppls = delegator.findByAndCache("ProductFeatureCategoryAppl", UtilMisc.toMap("productCategoryId", productCategoryId));
74             productFeatureCategoryAppls = EntityUtil.filterByDate(productFeatureCategoryAppls, true);
75             if (productFeatureCategoryAppls != null) {
76                 Iterator JavaDoc pfcasIter = productFeatureCategoryAppls.iterator();
77                 while (pfcasIter.hasNext()) {
78                     GenericValue productFeatureCategoryAppl = (GenericValue) pfcasIter.next();
79                     List JavaDoc productFeatures = delegator.findByAndCache("ProductFeature", UtilMisc.toMap("productFeatureCategoryId", productFeatureCategoryAppl.get("productFeatureCategoryId")));
80                     Iterator JavaDoc pfsIter = productFeatures.iterator();
81                     while (pfsIter.hasNext()) {
82                         GenericValue productFeature = (GenericValue) pfsIter.next();
83                         String JavaDoc productFeatureTypeId = productFeature.getString("productFeatureTypeId");
84                         Map JavaDoc featuresByType = (Map JavaDoc) productFeaturesByTypeMap.get(productFeatureTypeId);
85                         if (featuresByType == null) {
86                             featuresByType = new HashMap JavaDoc();
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 JavaDoc productFeatureCatGrpAppls = delegator.findByAndCache("ProductFeatureCatGrpAppl", UtilMisc.toMap("productCategoryId", productCategoryId));
101             productFeatureCatGrpAppls = EntityUtil.filterByDate(productFeatureCatGrpAppls, true);
102             if (productFeatureCatGrpAppls != null) {
103                 Iterator JavaDoc pfcgasIter = productFeatureCatGrpAppls.iterator();
104                 while (pfcgasIter.hasNext()) {
105                     GenericValue productFeatureCatGrpAppl = (GenericValue) pfcgasIter.next();
106                     List JavaDoc productFeatureGroupAppls = delegator.findByAndCache("ProductFeatureGroupAppl", UtilMisc.toMap("productFeatureGroupId", productFeatureCatGrpAppl.get("productFeatureGroupId")));
107                     Iterator JavaDoc 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 JavaDoc productFeatureTypeId = productFeature.getString("productFeatureTypeId");
113                         Map JavaDoc featuresByType = (Map JavaDoc) productFeaturesByTypeMap.get(productFeatureTypeId);
114                         if (featuresByType == null) {
115                             featuresByType = new HashMap JavaDoc();
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         // now before returning, order the features in each list by description
129
Iterator JavaDoc productFeatureTypeEntries = productFeaturesByTypeMap.entrySet().iterator();
130         while (productFeatureTypeEntries.hasNext()) {
131             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) productFeatureTypeEntries.next();
132             List JavaDoc sortedFeatures = EntityUtil.orderBy(((Map JavaDoc) entry.getValue()).values(), UtilMisc.toList("description"));
133             productFeaturesByTypeMap.put(entry.getKey(), sortedFeatures);
134         }
135         
136         return productFeaturesByTypeMap;
137     }
138     
139     public static Map JavaDoc getAllFeaturesByType(GenericDelegator delegator) {
140         return getAllFeaturesByType(delegator, DEFAULT_PER_TYPE_MAX_SIZE);
141     }
142     public static Map JavaDoc getAllFeaturesByType(GenericDelegator delegator, int perTypeMaxSize) {
143         Map JavaDoc productFeaturesByTypeMap = new HashMap JavaDoc();
144         try {
145             Set JavaDoc typesWithOverflowMessages = new HashSet JavaDoc();
146             EntityListIterator productFeatureEli = delegator.findListIteratorByCondition("ProductFeature", null, null, UtilMisc.toList("description"));
147             GenericValue productFeature = null;
148             while ((productFeature = (GenericValue) productFeatureEli.next()) != null) {
149                 String JavaDoc productFeatureTypeId = productFeature.getString("productFeatureTypeId");
150                 List JavaDoc featuresByType = (List JavaDoc) productFeaturesByTypeMap.get(productFeatureTypeId);
151                 if (featuresByType == null) {
152                     featuresByType = new LinkedList JavaDoc();
153                     productFeaturesByTypeMap.put(productFeatureTypeId, featuresByType);
154                 }
155                 if (featuresByType.size() > perTypeMaxSize) {
156                     if (!typesWithOverflowMessages.contains(productFeatureTypeId)) {
157                         typesWithOverflowMessages.add(productFeatureTypeId);
158                         // TODO: uh oh, how do we pass this message back? no biggie for now
159
}
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 JavaDoc makeFeatureIdByTypeMap(ServletRequest JavaDoc request) {
172         Map JavaDoc parameters = UtilHttp.getParameterMap((HttpServletRequest JavaDoc) request);
173         return makeFeatureIdByTypeMap(parameters);
174     }
175     
176     public static Map JavaDoc makeFeatureIdByTypeMap(Map JavaDoc parameters) {
177         Map JavaDoc featureIdByType = new HashMap JavaDoc();
178         if (parameters == null) return featureIdByType;
179         
180         Iterator JavaDoc parameterNameIter = parameters.keySet().iterator();
181         while (parameterNameIter.hasNext()) {
182             String JavaDoc parameterName = (String JavaDoc) parameterNameIter.next();
183             if (parameterName.startsWith("pft_")) {
184                 String JavaDoc productFeatureTypeId = parameterName.substring(4);
185                 String JavaDoc productFeatureId = (String JavaDoc) 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 JavaDoc makeFeatureIdByTypeString(Map JavaDoc featureIdByType) {
196         if (featureIdByType == null || featureIdByType.size() == 0) {
197             return "";
198         }
199         
200         StringBuffer JavaDoc outSb = new StringBuffer JavaDoc();
201         Iterator JavaDoc fbtIter = featureIdByType.entrySet().iterator();
202         while (fbtIter.hasNext()) {
203             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) fbtIter.next();
204             String JavaDoc productFeatureTypeId = (String JavaDoc) entry.getKey();
205             String JavaDoc productFeatureId = (String JavaDoc) 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     /* TODO: DEJ 20031025 delete this if not used in the near future
218     public static void filterProductIdListByFeatures(List productIds, Map featureIdByType, GenericDelegator delegator) {
219         if (productIds == null || productIds.size() == 0) return;
220         //filter search results by features
221         
222         // the fun part: go through each product and make sure it has all specified features
223         Iterator productIdsIter = productIds.iterator();
224         while (productIdsIter.hasNext()) {
225             String productId = (String) productIdsIter.next();
226             
227             boolean doRemove = false;
228             Iterator requiredFeaturesIter = featureIdByType.values().iterator();
229             while (!doRemove && requiredFeaturesIter.hasNext()) {
230                 String productFeatureId = (String) requiredFeaturesIter.next();
231                 List productFeatureAppl = null;
232                 try {
233                     // for now only constraining by productId and productFeatureId, so any appl type will be included...
234                     productFeatureAppl = delegator.findByAndCache("ProductFeatureAppl", UtilMisc.toMap("productId", productId, "productFeatureId", productFeatureId));
235                 } catch (GenericEntityException e) {
236                     Debug.logError(e, "Error getting feature appls associated with the productId: [" + productId + "] and the productFeatureId [" + productFeatureId + "], removing product from search match list.", module);
237                     doRemove = true;
238                     continue;
239                 }
240             
241                 productFeatureAppl = EntityUtil.filterByDate(productFeatureAppl, true);
242                 if (productFeatureAppl == null || productFeatureAppl.size() == 0) {
243                     doRemove = true;
244                 }
245             }
246             
247             if (doRemove) {
248                 productIdsIter.remove();
249             }
250         }
251     }
252     
253     public static ArrayList parametricKeywordSearch(Map featureIdByType, String keywordsString, GenericDelegator delegator, String categoryId, String visitId, boolean anyPrefix, boolean anySuffix, boolean isAnd) {
254         ArrayList productIds = KeywordSearch.productsByKeywords(keywordsString, delegator, categoryId, visitId, anyPrefix, anySuffix, isAnd);
255         filterProductIdListByFeatures(productIds, featureIdByType, delegator);
256         return productIds;
257     }
258      */

259 }
260
Popular Tags