Project

General

Profile

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 endpoint, not authenticated
58
		endPointInfo = new ConfigurableEcogridEndPoint();
59
		init();
60
	}
61
	
62
	public DataQuery(String sessionId) {
63
		// initialize the necessary parts
64
		endPointInfo = new ConfigurableAuthenticatedEcogridEndPoint(sessionId);
65
		init();
66
	}
67
	
68
	private void init() {
69
		connectionPool = DatabaseConnectionPoolFactory.getDatabaseConnectionPoolInterface();
70
		dataManager = 
71
			DataManager.getInstance(connectionPool, connectionPool.getDBAdapterName());
72
	}
73

    
74
	public ResultSet executeQuery(String xml) throws Exception {
75
		
76
		long startTime = System.currentTimeMillis();
77
		
78
		// parse the query
79
		DataquerySpecification specification = 
80
			new DataquerySpecification(xml, parserName, connectionPool, endPointInfo);
81

    
82
		long endTime = System.currentTimeMillis();
83
		log.debug((endTime - startTime) + " ms to parse query");
84
		startTime = System.currentTimeMillis();
85
		
86
		// get the results
87
		ResultSet resultset = null;
88

    
89
		Union union = specification.getUnion();
90

    
91
		if (union != null) {
92
			resultset = 
93
				dataManager.selectData(
94
						union, 
95
						specification.getDataPackages());
96
		} else {
97
			Query query = specification.getQuery();
98
			resultset = 
99
				dataManager.selectData(
100
						query, 
101
						specification.getDataPackages());
102
		}
103

    
104
		endTime = System.currentTimeMillis();
105
		log.debug((endTime - startTime) + " ms to select results");
106
		
107
		return resultset;
108

    
109
	}
110

    
111
}
(3-3/5)