Project

General

Profile

1
/**
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: daigle $'
7
 *     '$Date: 2008-12-26 13:17:22 -0800 (Fri, 26 Dec 2008) $'
8
 * '$Revision: 4708 $'
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.File;
28
import java.io.IOException;
29

    
30
import javax.servlet.RequestDispatcher;
31
import javax.servlet.ServletConfig;
32
import javax.servlet.ServletContext;
33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpServlet;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpSession;
38

    
39
import edu.ucsb.nceas.metacat.client.*;
40
import edu.ucsb.nceas.metacat.service.PropertyService;
41
import edu.ucsb.nceas.metacat.service.ServiceException;
42
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
43

    
44
/** 
45
 * @author dcosta
46
 * 
47
 * The AdvancedSearchServlet executes an advanced search.
48
 */
49
public class AdvancedSearchServlet extends HttpServlet {
50

    
51
  /*
52
   * Class fields
53
   */
54
  private static final String CONFIG_DIR = "WEB-INF";
55
  private static final String CONFIG_NAME = "metacat.properties";
56
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
57
	   
58
  // Instance Variables
59
  private String contextString = "";
60

    
61
  // Methods
62
  
63
  /**
64
   * Executes an advanced search.
65
   */
66
  public void doPost(HttpServletRequest request, HttpServletResponse response)
67
          throws IOException, ServletException {
68
    AdvancedSearch advancedSearch;
69
    AdvancedSearchBean advancedSearchBean;
70
    RequestDispatcher dispatcher;
71
    HttpSession httpSession = request.getSession();
72
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
73
    MetacatHelper metacatHelper = new MetacatHelper();
74
    String metacatURL;
75
    String qformat = (String) httpSession.getAttribute("qformat");
76
    String result;
77
    final String resultsJSP = metacatHelper.getResultsJSP();
78
    ServletContext servletContext = httpSession.getServletContext();
79
    String xslPath = metacatHelper.getResultsetXSL(request);
80

    
81
    // First check whether the metacat URL was set as a context-param.
82
    metacatURL = servletContext.getInitParameter("metacatURL");
83
 
84
    // If no metacat URL was configured, then derive the metacat URL from the
85
    // server name and server port.
86
    if (metacatURL == null || metacatURL.equals("")) {
87
      String serverName = request.getServerName();
88
      int serverPort = request.getServerPort();
89
      metacatURL = 
90
    	 metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
91
    }
92

    
93
    // Get the advancedSearchBean object that has been loaded up with user
94
    // input. Pass the bean to the advancedSearch object, execute the query,
95
    // and return the search result HTML string.
96
    advancedSearchBean = 
97
                (AdvancedSearchBean) request.getAttribute("advancedSearchBean");
98
    advancedSearch = new AdvancedSearch(advancedSearchBean);
99
    result = advancedSearch.executeAdvancedSearch(metacatURL, metacat, 
100
                                                  qformat, xslPath);
101
    
102
    // Store the result HTML string in the request for retrieval by
103
    // the metacatpathqueryresults.jsp form.
104
    request.setAttribute("result", result);
105
		
106
    dispatcher = request.getRequestDispatcher(resultsJSP);
107
    dispatcher.forward(request, response);
108
  }
109
  
110
    /**
111
	 * Initializes the servlet. Reads properties and initializes object fields.
112
	 * 
113
	 * @throws ServletException
114
	 */
115
	public void init(ServletConfig config) throws ServletException {
116
		ServletContext context = null;
117
		String dirPath;
118

    
119
		super.init(config);
120
		context = config.getServletContext();
121
		dirPath = context.getRealPath(CONFIG_DIR);
122

    
123
		try {
124
			PropertyService.getInstance();
125
			this.contextString = PropertyService.getProperty("application.context");
126
		} catch (ServiceException se) {
127
			System.err.println("Error in loading properties: " + se.getMessage());
128
		} catch (PropertyNotFoundException pnfe) {
129
			System.err.println("couldn't read property during initialization: " 
130
					+ pnfe.getMessage());
131
		}
132

    
133
	}
134
}
(6-6/14)