001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.KernelServiceException;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.libresource.search.LibresourceSearchResult;
042:
043: import org.libresource.web.Controller;
044: import org.libresource.web.SearchResult;
045: import org.libresource.web.config.Config;
046:
047: import java.net.URI;
048:
049: import java.util.Vector;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053:
054: public class SearchUnderController implements Controller {
055: public Object process(URI uri, HttpServletRequest request,
056: HttpServletResponse response) throws Exception {
057: KernelService kernelService = (KernelService) Libresource
058: .getService(KernelConstants.SERVICE);
059:
060: request.setAttribute("search", Boolean.TRUE);
061: request.setAttribute("types", Config.getInstance(null)
062: .listResourceTypeForSearchFilter());
063:
064: String type = request.getParameter("resourceType");
065: request.setAttribute("resourceType", type);
066:
067: if (request.getParameter("query") == null) {
068: request.setAttribute("uriPattern", uri.getPath() + "*");
069:
070: return "/pages/search.jsp";
071: }
072:
073: request.setAttribute("uriPattern", request
074: .getParameter("uriPattern"));
075:
076: // do not search empty string
077: if (request.getParameter("query").trim().length() == 0) {
078: request.setAttribute("error_search",
079: "Cannot search empty string");
080:
081: return "/pages/search.jsp";
082: }
083:
084: String query = request.getParameter("query").replaceAll("<",
085: "<");
086: query = query.replaceAll(">", ">");
087: request.setAttribute("query", query);
088:
089: if (type.toLowerCase().equals("all")) {
090: type = null;
091: }
092:
093: LibresourceSearchResult[] results = null;
094:
095: try {
096: results = kernelService.search(request
097: .getParameter("uriPattern"), null, type, request
098: .getParameter("query"));
099: } catch (KernelServiceException e) {
100: e.printStackTrace();
101: request.setAttribute("error_search",
102: "Problem during search : " + e.getMessage());
103: }
104:
105: Vector searchResults = null;
106:
107: if (results != null) {
108: searchResults = new Vector();
109:
110: for (int i = 0; i < results.length; i++) {
111: SearchResult result = new SearchResult(results[i]
112: .getScore(), results[i].getUri(), results[i]
113: .getName(), results[i].getHighlightedFragment(
114: "<span class=\"highlighted\">", "</span>"));
115: searchResults.add(result);
116: }
117: }
118:
119: request.setAttribute("results", searchResults);
120:
121: return "/pages/search.jsp";
122: }
123: }
|