001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/datastore/schema/TableRelation.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.schema;
037    
038    import org.deegree.io.datastore.DatastoreTransaction;
039    import org.deegree.io.datastore.idgenerator.IdGenerationException;
040    import org.deegree.io.datastore.idgenerator.IdGenerator;
041    import org.deegree.io.datastore.schema.content.MappingField;
042    
043    /**
044     * Describes a relation (join condition) between two database tables.
045     *
046     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
047     * @author last edited by: $Author: mschneider $
048     *
049     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
050     */
051    public class TableRelation {
052    
053        /**
054         * Used to represent the foreign key position.
055         */
056        public static enum FK_INFO {
057    
058            /**
059             * No foreign key information available (sufficient for read access).
060             */
061            noFKInfo,
062    
063            /**
064             * Foreign stored in 'To'-table, primary key in 'From'-table.
065             */
066            fkIsToField,
067    
068            /**
069             * Foreign stored in 'From'-table, primary key in 'To'-table.
070             */
071            fkIsFromField
072        }
073    
074        private MappingField[] fromFields;
075    
076        private MappingField[] toFields;
077    
078        private FK_INFO fkInfo;
079    
080        private IdGenerator idGenerator;
081    
082        /**
083         * Initializes a newly created <code>TableRelation</code> instance with the given
084         * parameters.
085         *
086         * @param fromFields
087         * @param toFields
088         * @param fkInfo
089         * @param idGenerator
090         */
091        public TableRelation( MappingField[] fromFields, MappingField[] toFields, FK_INFO fkInfo,
092                             IdGenerator idGenerator ) {
093            if ( fromFields.length < 1 ) {
094                throw new IllegalArgumentException(
095                    "A relation between two tables must have at least one 'from' field." );
096            }
097            if ( toFields.length < 1 ) {
098                throw new IllegalArgumentException(
099                    "A relation between two tables must have at least one 'to' field." );
100            }
101            if ( fromFields.length != toFields.length ) {
102                throw new IllegalArgumentException(
103                    "A relation between two tables must have the same number of 'from' and 'to' fields." );
104            }
105            this.fromFields = fromFields;
106            this.toFields = toFields;
107            this.fkInfo = fkInfo;
108            this.idGenerator = idGenerator;
109        }
110    
111        /**
112         * Returns the name of the table where the relation starts ('From'-table).
113         *
114         * @return the name of the table where the relation starts ('From'-table)
115         */
116        public String getFromTable() {
117            return fromFields[0].getTable();
118        }
119    
120        /**
121         * Returns the name of the table where the relation ends ('To'-table).
122         *
123         * @return the name of the table where the relation ends ('To'-table)
124         */
125        public String getToTable() {
126            return toFields[0].getTable();
127        }
128    
129        /**
130         * Returns the {@link MappingField}s that constitute the key in the 'From'-table.
131         *
132         * @return the MappingFields that constitute the key in the 'From'-table
133         */
134        public MappingField [] getFromFields () {
135            return this.fromFields;
136        }
137    
138        /**
139         * Returns the {@link MappingField}s that constitute the key in the 'To'-table.
140         *
141         * @return the MappingFields that constitute the key in the 'To'-table
142         */
143        public MappingField [] getToFields () {
144            return this.toFields;
145        }
146    
147        /**
148         * Returns the foreign key position.
149         *
150         * @return the foreign key position
151         */
152        public FK_INFO getFKInfo () {
153            return this.fkInfo;
154        }
155    
156        /**
157         * Returns whether the foreign key is stored in the 'From'-table.
158         *
159         * @return true, if foreign key information is available and foreign key is in 'From'-table,
160         *         false otherwise
161         */
162        public boolean isFromFK() {
163            boolean isFromFK = false;
164            switch (this.fkInfo) {
165            case fkIsFromField: {
166                isFromFK = true;
167                break;
168            }
169            case fkIsToField: {
170                isFromFK = false;
171                break;
172            }
173            default: {
174                throw new RuntimeException( "No foreign key information available for relation: "
175                    + this );
176            }
177            }
178            return isFromFK;
179        }
180    
181    
182        /**
183         * Returns the {@link IdGenerator} instance that may be used to generate new primary keys.
184         *
185         * @return IdGenerator instance that may be used to generate new primary keys
186         */
187        public IdGenerator getIdGenerator () {
188            return this.idGenerator;
189        }
190    
191        /**
192         * Returns a new primary key generated by the associated {@link IdGenerator}.
193         *
194         * @param ta
195         * @return a new primary key
196         * @throws IdGenerationException
197         */
198        public Object getNewPK( DatastoreTransaction ta ) throws IdGenerationException {
199            return this.idGenerator.getNewId( ta );
200        }
201    
202        @Override
203        public String toString() {
204            StringBuffer sb = new StringBuffer();
205            sb.append( getFromTable() );
206            if (this.fkInfo == FK_INFO.fkIsFromField) {
207                sb.append(" (fk) ");
208            }
209            sb.append( " -> " );
210            sb.append( getToTable() );
211            if (this.fkInfo == FK_INFO.fkIsToField) {
212                sb.append(" (fk) ");
213            }
214            sb.append( " (" );
215            for (int i = 0; i < fromFields.length; i++) {
216                sb.append( fromFields[i] );
217                sb.append( "=" );
218                sb.append( toFields[i] );
219                if ( i != fromFields.length - 1 ) {
220                    sb.append( " AND " );
221                }
222            }
223            sb.append( ")" );
224            return sb.toString();
225        }
226    }