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: */
018: /*
019: * Created on 18-apr-2004
020: * 12-jul-2006 D. Adler GEOT-728 Refactor FIDMapper classes
021: */
022: package org.geotools.data.jdbc.fidmapper;
023:
024: import org.geotools.data.DataSourceException;
025: import org.geotools.feature.Feature;
026: import java.io.IOException;
027: import java.io.UnsupportedEncodingException;
028: import java.net.URLDecoder;
029: import java.net.URLEncoder;
030: import java.sql.Connection;
031: import java.sql.Statement;
032:
033: /**
034: * A simple implementation of FIDMapper for multi column primary keys
035: *
036: * @author wolf
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/fidmapper/MultiColumnFIDMapper.java $
038: */
039: public class MultiColumnFIDMapper extends AbstractFIDMapper {
040: private static final long serialVersionUID = 1L;
041: private static final String UTF8 = "UTF-8";
042:
043: /**
044: * Builds a new instance of the MultiColumnFIDMapper
045: *
046: * @param tableSchemaName
047: * @param tableName
048: * @param colNames - column names
049: * @param colTypes - column types, see {@link java.sql.Types}
050: * @param colSizes - column sizes
051: * @param colDecimalDigits - column decimals
052: * @param autoIncrement - flags for auto-increment tests
053: *
054: * @throws IllegalArgumentException
055: */
056: public MultiColumnFIDMapper(String tableSchemaName,
057: String tableName, String[] colNames, int[] colTypes,
058: int[] colSizes, int[] colDecimalDigits,
059: boolean[] autoIncrement) {
060: super (tableSchemaName, tableName);
061: if ((colNames == null) || (colTypes == null)
062: || (autoIncrement == null)) {
063: throw new IllegalArgumentException(
064: "Column description arrays must be not null");
065: }
066:
067: if (colNames.length == 0) {
068: throw new IllegalArgumentException(
069: "Column description arrays must be not empty");
070: }
071:
072: if ((colNames.length != colTypes.length)
073: || (colNames.length != autoIncrement.length)) {
074: throw new IllegalArgumentException(
075: "Column description arrays must have the same size");
076: }
077:
078: this .colNames = colNames;
079: this .colTypes = colTypes;
080: this .colSizes = colSizes;
081: this .colDecimalDigits = colDecimalDigits;
082: this .autoIncrement = autoIncrement;
083: this .returnFIDColumnsAsAttributes = true;
084: }
085:
086: /**
087: * Builds a new instance of the MultiColumnFIDMapper
088: *
089: * @param colNames - column names
090: * @param colTypes - column types, see {@link java.sql.Types}
091: * @param colSizes - column sizes
092: * @param colDecimalDigits - column decimals
093: * @param autoIncrement - flags for auto-increment tests
094: *
095: * @throws IllegalArgumentException
096: */
097: public MultiColumnFIDMapper(String[] colNames, int[] colTypes,
098: int[] colSizes, int[] colDecimalDigits,
099: boolean[] autoIncrement) {
100: this (null, null, colNames, colTypes, colSizes,
101: colDecimalDigits, autoIncrement);
102: }
103:
104: /**
105: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getID(java.lang.Object[])
106: */
107: public String getID(Object[] attributes) {
108: StringBuffer sb = new StringBuffer();
109:
110: try {
111: for (int i = 0; i < attributes.length; i++) {
112: sb.append(URLEncoder.encode(attributes[i].toString(),
113: UTF8));
114:
115: if (i < (attributes.length - 1)) {
116: sb.append("&");
117: }
118: }
119: } catch (UnsupportedEncodingException e) {
120: // c'mon, don't tell me UTF-8 is not supported ;-)
121: }
122:
123: return sb.toString();
124: }
125:
126: /**
127: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getPKAttributes(java.lang.String)
128: */
129: public Object[] getPKAttributes(String FID) throws IOException {
130: String[] attributes = FID.split("&");
131:
132: if (attributes.length != colNames.length) {
133: throw new DataSourceException(
134: "The FID is not compatible with MultiColumnFIDMapper, was expecting "
135: + colNames.length
136: + " URL-encoded columns and got "
137: + attributes.length + " columns");
138: }
139:
140: for (int i = 0; i < attributes.length; i++) {
141: attributes[i] = URLDecoder.decode(attributes[i], UTF8);
142: }
143:
144: return attributes;
145: }
146:
147: /**
148: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#createID(java.sql.Connection,
149: * org.geotools.feature.Feature, Statement)
150: */
151: public String createID(Connection conn, Feature feature,
152: Statement statement) throws IOException {
153: String[] attValues = new String[colNames.length];
154:
155: for (int i = 0; i < colNames.length; i++) {
156: attValues[i] = feature.getAttribute(colNames[i]).toString();
157: }
158:
159: return getID(attValues);
160: }
161:
162: }
|