001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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 Move returnFIDColumnsAsAttributes here from subclasses.
017: */
018: /*
019: * 26-may-2005 D. Adler Move returnFIDColumnsAsAttributes here
020: * from subclasses.
021: * 17-Jul-2006 D. Adler GEOT-728 Refactor FIDMapper classes
022: */
023: package org.geotools.data.jdbc.fidmapper;
024:
025: /**
026: * Abstract implementation providing common methods that usually are coded the
027: * same way in all fid mappers.
028: *
029: * @author wolf
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/fidmapper/AbstractFIDMapper.java $
031: */
032: public abstract class AbstractFIDMapper implements FIDMapper {
033: protected boolean[] autoIncrement = new boolean[1];
034: protected int[] colDecimalDigits = new int[1];
035: protected String[] colNames = new String[1];
036: protected int[] colSizes = new int[1];
037: protected int[] colTypes = new int[1];
038: protected String tableName = null;
039: protected String tableSchemaName = null;
040: /** Set if table FID columns are to be returned as business attributes. */
041: protected boolean returnFIDColumnsAsAttributes = false;
042:
043: /**
044: * Default constructor for AbstractFIDMapper
045: *
046: */
047: public AbstractFIDMapper() {
048:
049: }
050:
051: /**
052: * Set common values for all FID mappers.
053: *
054: * @param tableSchemaName
055: * @param tableName
056: */
057: public AbstractFIDMapper(String tableSchemaName, String tableName) {
058: this .tableSchemaName = tableSchemaName;
059: this .tableName = tableName;
060: }
061:
062: /**
063: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnCount()
064: */
065: public int getColumnCount() {
066: if (colNames[0] == null)
067: return 0;
068: else
069: return colNames.length;
070: }
071:
072: /**
073: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnCount()
074: */
075: protected int getColumnDecimalDigits() {
076: return getColumnDecimalDigits(0);
077: }
078:
079: /**
080: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnDecimalDigits(int)
081: */
082: public int getColumnDecimalDigits(int colIndex) {
083: if (colIndex > 0 && colIndex < getColumnCount()) {
084: return colDecimalDigits[colIndex];
085: } else {
086: return 0;
087: }
088: }
089:
090: protected String getColumnName() {
091: return getColumnName(0);
092: }
093:
094: /**
095: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnName(int)
096: */
097: public String getColumnName(int colIndex) {
098: if (colIndex >= 0 && colIndex < getColumnCount()) {
099: return colNames[colIndex];
100: } else {
101: return null;
102: }
103: }
104:
105: protected int getColumnSize() {
106: return getColumnSize(0);
107: }
108:
109: /**
110: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnSize(int)
111: */
112: public int getColumnSize(int colIndex) {
113: if (colIndex >= 0 && colIndex < getColumnCount()) {
114: return colSizes[colIndex];
115: } else {
116: return -1;
117: }
118: }
119:
120: protected int getColumnType() {
121: return getColumnType(0);
122: }
123:
124: /**
125: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#getColumnType(int)
126: */
127: public int getColumnType(int colIndex) {
128: if (colIndex >= 0 && colIndex < getColumnCount()) {
129: return colTypes[colIndex];
130: } else {
131: throw new ArrayIndexOutOfBoundsException(colIndex);
132: }
133: }
134:
135: /**
136: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#hasAutoIncrementColumns()
137: */
138: public boolean hasAutoIncrementColumns() {
139: for (int i = 0; i < getColumnCount(); i++) {
140: if (isAutoIncrement(i)) {
141: return true;
142: }
143: }
144: return false;
145: }
146:
147: /**
148: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#initSupportStructures()
149: */
150: public void initSupportStructures() {
151: // nothing to do
152: }
153:
154: protected boolean isAutoIncrement() {
155: return isAutoIncrement(0);
156: }
157:
158: /**
159: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#isAutoIncrement(int)
160: */
161: public boolean isAutoIncrement(int colIndex) {
162: if (colIndex >= 0 && colIndex < getColumnCount()) {
163: return autoIncrement[colIndex];
164: } else {
165: return true;
166: }
167: }
168:
169: /**
170: * Convenience since most FID mappers should be persistent, override on the
171: * specific ones that aren't.
172: *
173: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#isVolatile()
174: */
175: public boolean isVolatile() {
176: return false;
177: }
178:
179: /**
180: * @see org.geotools.data.jdbc.fidmapper.FIDMapper#returnFIDColumnsAsAttributes()
181: */
182: public boolean returnFIDColumnsAsAttributes() {
183: return returnFIDColumnsAsAttributes;
184: }
185:
186: protected void setInfo(String colName, int colType, int colSize,
187: int colDecimalDigits, boolean autoIncrement) {
188: this .colNames[0] = colName;
189: this .colTypes[0] = colType;
190: this .colSizes[0] = colSize;
191: this .colDecimalDigits[0] = colDecimalDigits;
192: this .autoIncrement[0] = autoIncrement;
193: }
194:
195: /**
196: * Create a nice string representation of a FID Mapper
197: *
198: *
199: * @return the String representation
200: */
201: public String toString() {
202: String mapperName = getClass().toString();
203: String colInfo = "";
204:
205: if (getColumnCount() > 0) {
206: colInfo = getColumnName(0) + ":" + getColumnType(0) + ":"
207: + getColumnSize(0) + ":"
208: + getColumnDecimalDigits(0);
209: }
210:
211: String s = mapperName + ":" + getColumnCount() + ":" + colInfo
212: + ":" + returnFIDColumnsAsAttributes() + ":"
213: + hasAutoIncrementColumns() + ":" + "";
214:
215: return s;
216: }
217:
218: public String getTableName() {
219: return tableName;
220: }
221:
222: public String getTableSchemaName() {
223: return tableSchemaName;
224: }
225: }
|