001 //$HeadURL$
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036
037 package org.deegree.framework.concurrent;
038
039 import java.lang.reflect.Method;
040 import java.util.Map;
041 import java.util.concurrent.Callable;
042
043 import org.deegree.io.JDBCConnection;
044 import org.deegree.io.databaseloader.PostgisDataLoader;
045 import org.deegree.model.feature.FeatureCollection;
046 import org.deegree.model.spatialschema.Envelope;
047 import org.deegree.ogcwebservices.wfs.operation.FeatureResult;
048 import org.deegree.ogcwebservices.wms.configuration.DatabaseDataSource;
049 import org.deegree.ogcwebservices.wms.operation.DimensionValues;
050 import org.deegree.ogcwebservices.wms.operation.GetMap;
051 import org.deegree.ogcwebservices.wms.operation.DimensionValues.DimensionValue;
052
053 /**
054 *
055 *
056 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057 * @author last edited by: $Author: poth $
058 *
059 * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
060 */
061 public class DoDatabaseQueryTask implements Callable<Object> {
062
063 private DatabaseDataSource datasource;
064
065 private Envelope envelope;
066
067 private String sql;
068
069 private Map<String, String> dimProps;
070
071 private GetMap request;
072
073 /**
074 * Uses the sql template from the data source.
075 *
076 * @param datasource
077 * @param envelope
078 */
079 public DoDatabaseQueryTask( DatabaseDataSource datasource, Envelope envelope ) {
080 this.datasource = datasource;
081 this.envelope = envelope;
082 }
083
084 /**
085 * Uses the given sql template
086 *
087 * @param datasource
088 * @param envelope
089 * @param sql
090 * custom sql template to use for fetching the data
091 */
092 public DoDatabaseQueryTask( DatabaseDataSource datasource, Envelope envelope, String sql ) {
093 this.datasource = datasource;
094 this.envelope = envelope;
095 this.sql = sql;
096 }
097
098 /**
099 * @param datasource
100 * @param envelope
101 * @param sql
102 * @param dimProps
103 * @param request
104 */
105 public DoDatabaseQueryTask( DatabaseDataSource datasource, Envelope envelope, String sql,
106 Map<String, String> dimProps, GetMap request ) {
107 this.datasource = datasource;
108 this.envelope = envelope;
109 this.sql = sql;
110 this.dimProps = dimProps;
111 this.request = request;
112 }
113
114 public FeatureResult call()
115 throws Exception {
116 JDBCConnection jdbc = datasource.getJDBCConnection();
117 String driver = jdbc.getDriver();
118 FeatureCollection fc = null;
119
120 String dimClause = null;
121 if ( dimProps != null && dimProps.size() > 0 && ( request.getDimTime() != null || request.getDimElev() != null ) ) {
122 DimensionValues val;
123 String name;
124 if ( request.getDimTime() != null ) {
125 name = dimProps.get( "time" );
126 val = request.getDimTime();
127 } else {
128 name = dimProps.get( "elevation" );
129 val = request.getDimElev();
130 }
131
132 dimClause = "";
133
134 // TODO support ranges?
135 for ( DimensionValue v : val.values ) {
136 if ( v.value != null ) {
137 if ( driver.toUpperCase().indexOf( "MYSQL" ) > -1 ) {
138 dimClause += " and " + name + " = \"" + v.value.replace( "T", " " ).replace( "Z", " " ).trim()
139 + "\"";
140 } else {
141 dimClause += " and " + name + " = '" + v.value + "'";
142 }
143 }
144 }
145 }
146
147 if ( driver.toUpperCase().indexOf( "POSTGRES" ) > -1 ) {
148 fc = PostgisDataLoader.load( datasource, envelope, sql, dimClause );
149 } else if ( driver.toUpperCase().indexOf( "ORACLE" ) > -1 ) {
150 Class<?> cls = Class.forName( "org.deegree.io.databaseloader.OracleDataLoader" );
151 Method method = cls.getMethod( "load", DatabaseDataSource.class, Envelope.class, String.class, String.class );
152 fc = (FeatureCollection) method.invoke( cls, datasource, envelope, sql, dimClause );
153 } else if ( driver.toUpperCase().indexOf( "MYSQL" ) > -1 ) {
154 Class<?> cls = Class.forName( "org.deegree.io.databaseloader.MySQLDataLoader" );
155 Method method = cls.getMethod( "load", DatabaseDataSource.class, Envelope.class, String.class, String.class );
156 fc = (FeatureCollection) method.invoke( cls, datasource, envelope, sql, dimClause );
157 } else {
158 throw new Exception( "unsuported database type: " + driver );
159 }
160 FeatureResult result = new FeatureResult( null, fc );
161 return result;
162 }
163 }