KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > SimpleSearchServlet


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

40 package org.dspace.app.webui.servlet;
41
42 import java.io.IOException JavaDoc;
43 import java.net.URLEncoder JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49
50 import javax.servlet.ServletException JavaDoc;
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53
54 import org.apache.log4j.Logger;
55 import org.dspace.app.webui.util.JSPManager;
56 import org.dspace.app.webui.util.UIUtil;
57 import org.dspace.authorize.AuthorizeException;
58 import org.dspace.content.Collection;
59 import org.dspace.content.Community;
60 import org.dspace.content.Item;
61 import org.dspace.core.Constants;
62 import org.dspace.core.Context;
63 import org.dspace.core.LogManager;
64 import org.dspace.handle.HandleManager;
65 import org.dspace.search.DSQuery;
66 import org.dspace.search.QueryArgs;
67 import org.dspace.search.QueryResults;
68
69 /**
70  * Servlet for handling a simple search.
71  * <p>
72  * All metadata is search for the value contained in the "query" parameter. If
73  * the "location" parameter is present, the user's location is switched to that
74  * location using a redirect. Otherwise, the user's current location is used to
75  * constrain the query; i.e., if the user is "in" a collection, only results
76  * from the collection will be returned.
77  * <p>
78  * The value of the "location" parameter should be ALL (which means no
79  * location), a the ID of a community (e.g. "123"), or a community ID, then a
80  * slash, then a collection ID, e.g. "123/456".
81  *
82  * @author Robert Tansley
83  * @version $Id: SimpleSearchServlet.java,v 1.17 2004/12/15 15:21:10 jimdowning
84  * Exp $
85  */

