1 34 package org.dspace.app.webui.servlet; 35 36 import java.io.IOException ; 37 import java.net.URLEncoder ; 38 import java.sql.SQLException ; 39 import java.util.ArrayList ; 40 import java.util.Enumeration ; 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Vector ; 45 46 import javax.servlet.ServletException ; 47 import javax.servlet.http.HttpServletRequest ; 48 import javax.servlet.http.HttpServletResponse ; 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 72 public class ControlledVocabularySearchServlet extends DSpaceServlet 73 { 74 private static Logger log = Logger 76 .getLogger(ControlledVocabularySearchServlet.class); 77 78 private static final String SEARCH_JSP = "/controlledvocabulary/search.jsp"; 80 81 private static final String RESULTS_JSP = "/controlledvocabulary/results.jsp"; 83 84 87 protected void doDSGet(Context context, HttpServletRequest request, 88 HttpServletResponse response) throws ServletException , IOException , 89 SQLException , AuthorizeException 90 { 91 92 String action = request.getParameter("action") == null ? "" : request 93 .getParameter("action"); 94 95 if (action.equals("search")) 96 { 97 List keywords = extractKeywords(request); 98 String 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 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 122 private List extractKeywords(HttpServletRequest request) 123 { 124 List keywords = new Vector (); 125 Enumeration enumeration = request.getParameterNames(); 126 while (enumeration.hasMoreElements()) 127 { 128 String element = (String ) enumeration.nextElement(); 129 if (element.startsWith("cb_")) 130 { 131 keywords.add("\"" + request.getParameter(element) + "\""); 132 } 133 } 134 return keywords; 135 } 136 137 149 private void doSearch(Context context, HttpServletRequest request, 150 String query) throws IOException , SQLException 151 { 152 int start = UIUtil.getIntParameter(request, "start"); 155 String advanced = request.getParameter("advanced"); 156 String fromAdvanced = request.getParameter("from_advanced"); 157 String advancedQuery = ""; 158 HashMap queryHash = new HashMap (); 159 160 if (start < 0) 162 { 163 start = 0; 164 } 165 166 List itemHandles = new ArrayList (); 167 List collectionHandles = new ArrayList (); 168 List communityHandles = new ArrayList (); 169 170 Item[] resultsItems; 171 Collection[] resultsCollections; 172 Community[] resultsCommunities; 173 174 QueryResults qResults = null; 175 QueryArgs qArgs = new QueryArgs(); 176 177 if (advanced != null) 180 { 181 query = qArgs.buildQuery(request); 182 advancedQuery = qArgs.buildHTTPQuery(request); 183 } 184 185 if (query == null) 187 { 188 query = ""; 189 } 190 191 String location = request.getParameter("location"); 193 String newURL; 194 195 String logInfo = ""; 197 198 Community community = UIUtil.getCommunityLocation(request); 200 Collection collection = UIUtil.getCollectionLocation(request); 201 202 qArgs.setQuery(query); 205 qArgs.setStart(start); 206 207 if (collection != null) 209 { 210 logInfo = "collection_id=" + collection.getID() + ","; 211 212 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 request 226 .setAttribute("collection.array", community 227 .getCollections()); 228 229 qResults = DSQuery.doQuery(context, qArgs, community); 230 } 231 else 232 { 233 Community[] communities = Community.findAll(context); 235 request.setAttribute("community.array", communities); 236 237 qResults = DSQuery.doQuery(context, qArgs); 238 } 239 240 for (int i = 0; i < qResults.getHitHandles().size(); i++) 242 { 243 String myHandle = (String ) qResults.getHitHandles().get(i); 244 Integer myType = (Integer ) qResults.getHitTypes().get(i); 245 246 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 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 myhandle = (String ) itemHandles.get(i); 278 279 Object o = HandleManager.resolveToObject(context, myhandle); 280 281 resultsItems[i] = (Item) o; 282 283 if (resultsItems[i] == null) 284 { 285 throw new SQLException ("Query \"" + query 286 + "\" returned unresolvable handle: " + myhandle); 287 } 288 } 289 290 for (int i = 0; i < collectionHandles.size(); i++) 291 { 292 String myhandle = (String ) collectionHandles.get(i); 293 294 Object o = HandleManager.resolveToObject(context, myhandle); 295 296 resultsCollections[i] = (Collection) o; 297 298 if (resultsCollections[i] == null) 299 { 300 throw new SQLException ("Query \"" + query 301 + "\" returned unresolvable handle: " + myhandle); 302 } 303 } 304 305 for (int i = 0; i < communityHandles.size(); i++) 306 { 307 String myhandle = (String ) communityHandles.get(i); 308 309 Object o = HandleManager.resolveToObject(context, myhandle); 310 311 resultsCommunities[i] = (Community) o; 312 313 if (resultsCommunities[i] == null) 314 { 315 throw new SQLException ("Query \"" + query 316 + "\" returned unresolvable handle: " + myhandle); 317 } 318 } 319 320 log.info(LogManager.getHeader(context, "search", logInfo + "query=\"" 322 + query + "\",results=(" + resultsCommunities.length + "," 323 + resultsCollections.length + "," + resultsItems.length + ")")); 324 325 int pageTotal = 1 + ((qResults.getHitCount() - 1) / qResults 328 .getPageSize()); 329 330 int pageCurrent = 1 + (qResults.getStart() / qResults.getPageSize()); 332 333 int pageLast = ((pageCurrent + 9) > pageTotal) ? pageTotal 335 : (pageCurrent + 9); 336 337 int pageFirst = ((pageCurrent - 9) > 1) ? (pageCurrent - 9) : 1; 339 340 request.setAttribute("items", resultsItems); 342 request.setAttribute("communities", resultsCommunities); 343 request.setAttribute("collections", resultsCollections); 344 345 request.setAttribute("pagetotal", new Integer (pageTotal)); 346 request.setAttribute("pagecurrent", new Integer (pageCurrent)); 347 request.setAttribute("pagelast", new Integer (pageLast)); 348 request.setAttribute("pagefirst", new Integer (pageFirst)); 349 350 request.setAttribute("queryresults", qResults); 351 352 request.setAttribute("query", query); 354 355 } 356 357 367 public static String join(List list, String separator) 368 { 369 String result = ""; 370 Iterator 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 383 protected void doDSPost(Context context, HttpServletRequest request, 384 HttpServletResponse response) throws ServletException , IOException , 385 SQLException , AuthorizeException 386 { 387 doDSGet(context, request, response); 388 } 389 390 } 391 | Popular Tags |