1 40 package org.dspace.app.webui.servlet; 41 42 import java.io.IOException ; 43 import java.net.URLEncoder ; 44 import java.sql.SQLException ; 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 50 import javax.servlet.ServletException ; 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpServletResponse ; 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 86 public class SimpleSearchServlet extends DSpaceServlet 87 { 88 89 private static Logger log = Logger.getLogger(SimpleSearchServlet.class); 90 91 protected void doDSGet(Context context, HttpServletRequest request, 92 HttpServletResponse response) throws ServletException , IOException , 93 SQLException , AuthorizeException 94 { 95 String query = request.getParameter("query"); 97 int start = UIUtil.getIntParameter(request, "start"); 98 String advanced = request.getParameter("advanced"); 99 String fromAdvanced = request.getParameter("from_advanced"); 100 String advancedQuery = ""; 101 HashMap queryHash = new HashMap (); 102 103 if (start < 0) 105 { 106 start = 0; 107 } 108 109 List itemHandles = new ArrayList (); 110 List collectionHandles = new ArrayList (); 111 List communityHandles = new ArrayList (); 112 113 Item[] resultsItems; 114 Collection[] resultsCollections; 115 Community[] resultsCommunities; 116 117 QueryResults qResults = null; 118 QueryArgs qArgs = new QueryArgs(); 119 120 if (advanced != null) 123 { 124 query = qArgs.buildQuery(request); 125 advancedQuery = qArgs.buildHTTPQuery(request); 126 } 127 128 if (query == null) 130 { 131 query = ""; 132 } 133 134 String location = request.getParameter("location"); 136 String newURL; 137 138 if ((location != null) && !location.equals("")) 141 { 142 String url = ""; 143 144 if (!location.equals("/")) 145 { 146 url = "/handle/" + location; 148 } 149 150 query = URLEncoder.encode(query, Constants.DEFAULT_ENCODING); 152 153 if (advancedQuery.length() > 0) 154 { 155 query = query + "&from_advanced=true&" + advancedQuery; 156 } 157 158 response.sendRedirect(response.encodeRedirectURL(request 160 .getContextPath() 161 + url + "/simple-search?query=" + query)); 162 163 return; 164 } 165 166 String logInfo = ""; 168 169 Community community = UIUtil.getCommunityLocation(request); 171 Collection collection = UIUtil.getCollectionLocation(request); 172 173 qArgs.setQuery(query); 176 qArgs.setStart(start); 177 178 if (collection != null) 180 { 181 logInfo = "collection_id=" + collection.getID() + ","; 182 183 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 request 197 .setAttribute("collection.array", community 198 .getCollections()); 199 200 qResults = DSQuery.doQuery(context, qArgs, community); 201 } 202 else 203 { 204 Community[] communities = Community.findAll(context); 206 request.setAttribute("community.array", communities); 207 208 qResults = DSQuery.doQuery(context, qArgs); 209 } 210 211 for (int i = 0; i < qResults.getHitHandles().size(); i++) 213 { 214 String myHandle = (String ) qResults.getHitHandles().get(i); 215 Integer myType = (Integer ) qResults.getHitTypes().get(i); 216 217 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 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 myhandle = (String ) itemHandles.get(i); 249 250 Object o = HandleManager.resolveToObject(context, myhandle); 251 252 resultsItems[i] = (Item) o; 253 254 if (resultsItems[i] == null) 255 { 256 throw new SQLException ("Query \"" + query 257 + "\" returned unresolvable handle: " + myhandle); 258 } 259 } 260 261 for (int i = 0; i < collectionHandles.size(); i++) 262 { 263 String myhandle = (String ) collectionHandles.get(i); 264 265 Object o = HandleManager.resolveToObject(context, myhandle); 266 267 resultsCollections[i] = (Collection) o; 268 269 if (resultsCollections[i] == null) 270 { 271 throw new SQLException ("Query \"" + query 272 + "\" returned unresolvable handle: " + myhandle); 273 } 274 } 275 276 for (int i = 0; i < communityHandles.size(); i++) 277 { 278 String myhandle = (String ) communityHandles.get(i); 279 280 Object o = HandleManager.resolveToObject(context, myhandle); 281 282 resultsCommunities[i] = (Community) o; 283 284 if (resultsCommunities[i] == null) 285 { 286 throw new SQLException ("Query \"" + query 287 + "\" returned unresolvable handle: " + myhandle); 288 } 289 } 290 291 log.info(LogManager.getHeader(context, "search", logInfo + "query=\"" 293 + query + "\",results=(" + resultsCommunities.length + "," 294 + resultsCollections.length + "," + resultsItems.length + ")")); 295 296 int pageTotal = 1 + ((qResults.getHitCount() - 1) / qResults 299 .getPageSize()); 300 301 int pageCurrent = 1 + (qResults.getStart() / qResults.getPageSize()); 303 304 int pageLast = ((pageCurrent + 9) > pageTotal) ? pageTotal 306 : (pageCurrent + 9); 307 308 int pageFirst = ((pageCurrent - 9) > 1) ? (pageCurrent - 9) : 1; 310 311 request.setAttribute("items", resultsItems); 313 request.setAttribute("communities", resultsCommunities); 314 request.setAttribute("collections", resultsCollections); 315 316 request.setAttribute("pagetotal", new Integer (pageTotal)); 317 request.setAttribute("pagecurrent", new Integer (pageCurrent)); 318 request.setAttribute("pagelast", new Integer (pageLast)); 319 request.setAttribute("pagefirst", new Integer (pageFirst)); 320 321 request.setAttribute("queryresults", qResults); 322 323 request.setAttribute("query", query); 325 326 if ((fromAdvanced != null) && (qResults.getHitCount() == 0)) 327 { 328 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 i = queryHash.keySet().iterator(); 336 337 while (i.hasNext()) 338 { 339 String key = (String ) i.next(); 340 String value = (String ) 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 |