001    //$HeadURL: svn+ssh://mschneider@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/io/datastore/sql/transaction/delete/FeatureGraph.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;
044    
045    import java.sql.Connection;
046    import java.util.HashSet;
047    import java.util.List;
048    import java.util.Set;
049    import java.util.TreeSet;
050    
051    import org.deegree.datatypes.QualifiedName;
052    import org.deegree.framework.log.ILogger;
053    import org.deegree.framework.log.LoggerFactory;
054    import org.deegree.io.datastore.DatastoreException;
055    import org.deegree.io.datastore.FeatureId;
056    import org.deegree.io.datastore.schema.MappedFeatureType;
057    import org.deegree.io.datastore.sql.transaction.delete.FeatureGraph;
058    import org.deegree.ogcwebservices.wfs.operation.Lock;
059    import org.deegree.ogcwebservices.wfs.operation.LockFeature;
060    
061    /**
062     * Responsible for the handling of {@link LockFeature} requests.
063     * 
064     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
065     * @author last edited by: $Author:$
066     * 
067     * @version $Revision:$, $Date:$
068     */
069    public class LockHandler extends AbstractRequestHandler {
070    
071        protected static final ILogger LOG = LoggerFactory.getLogger( LockHandler.class );
072    
073        private List<Lock> requestParts;
074    
075        /**
076         * Creates a new <code>LockHandler</code> from the given parameters.
077         * 
078         * @param ds
079         * @param aliasGenerator
080         * @param conn
081         * @param requestParts
082         */
083        LockHandler( AbstractSQLDatastore ds, TableAliasGenerator aliasGenerator, Connection conn, List<Lock> requestParts ) {
084            super( ds, aliasGenerator, conn );
085            this.requestParts = requestParts;
086        }
087    
088        /**
089         * Determines all {@link FeatureId}s that have to be locked.
090         * 
091         * @return all <code>FeatureId</code>s that have to be locked
092         * @throws DatastoreException
093         */
094        Set<FeatureId> determineFidsToLock()
095                                throws DatastoreException {
096    
097            Set<FeatureId> rootFids = determineRootFids();
098            if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
099                LOG.logDebug( "Root features to be locked: " );
100                for ( FeatureId id : rootFids ) {
101                    LOG.logDebug( id.getAsString() );
102                }
103            }
104    
105            // build the feature graph to determine all descendant features
106            FeatureGraph featureGraph = new FeatureGraph( rootFids, this );
107            Set<FeatureId> lockedFids = new TreeSet<FeatureId>();
108            lockedFids.addAll( featureGraph.getAllFids() );
109    
110            // also add ids of super features (and super-super features, etc.)
111            addSuperFids( lockedFids );
112    
113            return lockedFids;
114        }
115    
116        private void addSuperFids( Set<FeatureId> fids )
117                                throws DatastoreException {
118            FeatureId[] origFids = fids.toArray( new FeatureId[fids.size()] );
119            for ( FeatureId fid : origFids ) {
120                Set<FeatureId> superFids = determineSuperFeatures( fid );
121                for ( FeatureId superFid : superFids ) {
122                    addSuperFids( superFid, fids );
123                }
124            }
125        }
126    
127        private void addSuperFids( FeatureId fid, Set<FeatureId> fids )
128                                throws DatastoreException {
129            if ( !fids.contains( fid ) ) {
130                fids.add( fid );
131                Set<FeatureId> superFids = determineSuperFeatures( fid );
132                for ( FeatureId superFid : superFids ) {
133                    addSuperFids( superFid, fids );
134                }
135            }
136        }
137    
138        /**
139         * Determines all "root" features that have to be locked by the associated {@link LockFeature}
140         * request (and that are served by the associated {@link AbstractSQLDatastore}.
141         * <p>
142         * NOTE: The returned set only contains the feature ids that are <b>directly</b> targeted by
143         * the request, but not necessarily all the subfeatures or superfeatures that have to be locked
144         * as well.
145         * 
146         * @return <b>directly</b> affected feature ids
147         * @throws DatastoreException
148         */
149        private Set<FeatureId> determineRootFids()
150                                throws DatastoreException {
151            Set<FeatureId> fids = new HashSet<FeatureId>();
152            for ( Lock lock : this.requestParts ) {
153                QualifiedName ftName = lock.getTypeName();
154                MappedFeatureType ft = this.datastore.getFeatureType( ftName );
155                if ( ft != null ) {
156                    fids.addAll( determineAffectedFIDs( ft, lock.getFilter() ) );
157                }
158            }
159            return fids;
160        }
161    }