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 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
}
(2-2/3)