KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ProductSearchEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.sql.Timestamp JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession 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.base.util.UtilProperties;
40 import org.ofbiz.base.util.UtilValidate;
41 import org.ofbiz.webapp.stats.VisitHandler;
42 import org.ofbiz.entity.GenericDelegator;
43 import org.ofbiz.entity.GenericEntityException;
44 import org.ofbiz.entity.GenericValue;
45 import org.ofbiz.entity.transaction.GenericTransactionException;
46 import org.ofbiz.entity.transaction.TransactionUtil;
47 import org.ofbiz.entity.util.EntityListIterator;
48 import org.ofbiz.product.product.ProductSearch.ProductSearchContext;
49 import org.ofbiz.product.product.ProductSearch.ResultSortOrder;
50
51 /**
52  * Product Search Related Events
53  *
54  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
55  * @version $Rev: 5462 $
56  * @since 3.0
57  */

58 public class ProductSearchEvents {
59
60     public static final String JavaDoc module = ProductSearchEvents.class.getName();
61     public static final String JavaDoc resource = "ProductUiLabels";
62
63     /** Removes the results of a search from the specified category
64      *@param request The HTTPRequest object for the current request
65      *@param response The HTTPResponse object for the current request
66      *@return String specifying the exit status of this event
67      */

68     public static String JavaDoc searchRemoveFromCategory(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
69         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
70         String JavaDoc productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
71         String JavaDoc errMsg=null;
72
73         EntityListIterator eli = getProductSearchResults(request);
74         if (eli == null) {
75             errMsg = UtilProperties.getMessage(resource,"productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
76             request.setAttribute("_ERROR_MESSAGE_", errMsg);
77             return "error";
78         }
79
80         try {
81             boolean beganTransaction = TransactionUtil.begin();
82             try {
83                 int numRemoved = 0;
84                 GenericValue searchResultView = null;
85                 while ((searchResultView = (GenericValue) eli.next()) != null) {
86                     String JavaDoc productId = searchResultView.getString("productId");
87                     numRemoved += delegator.removeByAnd("ProductCategoryMember", UtilMisc.toMap("productCategoryId", productCategoryId, "productId", productId )) ;
88                 }
89                 eli.close();
90                 TransactionUtil.commit(beganTransaction);
91                 Map JavaDoc messageMap = UtilMisc.toMap("numRemoved", Integer.toString(numRemoved));
92                 errMsg = UtilProperties.getMessage(resource,"productsearchevents.removed_x_items", messageMap, UtilHttp.getLocale(request));
93                 request.setAttribute("_EVENT_MESSAGE_", errMsg);
94             } catch (GenericEntityException e) {
95                 Map JavaDoc messageMap = UtilMisc.toMap("errSearchResult", e.toString());
96                 errMsg = UtilProperties.getMessage(resource,"productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
97                 Debug.logError(e, errMsg, module);
98                 request.setAttribute("_ERROR_MESSAGE_", errMsg);
99                 TransactionUtil.rollback(beganTransaction, errMsg, e);
100                 return "error";
101             }
102         } catch (GenericTransactionException e) {
103             Map JavaDoc messageMap = UtilMisc.toMap("errSearchResult", e.toString());
104             errMsg = UtilProperties.getMessage(resource,"productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
105             Debug.logError(e, errMsg, module);
106             request.setAttribute("_ERROR_MESSAGE_", errMsg);
107             return "error";
108         }
109         return "success";
110     }
111
112    /** Sets the thru date of the results of a search to the specified date for the specified catogory
113     *@param request The HTTPRequest object for the current request
114     *@param response The HTTPResponse object for the current request
115     *@return String specifying the exit status of this event
116     */

117    public static String JavaDoc searchExpireFromCategory(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
118        GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
119        String JavaDoc productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
120        String JavaDoc thruDateStr = request.getParameter("thruDate");
121        String JavaDoc errMsg=null;
122
123        Timestamp JavaDoc thruDate;
124        try {
125            thruDate = Timestamp.valueOf(thruDateStr);
126        } catch (RuntimeException JavaDoc e) {
127            Map JavaDoc messageMap = UtilMisc.toMap("errDateFormat", e.toString());
128            errMsg = UtilProperties.getMessage(resource,"productsearchevents.thruDate_not_formatted_properly", messageMap, UtilHttp.getLocale(request));
129            Debug.logError(e, errMsg, module);
130            request.setAttribute("_ERROR_MESSAGE_", errMsg);
131            return "error";
132        }
133
134        EntityListIterator eli = getProductSearchResults(request);
135        if (eli == null) {
136            errMsg = UtilProperties.getMessage(resource,"productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
137            request.setAttribute("_ERROR_MESSAGE_", errMsg);
138            return "error";
139        }
140
141        try {
142            boolean beganTransaction = TransactionUtil.begin();
143            try {
144
145                GenericValue searchResultView = null;
146                int numExpired=0;
147                while ((searchResultView = (GenericValue) eli.next()) != null) {
148                    String JavaDoc productId = searchResultView.getString("productId");
149                    //get all tuples that match product and category
150
List JavaDoc pcmList = delegator.findByAnd("ProductCategoryMember", UtilMisc.toMap("productCategoryId", productCategoryId, "productId", productId ));
151
152                    //set those thrudate to that specificed maybe remove then add new one
153
Iterator JavaDoc pcmListIter=pcmList.iterator();
154                    while (pcmListIter.hasNext()) {
155                        GenericValue pcm = (GenericValue) pcmListIter.next();
156                        if (pcm.get("thruDate") == null) {
157                            pcm.set("thruDate", thruDate);
158                            pcm.store();
159                            numExpired++;
160                        }
161                    }
162                }
163                Map JavaDoc messageMap = UtilMisc.toMap("numExpired", Integer.toString(numExpired));
164                errMsg = UtilProperties.getMessage(resource,"productsearchevents.expired_x_items", messageMap, UtilHttp.getLocale(request));
165                request.setAttribute("_EVENT_MESSAGE_", errMsg);
166                eli.close();
167                TransactionUtil.commit(beganTransaction);
168            } catch (GenericEntityException e) {
169                Map JavaDoc messageMap = UtilMisc.toMap("errSearchResult", e.toString());
170                errMsg = UtilProperties.getMessage(resource,"productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
171                Debug.logError(e, errMsg, module);
172                request.setAttribute("_ERROR_MESSAGE_", errMsg);
173                TransactionUtil.rollback(beganTransaction, errMsg, e);
174                return "error";
175            }
176        } catch (GenericTransactionException e) {
177            Map JavaDoc messageMap = UtilMisc.toMap("errSearchResult", e.toString());
178            errMsg = UtilProperties.getMessage(resource,"productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
179            Debug.logError(e, errMsg, module);
180            request.setAttribute("_ERROR_MESSAGE_", errMsg);
181            return "error";
182        }
183
184        return "success";
185    }
186
187    /** Adds the results of a search to the specified catogory
188     *@param request The HTTPRequest object for the current request
189     *@param response The HTTPResponse object for the current request
190     *@return String specifying the exit status of this event
191     */

192    public static String JavaDoc searchAddToCategory(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
193        GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
194        String JavaDoc productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
195        String JavaDoc fromDateStr = request.getParameter("fromDate");
196        Timestamp JavaDoc fromDate = null;
197        String JavaDoc errMsg = null;
198
199        try {
200            fromDate = Timestamp.valueOf(fromDateStr);
201         } catch (RuntimeException JavaDoc e) {
202            Map JavaDoc messageMap = UtilMisc.toMap("errDateFormat", e.toString());
203            errMsg = UtilProperties.getMessage(resource,"productsearchevents.fromDate_not_formatted_properly", messageMap, UtilHttp.getLocale(request));
204            request.setAttribute("_ERROR_MESSAGE_", errMsg);
205             return "error";
206         }
207
208        EntityListIterator eli = getProductSearchResults(request);
209        if (eli == null) {
210            errMsg = UtilProperties.getMessage(resource,"productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
211            request.setAttribute("_ERROR_MESSAGE_", errMsg);
212            return "error";
213        }
214
215        try {
216            boolean beganTransaction = TransactionUtil.begin();
217            try {
218
219                GenericValue searchResultView = null;
220                int numAdded = 0;
221                while ((searchResultView = (GenericValue) eli.next()) != null) {
222                    String JavaDoc productId = searchResultView.getString("productId");
223
224                    GenericValue pcm=delegator.makeValue("ProductCategoryMember", null);
225                    pcm.set("productCategoryId", productCategoryId);
226                    pcm.set("productId", productId);
227                    pcm.set("fromDate", fromDate);
228                    pcm.create();
229
230                    numAdded++;
231                }
232                Map JavaDoc messageMap = UtilMisc.toMap("numAdded", Integer.toString(numAdded));
233                errMsg = UtilProperties.getMessage(resource,"productsearchevents.added_x_product_category_members", messageMap, UtilHttp.getLocale(request));
234                request.setAttribute("_EVENT_MESSAGE_", errMsg);
235                eli.close();
236                TransactionUtil.commit(beganTransaction);
237            } catch (GenericEntityException e) {
238                Map JavaDoc messageMap = UtilMisc.toMap("errSearchResult", e.toString());
239                errMsg = UtilProperties.getMessage(resource,"productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
240                Debug.logError(e, errMsg, module);
241                request.setAttribute("_ERROR_MESSAGE_", errMsg);
242                TransactionUtil.rollback(beganTransaction, errMsg, e);
243                return "error";
244            }
245        } catch (GenericTransactionException e) {
246            Map JavaDoc messageMap = UtilMisc.toMap("errSearchResult", e.toString());
247            errMsg = UtilProperties.getMessage(resource,"productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
248            Debug.logError(e, errMsg, module);
249            request.setAttribute("_ERROR_MESSAGE_", errMsg);
250            return "error";
251        }
252
253        return "success";
254    }
255
256     /** Adds a feature to search results
257      *@param request The HTTPRequest object for the current request
258      *@param response The HTTPResponse object for the current request
259      *@return String specifying the exit status of this event
260      */

261     public static String JavaDoc searchAddFeature(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
262         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
263         Locale JavaDoc locale = UtilHttp.getLocale(request);
264
265         String JavaDoc productFeatureId = request.getParameter("productFeatureId");
266         String JavaDoc fromDateStr = request.getParameter("fromDate");
267         String JavaDoc thruDateStr = request.getParameter("thruDate");
268         String JavaDoc amountStr = request.getParameter("amount");
269         String JavaDoc sequenceNumStr = request.getParameter("sequenceNum");
270         String JavaDoc productFeatureApplTypeId = request.getParameter("productFeatureApplTypeId");
271
272         Timestamp JavaDoc thruDate = null;
273         Timestamp JavaDoc fromDate = null;
274         Double JavaDoc amount = null;
275         Long JavaDoc sequenceNum = null;
276
277         try {
278             if (UtilValidate.isNotEmpty(fromDateStr)) {
279                 fromDate = Timestamp.valueOf(fromDateStr);
280             }
281             if (UtilValidate.isNotEmpty(thruDateStr)) {
282                 thruDate = Timestamp.valueOf(thruDateStr);
283             }
284             if (UtilValidate.isNotEmpty(amountStr)) {
285                 amount = Double.valueOf(amountStr);
286             }
287             if (UtilValidate.isNotEmpty(sequenceNumStr)) {
288                 sequenceNum= Long.valueOf(sequenceNumStr);
289             }
290         } catch (RuntimeException JavaDoc e) {
291             String JavaDoc errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_casting_types", locale) + " : " + e.toString();
292             request.setAttribute("_ERROR_MESSAGE_", errorMsg);
293             Debug.logError(e, errorMsg, module);
294             return "error";
295         }
296
297         EntityListIterator eli = getProductSearchResults(request);
298         if (eli == null) {
299             String JavaDoc errMsg = UtilProperties.getMessage(resource,"productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
300             request.setAttribute("_ERROR_MESSAGE_", errMsg);
301             return "error";
302         }
303
304         try {
305             boolean beganTransaction = TransactionUtil.begin();
306             try {
307                 GenericValue searchResultView = null;
308                 int numAdded = 0;
309                 while ((searchResultView = (GenericValue) eli.next()) != null) {
310                     String JavaDoc productId = searchResultView.getString("productId");
311                     GenericValue pfa=delegator.makeValue("ProductFeatureAppl", null);
312                     pfa.set("productId", productId);
313                     pfa.set("productFeatureId", productFeatureId);
314                     pfa.set("fromDate", fromDate);
315                     pfa.set("thruDate", thruDate);
316                     pfa.set("productFeatureApplTypeId", productFeatureApplTypeId);
317                     pfa.set("amount", amount);
318                     pfa.set("sequenceNum", sequenceNum);
319                     pfa.create();
320                     numAdded++;
321                 }
322                 Map JavaDoc messageMap = UtilMisc.toMap("numAdded", new Integer JavaDoc(numAdded), "productFeatureId", productFeatureId);
323                 String JavaDoc eventMsg = UtilProperties.getMessage(resource, "productSearchEvents.added_param_features", messageMap, locale) + ".";
324                 request.setAttribute("_EVENT_MESSAGE_", eventMsg);
325                 eli.close();
326                 TransactionUtil.commit(beganTransaction);
327             } catch (GenericEntityException e) {
328                 String JavaDoc errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
329                 request.setAttribute("_ERROR_MESSAGE_", errorMsg);
330                 Debug.logError(e, errorMsg, module);
331                 TransactionUtil.rollback(beganTransaction, errorMsg, e);
332                 return "error";
333             }
334         } catch (GenericTransactionException e) {
335             String JavaDoc errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
336             request.setAttribute("_ERROR_MESSAGE_", errorMsg);
337             Debug.logError(e, errorMsg, module);
338             return "error";
339         }
340
341         return "success";
342     }
343
344     /** Removes a feature from search results
345      *@param request The HTTPRequest object for the current request
346      *@param response The HTTPResponse object for the current request
347      *@return String specifying the exit status of this event
348      */

349     public static String JavaDoc searchRemoveFeature(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
350         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
351         Locale JavaDoc locale = UtilHttp.getLocale(request);
352
353         String JavaDoc productFeatureId = request.getParameter("productFeatureId");
354
355         EntityListIterator eli = getProductSearchResults(request);
356         if (eli == null) {
357             String JavaDoc errMsg = UtilProperties.getMessage(resource,"productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
358             request.setAttribute("_ERROR_MESSAGE_", errMsg);
359             return "error";
360         }
361
362         try {
363             boolean beganTransaction = TransactionUtil.begin();
364             try {
365                 GenericValue searchResultView = null;
366                 int numRemoved = 0;
367                 while ((searchResultView = (GenericValue) eli.next()) != null) {
368                     String JavaDoc productId = searchResultView.getString("productId");
369                     numRemoved += delegator.removeByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", productId, "productFeatureId", productFeatureId));
370                 }
371                 Map JavaDoc messageMap = UtilMisc.toMap("numRemoved", new Integer JavaDoc(numRemoved), "productFeatureId", productFeatureId);
372                 String JavaDoc eventMsg = UtilProperties.getMessage(resource, "productSearchEvents.removed_param_features", messageMap, locale) + ".";
373                 request.setAttribute("_EVENT_MESSAGE_", eventMsg);
374                 eli.close();
375                 TransactionUtil.commit(beganTransaction);
376             } catch (GenericEntityException e) {
377                 String JavaDoc errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
378                 request.setAttribute("_ERROR_MESSAGE_", errorMsg);
379                 Debug.logError(e, errorMsg, module);
380                 TransactionUtil.rollback(beganTransaction, errorMsg, e);
381                 return "error";
382             }
383         } catch (GenericTransactionException e) {
384             String JavaDoc errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
385             request.setAttribute("_ERROR_MESSAGE_", errorMsg);
386             Debug.logError(e, errorMsg, module);
387             return "error";
388         }
389
390         return "success";
391     }
392
393     public static EntityListIterator getProductSearchResults(HttpServletRequest JavaDoc request) {
394         HttpSession JavaDoc session = request.getSession();
395         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
396         String JavaDoc visitId = VisitHandler.getVisitId(session);
397
398         List JavaDoc productSearchConstraintList = ProductSearchSession.ProductSearchOptions.getConstraintList(session);
399         // if no constraints, don't do a search...
400
if (productSearchConstraintList != null && productSearchConstraintList.size() > 0) {
401             ResultSortOrder resultSortOrder = ProductSearchSession.ProductSearchOptions.getResultSortOrder(session);
402             ProductSearchSession.checkSaveSearchOptionsHistory(session);
403             ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
404
405             productSearchContext.addProductSearchConstraints(productSearchConstraintList);
406             productSearchContext.setResultSortOrder(resultSortOrder);
407
408             return productSearchContext.doQuery(delegator);
409         } else {
410             return null;
411         }
412     }
413 }
414
Popular Tags