01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.postgis.fidmapper;
17:
18: import java.io.IOException;
19: import java.rmi.server.UID;
20: import java.sql.Connection;
21: import java.sql.Statement;
22:
23: import org.geotools.data.DataSourceException;
24: import org.geotools.data.jdbc.fidmapper.MultiColumnFIDMapper;
25: import org.geotools.feature.Feature;
26: import org.geotools.feature.IllegalAttributeException;
27:
28: /**
29: * Covers both the basic and multicolumn fid mappers
30: *
31: * @author aaime
32: * @since 2.4
33: */
34: class VersionedMulticolumnFIDMapper extends MultiColumnFIDMapper
35: implements VersionedFIDMapper {
36:
37: public VersionedMulticolumnFIDMapper(String tableSchemaName,
38: String tableName, String[] colNames, int[] colTypes,
39: int[] colSizes, int[] colDecimalDigits,
40: boolean[] autoIncrement) {
41: super (tableSchemaName, tableName, colNames, colTypes, colSizes,
42: colDecimalDigits, autoIncrement);
43: returnFIDColumnsAsAttributes = true;
44: }
45:
46: public String getUnversionedFid(String versionedFID) {
47: // we assume revision is the last column, since it has been added with
48: // an alter table "add". Also, we make the fid "typed" to ensure WFS keeps on working
49: return tableName + "."
50: + versionedFID.substring(versionedFID.indexOf('&') + 1);
51: }
52:
53: public String createVersionedFid(String externalFID, long revision) {
54: return revision + "&"
55: + externalFID.substring(tableName.length() + 1);
56: }
57:
58: public Object[] getUnversionedPKAttributes(String FID)
59: throws IOException {
60: // check we can parse this
61: if (!FID.startsWith(tableName + "."))
62: throw new DataSourceException(
63: "The FID is invalid, should start with '"
64: + tableName + "', it's " + FID + " instead");
65:
66: // leverage superclass parsing, then throw away the last element
67: Object[] values = super .getPKAttributes("0&"
68: + FID.substring(tableName.length() + 1));
69: Object[] unversioned = new Object[values.length - 1];
70: System.arraycopy(values, 1, unversioned, 0, unversioned.length);
71: return unversioned;
72: }
73:
74: public String createID(Connection conn, Feature feature,
75: Statement statement) throws IOException {
76: if (colNames.length == 2
77: && feature.getAttribute(colNames[1]) == null) {
78: try {
79: feature.setAttribute(colNames[1], (new UID())
80: .toString());
81: } catch (IllegalAttributeException e) {
82: throw new DataSourceException(
83: "Could not generate key for the "
84: + "unset primary key column "
85: + colNames[0], e);
86: }
87: }
88: return super .createID(conn, feature, statement);
89: }
90:
91: public Object[] getPKAttributes(String FID) throws IOException {
92: return super.getPKAttributes(FID);
93: }
94:
95: }
|