KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > search > QueryArgs


1 /*
2  * QueryArgs.java
3  *
4  * Version: $Revision: 1.12 $
5  *
6  * Date: $Date: 2006/09/01 14:31:46 $
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.search;
41
42 import java.io.UnsupportedEncodingException JavaDoc;
43 import java.net.URLEncoder JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48
49 import org.dspace.core.Constants;
50
51 import org.apache.oro.text.perl.Perl5Util;
52
53 /**
54  * Contains the arguments for a query. Fill it out and pass to the query engine
55  */

56 public class QueryArgs
57 {
58     // the query string
59
private String JavaDoc query;
60
61     // start and count defines a search 'cursor' or page
62
// query will return 'count' hits beginning at offset 'start'
63
private int start = 0; // default values
64

65     private int pageSize = 10;
66
67     /**
68      * set the query string
69      *
70      * @param newQuery
71      */

72     public void setQuery(String JavaDoc newQuery)
73     {
74         query = newQuery;
75     }
76
77     /**
78      * retrieve the query string
79      *
80      * @return the current query string
81      */

82     public String JavaDoc getQuery()
83     {
84         return query;
85     }
86
87     /**
88      * set the offset of the desired search results, beginning with 0 ; used to
89      * page results (the default value is 0)
90      *
91      * @param newStart
92      * index of first desired result
93      */

94     public void setStart(int newStart)
95     {
96         start = newStart;
97     }
98
99     /**
100      * read the search's starting offset
101      *
102      * @return current index of first desired result
103      */

104     public int getStart()
105     {
106         return start;
107     }
108
109     /**
110      * set the count of hits to return; used to implement paged searching see
111      * the initializer for the default value
112      *
113      * @param newSize
114      * number of hits per page
115      */

116     public void setPageSize(int newSize)
117     {
118         pageSize = newSize;
119     }
120
121     /**
122      * get the count of hits to return
123      *
124      * @return number of results per page
125      */

126     public int getPageSize()
127     {
128         return pageSize;
129     }
130
131     /**
132      * Builds an advanced-query description string.
133      *
134      * The string is built using the passed in values
135      * query{1,2,3}, field{1,2,3} and conjunction{1,2} taken from
136      * the parameter request.
137      *
138      * @param request the request object to take the values from
139      *
140      * @return the query description string built
141      */

142     public String JavaDoc buildQuery(HttpServletRequest JavaDoc request)
143     {
144         String JavaDoc newquery = "(";
145         String JavaDoc numFieldStr = request.getParameter("num_search_field");
146         // for backward compatibility
147
if (numFieldStr == null) numFieldStr ="3";
148         int numField = Integer.parseInt(numFieldStr);
149         ArrayList JavaDoc query = new ArrayList JavaDoc();
150         ArrayList JavaDoc field = new ArrayList JavaDoc();
151         ArrayList JavaDoc conjunction = new ArrayList JavaDoc();
152         
153         for (int i = 1; i <= numField; i++)
154         {
155             String JavaDoc tmp_query = request.getParameter("query"+i).trim();
156             String JavaDoc tmp_field = request.getParameter("field"+i).trim();
157             if (tmp_query != null && !tmp_query.equals(""))
158             {
159                 query.add(tmp_query);
160                 if (tmp_field == null)
161                     field.add("ANY");
162                 else
163                     field.add(tmp_field);
164                 if (i != numField)
165                 {
166                     conjunction.add(request.getParameter("conjunction"+i) != null?
167                             request.getParameter("conjunction"+i):"AND");
168                 }
169             }
170         }
171         Iterator JavaDoc iquery = query.iterator();
172         Iterator JavaDoc ifield = field.iterator();
173         Iterator JavaDoc iconj = conjunction.iterator();
174         
175         String JavaDoc conj_curr = "";
176         while (iquery.hasNext())
177         { newquery = newquery + conj_curr;
178             String JavaDoc query_curr = (String JavaDoc) iquery.next();
179             String JavaDoc field_curr = (String JavaDoc) ifield.next();
180             newquery = newquery + buildQueryPart(query_curr,field_curr);
181             if (iconj.hasNext())
182             {
183                 conj_curr = " " + (String JavaDoc)iconj.next() + " ";
184             }
185         }
186         
187         newquery = newquery + ")";
188         return (newquery);
189     }
190
191     /**
192      * Builds a query-part using the field and value passed in
193      * with ' --&gt; " (single to double quote) translation.
194      *
195      * @param myquery the value the query will look for
196      * @param myfield the field myquery will be looked for in
197      *
198      * @return the query created
199      */

200     private String JavaDoc buildQueryPart(String JavaDoc myquery, String JavaDoc myfield)
201     {
202         Perl5Util util = new Perl5Util();
203         String JavaDoc newquery = "(";
204
205         if (!myfield.equals("ANY"))
206         {
207             newquery = newquery + myfield + ":";
208             myquery = util.substitute("s/\'(.*)\'/\"$1\"/g", myquery);
209
210             if (!util.match("/\".*\"/", myquery))
211             {
212                 myquery = util.substitute("s/ / " + myfield + ":/g", myquery);
213             }
214         }
215
216         newquery = newquery + myquery + ")";
217
218         return (newquery);
219     }
220
221     /**
222      * Constructs a HashMap with the keys field{1,2,3}, query{1,2,3} and
223      * conjunction{1,2} taking the values from the passed-in argument
224      * defaulting to "".
225      *
226      * @param request the request-describing object to take the values from
227      *
228      * @return the created HashMap
229      */

230     public HashMap JavaDoc buildQueryHash(HttpServletRequest JavaDoc request)
231     {
232         HashMap JavaDoc queryHash = new HashMap JavaDoc();
233         String JavaDoc numFieldStr = request.getParameter("num_search_field");
234         // for backward compatibility
235
if (numFieldStr == null) numFieldStr = "3";
236         int numField = Integer.parseInt(numFieldStr);
237         for (int i = 1; i < numField; i++)
238         {
239             queryHash.put("query"+i, (request.getParameter("query"+i) == null) ? ""
240                     : request.getParameter("query"+i));
241             queryHash.put("field"+i,
242                     (request.getParameter("field"+i) == null) ? "ANY" : request
243                             .getParameter("field"+i));
244             queryHash.put("conjunction"+i,
245                     (request.getParameter("conjunction"+i) == null) ? "AND"
246                             : request.getParameter("conjunction"+i));
247         }
248         
249         queryHash.put("query"+numField, (request.getParameter("query"+numField) == null) ? ""
250                 : request.getParameter("query"+numField));
251         
252         queryHash.put("field"+numField,
253                 (request.getParameter("field"+numField) == null) ? "ANY"
254                 : request.getParameter("field"+numField));
255         
256         return (queryHash);
257     }
258
259     /**
260      * Builds an HTTP query string for some parameters with the value
261      * taken from the request context passed in.
262      *
263      * The returned string includes key/value pairs in the HTTP query string
264      * format (key1=value1&amp;key2=value2...) for the keys query{1,2,3},
265      * field{1,2,3} and conjunction{1,2} with values taken from request
266      * and defaulting to "".
267      * <P>
268      * Note, that the values are url-encoded using the UTF-8 encoding scheme
269      * as the corresponding W3C recommendation states.
270      * <P>
271      * Also note that neither leading ? (question mark)
272      * nor leading &amp; (ampersand mark) is included.
273      * Take this into account when appending to a real URL.
274      *
275      * @param request the request object to take the values from
276      *
277      * @return the query string that can be used without further
278      * transformationin URLs
279      *
280      */

281     public String JavaDoc buildHTTPQuery(HttpServletRequest JavaDoc request)
282             throws UnsupportedEncodingException JavaDoc
283     {
284         String JavaDoc querystring = "";
285         HashMap JavaDoc queryHash = buildQueryHash(request);
286
287         Iterator JavaDoc i = queryHash.keySet().iterator();
288
289         while (i.hasNext())
290         {
291             String JavaDoc key = (String JavaDoc) i.next();
292             String JavaDoc value = (String JavaDoc) queryHash.get(key);
293
294             querystring = querystring + "&" + key + "="
295                     + URLEncoder.encode(value, Constants.DEFAULT_ENCODING);
296         }
297         if (request.getParameter("num_search_field") != null)
298         {
299             querystring = querystring + "&num_search_field="+request.getParameter("num_search_field");
300         }
301
302         // return the result with the leading "&" removed
303
return (querystring.substring(1));
304     }
305 }
306
Popular Tags