1 40 package org.dspace.search; 41 42 import java.io.UnsupportedEncodingException ; 43 import java.net.URLEncoder ; 44 import java.util.ArrayList ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import javax.servlet.http.HttpServletRequest ; 48 49 import org.dspace.core.Constants; 50 51 import org.apache.oro.text.perl.Perl5Util; 52 53 56 public class QueryArgs 57 { 58 private String query; 60 61 private int start = 0; 65 private int pageSize = 10; 66 67 72 public void setQuery(String newQuery) 73 { 74 query = newQuery; 75 } 76 77 82 public String getQuery() 83 { 84 return query; 85 } 86 87 94 public void setStart(int newStart) 95 { 96 start = newStart; 97 } 98 99 104 public int getStart() 105 { 106 return start; 107 } 108 109 116 public void setPageSize(int newSize) 117 { 118 pageSize = newSize; 119 } 120 121 126 public int getPageSize() 127 { 128 return pageSize; 129 } 130 131 142 public String buildQuery(HttpServletRequest request) 143 { 144 String newquery = "("; 145 String numFieldStr = request.getParameter("num_search_field"); 146 if (numFieldStr == null) numFieldStr ="3"; 148 int numField = Integer.parseInt(numFieldStr); 149 ArrayList query = new ArrayList (); 150 ArrayList field = new ArrayList (); 151 ArrayList conjunction = new ArrayList (); 152 153 for (int i = 1; i <= numField; i++) 154 { 155 String tmp_query = request.getParameter("query"+i).trim(); 156 String 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 iquery = query.iterator(); 172 Iterator ifield = field.iterator(); 173 Iterator iconj = conjunction.iterator(); 174 175 String conj_curr = ""; 176 while (iquery.hasNext()) 177 { newquery = newquery + conj_curr; 178 String query_curr = (String ) iquery.next(); 179 String field_curr = (String ) ifield.next(); 180 newquery = newquery + buildQueryPart(query_curr,field_curr); 181 if (iconj.hasNext()) 182 { 183 conj_curr = " " + (String )iconj.next() + " "; 184 } 185 } 186 187 newquery = newquery + ")"; 188 return (newquery); 189 } 190 191 200 private String buildQueryPart(String myquery, String myfield) 201 { 202 Perl5Util util = new Perl5Util(); 203 String 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 230 public HashMap buildQueryHash(HttpServletRequest request) 231 { 232 HashMap queryHash = new HashMap (); 233 String numFieldStr = request.getParameter("num_search_field"); 234 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 281 public String buildHTTPQuery(HttpServletRequest request) 282 throws UnsupportedEncodingException 283 { 284 String querystring = ""; 285 HashMap queryHash = buildQueryHash(request); 286 287 Iterator i = queryHash.keySet().iterator(); 288 289 while (i.hasNext()) 290 { 291 String key = (String ) i.next(); 292 String value = (String ) 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 (querystring.substring(1)); 304 } 305 } 306 | Popular Tags |