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