001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/datastore/sql/transaction/delete/FeatureNode.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 java.util.ArrayList;
039    import java.util.Collection;
040    import java.util.HashSet;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.Set;
044    
045    import org.deegree.io.datastore.FeatureId;
046    import org.deegree.io.datastore.schema.MappedFeaturePropertyType;
047    import org.deegree.model.feature.Feature;
048    import org.deegree.ogcwebservices.csw.manager.Delete;
049    
050    /**
051     * A node of a {@link FeatureGraph}. Represents one {@link Feature} instance which must or must not be deleted during a
052     * {@link Delete} operation.
053     *
054     * @see FeatureGraph
055     *
056     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
057     * @author last edited by: $Author: mschneider $
058     *
059     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
060     */
061    class FeatureNode {
062    
063        private FeatureGraph graph;
064    
065        private FeatureId fid;
066    
067        private Set<FeatureId> subFids = new HashSet<FeatureId>();
068    
069        private Set<FeatureId> superFids;
070    
071        private Map<MappedFeaturePropertyType, List<FeatureId>> ptToSubFids;
072    
073        private boolean isDeletable = true;
074    
075        /**
076         * Creates a new <code>FeatureNode</code> instance that represents the feature with the given {@link FeatureId}.
077         *
078         * @param graph
079         *            <code>FeatureGraph</code> that the node belongs to
080         * @param fid
081         *            id of the represented feature
082         * @param subFeatureProperties
083         *            complex property types of the feature and the ids of the subfeatures that they contain
084         * @param superFeatures
085         *            ids of all features that contain the represented feature as a subfeature
086         */
087        FeatureNode( FeatureGraph graph, FeatureId fid,
088                     Map<MappedFeaturePropertyType, List<FeatureId>> subFeatureProperties, Set<FeatureId> superFeatures ) {
089            this.graph = graph;
090            this.fid = fid;
091            this.ptToSubFids = subFeatureProperties;
092            this.superFids = superFeatures;
093            for ( Collection<FeatureId> subFids : subFeatureProperties.values() ) {
094                this.subFids.addAll( subFids );
095            }
096        }
097    
098        /**
099         * Returns the associated {@link FeatureId}.
100         *
101         * @return the associated <code>FeatureId</code>
102         */
103        FeatureId getFid() {
104            return this.fid;
105        }
106    
107        /**
108         * Returns the ids of all subfeatures of the represented feature.
109         *
110         * @return the ids of all subfeatures of the represented feature
111         */
112        Set<FeatureId> getSubFeatureIds() {
113            return this.subFids;
114        }
115    
116        /**
117         * Returns the ids of all subfeatures stored in the specified property of the represented feature.
118         *
119         * @param pt
120         *            property type
121         * @return the ids of all subfeatures of the represented feature
122         */
123        List<FeatureId> getSubFeatureIds( MappedFeaturePropertyType pt ) {
124            return this.ptToSubFids.get( pt );
125        }
126    
127        /**
128         * Returns all subfeatures of the represented feature.
129         *
130         * @return all subfeatures of the represented feature
131         */
132        List<FeatureNode> getSubFeatures() {
133            List<FeatureNode> subFeatures = new ArrayList<FeatureNode>( this.subFids.size() );
134            for ( FeatureId subFid : this.subFids ) {
135                subFeatures.add( this.graph.getNode( subFid ) );
136            }
137            return subFeatures;
138        }
139    
140        /**
141         * Returns the ids of all superfeatures of the represented feature (features that contain the represented feature as
142         * a subfeature).
143         *
144         * @return the ids of all superfeatures of the represented feature
145         */
146        Set<FeatureId> getSuperFeatureIds() {
147            return this.superFids;
148        }
149    
150        /**
151         * Returns whether the represented feature may be deleted.
152         *
153         * @return true, if the represented feature may be deleted, otherwise false
154         */
155        boolean isDeletable() {
156            return this.isDeletable;
157        }
158    
159        /**
160         * Marks this feature as undeletable. This is also applied to all it's descendant subfeatures.
161         */
162        void markAsUndeletable() {
163            if ( this.isDeletable ) {
164                this.isDeletable = false;
165                for ( FeatureId subFid : this.subFids ) {
166                    this.graph.getNode( subFid ).markAsUndeletable();
167                }
168            }
169        }
170    
171        @Override
172        public int hashCode() {
173            return this.fid.hashCode();
174        }
175    
176        @Override
177        public boolean equals( Object obj ) {
178            if ( obj == null || !( obj instanceof FeatureNode ) ) {
179                return false;
180            }
181            return this.fid.equals( obj );
182        }
183    
184        /**
185         * Generates an indented string representation of this <code>FeatureNode</code> and all it's descendant subfeatures.
186         *
187         * @param indent
188         *            current indentation (string consisting of spaces)
189         * @param printedNodes
190         *            <code>FeatureNode</code>s that have all ready been encountered (to avoid endless recursion)
191         * @return an indented string representation
192         */
193        String toString( String indent, Set<FeatureNode> printedNodes ) {
194            StringBuffer sb = new StringBuffer();
195            if ( !printedNodes.contains( this ) ) {
196                printedNodes.add( this );
197                sb.append( indent );
198                sb.append( "- " + this.fid + ", super fids: [" );
199                for ( FeatureId superFid : superFids ) {
200                    sb.append( superFid );
201                    sb.append( " " );
202                }
203                sb.append( "], deletable: " + this.isDeletable + "\n" );
204                for ( MappedFeaturePropertyType pt : this.ptToSubFids.keySet() ) {
205                    for ( FeatureId subFid : this.ptToSubFids.get( pt ) ) {
206                        sb.append( indent );
207                        sb.append( " + " );
208                        sb.append( pt.getName().getLocalName() );
209                        sb.append( ":\n" );
210                        FeatureNode subNode = this.graph.getNode( subFid );
211                        sb.append( subNode.toString( "  " + indent, printedNodes ) );
212                    }
213                }
214            } else {
215                sb.append( indent );
216                sb.append( "- " + this.fid + " (already printed)\n" );
217            }
218            return sb.toString();
219        }
220    }