001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/datastore/sql/wherebuilder/FeatureTypeNode.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.wherebuilder;
037    
038    import java.util.ArrayList;
039    import java.util.HashMap;
040    import java.util.Iterator;
041    import java.util.List;
042    import java.util.Map;
043    
044    import org.deegree.io.datastore.schema.MappedFeatureType;
045    import org.deegree.io.datastore.schema.MappedPropertyType;
046    
047    /**
048     * Represents a {@link MappedFeatureType} as a node in a {@link QueryTableTree}.
049     *
050     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
051     * @author last edited by: $Author: mschneider $
052     *
053     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
054     */
055    class FeatureTypeNode {
056    
057        // associated MappedFeatureType instance (contains the table name)
058        private MappedFeatureType ft;
059    
060        // alias (may be null)
061        private String ftAlias;
062    
063        // unique alias for the corresponding table
064        private String tableAlias;
065    
066        private Map<MappedPropertyType, List<PropertyNode>> propertyMap = new HashMap<MappedPropertyType, List<PropertyNode>>();
067    
068        /**
069         * Creates a new <code>FeatureTypeNode</code> from the given parameters.
070         *
071         * @param ft
072         * @param ftAlias
073         * @param tableAlias
074         */
075        FeatureTypeNode( MappedFeatureType ft, String ftAlias, String tableAlias ) {
076            this.ft = ft;
077            this.ftAlias = ftAlias;
078            this.tableAlias = tableAlias;
079        }
080    
081        /**
082         * Returns the associated {@link MappedFeatureType}.
083         *
084         * @return associated MappedFeatureType
085         */
086        public MappedFeatureType getFeatureType() {
087            return this.ft;
088        }
089    
090        /**
091         * Returns the alias as specified in the corresponding query.
092         *
093         * @return the alias (may be null)
094         */
095        public String getFtAlias() {
096            return this.ftAlias;
097        }
098    
099        /**
100         * Returns the name of the associated table.
101         *
102         * @return the name of the associated table
103         */
104        public String getTable() {
105            return this.ft.getTable();
106        }
107    
108        /**
109         * Returns the alias that uniquely identifies the table (in an SQL query).
110         *
111         * @return the unique alias for the table
112         */
113        public String getTableAlias() {
114            return this.tableAlias;
115        }
116    
117        /**
118         * Returns all child {@link PropertyNode}s.
119         *
120         * @return all child PropertyNodes
121         */
122        public PropertyNode[] getPropertyNodes() {
123            List<PropertyNode> propertyNodeList = new ArrayList<PropertyNode>();
124            Iterator<?> iter = this.propertyMap.values().iterator();
125            while ( iter.hasNext() ) {
126                Iterator<?> iter2 = ( (List) iter.next() ).iterator();
127                while ( iter2.hasNext() ) {
128                    propertyNodeList.add( (PropertyNode) iter2.next() );
129                }
130            }
131            return propertyNodeList.toArray( new PropertyNode[propertyNodeList.size()] );
132        }
133    
134        /**
135         * Returns the child {@link PropertyNode}s with the given type.
136         *
137         * @param type
138         *            the property type to look up
139         * @return the child PropertyNode for the given property, may be null
140         */
141        public PropertyNode getPropertyNode( MappedPropertyType type ) {
142            PropertyNode propertyNode = null;
143            List<?> propertyNodeList = this.propertyMap.get( type );
144            if ( propertyNodeList != null ) {
145                Iterator<?> propertyNodeIter = propertyNodeList.iterator();
146                boolean found = false;
147                while ( propertyNodeIter.hasNext() ) {
148                    propertyNode = (PropertyNode) propertyNodeIter.next();
149                    if ( propertyNode.getProperty() == type ) {
150                        found = true;
151                        break;
152                    }
153                }
154                if ( !found ) {
155                    propertyNode = null;
156                }
157            }
158            return propertyNode;
159        }
160    
161        /**
162         * Adds the given property node as a child.
163         *
164         * @param propertyNode
165         *            the child node to add
166         */
167        public void addPropertyNode( PropertyNode propertyNode ) {
168            List<PropertyNode> propertyNodeList = this.propertyMap.get( propertyNode.getProperty() );
169            if ( propertyNodeList == null ) {
170                propertyNodeList = new ArrayList<PropertyNode>();
171                this.propertyMap.put( propertyNode.getProperty(), propertyNodeList );
172            }
173            propertyNodeList.add( propertyNode );
174        }
175    
176        /**
177         * Returns an indented string representation of the object.
178         *
179         * @param indent
180         *            current indentation (String consisting of spaces)
181         * @return an indented string representation of the object
182         */
183        String toString( String indent ) {
184            StringBuffer sb = new StringBuffer();
185            sb.append( indent );
186            sb.append( "- " );
187            sb.append( this.ft.getName() );
188            sb.append( " (FeatureTypeNode, alias: '" );
189            sb.append( this.ftAlias == null ? '-' : this.ftAlias );
190            sb.append( "', table: '" );
191            sb.append( this.ft.getTable() );
192            sb.append( "', tableAlias: '" );
193            sb.append( this.tableAlias );
194            sb.append( "')\n" );
195            Iterator<?> iter = this.propertyMap.values().iterator();
196            while ( iter.hasNext() ) {
197                List<?> propertyNodeList = (List) iter.next();
198                Iterator<?> iter2 = propertyNodeList.iterator();
199                while ( iter2.hasNext() ) {
200                    PropertyNode propertyNode = (PropertyNode) iter2.next();
201                    sb.append( propertyNode.toString( indent + "  " ) );
202                }
203            }
204            return sb.toString();
205        }
206    
207        @Override
208        public boolean equals( Object obj ) {
209            if ( obj == null || ( !( obj instanceof FeatureTypeNode ) ) ) {
210                return false;
211            }
212            FeatureTypeNode that = (FeatureTypeNode) obj;
213            if ( this.getTable().equals( that.getTable() ) && this.tableAlias.equals( that.tableAlias ) ) {
214                return true;
215            }
216            return false;
217        }
218    
219        @Override
220        public int hashCode() {
221            return this.tableAlias.hashCode();
222        }
223    }