Project

General

Profile

« Previous | Next » 

Revision 4221

add dataquery (from EML's Data Manager library) to the metacat servlet actions

View differences:

lib/style/skins/dev/dataquery.html
1
<html>
2
<head>
3
<title>Untitled Document</title>
4
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5
</head>
6

  
7
<body bgcolor="#FFFFFF" text="#000000">
8
<form name="form1" method="post" action="../../../metacat">
9
  <p>action 
10
    <input type="text" name="action">
11
  </p>
12
  <p>qformat 
13
    <input type="text" name="qformat">
14
  </p>
15
  <p>query
16
    <textarea name="dataquery" rows="20" cols="80"></textarea>
17
  </p>
18
  <p>
19
    <input type="submit" name="Submit" value="Submit">
20
  </p>
21
</form>
22
</body>
23
</html>
0 24

  
lib/metacat.properties
93 93
#turn on or off the query result cache
94 94
database.queryCacheOn=true
95 95

  
96
######## Datamanager section              #######################################
97
datamanager.adapter=PostgresAdapter
98
datamanager.implementation=edu.ucsb.nceas.metacat.dataquery.PostgresDatabaseConnectionPool
99
datamanager.server=localhost
100
datamanager.database=datamanager
101
datamanager.user=datamanager
102
datamanager.password=datamanager
103
datamanager.maxconnections=10
104

  
105
datamanager.endpoint.query=http://ecogrid.ecoinformatics.org/knb/services/QueryService
106
datamanager.endpoint.authentication=http://ecogrid.ecoinformatics.org/knb/services/AuthenticationService
107
datamanager.endpoint.put=http://ecogrid.ecoinformatics.org/knb/services/PutService
108
datamanager.endpoint.identifier=http://ecogrid.ecoinformatics.org/knb/services/IdentificationService
109
datamanager.srb.endpoint=
110
datamanager.srb.machinename=
111

  
96 112
######## LDAP ##############################################
97 113

  
98 114
auth.class=edu.ucsb.nceas.metacat.AuthLdap
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
31 31
import java.io.FileInputStream;
32 32
import java.io.FileReader;
33 33
import java.io.IOException;
34
import java.io.OutputStreamWriter;
34 35
import java.io.PrintWriter;
35 36
import java.io.StringReader;
37
import java.io.Writer;
36 38
import java.net.MalformedURLException;
37 39
import java.net.URL;
38 40
import java.sql.PreparedStatement;
......
63 65
import org.apache.log4j.PropertyConfigurator;
64 66
import org.ecoinformatics.eml.EMLParser;
65 67

  
68
import au.com.bytecode.opencsv.CSVWriter;
69

  
66 70
import com.oreilly.servlet.multipart.FilePart;
67 71
import com.oreilly.servlet.multipart.MultipartParser;
68 72
import com.oreilly.servlet.multipart.ParamPart;
69 73
import com.oreilly.servlet.multipart.Part;
70 74

  
75
import edu.ucsb.nceas.metacat.dataquery.DataQuery;
71 76
import edu.ucsb.nceas.metacat.service.PropertyService;
72 77
import edu.ucsb.nceas.metacat.service.ServiceException;
73 78
import edu.ucsb.nceas.metacat.service.SessionService;
......
760 765
							sess_id);
761 766
					out.close();
762 767

  
763
				} else if (action.equals("export")) {
768
				} 
769
				else if (action.trim().equals("dataquery")) {
764 770

  
771
					logMetacat
772
							.debug("******************* DATA QUERY ********************");
773
					handleDataquery(params, response);
774
				}
775
				else if (action.equals("export")) {
776

  
765 777
					handleExportAction(params, response, username, groupnames, password);
766 778
				} else if (action.equals("read")) {
767 779
					handleReadAction(params, request, response, username, password,
......
929 941
		}
930 942
	}
931 943
    
944
	
