001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/io/datastore/sde/SDETransaction.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 java.util.List;
028    import java.util.Map;
029    
030    import org.deegree.framework.log.ILogger;
031    import org.deegree.framework.log.LoggerFactory;
032    import org.deegree.io.datastore.DatastoreException;
033    import org.deegree.io.datastore.DatastoreTransaction;
034    import org.deegree.io.datastore.FeatureId;
035    import org.deegree.io.datastore.schema.MappedFeatureType;
036    import org.deegree.io.datastore.sql.TableAliasGenerator;
037    import org.deegree.io.sdeapi.SDEConnection;
038    import org.deegree.model.feature.Feature;
039    import org.deegree.model.feature.FeatureProperty;
040    import org.deegree.model.filterencoding.Filter;
041    import org.deegree.ogcbase.PropertyPath;
042    import org.deegree.ogcwebservices.wfs.operation.transaction.Native;
043    
044    /**
045     * Handles <code>Transaction</code> requests to SQL based datastores.
046     * 
047     * @author <a href="mailto:cpollmann@moss.de">Christoph Pollmann</a>
048     * @author last edited by: $Author: mschneider $
049     * 
050     * @version $Revision: 7145 $
051     */
052    public class SDETransaction extends AbstractSDERequestHandler implements DatastoreTransaction {
053    
054        private static final ILogger LOG = LoggerFactory.getLogger( SDETransaction.class );
055    
056        /**
057         * Creates a new instance of <code>SQLTransaction</code> from the given parameters.
058         * 
059         * @param datastore
060         * @param aliasGenerator
061         * @param conn
062         */
063        public SDETransaction( SDEDatastore datastore, TableAliasGenerator aliasGenerator, SDEConnection conn ) {
064            super( datastore, aliasGenerator, conn );
065        }
066    
067        /**
068         * Starts a new transaction.
069         * 
070         * @throws DatastoreException
071         */
072        public void begin()
073                                throws DatastoreException {
074            try {
075                conn.getConnection().startTransaction();
076            } catch ( Exception e ) {
077                String msg = "Unable to commit transaction: " + e.getMessage();
078                LOG.logError( msg );
079                throw new DatastoreException( msg, e );
080            }
081        }
082    
083        /**
084         * Makes the changes persistent that have been performed in this transaction.
085         * 
086         * @throws DatastoreException
087         */
088        public void commit()
089                                throws DatastoreException {
090            try {
091                conn.getConnection().commitTransaction();
092                conn.close();
093            } catch ( Exception e ) {
094                String msg = "Unable to commit transaction: " + e.getMessage();
095                LOG.logError( msg );
096                throw new DatastoreException( msg, e );
097            }
098        }
099    
100        /**
101         * Aborts the changes that have been performed in this transaction.
102         * 
103         * @throws DatastoreException
104         */
105        public void rollback()
106                                throws DatastoreException {
107            try {
108                conn.getConnection().rollbackTransaction();
109                conn.close();
110            } catch ( Exception e ) {
111                String msg = "Unable to rollback transaction: " + e.getMessage();
112                LOG.logError( msg );
113                throw new DatastoreException( msg, e );
114            }
115        }
116    
117        /**
118         * Returns the transaction instance so other clients may acquire a transaction (and underlying resources, such as
119         * JDBCConnections can be freed).
120         * 
121         * @throws DatastoreException
122         */
123        public void release()
124                                throws DatastoreException {
125            this.datastore.releaseTransaction( this );
126        }
127    
128        /**
129         * Inserts the given feature instances into the datastore.
130         * 
131         * @param features
132         * @return feature ids of the inserted (root) features
133         * @throws DatastoreException
134         */
135        public List<FeatureId> performInsert( List<Feature> features )
136                                throws DatastoreException {
137    
138            return null;
139            /*
140             * SDEInsertHandler handler = new SDEInsertHandler( this ); List<FeatureId> fids = handler.performInsert(
141             * features ); return fids;
142             */
143        }
144    
145        /**
146         * Performs an update operation against the datastore.
147         * 
148         * @param mappedFeatureType
149         *            feature type that is to be updated
150         * @param replacementProps
151         *            properties and their replacement values
152         * @param filter
153         *            selects the feature instances that are to be updated
154         * @param lockId
155         *            optional id of associated lock (may be null)
156         * @return number of updated feature instances
157         * @throws DatastoreException
158         */
159        public int performUpdate( MappedFeatureType mappedFeatureType, Map<PropertyPath, FeatureProperty> replacementProps,
160                                  Filter filter, String lockId )
161                                throws DatastoreException {
162    
163            SDEUpdateHandler handler = new SDEUpdateHandler( this, this.aliasGenerator, this.conn );
164            int updatedFeatures = handler.performUpdate( mappedFeatureType, replacementProps, filter );
165            return updatedFeatures;
166        }
167    
168        /**
169         * Performs an update operation against the datastore.
170         * <p>
171         * The filter is expected to match exactly one feature which will be replaced by the specified replacement feature.
172         * 
173         * @param mappedFeatureType
174         *            feature type that is to be updated
175         * @param replacementFeature
176         *            feature instance that will replace the selected feature
177         * @param filter
178         *            selects the single feature instances that is to be replaced
179         * @param lockId
180         *            optional id of associated lock (may be null)
181         * @return number of updated feature instances (must be 0 or 1)
182         * @throws DatastoreException
183         */
184        public int performUpdate( MappedFeatureType mappedFeatureType, Feature replacementFeature, Filter filter,
185                                  String lockId )
186                                throws DatastoreException {
187    
188            // SDEUpdateHandler handler = new SDEUpdateHandler( this, this.aliasGenerator, this.conn );
189            // int updatedFeatures = handler.performUpdate( mappedFeatureType, replacementFeature,
190            // filter );
191            return 0;// updatedFeatures;
192        }
193    
194        /**
195         * Deletes the features from the datastore that are matched by the given filter and type.
196         * 
197         * @param mappedFeatureType
198         * @param filter
199         * @param lockId
200         *            optional id of associated lock (may be null)
201         * @return number of deleted feature instances
202         * @throws DatastoreException
203         */
204        public int performDelete( MappedFeatureType mappedFeatureType, Filter filter, String lockId )
205                                throws DatastoreException {
206    
207            SDEDeleteHandler handler = new SDEDeleteHandler( this, this.aliasGenerator, this.conn );
208            int deletedFeatures = handler.performDelete( mappedFeatureType, filter );
209            return deletedFeatures;
210        }
211    
212        /**
213         * Performs a 'native' operation against the datastore.
214         * 
215         * @param operation
216         * @return number of processed feature instances.
217         * @throws DatastoreException
218         */
219        public int performNative( Native operation )
220                                throws DatastoreException {
221            throw new UnsupportedOperationException( "Native not implemented as yet." );
222        }
223    }