File Source: searchaction.java
/*
P/P * Method: net.sourceforge.pebble.web.action.SearchAction__static_init
*
* Postconditions:
* init'ed(log)
*/
1 /*
2 * Copyright (c) 2003-2006, Simon Brown
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * - Neither the name of Pebble nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.sourceforge.pebble.web.action;
33
34 import net.sourceforge.pebble.Constants;
35 import net.sourceforge.pebble.domain.Blog;
36 import net.sourceforge.pebble.search.SearchException;
37 import net.sourceforge.pebble.search.SearchHit;
38 import net.sourceforge.pebble.search.SearchResults;
39 import net.sourceforge.pebble.util.Pageable;
40 import net.sourceforge.pebble.web.view.RedirectView;
41 import net.sourceforge.pebble.web.view.View;
42 import net.sourceforge.pebble.web.view.impl.AdvancedSearchView;
43 import net.sourceforge.pebble.web.view.impl.SearchResultsView;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
/*
P/P * Method: void net.sourceforge.pebble.web.action.SearchAction()
*/
47 import javax.servlet.ServletException;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import java.io.UnsupportedEncodingException;
51
52 /**
53 * Performs a search on the current blog.
54 *
55 * @author Simon Brown
56 */
57 public class SearchAction extends Action {
58
59 /** the log used for this action */
60 private static final Log log = LogFactory.getLog(SearchAction.class);
61
62 /** the number of results to show per page */
63 static final int PAGE_SIZE = 20;
64
65
66 /**
67 * Peforms the processing associated with this action.
68 *
69 * @param request the HttpServletRequest instance
70 * @param response the HttpServletResponse instance
71 * @return the name of the next view
72 */
73 public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
74
/*
P/P * Method: View process(HttpServletRequest, HttpServletResponse)
*
* Preconditions:
* request != null
* this.model != null
* this.model.data != null
*
* Presumptions:
* java.util.HashMap:get(...)@63 != null
* java.util.List:get(...)@101 != null
* net.sourceforge.pebble.domain.Blog:getSearchIndex(...)@96 != null
* net.sourceforge.pebble.index.SearchIndex:search(...)@96 != null
* org.apache.commons.logging.LogFactory:getLog(...)@60 != null
* ...
*
* Postconditions:
* return_value in Addr_Set{&new AdvancedSearchView(process#1),&new SearchResultsView(process#4),&new RedirectView(process#2)}
* new AdvancedSearchView(process#1) num objects <= 1
* new RedirectView(process#2) num objects <= 1
* new SearchResultsView(process#4) num objects <= 1
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@106: {0}, {1}
* java.lang.String:length(...)@78: {1..232-1}, {0}
* java.lang.String:length(...)@86: {1..232-1}, {0}
* java.util.List:size(...)@112: {-231..0, 2..232-1}, {1}
* javax.servlet.http.HttpServletRequest:getParameter(...)@105: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@76: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@84: Addr_Set{null}, Inverse{null}
*/
75 Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
76 String query = request.getParameter("query");
77
78 if (query == null || query.trim().length() == 0) {
79 if (blog instanceof Blog) {
80 return new AdvancedSearchView();
81 }
82 }
83
84 String pageAsString = request.getParameter("page");
85 int page = 1;
86 if (pageAsString == null || pageAsString.length() == 0) {
87 page = 1;
88 } else {
89 try {
90 page = Integer.parseInt(pageAsString);
91 } catch (NumberFormatException nfe) {
92 }
93 }
94
95 try {
96 SearchResults results = blog.getSearchIndex().search(query);
97
98 if (results.getNumberOfHits() == 1) {
99 // if there is only one hit, redirect the user to it without the
100 // search results page
101 SearchHit hit = (SearchHit)results.getHits().get(0);
102 return new RedirectView(hit.getPermalink());
103 } else {
104 // show all results on the search results page
105 String sort = request.getParameter("sort");
106 if (sort != null && sort.equalsIgnoreCase("date")) {
107 results.sortByDateDescending();
108 } else {
109 results.sortByScoreDescending();
110 }
111
112 Pageable pageable = new Pageable(results.getHits());
113 pageable.setPageSize(PAGE_SIZE);
114 pageable.setPage(page);
115
116 try {
117 getModel().put("searchResults", results);
118 getModel().put("pageable", pageable);
119 getModel().put("query", java.net.URLEncoder.encode(query, blog.getCharacterEncoding()));
120 } catch (UnsupportedEncodingException uee) {
121 log.error(uee);
122 }
123
124 return new SearchResultsView();
125 }
126 } catch (SearchException se) {
127 throw new ServletException(se);
128 }
129 }
130
131 }
SofCheck Inspector Build Version : 2.22510
| searchaction.java |
2010-Jun-25 19:40:34 |
| searchaction.class |
2010-Jul-19 20:23:38 |