001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.geomedia.attributeio;
017:
018: import java.io.IOException;
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Types;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.geotools.data.DataSourceException;
027: import org.geotools.data.jdbc.attributeio.AttributeIO;
028:
029: import com.vividsolutions.jts.geom.Geometry;
030:
031: /**
032: * AttributeIO implementation that read and writes geomedia columns
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geomedia/src/main/java/org/geotools/data/geomedia/attributeio/GeoMediaAttributeIO.java $
034: */
035: public class GeoMediaAttributeIO implements AttributeIO {
036: private static final Logger LOGGER = org.geotools.util.logging.Logging
037: .getLogger("org.geotools.data.geomedia");
038:
039: // geometry adpater
040: private GeoMediaGeometryAdapter geometryAdapter = null;
041:
042: /**
043: * Creates a new instance of the GeomediaAttributeIO for a specific QueryData instance
044: *
045: * @throws DataSourceException
046: */
047: public GeoMediaAttributeIO() throws DataSourceException {
048:
049: geometryAdapter = new GeoMediaGeometryAdapter();
050: }
051:
052: /**
053: * @see org.geotools.data.jdbc.attributeio.AttributeIO#read(java.sql.ResultSet,
054: * int)
055: */
056: public Object read(ResultSet rs, int position) throws IOException {
057: try {
058: return geometryAdapter.deSerialize((byte[]) rs
059: .getObject(position));
060: } catch (SQLException e) {
061: String msg = "SQL Exception reading geometry column";
062: LOGGER.log(Level.SEVERE, msg, e);
063: throw new DataSourceException(msg, e);
064: } catch (GeoMediaGeometryTypeNotKnownException e) {
065: String msg = "Geometry Conversion type error";
066: LOGGER.log(Level.SEVERE, msg, e);
067: throw new DataSourceException(msg, e);
068: } catch (GeoMediaUnsupportedGeometryTypeException e) {
069: String msg = "Geometry Conversion type error";
070: LOGGER.log(Level.SEVERE, msg, e);
071: throw new DataSourceException(msg, e);
072: }
073: }
074:
075: /**
076: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
077: * int, java.lang.Object)
078: */
079: public void write(ResultSet rs, int position, Object value)
080: throws IOException {
081: try {
082: if (value == null) {
083: rs.updateNull(position);
084: } else {
085: byte[] blob = geometryAdapter
086: .serialize((Geometry) value);
087: rs.updateObject(position, (Object) blob);
088: }
089: } catch (SQLException sqlException) {
090: String msg = "SQL Exception writing geometry column";
091: LOGGER.log(Level.SEVERE, msg, sqlException);
092: throw new DataSourceException(msg, sqlException);
093: } catch (GeoMediaUnsupportedGeometryTypeException e) {
094: String msg = "Geometry Conversion type error";
095: LOGGER.log(Level.SEVERE, msg, e);
096: throw new DataSourceException(msg, e);
097: }
098: }
099:
100: /**
101: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.PreparedStatement, int, java.lang.Object)
102: */
103: public void write(PreparedStatement ps, int position, Object value)
104: throws IOException {
105: try {
106: if (value == null) {
107: ps.setNull(position, Types.OTHER);
108: } else {
109: byte[] blob = geometryAdapter
110: .serialize((Geometry) value);
111: ps.setObject(position, (Object) blob);
112: }
113: } catch (SQLException sqlException) {
114: String msg = "SQL Exception writing geometry column";
115: LOGGER.log(Level.SEVERE, msg, sqlException);
116: throw new DataSourceException(msg, sqlException);
117: } catch (GeoMediaUnsupportedGeometryTypeException e) {
118: String msg = "Geometry Conversion type error";
119: LOGGER.log(Level.SEVERE, msg, e);
120: throw new DataSourceException(msg, e);
121: }
122:
123: }
124: }
|