KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > SearchAction


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 14/01/2004 / 22:02:56
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum;
44
45 import java.lang.reflect.Field JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Date JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54
55 import net.jforum.ActionServletRequest;
56 import net.jforum.Command;
57 import net.jforum.JForumExecutionContext;
58 import net.jforum.dao.DataAccessDriver;
59 import net.jforum.dao.SearchDAO;
60 import net.jforum.dao.SearchData;
61 import net.jforum.entities.Forum;
62 import net.jforum.entities.Topic;
63 import net.jforum.repository.ForumRepository;
64 import net.jforum.util.I18n;
65 import net.jforum.util.preferences.ConfigKeys;
66 import net.jforum.util.preferences.SystemGlobals;
67 import net.jforum.util.preferences.TemplateKeys;
68 import net.jforum.view.forum.common.TopicsCommon;
69 import net.jforum.view.forum.common.ViewCommon;
70 import freemarker.template.SimpleHash;
71
72 /**
73  * @author Rafael Steil
74  * @version $Id: SearchAction.java,v 1.27 2006/01/29 15:06:58 rafaelsteil Exp $
75  */

76 public class SearchAction extends Command
77 {
78     private String JavaDoc searchTerms;
79     private String JavaDoc forum;
80     private String JavaDoc category;
81     private String JavaDoc sortBy;
82     private String JavaDoc sortDir;
83     private String JavaDoc kw;
84     private String JavaDoc author;
85     private String JavaDoc postTime;
86     
87     private static Map JavaDoc fieldsMap = new HashMap JavaDoc();
88     private static Map JavaDoc sortByMap = new HashMap JavaDoc();
89     
90     static {
91         fieldsMap.put("search_terms", "searchTerms");
92         fieldsMap.put("search_forum", "forum");
93         fieldsMap.put("search_cat", "catgory");
94         fieldsMap.put("sort_by", "sortBy");
95         fieldsMap.put("sort_dir", "sortDir");
96         fieldsMap.put("search_keywords", "kw");
97         fieldsMap.put("search_author", "author");
98         fieldsMap.put("post_time", "postTime");
99         
100         sortByMap.put("time", "p.post_time");
101         sortByMap.put("title", "t.topic_title");
102         sortByMap.put("username", "u.username");
103         sortByMap.put("forum", "t.forum_id");
104     }
105     
106     public SearchAction() {}
107     
108     public SearchAction(ActionServletRequest request, HttpServletResponse JavaDoc response,
109             SimpleHash context) {
110         this.request = request;
111         this.response = response;
112         this.context = context;
113     }
114     
115     public void filters() throws Exception JavaDoc
116     {
117         this.setTemplateName(TemplateKeys.SEARCH_FILTERS);
118         this.context.put("categories", ForumRepository.getAllCategories());
119         this.context.put("pageTitle", I18n.getMessage("ForumBase.search"));
120     }
121     
122     private void getSearchFields()
123     {
124         this.searchTerms = this.addSlashes(this.request.getParameter("search_terms"));
125         this.forum = this.addSlashes(this.request.getParameter("search_forum"));
126         this.category = this.addSlashes(this.request.getParameter("search_cat"));
127         
128         this.sortBy = (String JavaDoc)sortByMap.get(this.addSlashes(this.request.getParameter("sort_by")));
129         
130         if (this.sortBy == null) {
131             this.sortBy = (String JavaDoc)sortByMap.get("time");
132         }
133         
134         this.sortDir = this.addSlashes(this.request.getParameter("sort_dir"));
135         
136         if (!"ASC".equals(this.sortDir) && !"DESC".equals(this.sortDir)) {
137             this.sortDir = "DESC";
138         }
139         
140         this.kw = this.addSlashes(this.request.getParameter("search_keywords"));
141         this.author = this.addSlashes(this.request.getParameter("search_author"));
142         this.postTime = this.addSlashes(this.request.getParameter("post_time"));
143     }
144     
145     public void search() throws Exception JavaDoc
146     {
147         this.getSearchFields();
148         
149         SearchData sd = new SearchData();
150         sd.setKeywords(kw);
151         sd.setAuthor(author);
152         sd.setOrderByField(sortBy);
153         sd.setOrderBy(sortDir);
154         
155         if (postTime != null) {
156             sd.setTime(new Date JavaDoc(Long.parseLong(postTime)));
157         }
158         
159         if (searchTerms != null) {
160             sd.setUseAllWords(searchTerms.equals("any") ? false : true);
161         }
162         else {
163             sd.setUseAllWords(true);
164         }
165         
166         if (forum != null && !forum.equals("")) {
167             sd.setForumId(Integer.parseInt(forum));
168         }
169         
170         if (category != null && !category.equals("")) {
171             sd.setCategoryId(Integer.parseInt(category));
172         }
173         
174         int start = ViewCommon.getStartPage();
175         int recordsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
176         
177         SearchDAO sm = DataAccessDriver.getInstance().newSearchDAO();
178
179         // Clean the search
180
if (this.request.getParameter("clean") != null) {
181             sm.cleanSearch();
182         }
183         else {
184             sd.setSearchStarted(true);
185         }
186         
187         List JavaDoc allTopics = this.onlyAllowedData(sm.search(sd));
188         int totalTopics = allTopics.size();
189         int sublistLimit = recordsPerPage + start > totalTopics ? totalTopics : recordsPerPage + start;
190         
191         this.setTemplateName(TemplateKeys.SEARCH_SEARCH);
192         
193         this.context.put("fr", new ForumRepository());
194         
195         this.context.put("topics", TopicsCommon.prepareTopics(allTopics.subList(start, sublistLimit)));
196         this.context.put("categories", ForumRepository.getAllCategories());
197         
198         this.context.put("kw", kw);
199         this.context.put("terms", searchTerms);
200         this.context.put("forum", forum);
201         this.context.put("category", category);
202         this.context.put("orderField", sortBy);
203         this.context.put("orderBy", sortDir);
204         this.context.put("author", author);
205         this.context.put("postTime", postTime);
206         this.context.put("openModeration", "1".equals(this.request.getParameter("openModeration")));
207         this.context.put("postsPerPage", new Integer JavaDoc(SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE)));
208         
209         ViewCommon.contextToPagination(start, totalTopics, recordsPerPage);
210         
211         this.context.put("pageTitle", I18n.getMessage("ForumBase.search"));
212         TopicsCommon.topicListingBase();
213     }
214     
215     private List JavaDoc onlyAllowedData(List JavaDoc topics) throws Exception JavaDoc
216     {
217         List JavaDoc l = new ArrayList JavaDoc();
218         
219         for (Iterator JavaDoc iter = topics.iterator(); iter.hasNext(); ) {
220             Topic t = (Topic)iter.next();
221             Forum f = ForumRepository.getForum(t.getForumId());
222             
223             if (f != null && ForumRepository.isCategoryAccessible(f.getCategoryId())) {
224                 l.add(t);
225             }
226         }
227         
228         return l;
229     }
230     
231     public void doModeration() throws Exception JavaDoc
232     {
233         new ModerationHelper().doModeration(this.makeRedirect());
234         
235         if (JForumExecutionContext.getRequest().getParameter("topicMove") != null) {
236             this.setTemplateName(TemplateKeys.MODERATION_MOVE_TOPICS);
237         }
238     }
239     
240     public void moveTopic() throws Exception JavaDoc
241     {
242         new ModerationHelper().moveTopicsSave(this.makeRedirect());
243     }
244     
245     public void moderationDone() throws Exception JavaDoc
246     {
247         this.setTemplateName(new ModerationHelper().moderationDone(this.makeRedirect()));
248     }
249     
250     private String JavaDoc makeRedirect() throws Exception JavaDoc
251     {
252         String JavaDoc persistData = this.request.getParameter("persistData");
253         if (persistData == null) {
254             this.getSearchFields();
255         }
256         else {
257             String JavaDoc[] p = persistData.split("&");
258             
259             for (int i = 0; i < p.length; i++) {
260                 String JavaDoc[] v = p[i].split("=");
261                 
262                 String JavaDoc name = (String JavaDoc)fieldsMap.get(v[0]);
263                 if (name != null) {
264                     Field JavaDoc field = this.getClass().getDeclaredField(name);
265                     if (field != null && v[1] != null && !v[1].equals("")) {
266                         field.set(this, v[1]);
267                     }
268                 }
269             }
270         }
271
272         StringBuffer JavaDoc path = new StringBuffer JavaDoc(512);
273         path.append(this.request.getContextPath()).append("/jforum"
274                 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)
275                 + "?module=search&action=search&clean=1");
276         
277         if (this.forum != null) {
278             path.append("&search_forum=").append(this.forum);
279         }
280         
281         if (this.searchTerms != null) {
282             path.append("&search_terms=").append(this.searchTerms);
283         }
284         
285         if (this.category != null) {
286             path.append("&search_cat=").append(this.category);
287         }
288         
289         if (this.sortDir != null) {
290             path.append("&sort_dir=").append(this.sortDir);
291         }
292         
293         if (this.sortBy != null) {
294             path.append("&sort_by=").append(this.sortBy);
295         }
296         
297         if (this.kw != null) {
298             path.append("&search_keywords=").append(this.kw);
299         }
300         
301         if (this.postTime != null) {
302             path.append("&post_time=").append(this.postTime);
303         }
304         
305         path.append("&start=").append(ViewCommon.getStartPage());
306         
307         return path.toString();
308     }
309     
310     private String JavaDoc addSlashes(String JavaDoc s)
311     {
312         if (s != null) {
313             s = s.replaceAll("'", "\\'");
314             s = s.replaceAll("\"", "\\\"");
315         }
316         
317         return s;
318     }
319     
320     /**
321      * @see net.jforum.Command#list()
322      */

323     public void list() throws Exception JavaDoc
324     {
325         this.filters();
326     }
327 }
328
Popular Tags