KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 package org.dspace.app.webui.servlet;
35
36 import java.io.IOException JavaDoc;
37 import java.net.URLEncoder JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Vector JavaDoc;
45
46 import javax.servlet.ServletException JavaDoc;
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.http.HttpServletResponse JavaDoc;
49
50 import org.apache.log4j.Logger;
51 import org.dspace.app.webui.util.JSPManager;
52 import org.dspace.app.webui.util.UIUtil;
53 import org.dspace.authorize.AuthorizeException;
54 import org.dspace.content.Collection;
55 import org.dspace.content.Community;
56 import org.dspace.content.Item;
57 import org.dspace.core.Constants;
58 import org.dspace.core.Context;
59 import org.dspace.core.LogManager;
60 import org.dspace.handle.HandleManager;
61 import org.dspace.search.DSQuery;
62 import org.dspace.search.QueryArgs;
63 import org.dspace.search.QueryResults;
64
65 /**
66  * Servlet that provides funcionality for searching the repository using a
67  * controlled vocabulary as a basis for selecting the search keywords.
68  *
69  * @author Miguel Ferreira
70  * @version $Revision: 1.1 $
71  */

72 public class ControlledVocabularySearchServlet extends DSpaceServlet
73 {
74     // the log
75
private static Logger log = Logger
76             .getLogger(ControlledVocabularySearchServlet.class);
77
78     // the jsp that displays the HTML version of controlled-vocabulary
79
private static final String JavaDoc SEARCH_JSP = "/controlledvocabulary/search.jsp";
80
81     // the jsp that will show the search results
82
private static final String JavaDoc RESULTS_JSP = "/controlledvocabulary/results.jsp";
83
84     /**
85      * Handles requests
86      */

87     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
88             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
89             SQLException JavaDoc, AuthorizeException
90     {
91
92         String JavaDoc action = request.getParameter("action") == null ? "" : request
93                 .getParameter("action");
94
95         if (action.equals("search"))
96         {
97             List JavaDoc keywords = extractKeywords(request);
98             String JavaDoc query = join(keywords, " or ");
99             doSearch(context, request, query);
100             JSPManager.showJSP(request, response, RESULTS_JSP);
101         }
102         else if (action.equals("filter"))
103         {
104             String JavaDoc filter = request.getParameter("filter");
105             request.getSession().setAttribute("conceptsearch.filter", filter);
106             JSPManager.showJSP(request, response, SEARCH_JSP);
107         }
108         else
109         {
110             JSPManager.showJSP(request, response, SEARCH_JSP);
111         }
112     }
113
114     /**
115      * Collects the selected terms from the HTML taxonomy displayed on the
116      * search form
117      *
118      * @param request
119      * The HttpServletRequest
120      * @return A Vector with the selected terms from the taxonomy.
121      */

122     private List JavaDoc extractKeywords(HttpServletRequest JavaDoc request)
123     {
124         List JavaDoc keywords = new Vector JavaDoc();
125         Enumeration JavaDoc enumeration = request.getParameterNames();
126         while (enumeration.hasMoreElements())
127         {
128             String JavaDoc element = (String JavaDoc) enumeration.nextElement();
129             if (element.startsWith("cb_"))
130             {
131                 keywords.add("\"" + request.getParameter(element) + "\"");
132             }
133         }
134         return keywords;
135     }
136
137     /**
138      * Searches the repository and and puts the results in the request object
139      *
140      * @param context
141      * The DSpace context
142      * @param request
143      * The request object
144      * @param query
145      * The query expression
146      * @throws IOException
147      * @throws SQLException
148      */

149     private void doSearch(Context context, HttpServletRequest JavaDoc request,
150             String JavaDoc query) throws IOException JavaDoc, SQLException JavaDoc
151     {
152         // Get the query
153
// String query = request.getParameter("query");
154
int start = UIUtil.getIntParameter(request, "start");
155         String JavaDoc advanced = request.getParameter("advanced");
156         String JavaDoc fromAdvanced = request.getParameter("from_advanced");
157         String JavaDoc advancedQuery = "";
158         HashMap JavaDoc queryHash = new HashMap JavaDoc();
159
160         // can't start earlier than 0 in the results!
161
if (start < 0)
162         {
163             start = 0;
164         }
165
166         List JavaDoc itemHandles = new ArrayList JavaDoc();
167         List JavaDoc collectionHandles = new ArrayList JavaDoc();
168         List JavaDoc communityHandles = new ArrayList JavaDoc();
169
170         Item[] resultsItems;
171         Collection[] resultsCollections;
172         Community[] resultsCommunities;
173
174         QueryResults qResults = null;
175         QueryArgs qArgs = new QueryArgs();
176
177         // if the "advanced" flag is set, build the query string from the
178
// multiple query fields
179
if (advanced != null)
180         {
181             query = qArgs.buildQuery(request);
182             advancedQuery = qArgs.buildHTTPQuery(request);
183         }
184
185         // Ensure the query is non-null
186
if (query == null)
187         {
188             query = "";
189         }
190
191         // Get the location parameter, if any
192
String JavaDoc location = request.getParameter("location");
193         String JavaDoc newURL;
194
195         // Build log information
196
String JavaDoc logInfo = "";
197
198         // Get our location
199
Community community = UIUtil.getCommunityLocation(request);
200         Collection collection = UIUtil.getCollectionLocation(request);
201
202         // get the start of the query results page
203
// List resultObjects = null;
204
qArgs.setQuery(query);
205         qArgs.setStart(start);
206
207         // Perform the search
208
if (collection != null)
209         {
210             logInfo = "collection_id=" + collection.getID() + ",";
211
212             // Values for drop-down box
213
request.setAttribute("community", community);
214             request.setAttribute("collection", collection);
215
216             qResults = DSQuery.doQuery(context, qArgs, collection);
217         }
218         else if (community != null)
219         {
220             logInfo = "community_id=" + community.getID() + ",";
221
222             request.setAttribute("community", community);
223
224             // Get the collections within the community for the dropdown box
225
request
226                     .setAttribute("collection.array", community
227                             .getCollections());
228
229             qResults = DSQuery.doQuery(context, qArgs, community);
230         }
231         else
232         {
233             // Get all communities for dropdown box
234
Community[] communities = Community.findAll(context);
235             request.setAttribute("community.array", communities);
236
237             qResults = DSQuery.doQuery(context, qArgs);
238         }
239
240         // now instantiate the results and put them in their buckets
241
for (int i = 0; i < qResults.getHitHandles().size(); i++)
242         {
243             String JavaDoc myHandle = (String JavaDoc) qResults.getHitHandles().get(i);
244             Integer JavaDoc myType = (Integer JavaDoc) qResults.getHitTypes().get(i);
245
246             // add the handle to the appropriate lists
247
switch (myType.intValue())
248             {
249             case Constants.ITEM:
250                 itemHandles.add(myHandle);
251
252                 break;
253
254             case Constants.COLLECTION:
255                 collectionHandles.add(myHandle);
256
257                 break;
258
259             case Constants.COMMUNITY:
260                 communityHandles.add(myHandle);
261
262                 break;
263             }
264         }
265
266         int numCommunities = communityHandles.size();
267         int numCollections = collectionHandles.size();
268         int numItems = itemHandles.size();
269
270         // Make objects from the handles - make arrays, fill them out
271
resultsCommunities = new Community[numCommunities];
272         resultsCollections = new Collection[numCollections];
273         resultsItems = new Item[numItems];
274
275         for (int i = 0; i < numItems; i++)
276         {
277             String JavaDoc myhandle = (String JavaDoc) itemHandles.get(i);
278
279             Object JavaDoc o = HandleManager.resolveToObject(context, myhandle);
280
281             resultsItems[i] = (Item) o;
282
283             if (resultsItems[i] == null)
284             {
285                 throw new SQLException JavaDoc("Query \"" + query
286                         + "\" returned unresolvable handle: " + myhandle);
287             }
288         }
289
290         for (int i = 0; i < collectionHandles.size(); i++)
291         {
292             String JavaDoc myhandle = (String JavaDoc) collectionHandles.get(i);
293
294             Object JavaDoc o = HandleManager.resolveToObject(context, myhandle);
295
296             resultsCollections[i] = (Collection) o;
297
298             if (resultsCollections[i] == null)
299             {
300                 throw new SQLException JavaDoc("Query \"" + query
301                         + "\" returned unresolvable handle: " + myhandle);
302             }
303         }
304
305         for (int i = 0; i < communityHandles.size(); i++)
306         {
307             String JavaDoc myhandle = (String JavaDoc) communityHandles.get(i);
308
309             Object JavaDoc o = HandleManager.resolveToObject(context, myhandle);
310
311             resultsCommunities[i] = (Community) o;
312
313             if (resultsCommunities[i] == null)
314             {
315                 throw new SQLException JavaDoc("Query \"" + query
316                         + "\" returned unresolvable handle: " + myhandle);
317             }
318         }
319
320         // Log
321
log.info(LogManager.getHeader(context, "search", logInfo + "query=\""
322                 + query + "\",results=(" + resultsCommunities.length + ","
323                 + resultsCollections.length + "," + resultsItems.length + ")"));
324
325         // Pass in some page qualities
326
// total number of pages
327
int pageTotal = 1 + ((qResults.getHitCount() - 1) / qResults
328                 .getPageSize());
329
330         // current page being displayed
331
int pageCurrent = 1 + (qResults.getStart() / qResults.getPageSize());
332
333         // pageLast = min(pageCurrent+9,pageTotal)
334
int pageLast = ((pageCurrent + 9) > pageTotal) ? pageTotal
335                 : (pageCurrent + 9);
336
337         // pageFirst = max(1,pageCurrent-9)
338
int pageFirst = ((pageCurrent - 9) > 1) ? (pageCurrent - 9) : 1;
339
340         // Pass the results to the display JSP
341
request.setAttribute("items", resultsItems);
342         request.setAttribute("communities", resultsCommunities);
343         request.setAttribute("collections", resultsCollections);
344
345         request.setAttribute("pagetotal", new Integer JavaDoc(pageTotal));
346         request.setAttribute("pagecurrent", new Integer JavaDoc(pageCurrent));
347         request.setAttribute("pagelast", new Integer JavaDoc(pageLast));
348         request.setAttribute("pagefirst", new Integer JavaDoc(pageFirst));
349
350         request.setAttribute("queryresults", qResults);
351
352         // And the original query string
353
request.setAttribute("query", query);
354
355     }
356
357     /**
358      * Joins each element present in a list with a separator
359      *
360      * @param list
361      * The list of elements
362      * @param separator
363      * The separator that will be used between each element
364      * @return A string with all the elements concatened and separated by the
365      * provided connector
366      */

367     public static String JavaDoc join(List JavaDoc list, String JavaDoc separator)
368     {
369         String JavaDoc result = "";
370         Iterator JavaDoc iterator = list.listIterator();
371         while (iterator.hasNext())
372         {
373             result += iterator.next().toString();
374             if (iterator.hasNext())
375                 result += separator;
376         }
377         return result;
378     }
379
380     /**
381      * Handle posts
382      */

383     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
384             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
385             SQLException JavaDoc, AuthorizeException
386     {
387         doDSGet(context, request, response);
388     }
389
390 }
391
Popular Tags