001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/datastore/sql/transaction/delete/TableReference.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     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.delete;
044    
045    import org.deegree.io.datastore.schema.TableRelation;
046    import org.deegree.io.datastore.schema.TableRelation.FK_INFO;
047    import org.deegree.io.datastore.schema.content.MappingField;
048    
049    /**
050     * Represents a reference from a table to another table. This corresponds to a {@link TableRelation}
051     * declaration in an annotated GML schema, but with unambigious direction: the source (from) table
052     * contains the foreign key.
053     * 
054     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
055     * @author last edited by: $Author: apoth $
056     * 
057     * @version $Revision: 9342 $, $Date: 2007-12-27 13:32:57 +0100 (Do, 27 Dez 2007) $
058     */
059    class TableReference {
060    
061        private String fromTable;
062    
063        private String toTable;
064    
065        private MappingField[] fkColumns;
066    
067        private MappingField[] keyColumns;
068    
069        /**
070         * Creates a new <code>TableReference</code> instance from the given {@link TableRelation}.
071         * 
072         * @param relation
073         */
074        TableReference( TableRelation relation ) {
075            if ( relation.getFKInfo() == FK_INFO.fkIsFromField ) {
076                this.fromTable = relation.getFromTable();
077                this.fkColumns = relation.getFromFields();
078                this.toTable = relation.getToTable();
079                this.keyColumns = relation.getToFields();
080            } else if ( relation.getFKInfo() == FK_INFO.fkIsToField ) {
081                this.fromTable = relation.getToTable();
082                this.fkColumns = relation.getToFields();
083                this.toTable = relation.getFromTable();
084                this.keyColumns = relation.getFromFields();
085            } else {
086                throw new IllegalArgumentException( "Cannot create TableReference without "
087                                                    + "fk (foreign key) information: " + relation );
088            }
089        }
090    
091        /**
092         * Returns the columns that build the foreign key (in the 'from' table).
093         * 
094         * @return the columns that build the foreign key (in the 'from' table)
095         */
096        MappingField[] getFkColumns() {
097            return this.fkColumns;
098        }
099    
100        /**
101         * Returns the name of the 'from' table.
102         * 
103         * @return the name of the 'from' table
104         */
105        String getFromTable() {
106            return this.fromTable;
107        }
108    
109        /**
110         * Returns the columns that build the key (in the 'to' table).
111         * 
112         * @return the columns that build the key (in the 'to' table)
113         */
114        MappingField[] getKeyColumns() {
115            return this.keyColumns;
116        }
117    
118        /**
119         * Returns the name of the 'to' table.
120         * 
121         * @return the name of the 'to' table
122         */
123        String getToTable() {
124            return this.toTable;
125        }
126    
127        @Override
128        public String toString() {
129            StringBuffer sb = new StringBuffer();
130            sb.append( this.fromTable );
131            sb.append( " [" );
132            for ( int i = 0; i < this.fkColumns.length; i++ ) {
133                sb.append( fkColumns[i].getField() );
134                if ( i != this.fkColumns.length - 1 ) {
135                    sb.append( ',' );
136                }
137            }
138            sb.append( "] -> " );
139            sb.append( this.toTable );
140            sb.append( " [" );
141            for ( int i = 0; i < this.fkColumns.length; i++ ) {
142                sb.append( keyColumns[i].getField() );
143                if ( i != this.fkColumns.length - 1 ) {
144                    sb.append( ',' );
145                }
146            }
147            sb.append( "]" );
148            return sb.toString();
149        }
150    }