1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
|
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 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | public class SearchAction extends Action { |
58 | |
|
59 | |
|
60 | 0 | private static final Log log = LogFactory.getLog(SearchAction.class); |
61 | |
|
62 | |
|
63 | |
static final int PAGE_SIZE = 20; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
74 | |
|
75 | 0 | Blog blog = (Blog)getModel().get(Constants.BLOG_KEY); |
76 | 0 | String query = request.getParameter("query"); |
77 | |
|
78 | 0 | if (query == null || query.trim().length() == 0) { |
79 | 0 | if (blog instanceof Blog) { |
80 | 0 | return new AdvancedSearchView(); |
81 | |
} |
82 | |
} |
83 | |
|
84 | 0 | String pageAsString = request.getParameter("page"); |
85 | 0 | int page = 1; |
86 | 0 | if (pageAsString == null || pageAsString.length() == 0) { |
87 | 0 | page = 1; |
88 | |
} else { |
89 | |
try { |
90 | 0 | page = Integer.parseInt(pageAsString); |
91 | 0 | } catch (NumberFormatException nfe) { |
92 | 0 | } |
93 | |
} |
94 | |
|
95 | |
try { |
96 | 0 | SearchResults results = blog.getSearchIndex().search(query); |
97 | |
|
98 | 0 | if (results.getNumberOfHits() == 1) { |
99 | |
|
100 | |
|
101 | 0 | SearchHit hit = (SearchHit)results.getHits().get(0); |
102 | 0 | return new RedirectView(hit.getPermalink()); |
103 | |
} else { |
104 | |
|
105 | 0 | String sort = request.getParameter("sort"); |
106 | 0 | if (sort != null && sort.equalsIgnoreCase("date")) { |
107 | 0 | results.sortByDateDescending(); |
108 | |
} else { |
109 | 0 | results.sortByScoreDescending(); |
110 | |
} |
111 | |
|
112 | 0 | Pageable pageable = new Pageable(results.getHits()); |
113 | 0 | pageable.setPageSize(PAGE_SIZE); |
114 | 0 | pageable.setPage(page); |
115 | |
|
116 | |
try { |
117 | 0 | getModel().put("searchResults", results); |
118 | 0 | getModel().put("pageable", pageable); |
119 | 0 | getModel().put("query", java.net.URLEncoder.encode(query, blog.getCharacterEncoding())); |
120 | 0 | } catch (UnsupportedEncodingException uee) { |
121 | 0 | log.error(uee); |
122 | 0 | } |
123 | |
|
124 | 0 | return new SearchResultsView(); |
125 | |
} |
126 | 0 | } catch (SearchException se) { |
127 | 0 | throw new ServletException(se); |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
} |