KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > product > ProductSearchSession


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

24 package org.ofbiz.product.product;
25
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import javax.servlet.ServletContext JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import javax.servlet.http.HttpSession JavaDoc;
41
42 import org.ofbiz.base.util.Debug;
43 import org.ofbiz.base.util.UtilHttp;
44 import org.ofbiz.base.util.UtilMisc;
45 import org.ofbiz.base.util.UtilValidate;
46 import org.ofbiz.webapp.stats.VisitHandler;
47 import org.ofbiz.webapp.control.RequestHandler;
48 import org.ofbiz.entity.GenericDelegator;
49 import org.ofbiz.entity.GenericEntityException;
50 import org.ofbiz.entity.GenericValue;
51 import org.ofbiz.entity.util.EntityUtil;
52 import org.ofbiz.product.catalog.CatalogWorker;
53 import org.ofbiz.product.feature.ParametricSearch;
54 import org.ofbiz.product.product.ProductSearch.CategoryConstraint;
55 import org.ofbiz.product.product.ProductSearch.FeatureConstraint;
56 import org.ofbiz.product.product.ProductSearch.KeywordConstraint;
57 import org.ofbiz.product.product.ProductSearch.ProductSearchConstraint;
58 import org.ofbiz.product.product.ProductSearch.ProductSearchContext;
59 import org.ofbiz.product.product.ProductSearch.ResultSortOrder;
60 import org.ofbiz.product.product.ProductSearch.SortKeywordRelevancy;
61 import org.ofbiz.product.store.ProductStoreWorker;
62
63 /**
64  * Utility class with methods to prepare and perform ProductSearch operations in the content of an HttpSession
65  *
66  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
67  * @version $Rev: 5462 $
68  * @since 3.0
69  */

