001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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: * Created on 18-apr-2004
017: * TODO: 26-may-2005 D. Adler Removed returnIDAsAttribute variable and method.
018: */
019: /*
020: * Created on 18-apr-2004
021: * 26-may-2005 D. Adler Removed returnIDAsAttribute variable and method.
022: * 12-jul-2006 D. Adler GEOT-728 Refactor FIDMapper classes
023: */
024: package org.geotools.data.jdbc.fidmapper;
025:
026: import org.geotools.data.DataSourceException;
027: import org.geotools.feature.Feature;
028: import java.io.IOException;
029: import java.sql.Connection;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: /**
037: * A FID mapper that uses a single integer column as the primary key and that
038: * does a <code>SELECT MAX(fixColumn) + 1</code> to generate new ones. This is
039: * a fragile generation strategy, better use a sequence or a serial to get
040: * reliable results.
041: *
042: * @author aaime
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/fidmapper/MaxIncFIDMapper.java $
044: */
045: public class MaxIncFIDMapper extends AbstractFIDMapper {
046: private static final long serialVersionUID = 5719859796485477701L;
047:
048: /** A logger */
049: private static final Logger LOGGER = org.geotools.util.logging.Logging
050: .getLogger("org.geotools.data.jdbc.fidmapper");
051:
052: /**
053: * Creates a new MaxIncFIDMapper object.
054: *
055: * @param tableName the table name
056: * @param FIDColumn the name of the FID column
057: * @param FIDColumnType The SQL type of the column - must be a numeric type
058: */
059: public MaxIncFIDMapper(String tableName, String FIDColumn,
060: int FIDColumnType) {
061: this (null, tableName, FIDColumn, FIDColumnType, false);
062: }
063:
064: /**
065: * Creates a new MaxIncFIDMapper object that will return the FID columns as
066: * business attributes.
067: *
068: * @param tableSchemaName the schema of this table
069: * @param tableName the table name
070: * @param FIDColumn the name of the FID column
071: * @param FIDColumnType The SQL type of the column - must be a numeric type
072: * @param returnFIDColumnsAsAttributes true to return FID columns as
073: * attributes.
074: */
075: public MaxIncFIDMapper(String tableSchemaName, String tableName,
076: String FIDColumn, int FIDColumnType,
077: boolean returnFIDColumnsAsAttributes) {
078: super (tableSchemaName, tableName);
079: this .returnFIDColumnsAsAttributes = returnFIDColumnsAsAttributes;
080: setInfo(FIDColumn, FIDColumnType, 0, 0, false);
081: }
082:
083: /**
084: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getID(java.lang.Object[])
085: */
086: public String getID(Object[] attributes) {
087: return String.valueOf(attributes[0]);
088: }
089:
090: /**
091: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getPKAttributes(java.lang.String)
092: */
093: public Object[] getPKAttributes(String FID) {
094: try {
095: return new Object[] { new Long(Long.parseLong(FID)) };
096: } catch (NumberFormatException e) {
097: return new Object[] { new Long(-1) };
098: }
099: }
100:
101: /**
102: * @see java.lang.Object#equals(java.lang.Object)
103: */
104: public boolean equals(Object object) {
105: if (!(object instanceof TypedFIDMapper)) {
106: return false;
107: }
108:
109: MaxIncFIDMapper other = (MaxIncFIDMapper) object;
110:
111: return (other.getColumnName() == this .getColumnName())
112: && (other.getColumnType() == this .getColumnType())
113: && (other.returnFIDColumnsAsAttributes == this .returnFIDColumnsAsAttributes);
114: }
115:
116: /**
117: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#createID(java.sql.Connection,
118: * Feature, Statement)
119: */
120: public String createID(Connection conn, Feature feature,
121: Statement statement) throws IOException {
122: Statement stmt = null;
123: ResultSet rs = null;
124: try {
125: stmt = conn.createStatement();
126: rs = stmt.executeQuery("Select MAX(" + getColumnName()
127: + ") from " + tableName);
128:
129: if (rs.next()) {
130: long maxFid = rs.getLong(1);
131:
132: return String.valueOf(maxFid + 1);
133: } else {
134: throw new DataSourceException("Could not get MAX for "
135: + tableName + "." + getColumnName()
136: + ": No result returned from query");
137: }
138: } catch (SQLException e) {
139: throw new DataSourceException(
140: "An sql problem occurred. Are the table and the fid column there?",
141: e);
142: } finally {
143: if (stmt != null) {
144: try {
145: stmt.close();
146: } catch (SQLException e) {
147: LOGGER.log(Level.WARNING,
148: "MaxIncFidMapper could not close statement:"
149: + e, e);
150: }
151: }
152: if (rs != null) {
153: try {
154: rs.close();
155: } catch (SQLException e) {
156: LOGGER.log(Level.WARNING,
157: "MaxIncFidMapper could not close resultset:"
158: + e, e);
159: }
160: }
161: }
162: }
163: }
|