001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 14/01/2004 / 22:02:56
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import java.util.Date;
046:
047: import net.jforum.Command;
048: import net.jforum.context.RequestContext;
049: import net.jforum.context.ResponseContext;
050: import net.jforum.repository.ForumRepository;
051: import net.jforum.search.ContentSearchOperation;
052: import net.jforum.search.NewMessagesSearchOperation;
053: import net.jforum.search.SearchArgs;
054: import net.jforum.search.SearchOperation;
055: import net.jforum.search.SearchResult;
056: import net.jforum.util.I18n;
057: import net.jforum.util.preferences.ConfigKeys;
058: import net.jforum.util.preferences.SystemGlobals;
059: import net.jforum.util.preferences.TemplateKeys;
060: import net.jforum.view.forum.common.TopicsCommon;
061: import net.jforum.view.forum.common.ViewCommon;
062: import freemarker.template.SimpleHash;
063:
064: /**
065: * @author Rafael Steil
066: * @version $Id: SearchAction.java,v 1.57 2007/08/20 19:35:52 rafaelsteil Exp $
067: */
068: public class SearchAction extends Command {
069: public SearchAction() {
070: }
071:
072: public SearchAction(RequestContext request,
073: ResponseContext response, SimpleHash context) {
074: this .request = request;
075: this .response = response;
076: this .context = context;
077: }
078:
079: public void filters() {
080: this .setTemplateName(TemplateKeys.SEARCH_FILTERS);
081: this .context.put("categories", ForumRepository
082: .getAllCategories());
083: this .context.put("pageTitle", I18n
084: .getMessage("ForumBase.search"));
085: }
086:
087: public void newMessages() {
088: this .search(new NewMessagesSearchOperation());
089: }
090:
091: public void search() {
092: this .search(new ContentSearchOperation());
093: }
094:
095: private void search(SearchOperation operation) {
096: SearchArgs args = this .buildSearchArgs();
097:
098: int start = args.startFrom();
099: int recordsPerPage = SystemGlobals
100: .getIntValue(ConfigKeys.TOPICS_PER_PAGE);
101:
102: SearchResult searchResult = operation.performSearch(args);
103: operation.prepareForDisplay();
104:
105: this .setTemplateName(operation.viewTemplate());
106:
107: this .context.put("results", operation.filterResults(operation
108: .results()));
109: this .context.put("categories", ForumRepository
110: .getAllCategories());
111: this .context.put("searchArgs", args);
112: this .context.put("fr", new ForumRepository());
113: this .context.put("pageTitle", I18n
114: .getMessage("ForumBase.search"));
115: this .context.put("openModeration", "1".equals(this .request
116: .getParameter("openModeration")));
117: this .context.put("postsPerPage", new Integer(SystemGlobals
118: .getIntValue(ConfigKeys.POSTS_PER_PAGE)));
119:
120: ViewCommon.contextToPagination(start, searchResult
121: .numberOfHits(), recordsPerPage);
122: TopicsCommon.topicListingBase();
123: }
124:
125: private SearchArgs buildSearchArgs() {
126: SearchArgs args = new SearchArgs();
127:
128: args.setKeywords(this .request.getParameter("search_keywords"));
129:
130: if (this .request.getParameter("search_author") != null) {
131: args.setAuthor(this .request
132: .getIntParameter("search_author"));
133: }
134:
135: args.setOrderBy(this .request.getParameter("sort_by"));
136: args.setOrderDir(this .request.getParameter("sort_dir"));
137: args.startFetchingAtRecord(ViewCommon.getStartPage());
138: args.setMatchType(this .request.getParameter("match_type"));
139:
140: if (this .request.getObjectParameter("from_date") != null
141: && this .request.getObjectParameter("to_date") != null) {
142: args.setDateRange((Date) this .request
143: .getObjectParameter("from_date"),
144: (Date) this .request.getObjectParameter("to_date"));
145: }
146:
147: if ("all".equals(args.getMatchType())) {
148: args.matchAllKeywords();
149: }
150:
151: if (this .request.getParameter("forum") != null) {
152: args.setForumId(this .request.getIntParameter("forum"));
153: }
154:
155: return args;
156: }
157:
158: /**
159: * @see net.jforum.Command#list()
160: */
161: public void list() {
162: this.filters();
163: }
164: }
|