001: /*
002: * GeoLBS - OpenSource Location Based Servces toolkit
003: * (C) 2004, Julian J. Ray, All Rights Reserved
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: */
016:
017: package org.geotools.data.geomedia;
018:
019: import java.io.IOException;
020: import java.sql.Connection;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024: import java.util.HashMap;
025: import java.util.Hashtable;
026: import java.util.Map;
027: import java.util.logging.Logger;
028:
029: import javax.sql.DataSource;
030:
031: import org.geotools.data.DataSourceException;
032: import org.geotools.data.Transaction;
033: import org.geotools.data.geomedia.attributeio.GeoMediaAttributeIO;
034: import org.geotools.data.jdbc.ConnectionPool;
035: import org.geotools.data.jdbc.JDBCDataStore;
036: import org.geotools.data.jdbc.JDBCDataStoreConfig;
037: import org.geotools.data.jdbc.JDBCUtils;
038: import org.geotools.data.jdbc.QueryData;
039: import org.geotools.data.jdbc.attributeio.AttributeIO;
040: import org.geotools.feature.AttributeType;
041: import org.geotools.feature.AttributeTypeFactory;
042:
043: import com.vividsolutions.jts.geom.Geometry;
044:
045: /**
046: * Geomedia data store implementation
047: *
048: * @todo really fix and test this datastore, for the moment it's just a way
049: * to make it compile
050: *
051: * @author Julian J. Ray
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geomedia/src/main/java/org/geotools/data/geomedia/GeoMediaDataStore.java $
053: * @version 1.0
054: */
055: public class GeoMediaDataStore extends JDBCDataStore {
056: private static final Logger LOGGER = org.geotools.util.logging.Logging
057: .getLogger("org.geotools.data.geomedia");
058:
059: /** Maps SQL Server SQL types to Java classes */
060: private static final Map TYPE_MAPPINGS = new HashMap();
061:
062: static {
063: TYPE_MAPPINGS.put("bigint", Long.class);
064: TYPE_MAPPINGS.put("bigint identity", Long.class);
065: TYPE_MAPPINGS.put("int", Integer.class);
066: TYPE_MAPPINGS.put("int identity", Integer.class);
067: TYPE_MAPPINGS.put("smallint", Integer.class);
068: TYPE_MAPPINGS.put("smallint identity", Integer.class);
069: TYPE_MAPPINGS.put("char", Byte.class);
070: TYPE_MAPPINGS.put("decimal", Double.class);
071: TYPE_MAPPINGS.put("float", Float.class);
072: TYPE_MAPPINGS.put("money", Double.class);
073: TYPE_MAPPINGS.put("numeric", Double.class);
074: TYPE_MAPPINGS.put("real", Double.class);
075: TYPE_MAPPINGS.put("varchar", String.class);
076: TYPE_MAPPINGS.put("nvarchar", String.class);
077: TYPE_MAPPINGS.put("nchar", String.class);
078: }
079:
080: // Used to cache GFeature table to remove need for subsequent reads
081: private Hashtable mGFeatureCache = null;
082:
083: /**
084: * Creates a new GeoMediaDataStore instance
085: *
086: * @param connectionPool ConnectionPool
087: *
088: * @throws IOException
089: */
090: public GeoMediaDataStore(DataSource dataSource) throws IOException {
091: super (dataSource, new JDBCDataStoreConfig());
092:
093: mGFeatureCache = null;
094: }
095:
096: /**
097: * DOCUMENT ME!
098: *
099: * @throws DataSourceException
100: */
101:
102: /*
103: public GeoMediaDataStore(ConnectionPool connectionPool, String namespace, String databaseName) throws IOException
104: {
105: super(connectionPool, namespace, databaseName);
106: mGFeatureCache = null;
107: }
108: */
109:
110: /**
111: * Reads the GeoMedia GFeatures table and caches metadata information. If the metadata has already been read, it is
112: * overwritten. Used to update the DataStore if changes have been made by GeoMedia.
113: *
114: * @throws DataSourceException
115: */
116: public void readMetadata() throws DataSourceException {
117: // See if this is the first time reading metadata
118: if (mGFeatureCache == null) {
119: mGFeatureCache = new Hashtable();
120:
121: // Empty existing cache if populated
122: }
123:
124: if (mGFeatureCache.size() > 0) {
125: mGFeatureCache.clear();
126: }
127:
128: Connection conn = null;
129: ResultSet rs = null;
130: Statement stmt = null;
131:
132: try {
133: conn = getConnection(Transaction.AUTO_COMMIT);
134:
135: String sql = "SELECT featurename, geometrytype, primarygeometryfieldname, featuredescription FROM gfeatures";
136: stmt = conn.createStatement();
137: rs = stmt.executeQuery(sql);
138:
139: while (rs.next()) {
140: GFeatureType gFeature = new GFeatureType();
141: gFeature.setTypeName(rs.getString(1));
142: gFeature.setGeoMediaFeatureType(rs.getInt(2));
143: gFeature.setGeomColName(rs.getString(3));
144: gFeature.setDescription(rs.getString(4));
145:
146: mGFeatureCache.put(
147: gFeature.getTypeName().toUpperCase(), gFeature);
148: }
149: } catch (IOException e) {
150: LOGGER.warning("IOException reading metadata: "
151: + e.getMessage());
152: throw new DataSourceException(e.getMessage());
153: } catch (SQLException e) {
154: LOGGER.warning("SQLException reading metadata: "
155: + e.getMessage());
156: throw new DataSourceException(e.getMessage());
157: } finally {
158: JDBCUtils.close(rs);
159: JDBCUtils.close(stmt);
160: JDBCUtils.close(conn, null, null);
161: }
162: }
163:
164: /**
165: * Returns feature tables which have been entered into the GeoMedia metadata table.
166: *
167: * @param tablename String - the table to test
168: *
169: * @return boolean - true if the table is a spatial table, false otherwise.
170: */
171: protected boolean allowTable(String tablename) {
172: // If the metadata has not been initialized then we attempt to do it here. Note that
173: // we are swallowing any exceptions which are thrown
174: if (mGFeatureCache == null) {
175: try {
176: readMetadata();
177: } catch (IOException e) {
178: return false;
179: }
180: }
181:
182: // This just allows feature tables which are stored in the GFeature metadata table
183: boolean res = mGFeatureCache.containsKey(tablename
184: .toUpperCase());
185:
186: return res;
187: }
188:
189: /**
190: * Overrides the buildAttributeType method to check for GDO_GEOMETRY columns. This function fromes any GeoMedia
191: * managed columns which do not contain spatial data such as GDO_GEOMETRY_XHI and spatial indexes.
192: *
193: * @param rs ResultSet
194: *
195: * @return AttributeType
196: *
197: * @throws SQLException
198: * @throws DataSourceException
199: *
200: * @see org.geolbs.data.jdbc.JDBCDataStore#buildAttributeType(java.sql.ResultSet)
201: */
202: protected AttributeType buildAttributeType(ResultSet rs)
203: throws IOException {
204: try {
205: final int TABLE_NAME = 3;
206: final int COLUMN_NAME = 4;
207:
208: // Need to check...
209: if (mGFeatureCache == null) {
210: readMetadata();
211: }
212:
213: String columnName = rs.getString(COLUMN_NAME);
214: String tableName = rs.getString(TABLE_NAME);
215:
216: // We have to check to see if this column name is in the GeoMedia GFeatures geometry col names
217: GFeatureType gFeature = (GFeatureType) mGFeatureCache
218: .get(tableName.toUpperCase());
219:
220: if (gFeature != null) {
221: if (columnName.compareToIgnoreCase(gFeature
222: .getGeomColName()) == 0) {
223: return AttributeTypeFactory.newAttributeType(
224: columnName, Geometry.class);
225: }
226: }
227:
228: // Filter out the GDO bounding box columns GDO_GEOMETRY_XHI, GDO_GEOMETRY_XLO, GDO_GEOMETRY_YHI and GDO_GEOMETRY_YLO
229: if (columnName.startsWith("GDO_GEOMETRY") == true) {
230: return null;
231: }
232:
233: return super .buildAttributeType(rs);
234: } catch (SQLException e) {
235: throw new DataSourceException(
236: "Sql exception while parsing feature type", e);
237: }
238: }
239:
240: /**
241: * @see org.geotools.data.jdbc.JDBCDataStore#getGeometryAttributeIO(org.geotools.feature.AttributeType, org.geotools.data.jdbc.QueryData)
242: */
243: protected AttributeIO getGeometryAttributeIO(AttributeType type,
244: QueryData queryData) throws IOException {
245: return new GeoMediaAttributeIO();
246: }
247:
248: }
|