86 public class SimpleSearchServlet extends DSpaceServlet
87 {
88     /** log4j category */
89     private static Logger log = Logger.getLogger(SimpleSearchServlet.class);
90
91     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
92             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
93             SQLException JavaDoc, AuthorizeException
94     {
95         // Get the query
96
String JavaDoc query = request.getParameter("query");
97         int start = UIUtil.getIntParameter(request, "start");
98         String JavaDoc advanced = request.getParameter("advanced");
99         String JavaDoc fromAdvanced = request.getParameter("from_advanced");
100         String JavaDoc advancedQuery = "";
101         HashMap JavaDoc queryHash = new HashMap JavaDoc();
102
103         // can't start earlier than 0 in the results!
104
if (start < 0)
105         {
106             start = 0;
107         }
108
109         List JavaDoc itemHandles = new ArrayList JavaDoc();
110         List JavaDoc collectionHandles = new ArrayList JavaDoc();
111         List JavaDoc communityHandles = new ArrayList JavaDoc();
112
113         Item[] resultsItems;
114         Collection[] resultsCollections;
115         Community[] resultsCommunities;
116
117         QueryResults qResults = null;
118         QueryArgs qArgs = new QueryArgs();
119
120         // if the "advanced" flag is set, build the query string from the
121
// multiple query fields
122
if (advanced != null)
123         {
124             query = qArgs.buildQuery(request);
125             advancedQuery = qArgs.buildHTTPQuery(request);
126         }
127
128         // Ensure the query is non-null
129
if (query == null)
130         {
131             query = "";
132         }
133
134         // Get the location parameter, if any
135
String JavaDoc location = request.getParameter("location");
136         String JavaDoc newURL;
137
138         // If there is a location parameter, we should redirect to
139
// do the search with the correct location.
140
if ((location != null) && !location.equals(""))
141         {
142             String JavaDoc url = "";
143
144             if (!location.equals("/"))
145             {
146                 // Location is a Handle
147
url = "/handle/" + location;
148             }
149
150             // Encode the query
151
query = URLEncoder.encode(query, Constants.DEFAULT_ENCODING);
152
153             if (advancedQuery.length() > 0)
154             {
155                 query = query + "&from_advanced=true&" + advancedQuery;
156             }
157
158             // Do the redirect
159
response.sendRedirect(response.encodeRedirectURL(request
160                     .getContextPath()
161                     + url + "/simple-search?query=" + query));
162
163             return;
164         }
165
166         // Build log information
167
String JavaDoc logInfo = "";
168
169         // Get our location
170
Community community = UIUtil.getCommunityLocation(request);
171         Collection collection = UIUtil.getCollectionLocation(request);
172
173         // get the start of the query results page
174
// List resultObjects = null;
175
qArgs.setQuery(query);
176         qArgs.setStart(start);
177
178         // Perform the search
179
if (collection != null)
180         {
181             logInfo = "collection_id=" + collection.getID() + ",";
182
183             // Values for drop-down box
184
request.setAttribute("community", community);
185             request.setAttribute("collection", collection);
186
187             qResults = DSQuery.doQuery(context, qArgs, collection);
188         }
189         else if (community != null)
190         {
191             logInfo = "community_id=" + community.getID() + ",";
192
193             request.setAttribute("community", community);
194
195             // Get the collections within the community for the dropdown box
196
request
197                     .setAttribute("collection.array", community
198                             .getCollections());
199
200             qResults = DSQuery.doQuery(context, qArgs, community);
201         }
202         else
203         {
204             // Get all communities for dropdown box
205
Community[] communities = Community.findAll(context);
206             request.setAttribute("community.array", communities);
207
208             qResults = DSQuery.doQuery(context, qArgs);
209         }
210
211         // now instantiate the results and put them in their buckets
212
for (int i = 0; i < qResults.getHitHandles().size(); i++)
213         {
214             String JavaDoc myHandle = (String JavaDoc) qResults.getHitHandles().get(i);
215             Integer JavaDoc myType = (Integer JavaDoc) qResults.getHitTypes().get(i);
216
217             // add the handle to the appropriate lists
218
switch (myType.intValue())
219             {
220             case Constants.ITEM:
221                 itemHandles.add(myHandle);
222
223                 break;
224
225             case Constants.COLLECTION:
226                 collectionHandles.add(myHandle);
227
228                 break;
229
230             case Constants.COMMUNITY:
231                 communityHandles.add(myHandle);
232
233                 break;
234             }
235         }
236
237         int numCommunities = communityHandles.size();
238         int numCollections = collectionHandles.size();
239         int numItems = itemHandles.size();
240
241         // Make objects from the handles - make arrays, fill them out
242
resultsCommunities = new Community[numCommunities];
243         resultsCollections = new Collection[numCollections];
244         resultsItems = new Item[numItems];
245
246         for (int i = 0; i < numItems; i++)
247         {
248             String JavaDoc myhandle = (String JavaDoc) itemHandles.get(i);
249
250             Object JavaDoc o = HandleManager.resolveToObject(context, myhandle);
251
252             resultsItems[i] = (Item) o;
253
254             if (resultsItems[i] == null)
255             {
256                 throw new SQLException JavaDoc("Query \"" + query
257                         + "\" returned unresolvable handle: " + myhandle);
258             }
259         }
260
261         for (int i = 0; i < collectionHandles.size(); i++)
262         {
263             String JavaDoc myhandle = (String JavaDoc) collectionHandles.get(i);
264
265             Object JavaDoc o = HandleManager.resolveToObject(context, myhandle);
266
267             resultsCollections[i] = (Collection) o;
268
269             if (resultsCollections[i] == null)
270             {
271                 throw new SQLException JavaDoc("Query \"" + query
272                         + "\" returned unresolvable handle: " + myhandle);
273             }
274         }
275
276         for (int i = 0; i < communityHandles.size(); i++)
277         {
278             String JavaDoc myhandle = (String JavaDoc) communityHandles.get(i);
279
280             Object JavaDoc o = HandleManager.resolveToObject(context, myhandle);
281
282             resultsCommunities[i] = (Community) o;
283
284             if (resultsCommunities[i] == null)
285             {
286                 throw new SQLException JavaDoc("Query \"" + query
287                         + "\" returned unresolvable handle: " + myhandle);
288             }
289         }
290
291         // Log
292
log.info(LogManager.getHeader(context, "search", logInfo + "query=\""
293                 + query + "\",results=(" + resultsCommunities.length + ","
294                 + resultsCollections.length + "," + resultsItems.length + ")"));
295
296         // Pass in some page qualities
297
// total number of pages
298
int pageTotal = 1 + ((qResults.getHitCount() - 1) / qResults
299                 .getPageSize());
300
301         // current page being displayed
302
int pageCurrent = 1 + (qResults.getStart() / qResults.getPageSize());
303
304         // pageLast = min(pageCurrent+9,pageTotal)
305
int pageLast = ((pageCurrent + 9) > pageTotal) ? pageTotal
306                 : (pageCurrent + 9);
307
308         // pageFirst = max(1,pageCurrent-9)
309
int pageFirst = ((pageCurrent - 9) > 1) ? (pageCurrent - 9) : 1;
310
311         // Pass the results to the display JSP
312
request.setAttribute("items", resultsItems);
313         request.setAttribute("communities", resultsCommunities);
314         request.setAttribute("collections", resultsCollections);
315
316         request.setAttribute("pagetotal", new Integer JavaDoc(pageTotal));
317         request.setAttribute("pagecurrent", new Integer JavaDoc(pageCurrent));
318         request.setAttribute("pagelast", new Integer JavaDoc(pageLast));
319         request.setAttribute("pagefirst", new Integer JavaDoc(pageFirst));
320
321         request.setAttribute("queryresults", qResults);
322
323         // And the original query string
324
request.setAttribute("query", query);
325
326         if ((fromAdvanced != null) && (qResults.getHitCount() == 0))
327         {
328             // send back to advanced form if no results
329
Community[] communities = Community.findAll(context);
330             request.setAttribute("communities", communities);
331             request.setAttribute("no_results", "yes");
332
333             queryHash = qArgs.buildQueryHash(request);
334
335             Iterator JavaDoc i = queryHash.keySet().iterator();
336
337             while (i.hasNext())
338             {
339                 String JavaDoc key = (String JavaDoc) i.next();
340                 String JavaDoc value = (String JavaDoc) queryHash.get(key);
341
342                 request.setAttribute(key, value);
343             }
344
345             JSPManager.showJSP(request, response, "/search/advanced.jsp");
346         }
347         else
348         {
349             JSPManager.showJSP(request, response, "/search/results.jsp");
350         }
351     }
352 }
353
Popular Tags