001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/datastore/sde/SDEDeleteHandler.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2006 by: M.O.S.S. Computer Grafik Systeme GmbH
006     Hohenbrunner Weg 13
007     D-82024 Taufkirchen
008     http://www.moss.de/
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
023    
024     ---------------------------------------------------------------------------*/
025    package org.deegree.io.datastore.sde;
026    
027    import org.deegree.framework.log.ILogger;
028    import org.deegree.framework.log.LoggerFactory;
029    import org.deegree.io.datastore.DatastoreException;
030    import org.deegree.io.datastore.FeatureId;
031    import org.deegree.io.datastore.schema.MappedFeatureType;
032    import org.deegree.io.datastore.schema.content.MappingField;
033    import org.deegree.io.datastore.sql.TableAliasGenerator;
034    import org.deegree.io.sdeapi.SDEConnection;
035    import org.deegree.model.filterencoding.Filter;
036    
037    import com.esri.sde.sdk.client.SeDelete;
038    import com.esri.sde.sdk.client.SeObjectId;
039    import com.esri.sde.sdk.client.SeState;
040    
041    /**
042     * Handler for <code>Delete</code> operations contained in <code>Transaction</code> requests.
043     * 
044     * @author <a href="mailto:cpollmann@moss.de">Christoph Pollmann</a>
045     * @author last edited by: $Author: rbezema $
046     * 
047     * @version $Revision: 12183 $, $Date: 2008-06-05 11:17:23 +0200 (Do, 05 Jun 2008) $
048     */
049    public class SDEDeleteHandler extends AbstractSDERequestHandler {
050    
051        private static final ILogger LOG = LoggerFactory.getLogger( SDEDeleteHandler.class );
052    
053        /**
054         * Creates a new <code>DeleteHandler</code> from the given parameters.
055         * 
056         * @param dsTa
057         * @param aliasGenerator
058         * @param conn
059         */
060        SDEDeleteHandler( SDETransaction dsTa, TableAliasGenerator aliasGenerator, SDEConnection conn ) {
061            super( dsTa.getDatastore(), aliasGenerator, conn );
062        }
063    
064        /**
065         * Deletes the features from the datastore that are matched by the given filter and type.
066         * 
067         * @param mappedFeatureType
068         * @param filter
069         * @return number of deleted feature instances
070         * @throws DatastoreException
071         */
072        int performDelete( MappedFeatureType mappedFeatureType, Filter filter )
073                                throws DatastoreException {
074    
075            FeatureId[] fids = determineAffectedFIDs( mappedFeatureType, filter );
076            try {
077                for ( int i = 0; i < fids.length; i++ ) {
078                    SeDelete deleter = deleteFeature( mappedFeatureType, fids[i] );
079                    deleter.execute();
080                }
081            } catch ( Exception e ) {
082                LOG.logDebug( "delete error occured", e );
083                throw new DatastoreException( "delete error occured", e );
084            }
085            // return count of featureids deleted.
086            return fids.length;
087        }
088    
089        /**
090         * Deletes the feature with the given feature id.
091         * 
092         * @param mappedFeatureType
093         * @param fid
094         * @throws DatastoreException
095         */
096        private SeDelete deleteFeature( MappedFeatureType mappedFeatureType, FeatureId fid )
097                                throws Exception {
098    
099            LOG.logDebug( "Deleting feature with id '" + fid + "' and type '" + mappedFeatureType.getName() + "'..." );
100    
101            // delete feature type table
102            SeDelete deleter = new SeDelete( conn.getConnection() );
103            String table = mappedFeatureType.getTable();
104            StringBuffer where = buildFIDWhereClause( fid );
105            deleter.fromTable( table, where.toString() );
106            deleter.setState( conn.getState().getId(), new SeObjectId( SeState.SE_NULL_STATE_ID ),
107                              SeState.SE_STATE_DIFF_NOCHECK );
108            return deleter;
109    
110        }
111    
112        /**
113         * @param fid
114         */
115        private StringBuffer buildFIDWhereClause( FeatureId fid ) {
116            StringBuffer where = new StringBuffer();
117            MappingField[] fidFields = fid.getFidDefinition().getIdFields();
118            for ( int i = 0; i < fidFields.length; i++ ) {
119                if ( 0 != i ) {
120                    where.append( " AND " );
121                }
122                where.append( fidFields[i].getField() );
123                where.append( "='" );
124                where.append( fid.getValue( i ).toString() );
125                where.append( "'" );
126            }
127            return where;
128        }
129    }