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.sql.Connection;
20: import java.sql.Statement;
21: import java.sql.Types;
22:
23: import org.geotools.data.DataSourceException;
24: import org.geotools.data.jdbc.fidmapper.MultiColumnFIDMapper;
25: import org.geotools.feature.Feature;
26:
27: /**
28: * Covers both the basic and multicolumn fid mappers
29: *
30: * @author aaime
31: * @since 2.4
32: *
33: */
34: class VersionedAutoincrementFIDMapper extends MultiColumnFIDMapper
35: implements VersionedFIDMapper {
36: protected PostGISAutoIncrementFIDMapper autoIncrementMapper;
37:
38: public VersionedAutoincrementFIDMapper(String tableSchemaName,
39: String tableName, String colName, int colType, int colSize) {
40: super (tableSchemaName, tableName, new String[] { "revision",
41: colName }, new int[] { colType, Types.NUMERIC },
42: new int[] { colSize, 8 }, new int[] { 0, 0 },
43: new boolean[] { false, true });
44: returnFIDColumnsAsAttributes = true;
45: autoIncrementMapper = new PostGISAutoIncrementFIDMapper(
46: tableSchemaName, tableName, colName, colType);
47: }
48:
49: public String getUnversionedFid(String versionedFID) {
50: // we assume revision is the last column, since it has been added with
51: // an alter table "add". Also, we make the fid "typed" to ensure WFS keeps on working
52: return tableName
53: + "."
54: + versionedFID
55: .substring(versionedFID.lastIndexOf('&') + 1);
56: }
57:
58: public String createVersionedFid(String extenalFID, long revision) {
59: return revision + "&"
60: + extenalFID.substring(tableName.length() + 1);
61: }
62:
63: public Object[] getUnversionedPKAttributes(String FID)
64: throws IOException {
65: // check we can parse this
66: if (!FID.startsWith(tableName + "."))
67: throw new DataSourceException(
68: "The FID is invalid, should start with '"
69: + tableName + ".'");
70:
71: // leverate superclass parsing, then throw away the last element
72: Object[] values = getPKAttributes(FID.substring(tableName
73: .length() + 1)
74: + "&0");
75: Object[] unversioned = new Object[values.length - 1];
76: System.arraycopy(values, 0, unversioned, 0, unversioned.length);
77: return unversioned;
78: }
79:
80: public String createID(Connection conn, Feature feature,
81: Statement statement) throws IOException {
82: if (feature.getAttribute(colNames[1]) == null) {
83: try {
84: String id = autoIncrementMapper.createID(conn, feature,
85: statement);
86: feature.setAttribute(colNames[1], new Long(id));
87: } catch (Exception e) {
88: throw new DataSourceException(
89: "Could not generate key for the "
90: + "unset primary key column "
91: + colNames[1], e);
92: }
93: }
94: return super.createID(conn, feature, statement);
95: }
96:
97: }
|