001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/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: 18523 $
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            throw new UnsupportedOperationException( "Insert on a SDE-Datastore is not supported yet" );
138            /*
139             * SDEInsertHandler handler = new SDEInsertHandler( this ); List<FeatureId> fids = handler.performInsert(
140             * features ); return fids;
141             */
142        }
143    
144        /**
145         * Performs an update operation against the datastore.
146         * 
147         * @param mappedFeatureType
148         *            feature type that is to be updated
149         * @param replacementProps
150         *            properties and their replacement values
151         * @param filter
152         *            selects the feature instances that are to be updated
153         * @param lockId
154         *            optional id of associated lock (may be null)
155         * @return number of updated feature instances
156         * @throws DatastoreException
157         */
158        public int performUpdate( MappedFeatureType mappedFeatureType, Map<PropertyPath, FeatureProperty> replacementProps,
159                                  Filter filter, String lockId )
160                                throws DatastoreException {
161    
162            SDEUpdateHandler handler = new SDEUpdateHandler( this, this.aliasGenerator, this.conn );
163            int updatedFeatures = handler.performUpdate( mappedFeatureType, replacementProps, filter );
164            return updatedFeatures;
165        }
166    
167        /**
168         * Performs an update operation against the datastore.
169         * <p>
170         * The filter is expected to match exactly one feature which will be replaced by the specified replacement feature.
171         * 
172         * @param mappedFeatureType
173         *            feature type that is to be updated
174         * @param replacementFeature
175         *            feature instance that will replace the selected feature
176         * @param filter
177         *            selects the single feature instances that is to be replaced
178         * @param lockId
179         *            optional id of associated lock (may be null)
180         * @return number of updated feature instances (must be 0 or 1)
181         * @throws DatastoreException
182         */
183        public int performUpdate( MappedFeatureType mappedFeatureType, Feature replacementFeature, Filter filter,
184                                  String lockId )
185                                throws DatastoreException {
186    
187            // SDEUpdateHandler handler = new SDEUpdateHandler( this, this.aliasGenerator, this.conn );
188            // int updatedFeatures = handler.performUpdate( mappedFeatureType, replacementFeature,
189            // filter );
190            return 0;// updatedFeatures;
191        }
192    
193        /**
194         * Performs a replace operation against the datastore.
195         * 
196         * @param mappedFeatureType
197         *            feature type that is to be replaced
198         * @param replacementFeature
199         *            feature instance that will be used to replace the properties of the selected features
200         * @param filter
201         *            selects the feature instances that are to be replaced
202         * @param lockId
203         *            optional id of associated lock (may be null)
204         * @return number of replaced feature instances
205         * @throws DatastoreException
206         */
207        public int performReplace( MappedFeatureType mappedFeatureType, Feature replacementFeature, Filter filter,
208                                   String lockId )
209                                throws DatastoreException {
210            throw new UnsupportedOperationException();
211        }
212    
213        /**
214         * Deletes the features from the datastore that are matched by the given filter and type.
215         * 
216         * @param mappedFeatureType
217         * @param filter
218         * @param lockId
219         *            optional id of associated lock (may be null)
220         * @return number of deleted feature instances
221         * @throws DatastoreException
222         */
223        public int performDelete( MappedFeatureType mappedFeatureType, Filter filter, String lockId )
224                                throws DatastoreException {
225    
226            SDEDeleteHandler handler = new SDEDeleteHandler( this, this.aliasGenerator, this.conn );
227            int deletedFeatures = handler.performDelete( mappedFeatureType, filter );
228            return deletedFeatures;
229        }
230    
231        /**
232         * Performs a 'native' operation against the datastore.
233         * 
234         * @param operation
235         * @return number of processed feature instances.
236         * @throws DatastoreException
237         */
238        public int performNative( Native operation )
239                                throws DatastoreException {
240            throw new UnsupportedOperationException( "Native not implemented yet." );
241        }
242    }