945
	private void handleDataquery(
946
			Hashtable<String, String[]> params,
947
            HttpServletResponse response) throws PropertyNotFoundException, IOException {
948
		
949
		DataQuery dq = new DataQuery();
950
		
951
    	String dataqueryXML = ((String[])params.get("dataquery"))[0];
952

  
953
    	ResultSet rs = null;
954
		try {
955
			rs = dq.executeQuery(dataqueryXML);
956
		} catch (Exception e) {
957
			//probably need to do something here
958
			e.printStackTrace();
959
			return;
960
		}
961
    	
962
		//process the result set
963
		String qformat = "csv";
964
		String[] temp = params.get("qformat");
965
		if (temp != null && temp.length > 0) {
966
			qformat = temp[0];
967
		}
968
		String fileName = "query-results." + qformat;
969
		
970
		//get the results as csv file
971
		if (qformat != null && qformat.equalsIgnoreCase("csv")) {
972
			response.setContentType("text/csv");
973
			//response.setContentType("application/csv");
974
	        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
975
	        
976
			Writer writer = new OutputStreamWriter(response.getOutputStream());
977
			CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
978
			try {
979
				csv.writeAll(rs, true);
980
				
981
				csv.flush();
982
				response.flushBuffer();
983
				
984
				rs.close();
985
				
986
			} catch (SQLException e) {
987
				e.printStackTrace();
988
			}
989
			
990
			return;
991
		}
992
		
993
	}
994
	
932 995
    // ///////////////////////////// METACAT SPATIAL ///////////////////////////
933 996
    
934 997
    /**
src/edu/ucsb/nceas/metacat/dataquery/DataQuery.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2008 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 * Author: Benjamin Leinfelder 
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.dataquery;
26

  
27
import java.sql.ResultSet;
28

  
29
import org.apache.commons.logging.Log;
30
import org.apache.commons.logging.LogFactory;
31
import org.ecoinformatics.datamanager.DataManager;
32
import org.ecoinformatics.datamanager.database.DatabaseConnectionPoolInterface;
33
import org.ecoinformatics.datamanager.database.Query;
34
import org.ecoinformatics.datamanager.database.Union;
35
import org.ecoinformatics.datamanager.dataquery.DataquerySpecification;
36
import org.ecoinformatics.datamanager.download.EcogridEndPointInterface;
37

  
38
/**
39
 * Class to query data
40
 */
41
public class DataQuery {
42

  
43
	private static Log log = LogFactory.getLog(DataQuery.class);
44

  
45
	private EcogridEndPointInterface endPointInfo;
46

  
47
	private DatabaseConnectionPoolInterface connectionPool;
48

  
49
	private DataManager dataManager;
50

  
51
	private String parserName = "org.apache.xerces.parsers.SAXParser";
52

  
53
	/**
54
	 * empty constructor to initialize query
55
	 */
56
	public DataQuery() {
57
		// initialize the necessary parts
58
		endPointInfo = new ConfigurableEcogridEndPoint();
59
		connectionPool = new PostgresDatabaseConnectionPool();
60
		dataManager = 
61
			DataManager.getInstance(connectionPool, connectionPool.getDBAdapterName());
62

  
63
	}
64

  
65
	public ResultSet executeQuery(String xml) throws Exception {
66
		// parse the query
67
		DataquerySpecification specification = 
68
			new DataquerySpecification(xml, parserName, connectionPool, endPointInfo);
69

  
70
		// get the results
71
		ResultSet resultset = null;
72

  
73
		Union union = specification.getUnion();
74

  
75
		if (union != null) {
76
			resultset = 
77
				dataManager.selectData(
78
						union, 
79
						specification.getDataPackages());
80
		} else {
81
			Query query = specification.getQuery();
82
			resultset = 
83
				dataManager.selectData(
84
						query, 
85
						specification.getDataPackages());
86
		}
87

  
88
		return resultset;
89

  
90
	}
91

  
92
}
0 93

  
src/edu/ucsb/nceas/metacat/dataquery/PostgresDatabaseConnectionPool.java
1
/**
2
 *    '$RCSfile: PostgresDatabaseConnectionPool.java,v $'
3
 *
4
 *     '$Author: leinfelder $'
5
 *       '$Date: 2007/10/19 18:50:11 $'
6
 *   '$Revision: 1.1 $'
7
 *
8
 *  For Details: http://ecoinformatics.org
9
 *
10
 * Copyright (c) 2007 The Regents of the University of California.
11
 * All rights reserved.
12
 * 
13
 * Permission is hereby granted, without written agreement and without
14
 * license or royalty fees, to use, copy, modify, and distribute this
15
 * software and its documentation for any purpose, provided that the
16
 * above copyright notice and the following two paragraphs appear in
17
 * all copies of this software.
18
 * 
19
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
20
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
22
 * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
23
 * OF SUCH DAMAGE.
24
 * 
25
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
28
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
29
 * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
30
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
31
 */
