SearchAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.util.Date;
  45. import net.jforum.Command;
  46. import net.jforum.context.RequestContext;
  47. import net.jforum.context.ResponseContext;
  48. import net.jforum.repository.ForumRepository;
  49. import net.jforum.search.ContentSearchOperation;
  50. import net.jforum.search.NewMessagesSearchOperation;
  51. import net.jforum.search.SearchArgs;
  52. import net.jforum.search.SearchOperation;
  53. import net.jforum.search.SearchResult;
  54. import net.jforum.util.I18n;
  55. import net.jforum.util.preferences.ConfigKeys;
  56. import net.jforum.util.preferences.SystemGlobals;
  57. import net.jforum.util.preferences.TemplateKeys;
  58. import net.jforum.view.forum.common.TopicsCommon;
  59. import net.jforum.view.forum.common.ViewCommon;
  60. import freemarker.template.SimpleHash;
  61. /**
  62.  * @author Rafael Steil
  63.  * @version $Id: SearchAction.java,v 1.57 2007/08/20 19:35:52 rafaelsteil Exp $
  64.  */
  65. public class SearchAction extends Command 
  66. {
  67. public SearchAction() { }
  68. public SearchAction(RequestContext request, ResponseContext response, SimpleHash context) 
  69. {
  70. this.request = request;
  71. this.response = response;
  72. this.context = context;
  73. }
  74. public void filters()
  75. {
  76. this.setTemplateName(TemplateKeys.SEARCH_FILTERS);
  77. this.context.put("categories", ForumRepository.getAllCategories());
  78. this.context.put("pageTitle", I18n.getMessage("ForumBase.search"));
  79. }
  80. public void newMessages()
  81. {
  82. this.search(new NewMessagesSearchOperation());
  83. }
  84. public void search()
  85. {
  86. this.search(new ContentSearchOperation());
  87. }
  88. private void search(SearchOperation operation)
  89. {
  90. SearchArgs args = this.buildSearchArgs();
  91. int start = args.startFrom();
  92. int recordsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
  93. SearchResult searchResult = operation.performSearch(args);
  94. operation.prepareForDisplay();
  95. this.setTemplateName(operation.viewTemplate());
  96. this.context.put("results", operation.filterResults(operation.results()));
  97. this.context.put("categories", ForumRepository.getAllCategories());
  98. this.context.put("searchArgs", args);
  99. this.context.put("fr", new ForumRepository());
  100. this.context.put("pageTitle", I18n.getMessage("ForumBase.search"));
  101. this.context.put("openModeration", "1".equals(this.request.getParameter("openModeration")));
  102. this.context.put("postsPerPage", new Integer(SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE)));
  103. ViewCommon.contextToPagination(start, searchResult.numberOfHits(), recordsPerPage);
  104. TopicsCommon.topicListingBase();
  105. }
  106. private SearchArgs buildSearchArgs()
  107. {
  108. SearchArgs args = new SearchArgs();
  109. args.setKeywords(this.request.getParameter("search_keywords"));
  110. if (this.request.getParameter("search_author") != null) {
  111. args.setAuthor(this.request.getIntParameter("search_author"));
  112. }
  113. args.setOrderBy(this.request.getParameter("sort_by"));
  114. args.setOrderDir(this.request.getParameter("sort_dir"));
  115. args.startFetchingAtRecord(ViewCommon.getStartPage());
  116. args.setMatchType(this.request.getParameter("match_type"));
  117. if (this.request.getObjectParameter("from_date") != null
  118. && this.request.getObjectParameter("to_date") != null) {
  119. args.setDateRange((Date)this.request.getObjectParameter("from_date"), 
  120. (Date)this.request.getObjectParameter("to_date"));     
  121. }
  122. if ("all".equals(args.getMatchType())) {
  123. args.matchAllKeywords();
  124. }
  125. if (this.request.getParameter("forum") != null) {
  126. args.setForumId(this.request.getIntParameter("forum"));
  127. }
  128. return args;
  129. }
  130. /** 
  131.  * @see net.jforum.Command#list()
  132.  */
  133. public void list()  
  134. {
  135. this.filters();
  136. }
  137. }