70 public class ProductSearchSession {
71
72     public static final String JavaDoc module = ProductSearchSession.class.getName();
73
74     public static class ProductSearchOptions implements java.io.Serializable JavaDoc {
75         protected List JavaDoc constraintList = null;
76         protected ResultSortOrder resultSortOrder = null;
77         protected Integer JavaDoc viewIndex = null;
78         protected Integer JavaDoc viewSize = null;
79         protected boolean changed = false;
80
81         public ProductSearchOptions() { }
82
83         /** Basic copy constructor */
84         public ProductSearchOptions(ProductSearchOptions productSearchOptions) {
85             this.constraintList = new LinkedList JavaDoc(productSearchOptions.constraintList);
86             this.resultSortOrder = productSearchOptions.resultSortOrder;
87             this.viewIndex = productSearchOptions.viewIndex;
88             this.viewSize = productSearchOptions.viewSize;
89             this.changed = productSearchOptions.changed;
90         }
91
92         public List JavaDoc getConstraintList() {
93             return this.constraintList;
94         }
95         public static List JavaDoc getConstraintList(HttpSession JavaDoc session) {
96             return getProductSearchOptions(session).constraintList;
97         }
98         public static void addConstraint(ProductSearchConstraint productSearchConstraint, HttpSession JavaDoc session) {
99             ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
100             if (productSearchOptions.constraintList == null) {
101                 productSearchOptions.constraintList = new LinkedList JavaDoc();
102             }
103             if (!productSearchOptions.constraintList.contains(productSearchConstraint)) {
104                 productSearchOptions.constraintList.add(productSearchConstraint);
105                 productSearchOptions.changed = true;
106             }
107         }
108
109         public ResultSortOrder getResultSortOrder() {
110             if (this.resultSortOrder == null) {
111                 this.resultSortOrder = new SortKeywordRelevancy();
112                 this.changed = true;
113             }
114             return this.resultSortOrder;
115         }
116         public static ResultSortOrder getResultSortOrder(HttpSession JavaDoc session) {
117             ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
118             return productSearchOptions.getResultSortOrder();
119         }
120         public static void setResultSortOrder(ResultSortOrder resultSortOrder, HttpSession JavaDoc session) {
121             ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
122             productSearchOptions.resultSortOrder = resultSortOrder;
123             productSearchOptions.changed = true;
124         }
125         
126         public static void clearSearchOptions(HttpSession JavaDoc session) {
127             ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
128             productSearchOptions.constraintList = null;
129             productSearchOptions.resultSortOrder = null;
130         }
131         
132         public void clearViewInfo() {
133             this.viewIndex = null;
134             this.viewSize = null;
135         }
136
137         /**
138          * @return Returns the viewIndex.
139          */

140         public Integer JavaDoc getViewIndex() {
141             return viewIndex;
142         }
143         /**
144          * @param viewIndex The viewIndex to set.
145          */

146         public void setViewIndex(Integer JavaDoc viewIndex) {
147             this.viewIndex = viewIndex;
148         }
149         /**
150          * @return Returns the viewSize.
151          */

152         public Integer JavaDoc getViewSize() {
153             return viewSize;
154         }
155         /**
156          * @param viewSize The viewSize to set.
157          */

158         public void setViewSize(Integer JavaDoc viewSize) {
159             this.viewSize = viewSize;
160         }
161
162         public List JavaDoc searchGetConstraintStrings(boolean detailed, GenericDelegator delegator) {
163             List JavaDoc productSearchConstraintList = this.getConstraintList();
164             List JavaDoc constraintStrings = new ArrayList JavaDoc();
165             if (productSearchConstraintList == null) {
166                 return constraintStrings;
167             }
168             Iterator JavaDoc productSearchConstraintIter = productSearchConstraintList.iterator();
169             while (productSearchConstraintIter.hasNext()) {
170                 ProductSearchConstraint productSearchConstraint = (ProductSearchConstraint) productSearchConstraintIter.next();
171                 if (productSearchConstraint == null) continue;
172                 String JavaDoc constraintString = productSearchConstraint.prettyPrintConstraint(delegator, detailed);
173                 if (UtilValidate.isNotEmpty(constraintString)) {
174                     constraintStrings.add(constraintString);
175                 } else {
176                     constraintStrings.add("Description not available");
177                 }
178             }
179             return constraintStrings;
180         }
181     }
182
183     public static ProductSearchOptions getProductSearchOptions(HttpSession JavaDoc session) {
184         ProductSearchOptions productSearchOptions = (ProductSearchOptions) session.getAttribute("_PRODUCT_SEARCH_OPTIONS_CURRENT_");
185         if (productSearchOptions == null) {
186             productSearchOptions = new ProductSearchOptions();
187             session.setAttribute("_PRODUCT_SEARCH_OPTIONS_CURRENT_", productSearchOptions);
188         }
189         return productSearchOptions;
190     }
191
192     public static void checkSaveSearchOptionsHistory(HttpSession JavaDoc session) {
193         ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
194         // if the options have changed since the last search, add it to the beginning of the search options history
195
if (productSearchOptions.changed) {
196             List JavaDoc optionsHistoryList = getSearchOptionsHistoryList(session);
197             optionsHistoryList.add(0, new ProductSearchOptions(productSearchOptions));
198             productSearchOptions.changed = false;
199         }
200     }
201     public static List JavaDoc getSearchOptionsHistoryList(HttpSession JavaDoc session) {
202         List JavaDoc optionsHistoryList = (List JavaDoc) session.getAttribute("_PRODUCT_SEARCH_OPTIONS_HISTORY_");
203         if (optionsHistoryList == null) {
204             optionsHistoryList = new LinkedList JavaDoc();
205             session.setAttribute("_PRODUCT_SEARCH_OPTIONS_HISTORY_", optionsHistoryList);
206         }
207         return optionsHistoryList;
208     }
209     public static void clearSearchOptionsHistoryList(HttpSession JavaDoc session) {
210         session.removeAttribute("_PRODUCT_SEARCH_OPTIONS_HISTORY_");
211     }
212     
213     public static void setCurrentSearchFromHistory(int index, boolean removeOld, HttpSession JavaDoc session) {
214         List JavaDoc searchOptionsHistoryList = getSearchOptionsHistoryList(session);
215         if (index < searchOptionsHistoryList.size()) {
216             ProductSearchOptions productSearchOptions = (ProductSearchOptions) searchOptionsHistoryList.get(index);
217             if (removeOld) {
218                 searchOptionsHistoryList.remove(index);
219             }
220             if (productSearchOptions != null) {
221                 session.setAttribute("_PRODUCT_SEARCH_OPTIONS_CURRENT_", new ProductSearchOptions(productSearchOptions));
222             }
223         } else {
224             throw new IllegalArgumentException JavaDoc("Could not set current search options to history index [" + index + "], only [" + searchOptionsHistoryList.size() + "] entries in the history list.");
225         }
226     }
227
228     public static String JavaDoc clearSearchOptionsHistoryList(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
229         HttpSession JavaDoc session = request.getSession();
230         clearSearchOptionsHistoryList(session);
231         return "success";
232     }
233     
234     public static String JavaDoc setCurrentSearchFromHistory(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
235         HttpSession JavaDoc session = request.getSession();
236         String JavaDoc searchHistoryIndexStr = request.getParameter("searchHistoryIndex");
237         String JavaDoc removeOldStr = request.getParameter("removeOld");
238         
239         if (UtilValidate.isEmpty(searchHistoryIndexStr)) {
240             request.setAttribute("_ERROR_MESSAGE_", "No search history index passed, cannot set current search to previous.");
241             return "error";
242         }
243         
244         try {
245             int searchHistoryIndex = Integer.parseInt(searchHistoryIndexStr);
246             boolean removeOld = true;
247             if (UtilValidate.isNotEmpty(removeOldStr)) {
248                 removeOld = !"false".equals(removeOldStr);
249             }
250             setCurrentSearchFromHistory(searchHistoryIndex, removeOld, session);
251         } catch (Exception JavaDoc e) {
252             request.setAttribute("_ERROR_MESSAGE_", e.toString());
253             return "error";
254         }
255         
256         return "success";
257     }
258     
259     /** A ControlServlet event method used to check to see if there is an override for any of the current keywords in the search */
260     public static final String JavaDoc checkDoKeywordOverride(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
261         HttpSession JavaDoc session = request.getSession();
262         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
263         Map JavaDoc requestParams = UtilHttp.getParameterMap(request);
264         ProductSearchSession.processSearchParameters(requestParams, session);
265
266         // get the current productStoreId
267
String JavaDoc productStoreId = ProductStoreWorker.getProductStoreId(request);
268         if (productStoreId != null) {
269             // get a Set of all keywords in the search, if there are any...
270
Set JavaDoc keywords = new HashSet JavaDoc();
271             List JavaDoc constraintList = ProductSearchOptions.getConstraintList(session);
272             if (constraintList != null) {
273                 Iterator JavaDoc constraintIter = constraintList.iterator();
274                 while (constraintIter.hasNext()) {
275                     Object JavaDoc constraint = constraintIter.next();
276                     if (constraint instanceof KeywordConstraint) {
277                         KeywordConstraint keywordConstraint = (KeywordConstraint) constraint;
278                         Set JavaDoc keywordSet = keywordConstraint.makeFullKeywordSet(delegator);
279                         if (keywordSet != null) keywords.addAll(keywordSet);
280                     }
281                 }
282             }
283
284             if (keywords.size() > 0) {
285                 List JavaDoc productStoreKeywordOvrdList = null;
286                 try {
287                     productStoreKeywordOvrdList = delegator.findByAndCache("ProductStoreKeywordOvrd", UtilMisc.toMap("productStoreId", productStoreId), UtilMisc.toList("-fromDate"));
288                     productStoreKeywordOvrdList = EntityUtil.filterByDate(productStoreKeywordOvrdList, true);
289                 } catch (GenericEntityException e) {
290                     Debug.logError(e, "Error reading ProductStoreKeywordOvrd list, not doing keyword override", module);
291                 }
292
293                 if (productStoreKeywordOvrdList != null && productStoreKeywordOvrdList.size() > 0) {
294                     Iterator JavaDoc productStoreKeywordOvrdIter = productStoreKeywordOvrdList.iterator();
295                     while (productStoreKeywordOvrdIter.hasNext()) {
296                         GenericValue productStoreKeywordOvrd = (GenericValue) productStoreKeywordOvrdIter.next();
297                         String JavaDoc ovrdKeyword = productStoreKeywordOvrd.getString("keyword");
298                         if (keywords.contains(ovrdKeyword)) {
299                             String JavaDoc targetTypeEnumId = productStoreKeywordOvrd.getString("targetTypeEnumId");
300                             String JavaDoc target = productStoreKeywordOvrd.getString("target");
301                             ServletContext JavaDoc ctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
302                             RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
303                             if ("KOTT_PRODCAT".equals(targetTypeEnumId)) {
304                                 String JavaDoc requestName = "/category/~category_id=" + target;
305                                 target = rh.makeLink(request, response, requestName, false, false, false);
306                             } else if ("KOTT_PRODUCT".equals(targetTypeEnumId)) {
307                                 String JavaDoc requestName = "/product/~product_id=" + target;
308                                 target = rh.makeLink(request, response, requestName, false, false, false);
309                             } else if ("KOTT_OFBURL".equals(targetTypeEnumId)) {
310                                 target = rh.makeLink(request, response, target, false, false, false);
311                             } else if ("KOTT_AURL".equals(targetTypeEnumId)) {
312                                 // do nothing, is absolute URL
313
} else {
314                                 Debug.logError("The targetTypeEnumId [] is not recognized, not doing keyword override", module);
315                                 // might as well see if there are any others...
316
continue;
317                             }
318                             try {
319                                 response.sendRedirect(target);
320                                 return "none";
321                             } catch (IOException JavaDoc e) {
322                                 Debug.logError(e, "Could not send redirect to: " + target, module);
323                                 continue;
324                             }
325                         }
326                     }
327                 }
328             }
329         }
330
331         return "success";
332     }
333
334     public static ArrayList JavaDoc searchDo(HttpSession JavaDoc session, GenericDelegator delegator, String JavaDoc prodCatalogId) {
335         String JavaDoc visitId = VisitHandler.getVisitId(session);
336         ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
337         List JavaDoc productSearchConstraintList = productSearchOptions.getConstraintList();
338         if (productSearchConstraintList == null || productSearchConstraintList.size() == 0) {
339             // no constraints, don't do a search...
340
return new ArrayList JavaDoc();
341         }
342
343         // make sure the view allow category is included
344
productSearchConstraintList = ensureViewAllowConstraint(productSearchConstraintList, prodCatalogId, delegator);
345         ResultSortOrder resultSortOrder = productSearchOptions.getResultSortOrder();
346
347         // if the search options have changed since the last search, put at the beginning of the options history list
348
checkSaveSearchOptionsHistory(session);
349         
350         return ProductSearch.searchProducts(productSearchConstraintList, resultSortOrder, delegator, visitId);
351     }
352
353     public static List JavaDoc ensureViewAllowConstraint(List JavaDoc productSearchConstraintList, String JavaDoc prodCatalogId, GenericDelegator delegator) {
354         String JavaDoc viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
355         if (UtilValidate.isNotEmpty(viewProductCategoryId)) {
356             ProductSearchConstraint viewAllowConstraint = new CategoryConstraint(viewProductCategoryId, true);
357             if (!productSearchConstraintList.contains(viewAllowConstraint)) {
358                 // don't add to same list, will modify the one in the session, create new list
359
productSearchConstraintList = new ArrayList JavaDoc(productSearchConstraintList);
360                 productSearchConstraintList.add(viewAllowConstraint);
361             }
362         }
363         return productSearchConstraintList;
364     }
365
366     public static void searchClear(HttpSession JavaDoc session) {
367         ProductSearchOptions.clearSearchOptions(session);
368     }
369     
370     public static List JavaDoc searchGetConstraintStrings(boolean detailed, HttpSession JavaDoc session, GenericDelegator delegator) {
371         ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
372         return productSearchOptions.searchGetConstraintStrings(detailed, delegator);
373     }
374
375     public static String JavaDoc searchGetSortOrderString(boolean detailed, HttpSession JavaDoc session) {
376         ResultSortOrder resultSortOrder = ProductSearchOptions.getResultSortOrder(session);
377         if (resultSortOrder == null) return "";
378         return resultSortOrder.prettyPrintSortOrder(detailed);
379     }
380
381     public static void searchSetSortOrder(ResultSortOrder resultSortOrder, HttpSession JavaDoc session) {
382         ProductSearchOptions.setResultSortOrder(resultSortOrder, session);
383     }
384
385     public static void searchAddFeatureIdConstraints(Collection JavaDoc featureIds, HttpSession JavaDoc session) {
386         if (featureIds == null || featureIds.size() == 0) {
387             return;
388         }
389         Iterator JavaDoc featureIdIter = featureIds.iterator();
390         while (featureIdIter.hasNext()) {
391             String JavaDoc productFeatureId = (String JavaDoc) featureIdIter.next();
392             searchAddConstraint(new FeatureConstraint(productFeatureId), session);
393         }
394     }
395
396     public static void searchAddConstraint(ProductSearchConstraint productSearchConstraint, HttpSession JavaDoc session) {
397         ProductSearchOptions.addConstraint(productSearchConstraint, session);
398     }
399
400     public static void searchRemoveConstraint(int index, HttpSession JavaDoc session) {
401         List JavaDoc productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
402         if (productSearchConstraintList == null) {
403             return;
404         } else if (index >= productSearchConstraintList.size()) {
405             return;
406         } else {
407             productSearchConstraintList.remove(index);
408         }
409     }
410
411     public static void processSearchParameters(Map JavaDoc parameters, HttpSession JavaDoc session) {
412         boolean constraintsChanged = false;
413         
414         // clear search? by default yes, but if the clearSearch parameter is N then don't
415
String JavaDoc clearSearchString = (String JavaDoc) parameters.get("clearSearch");
416         if (!"N".equals(clearSearchString)) {
417             searchClear(session);
418             constraintsChanged = true;
419         } else {
420             String JavaDoc removeConstraint = (String JavaDoc) parameters.get("removeConstraint");
421             if (UtilValidate.isNotEmpty(removeConstraint)) {
422                 try {
423                     searchRemoveConstraint(Integer.parseInt(removeConstraint), session);
424                     constraintsChanged = true;
425                 } catch (Exception JavaDoc e) {
426                     Debug.logError(e, "Error removing constraint [" + removeConstraint + "]", module);
427                 }
428             }
429         }
430
431         // if there is another category, add a constraint for it
432
if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_CATEGORY_ID"))) {
433             String JavaDoc searchCategoryId = (String JavaDoc) parameters.get("SEARCH_CATEGORY_ID");
434             String JavaDoc searchSubCategories = (String JavaDoc) parameters.get("SEARCH_SUB_CATEGORIES");
435             searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories)), session);
436             constraintsChanged = true;
437         }
438         if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_CATEGORY_ID2"))) {
439             String JavaDoc searchCategoryId = (String JavaDoc) parameters.get("SEARCH_CATEGORY_ID2");
440             String JavaDoc searchSubCategories = (String JavaDoc) parameters.get("SEARCH_SUB_CATEGORIES2");
441             searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories)), session);
442             constraintsChanged = true;
443         }
444         if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_CATEGORY_ID3"))) {
445             String JavaDoc searchCategoryId = (String JavaDoc) parameters.get("SEARCH_CATEGORY_ID3");
446             String JavaDoc searchSubCategories = (String JavaDoc) parameters.get("SEARCH_SUB_CATEGORIES3");
447             searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories)), session);
448             constraintsChanged = true;
449         }
450
451         // if keywords were specified, add a constraint for them
452
if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_STRING"))) {
453             String JavaDoc keywordString = (String JavaDoc) parameters.get("SEARCH_STRING");
454             String JavaDoc searchOperator = (String JavaDoc) parameters.get("SEARCH_OPERATOR");
455             // defaults to true/Y, ie anything but N is true/Y
456
boolean anyPrefixSuffix = !"N".equals((String JavaDoc) parameters.get("SEARCH_ANYPRESUF"));
457             searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
458             constraintsChanged = true;
459         }
460         if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_STRING2"))) {
461             String JavaDoc keywordString = (String JavaDoc) parameters.get("SEARCH_STRING2");
462             String JavaDoc searchOperator = (String JavaDoc) parameters.get("SEARCH_OPERATOR2");
463             // defaults to true/Y, ie anything but N is true/Y
464
boolean anyPrefixSuffix = !"N".equals((String JavaDoc) parameters.get("SEARCH_ANYPRESUF2"));
465             searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
466             constraintsChanged = true;
467         }
468         if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_STRING3"))) {
469             String JavaDoc keywordString = (String JavaDoc) parameters.get("SEARCH_STRING3");
470             String JavaDoc searchOperator = (String JavaDoc) parameters.get("SEARCH_OPERATOR3");
471             // defaults to true/Y, ie anything but N is true/Y
472
boolean anyPrefixSuffix = !"N".equals((String JavaDoc) parameters.get("SEARCH_ANYPRESUF3"));
473             searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
474             constraintsChanged = true;
475         }
476
477         // get independently defined features, ie not with a type parameter
478
if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_FEAT"))) {
479             searchAddConstraint(new ProductSearch.FeatureConstraint((String JavaDoc) parameters.get("SEARCH_FEAT")), session);
480             constraintsChanged = true;
481         }
482         if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_FEAT2"))) {
483             searchAddConstraint(new ProductSearch.FeatureConstraint((String JavaDoc) parameters.get("SEARCH_FEAT2")), session);
484             constraintsChanged = true;
485         }
486         if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_FEAT3"))) {
487             searchAddConstraint(new ProductSearch.FeatureConstraint((String JavaDoc) parameters.get("SEARCH_FEAT3")), session);
488             constraintsChanged = true;
489         }
490         
491         // if features were selected add a constraint for each
492
Map JavaDoc featureIdByType = ParametricSearch.makeFeatureIdByTypeMap(parameters);
493         if (featureIdByType.size() > 0) {
494             constraintsChanged = true;
495             searchAddFeatureIdConstraints(featureIdByType.values(), session);
496         }
497
498         // add a supplier to the search
499
if (UtilValidate.isNotEmpty((String JavaDoc) parameters.get("SEARCH_SUPPLIER_ID"))) {
500             String JavaDoc supplierPartyId = (String JavaDoc) parameters.get("SEARCH_SUPPLIER_ID");
501             searchAddConstraint(new ProductSearch.SupplierConstraint(supplierPartyId), session);
502             constraintsChanged = true;
503         }
504
505         // set the sort order
506
String JavaDoc sortOrder = (String JavaDoc) parameters.get("sortOrder");
507         String JavaDoc sortAscending = (String JavaDoc) parameters.get("sortAscending");
508         boolean ascending = !"N".equals(sortAscending);
509         if (sortOrder != null) {
510             if (sortOrder.equals("SortKeywordRelevancy")) {
511                 searchSetSortOrder(new ProductSearch.SortKeywordRelevancy(), session);
512             } else if (sortOrder.startsWith("SortProductField:")) {
513                 String JavaDoc fieldName = sortOrder.substring("SortProductField:".length());
514                 searchSetSortOrder(new ProductSearch.SortProductField(fieldName, ascending), session);
515             } else if (sortOrder.startsWith("SortProductPrice:")) {
516                 String JavaDoc priceTypeId = sortOrder.substring("SortProductPrice:".length());
517                 searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
518             }
519         }
520         
521         ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
522         if (constraintsChanged) {
523             // query changed, clear out the VIEW_INDEX & VIEW_SIZE
524
productSearchOptions.clearViewInfo();
525         }
526
527         String JavaDoc viewIndexStr = (String JavaDoc) parameters.get("VIEW_INDEX");
528         if (UtilValidate.isNotEmpty(viewIndexStr)) {
529             try {
530                 productSearchOptions.setViewIndex(Integer.valueOf(viewIndexStr));
531             } catch (Exception JavaDoc e) {
532                 Debug.logError(e, "Error formatting VIEW_INDEX, setting to 0", module);
533                 // we could just do nothing here, but we know something was specified so we don't want to use the previous value from the session
534
productSearchOptions.setViewIndex(new Integer JavaDoc(0));
535             }
536         }
537
538         String JavaDoc viewSizeStr = (String JavaDoc) parameters.get("VIEW_SIZE");
539         if (UtilValidate.isNotEmpty(viewSizeStr)) {
540             try {
541                 productSearchOptions.setViewSize(Integer.valueOf(viewSizeStr));
542             } catch (Exception JavaDoc e) {
543                 Debug.logError(e, "Error formatting VIEW_SIZE, setting to 20", module);
544                 productSearchOptions.setViewSize(new Integer JavaDoc(20));
545             }
546         }
547     }
548
549     public static Map JavaDoc getProductSearchResult(HttpSession JavaDoc session, GenericDelegator delegator, String JavaDoc prodCatalogId) {
550
551         // ========== Create View Indexes
552
int viewIndex = 0;
553         int viewSize = 20;
554         int highIndex = 0;
555         int lowIndex = 0;
556         int listSize = 0;
557
558         ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
559         
560         Integer JavaDoc viewIndexInteger = productSearchOptions.getViewIndex();
561         if (viewIndexInteger != null) viewIndex = viewIndexInteger.intValue();
562         Integer JavaDoc viewSizeInteger = productSearchOptions.getViewSize();
563         if (viewSizeInteger != null) viewSize = viewSizeInteger.intValue();
564
565         lowIndex = viewIndex * viewSize;
566         highIndex = (viewIndex + 1) * viewSize;
567
568         // setup resultOffset and maxResults, noting that resultOffset is 1 based, not zero based as these numbers
569
Integer JavaDoc resultOffset = new Integer JavaDoc(lowIndex + 1);
570         Integer JavaDoc maxResults = new Integer JavaDoc(viewSize);
571
572         // ========== Do the actual search
573
ArrayList JavaDoc productIds = null;
574         String JavaDoc visitId = VisitHandler.getVisitId(session);
575         List JavaDoc productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
576         // if no constraints, don't do a search...
577
if (productSearchConstraintList != null && productSearchConstraintList.size() > 0) {
578             // if the search options have changed since the last search, put at the beginning of the options history list
579
checkSaveSearchOptionsHistory(session);
580
581             productSearchConstraintList = ensureViewAllowConstraint(productSearchConstraintList, prodCatalogId, delegator);
582             ResultSortOrder resultSortOrder = ProductSearchOptions.getResultSortOrder(session);
583
584             ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
585             productSearchContext.addProductSearchConstraints(productSearchConstraintList);
586             productSearchContext.setResultSortOrder(resultSortOrder);
587             productSearchContext.setResultOffset(resultOffset);
588             productSearchContext.setMaxResults(maxResults);
589
590             productIds = productSearchContext.doSearch();
591
592             Integer JavaDoc totalResults = productSearchContext.getTotalResults();
593             if (totalResults != null) {
594                 listSize = totalResults.intValue();
595             }
596         }
597
598         if (listSize < highIndex) {
599             highIndex = listSize;
600         }
601
602         // ========== Setup other display info
603
List JavaDoc searchConstraintStrings = searchGetConstraintStrings(false, session, delegator);
604         String JavaDoc searchSortOrderString = searchGetSortOrderString(false, session);
605
606         // ========== populate the result Map
607
Map JavaDoc result = new HashMap JavaDoc();
608
609         result.put("productIds", productIds);
610         result.put("viewIndex", new Integer JavaDoc(viewIndex));
611         result.put("viewSize", new Integer JavaDoc(viewSize));
612         result.put("listSize", new Integer JavaDoc(listSize));
613         result.put("lowIndex", new Integer JavaDoc(lowIndex));
614         result.put("highIndex", new Integer JavaDoc(highIndex));
615         result.put("searchConstraintStrings", searchConstraintStrings);
616         result.put("searchSortOrderString", searchSortOrderString);
617
618         return result;
619     }
620 }
621
Popular Tags