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