001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/datastore/sql/transaction/insert/InsertField.java $
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036 package org.deegree.io.datastore.sql.transaction.insert;
037
038 import java.util.ArrayList;
039 import java.util.Collection;
040 import java.util.Iterator;
041 import java.util.List;
042
043 import org.deegree.datatypes.Types;
044 import org.deegree.datatypes.UnknownTypeException;
045 import org.deegree.framework.log.ILogger;
046 import org.deegree.framework.log.LoggerFactory;
047
048 /**
049 * A field of an {@link InsertRow}.
050 *
051 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
052 * @author last edited by: $Author: mschneider $
053 *
054 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
055 */
056 public class InsertField {
057
058 private static final ILogger LOG = LoggerFactory.getLogger( InsertField.class );
059
060 private String columnName;
061
062 private Object value;
063
064 private int sqlType;
065
066 private boolean isPK;
067
068 private InsertField referencedField;
069
070 private List<InsertField> referencingFields = new ArrayList<InsertField>();
071
072 private InsertRow row;
073
074 /**
075 * Creates a new {@link InsertField} instance.
076 *
077 * @param row
078 * @param columnName
079 * @param sqlType
080 * @param value
081 * @param isPK
082 */
083 InsertField( InsertRow row, String columnName, int sqlType, Object value, boolean isPK ) {
084 this.row = row;
085 this.columnName = columnName;
086 this.sqlType = sqlType;
087 this.value = value;
088 this.isPK = isPK;
089 }
090
091 /**
092 * Creates a new {@link InsertField} instance.
093 *
094 * @param row
095 * @param columnName
096 * @param referencedField
097 */
098 InsertField( InsertRow row, String columnName, InsertField referencedField ) {
099 this.row = row;
100 this.columnName = columnName;
101 this.referencedField = referencedField;
102 }
103
104 /**
105 * Returns the {@link InsertRow} that this <code>InsertField</code> belongs to.
106 *
107 * @return the {@link InsertRow} that this <code>InsertField</code> belongs to
108 */
109 public InsertRow getRow() {
110 return this.row;
111 }
112
113 /**
114 * Returns the name of the table that this <code>InsertField</code> belongs to.
115 *
116 * @return the name of the table that this <code>InsertField</code> belongs to
117 */
118 public String getTable() {
119 return this.row.getTable();
120 }
121
122 /**
123 * Returns whether this <code>InsertField</code> is a primary key of the table.
124 *
125 * @return true, if this InsertField is a primary key of the table, false otherwise
126 */
127 public boolean isPK() {
128 return this.isPK;
129 }
130
131 /**
132 * Returns the name of the column that this <code>InsertField</code> represents.
133 *
134 * @return the name of the column that this <code>InsertField</code> represents
135 */
136 public String getColumnName() {
137 return this.columnName;
138 }
139
140 /**
141 * Returns the sql type code for the column.
142 *
143 * @return the sql type code for the column
144 */
145 public int getSQLType() {
146 if ( this.referencedField != null ) {
147 return this.referencedField.getSQLType();
148 }
149 return this.sqlType;
150 }
151
152 /**
153 * Returns the value to be inserted.
154 *
155 * @return the value to be inserted
156 */
157 public Object getValue() {
158 if ( this.referencedField != null ) {
159 return this.referencedField.getValue();
160 }
161 return this.value;
162 }
163
164 /**
165 * Returns the <code>InsertField</code> that this <code>InsertField</code> references.
166 *
167 * @return the referenced InsertField or null, if no field is referenced
168 */
169 public InsertField getReferencedField() {
170 return this.referencedField;
171 }
172
173 /**
174 * Returns the {@link InsertRow} that is referenced by this <code>InsertField</code>.
175 *
176 * @return the referenced {@link InsertRow}
177 */
178 InsertRow getReferencedRow() {
179 if ( this.referencedField == null ) {
180 return null;
181 }
182 return this.referencedField.row;
183 }
184
185 /**
186 * Sets this <code>InsertField</code> to be a reference to the given <code>InsertField</code>.
187 *
188 * @param field
189 */
190 public void relinkField( InsertField field ) {
191 if ( referencedField != null ) {
192 this.referencedField.removeReferencingField( this );
193 }
194 this.referencedField = field;
195 field.addReferencingField( this );
196 }
197
198 /**
199 * @param referencingField
200 */
201 void addReferencingField( InsertField referencingField ) {
202 if ( referencingField == null ) {
203 LOG.logError( "addReferencingField() called with null value" );
204 } else {
205 this.referencingFields.add( referencingField );
206 }
207 }
208
209 /**
210 * Removes the given <code>InsertField</code> from the list of references to this <code>InsertField</code>.
211 *
212 * @param referencingField
213 */
214 public void removeReferencingField( InsertField referencingField ) {
215 this.referencingFields.remove( referencingField );
216 }
217
218 /**
219 * Returns all fields that reference this field.
220 *
221 * @return all fields that reference this field
222 */
223 Collection<InsertField> getReferencingFields() {
224 return this.referencingFields;
225 }
226
227 /**
228 * Returns all rows that reference this field.
229 *
230 * @return all rows that reference this field
231 */
232 Collection<InsertRow> getReferencingRows() {
233 List<InsertRow> referencingRows = new ArrayList<InsertRow>();
234 Iterator<InsertField> iter = this.referencingFields.iterator();
235 while ( iter.hasNext() ) {
236 referencingRows.add( iter.next().row );
237 }
238 return referencingRows;
239 }
240
241 /**
242 * Returns a string representation of the object.
243 *
244 * @return a string representation of the object
245 */
246 @Override
247 public String toString() {
248 StringBuffer sb = new StringBuffer();
249 String stringValue = getValue().toString();
250 if ( stringValue.length() > 20 ) {
251 stringValue = stringValue.substring( 0, 19 );
252 stringValue += "...";
253 }
254
255 int type = getSQLType();
256 String typeString = "" + type;
257 try {
258 typeString = Types.getTypeNameForSQLTypeCode( type );
259 } catch ( UnknownTypeException e ) {
260 LOG.logError( e.getMessage(), e );
261 }
262 sb.append( getColumnName() );
263 sb.append( "(" );
264 sb.append( typeString );
265 sb.append( ")='" );
266 sb.append( stringValue );
267 sb.append( '\'' );
268
269 InsertField referencedField = getReferencedField();
270 if ( referencedField != null ) {
271 sb.append( " [fk to '" );
272 sb.append( referencedField.getTable() );
273 sb.append( '.' );
274 sb.append( referencedField.getColumnName() );
275 sb.append( "']" );
276 }
277 return sb.toString();
278 }
279 }