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