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