32
package edu.ucsb.nceas.metacat.dataquery;
33

  
34
import java.sql.Connection;
35
import java.sql.SQLException;
36

  
37
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39
import org.ecoinformatics.datamanager.database.ConnectionNotAvailableException;
40
import org.ecoinformatics.datamanager.database.DatabaseConnectionPoolInterface;
41
import org.postgresql.jdbc3.Jdbc3PoolingDataSource;
42

  
43
import edu.ucsb.nceas.metacat.service.PropertyService;
44
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
45

  
46
/**
47
 * This class implements DataConnetionPoolInterface to provide a connection for
48
 * testing. Database information in this class will be read from property file.
49
 * 
50
 * @author tao
51
 * 
52
 */
53
public class PostgresDatabaseConnectionPool implements
54
		DatabaseConnectionPoolInterface {
55
	
56
	public static Log log = LogFactory.getLog(PostgresDatabaseConnectionPool.class);
57

  
58
	
59
	/* Configuration properties file */
60
	private static String serverName = null;
61
	private static String databaseName = null;
62
	private static String user = null;
63
	private static String password = null;
64
	private static int maxConnections = 0;
65
	private static String databaseAdapterName = null;
66

  
67
	private static Jdbc3PoolingDataSource source = null;
68
	
69
	private static int connCount = 0;
70
	
71
	/**
72
	 * Constructor. Loading database parameter from property file
73
	 * 
74
	 */
75
	public PostgresDatabaseConnectionPool() {
76
		try {
77
			loadOptions();
78
		}
79
		catch (PropertyNotFoundException e) {
80
			log.error(e.getMessage());
81
			e.printStackTrace();
82
		}
83
		initPool();
84
	}
85

  
86
	private static void initPool() {
87
		source = new Jdbc3PoolingDataSource();
88
		//source.setDataSourceName(databaseAdapterName);
89
		source.setServerName(serverName);
90
		source.setDatabaseName(databaseName);
91
		source.setUser(user);
92
		source.setPassword(password);
93
		source.setMaxConnections(maxConnections);
94
	}
95

  
96
	/**
97
	 * Loads Data Manager options from a configuration file.
98
	 * @throws PropertyNotFoundException 
99
	 */
100
	private static void loadOptions() throws PropertyNotFoundException {
101

  
102
		serverName = PropertyService.getProperty("datamanager.server");
103
		databaseName = PropertyService.getProperty("datamanager.database");
104
		user = PropertyService.getProperty("datamanager.user");
105
		password = PropertyService.getProperty("datamanager.password");
106
		maxConnections = 
107
			Integer.parseInt(
108
				PropertyService.getProperty("datamanager.maxconnections"));
109
		databaseAdapterName = PropertyService.getProperty("datamanager.adapter");
110
		
111
	}
112

  
113
	/**
114
	 * Get dabase adpater name.
115
	 * 
116
	 * @return database adapter name
117
	 */
118
	public String getDBAdapterName() {
119
		return databaseAdapterName;
120
	}
121

  
122
	/**
123
	 * Gets a database connection from the pool
124
	 * 
125
	 * @return checked out connection
126
	 * @throws SQLException
127
	 */
128
	public Connection getConnection() throws SQLException,
129
			ConnectionNotAvailableException {
130
		Connection connection = null;
131

  
132
		try {
133
			connection = source.getConnection();
134
			connCount++;
135
		} catch (SQLException e) {
136
			System.err.println("SQLException: " + e.getMessage());
137
			throw (e);
138
		}
139

  
140
		return connection;
141
	}
142

  
143
	/**
144
	 * Returns checked out dabase connection to the pool
145
	 * 
146
	 * @param conn
147
	 *            Connection needs to be returned.
148
	 * @return indicator if the connection was returned successfully
149
	 */
150
	public boolean returnConnection(Connection conn) {
151
		boolean success = false;
152

  
153
		try {
154
			conn.close();
155
			success = true;
156
			connCount--;
157
		} catch (Exception e) {
158
			success = false;
159
		}
160

  
161
		//log.debug(Thread.currentThread().getName() + ": connection count=" + connCount);
162

  
163
		return success;
164
	}
165

  
166
	public static void main(String arg[]) {
167
		PostgresDatabaseConnectionPool pool = new PostgresDatabaseConnectionPool();
168
		try {
169
			Connection conn = pool.getConnection();
170
			log.debug("conn=" + conn);
171
			conn.close();
172
		} catch (SQLException e) {
173
			// TODO Auto-generated catch block
174
			e.printStackTrace();
175
		} catch (ConnectionNotAvailableException e) {
176
			// TODO Auto-generated catch block
177
			e.printStackTrace();
178
		}
179
	}
180
}
src/edu/ucsb/nceas/metacat/dataquery/ConfigurableEcogridEndPoint.java
1
/**
2
 *    '$RCSfile: ConfigurableEcogridEndPoint.java,v $'
3
 *
4
 *     '$Author: leinfelder $'
5
 *       '$Date: 2008/06/20 17:47:12 $'
6
 *   '$Revision: 1.1 $'
7
 *
8
 *  For Details: http://ecoinformatics.org
9
 *
10
 * Copyright (c) 2007 The Regents of the University of California.
11
 * All rights reserved.
12
 * 
13
 * Permission is hereby granted, without written agreement and without
14
 * license or royalty fees, to use, copy, modify, and distribute this
15
 * software and its documentation for any purpose, provided that the
16
 * above copyright notice and the following two paragraphs appear in
17
 * all copies of this software.
18
 * 
19
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
20
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
22
 * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
23
 * OF SUCH DAMAGE.
24
 * 
25
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
28
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
29
 * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
30
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
31
 */
