Project

General

Profile

1 2741 costa
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24
25
package edu.ucsb.nceas.metacat.advancedsearch;
26
27
import java.io.IOException;
28
29
import javax.servlet.RequestDispatcher;
30 2822 costa
import javax.servlet.ServletConfig;
31 2741 costa
import javax.servlet.ServletContext;
32
import javax.servlet.ServletException;
33
import javax.servlet.http.HttpServlet;
34
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletResponse;
36
import javax.servlet.http.HttpSession;
37
38
import edu.ucsb.nceas.metacat.client.*;
39 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
40 4124 daigle
import edu.ucsb.nceas.metacat.service.ServiceException;
41 4732 daigle
import edu.ucsb.nceas.metacat.util.SystemUtil;
42 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
43 2741 costa
44
/**
45
 * @author dcosta
46
 *
47
 * The AdvancedSearchServlet executes an advanced search.
48
 */
49
public class AdvancedSearchServlet extends HttpServlet {
50
51 2822 costa
  /*
52
   * Class fields
53
   */
54 3011 costa
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
55 2822 costa
56
  // Instance Variables
57 2741 costa
58
  // Methods
59
60
  /**
61
   * Executes an advanced search.
62
   */
63
  public void doPost(HttpServletRequest request, HttpServletResponse response)
64
          throws IOException, ServletException {
65
    AdvancedSearch advancedSearch;
66
    AdvancedSearchBean advancedSearchBean;
67
    RequestDispatcher dispatcher;
68
    HttpSession httpSession = request.getSession();
69
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
70 3011 costa
    MetacatHelper metacatHelper = new MetacatHelper();
71 2741 costa
    String metacatURL;
72 3011 costa
    String qformat = (String) httpSession.getAttribute("qformat");
73 2741 costa
    String result;
74 3011 costa
    final String resultsJSP = metacatHelper.getResultsJSP();
75 2741 costa
    ServletContext servletContext = httpSession.getServletContext();
76 3011 costa
    String xslPath = metacatHelper.getResultsetXSL(request);
77 2741 costa
78
    // First check whether the metacat URL was set as a context-param.
79
    metacatURL = servletContext.getInitParameter("metacatURL");
80
81 4732 daigle
    // If no metacat URL was configured, then get metacat URL from system utility
82 2741 costa
    if (metacatURL == null || metacatURL.equals("")) {
83 4732 daigle
		try {
84
			metacatURL = SystemUtil.getContextURL() + "/metacat";
85
		} catch (PropertyNotFoundException pnfe) {
86
			throw new ServletException("Could not get Metacat URL: "
87
					+ pnfe.getMessage());
88
		}
89
	}
90 2741 costa
91
    // Get the advancedSearchBean object that has been loaded up with user
92
    // input. Pass the bean to the advancedSearch object, execute the query,
93
    // and return the search result HTML string.
94
    advancedSearchBean =
95
                (AdvancedSearchBean) request.getAttribute("advancedSearchBean");
96
    advancedSearch = new AdvancedSearch(advancedSearchBean);
97 3011 costa
    result = advancedSearch.executeAdvancedSearch(metacatURL, metacat,
98
                                                  qformat, xslPath);
99 2741 costa
100
    // Store the result HTML string in the request for retrieval by
101
    // the metacatpathqueryresults.jsp form.
102
    request.setAttribute("result", result);
103
104
    dispatcher = request.getRequestDispatcher(resultsJSP);
105
    dispatcher.forward(request, response);
106
  }
107
108 4080 daigle
    /**
109
	 * Initializes the servlet. Reads properties and initializes object fields.
110
	 *
111
	 * @throws ServletException
112
	 */
113
	public void init(ServletConfig config) throws ServletException {
114 2822 costa
115 4080 daigle
		super.init(config);
116 2822 costa
117 4080 daigle
		try {
118 4708 daigle
			PropertyService.getInstance();
119 4124 daigle
		} catch (ServiceException se) {
120
			System.err.println("Error in loading properties: " + se.getMessage());
121 4732 daigle
		}
122 4080 daigle
	}
123 2741 costa
}