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: * TODO: 26-may-2005 D. Adler Removed returnFIDColumnsAsAttributes
017: * variable and related accessor method.
018: * 12-jul-2006 D. Adler GEOT-728 Refactor FIDMapper classes
019: */
020: package org.geotools.data.jdbc.fidmapper;
021:
022: import org.geotools.feature.Feature;
023: import java.io.IOException;
024: import java.rmi.server.UID;
025: import java.sql.Connection;
026: import java.sql.Statement;
027: import java.sql.Types;
028:
029: /**
030: * Basic FIDMapper implementation that maps the FID of a Feature to a VARCHAR
031: * column
032: *
033: * @author aaime Andrea Aime
034: * @author Dani Daniele Franzoni
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/fidmapper/BasicFIDMapper.java $
036: */
037: public class BasicFIDMapper extends AbstractFIDMapper {
038: private static final long serialVersionUID = 1L;
039:
040: /**
041: * Create a new BasicFIDMapper
042: *
043: * @param fieldName
044: * @param fieldSize
045: */
046: public BasicFIDMapper(String fieldName, int fieldSize) {
047: this (fieldName, fieldSize, false);
048: }
049:
050: /**
051: * Create a new BasicFIDMapper
052: *
053: * @param tableSchemaName
054: * @param tableName
055: * @param fieldName
056: * @param fieldSize
057: * @param returnFIDColumnsAsAttributes
058: */
059: public BasicFIDMapper(String tableSchemaName, String tableName,
060: String fieldName, int fieldSize,
061: boolean returnFIDColumnsAsAttributes) {
062: super (tableSchemaName, tableName);
063: this .returnFIDColumnsAsAttributes = returnFIDColumnsAsAttributes;
064: setInfo(fieldName, Types.VARCHAR, fieldSize, 0, false);
065: }
066:
067: /**
068: * Create a new BasicFIDMapper
069: *
070:
071: * @param fieldName
072: * @param fieldSize
073: * @param returnFIDColumnsAsAttributes
074: */
075: public BasicFIDMapper(String fieldName, int fieldSize,
076: boolean returnFIDColumnsAsAttributes) {
077: this (null, null, fieldName, fieldSize,
078: returnFIDColumnsAsAttributes);
079: }
080:
081: /**
082: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getID(java.lang.Object[])
083: */
084: public String getID(Object[] attributes) {
085: if ((attributes != null) && (attributes.length == 1)
086: && (attributes[0] != null)) {
087: return attributes[0].toString();
088: } else {
089: return null;
090: }
091: }
092:
093: /**
094: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getPKAttributes(java.lang.String)
095: */
096: public Object[] getPKAttributes(String FID) {
097: return new Object[] { FID };
098: }
099:
100: /**
101: * @see java.lang.Object#equals(java.lang.Object)
102: */
103: public boolean equals(Object object) {
104: if (!(object instanceof BasicFIDMapper)) {
105: return false;
106: }
107:
108: BasicFIDMapper other = (BasicFIDMapper) object;
109:
110: return other.getColumnName().equals(getColumnName())
111: && (other.getColumnSize() == getColumnSize());
112: }
113:
114: /**
115: * This kind of FIDMapper does not generate keys, they must be alreadyT
116: * present in the primary key.
117: *
118: * @see org.geotools.data.fidmapper.FIDMapper#createID(Connection, Feature,
119: * Statement)
120: */
121: public String createID(Connection conn, Feature feature,
122: Statement statement) throws IOException {
123: //JD: replacing no word characters with underscore
124: //JD: forcing to start with a latter
125: // According to GML and XML schema standards, FID is a XML ID
126: // (http://www.w3.org/TR/xmlschema-2/#ID), whose acceptable values are those that match an
127: // NCNAME production (http://www.w3.org/TR/1999/REC-xml-names-19990114/#NT-NCName):
128: // NCName ::= (Letter | '_') (NCNameChar)* /* An XML Name, minus the ":" */
129: // NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
130: // We have to fix the generated UID replacing all non word chars with an _ (it seems
131: // they area all ":")
132: // return "fid-" + new UID().toString().replaceAll( "\\W","_" );
133: // optimization, since the UID toString uses only ":" and converts long and integers
134: // to strings for the rest, so the only non word character is really ":"
135: return "fid-" + new UID().toString().replace(':', '_');
136: }
137:
138: }
|