001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.jdbc;
017:
018: import java.net.URLDecoder;
019: import java.sql.ResultSet;
020:
021: /**
022: * Primary key of a table.
023: *
024: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
025: *
026: */
027: public class PrimaryKey {
028: /**
029: * The columns making up the primary key
030: */
031: public Column[] columns;
032:
033: /**
034: * Creates a new primary key.
035: */
036: public PrimaryKey(Column[] columns) {
037: this .columns = columns;
038: }
039:
040: /**
041: * Decodes a featureId into an array of objects which map to the columns
042: * of the primary key.
043: *
044: * @param fid The featureId.
045: *
046: * @return An array of values which map the primary key columns making up
047: * the featureId.
048: *
049: * @throws Exception
050: */
051: public Object[] decode(String fid) throws Exception {
052: Object[] values = new Object[columns.length];
053: String[] tokens = fid.split("&");
054:
055: if (tokens.length != columns.length) {
056: throw new RuntimeException("fid: " + fid
057: + " does not map to primary key with "
058: + columns.length + " elements");
059: }
060:
061: for (int i = 0; i < tokens.length; i++) {
062: values[i] = URLDecoder.decode(tokens[i], "UTF-8");
063: }
064:
065: return values;
066: }
067:
068: /**
069: * Encodes a table row into a featureId by obtaining the primary key values
070: * from the row.
071: *
072: * @param rs A result set pointing to a paritcular table row.
073: *
074: * @return A featureid for the row.
075: *
076: * @throws Exception
077: */
078: public String encode(ResultSet rs) throws Exception {
079: StringBuffer fid = new StringBuffer();
080:
081: for (int i = 0; i < columns.length; i++) {
082: //TODO: run column[i].type through converter to string
083: Object value = rs.getObject(columns[i].name);
084: fid.append(value.toString());
085:
086: if (i < (columns.length - 1)) {
087: fid.append("&");
088: }
089: }
090:
091: return fid.toString();
092: }
093:
094: /**
095: * A column in a primary key.
096: *
097: */
098: public static class Column {
099: /**
100: * THe column name;
101: */
102: public String name;
103:
104: /**
105: * The column type.
106: */
107: public Class type;
108:
109: /**
110: * Flag indicating wether value for the columns is auto-generated by the
111: * database
112: */
113: public boolean isAutoGenerated = false;
114:
115: /**
116: * Name of a sequence which generates values for the column.
117: */
118: public String sequence = null;
119:
120: public Column(String name, Class type) {
121: this.name = name;
122: this.type = type;
123: }
124: }
125: }
|