32
package edu.ucsb.nceas.metacat.dataquery;
33

  
34
import org.ecoinformatics.datamanager.download.EcogridEndPointInterface;
35

  
36
import edu.ucsb.nceas.metacat.service.PropertyService;
37
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38

  
39
/**
40
 * This class implements EcogridEndPointInterface and is configurable using a properties file.
41
 * Should be useful for switching to remote endpoints etc.
42
 * 
43
 * @author leinfelder
44
 *
45
 */
46
public class ConfigurableEcogridEndPoint implements EcogridEndPointInterface 
47
{  
48
		
49
	private String metacatEcogridEndPoint = null;
50
	private String metacatEcogridAuthEndPoint = null;
51
	private String metacatEcogridPutEndPoint = null;
52
	private String metacatEcogridIdentifierEndPoint = null;
53
	private String srbEcogridEndPoint = null;
54
	private String srbMachineName = null;
55
	
56
	public ConfigurableEcogridEndPoint() {
57
		
58
		try {
59
			metacatEcogridEndPoint 				= PropertyService.getProperty("datamanager.endpoint.query");
60
			metacatEcogridAuthEndPoint 			= PropertyService.getProperty("datamanager.endpoint.authentication");
61
			metacatEcogridPutEndPoint 			= PropertyService.getProperty("datamanager.endpoint.put");
62
			metacatEcogridIdentifierEndPoint 	= PropertyService.getProperty("datamanager.endpoint.identifier");
63
			srbEcogridEndPoint 					= PropertyService.getProperty("datamanager.srb.endpoint");
64
			srbMachineName 						= PropertyService.getProperty("datamanager.srb.machinename");
65
		} catch (PropertyNotFoundException e) {
66
			e.printStackTrace();
67
		}
68
		
69
	}
70
	
71
	 /**
72
	    * Gets the end point which Metacat implements ecogrid interface.
73
	    * This end point will be used to handle ecogrid protocol
74
        * 
75
	    * @return end point url string
76
	    */
77
	   public String getMetacatEcogridEndPoint()
78
	   {
79
		   return metacatEcogridEndPoint;
80
	   }
81
	   
82
	   public String getMetacatEcogridAuthEndPoint()
83
	   {
84
		   return metacatEcogridAuthEndPoint;
85
	   }
86
	   
87
	   public String getMetacatEcogridPutEndPoint()
88
	   {
89
		   return metacatEcogridPutEndPoint;
90
	   }
91
	   
92
	   public String getMetacatEcogridIdentifierEndPoint() 
93
	   {
94
			return metacatEcogridIdentifierEndPoint;
95
		}
96
	   
97
       
98
	   /**
99
	    * Gets the end point which SRB implements ecogrid interface.
100
	    * This end point will be used to handle srb protocol.
101
        * 
102
	    * @return end point url string
103
	    */
104
	   public String getSRBEcogridEndPoint()
105
	   {
106
		   return srbEcogridEndPoint;
107
	   }
108

  
109
       
110
	   /**
111
	    * Gets the machine name which srb protocol will be used.
112
	    * The default value for this class is "srb-mcat.sdsc.edu".
113
        * 
114
	    * @return the machine name of srb server
115
	    */
116
	   public String getSRBMachineName()
117
	   {
118
		   return srbMachineName;
119
	   }
120
       
121
}
122

  

Also available in: Unified diff