001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/datastore/sql/transaction/SQLTransaction.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, 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.transaction;
044:
045: import java.sql.Connection;
046: import java.sql.SQLException;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.deegree.framework.log.ILogger;
051: import org.deegree.framework.log.LoggerFactory;
052: import org.deegree.io.datastore.DatastoreException;
053: import org.deegree.io.datastore.DatastoreTransaction;
054: import org.deegree.io.datastore.FeatureId;
055: import org.deegree.io.datastore.schema.MappedFeatureType;
056: import org.deegree.io.datastore.sql.AbstractRequestHandler;
057: import org.deegree.io.datastore.sql.AbstractSQLDatastore;
058: import org.deegree.io.datastore.sql.TableAliasGenerator;
059: import org.deegree.io.datastore.sql.transaction.delete.DeleteHandler;
060: import org.deegree.io.datastore.sql.transaction.insert.InsertHandler;
061: import org.deegree.model.feature.Feature;
062: import org.deegree.model.feature.FeatureProperty;
063: import org.deegree.model.filterencoding.Filter;
064: import org.deegree.ogcbase.PropertyPath;
065: import org.deegree.ogcwebservices.wfs.operation.transaction.Native;
066: import org.deegree.ogcwebservices.wfs.operation.transaction.Transaction;
067:
068: /**
069: * Handles {@link Transaction} requests to SQL based datastores.
070: *
071: * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
072: * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a>
073: * @author last edited by: $Author: apoth $
074: *
075: * @version $Revision: 9342 $, $Date: 2007-12-27 04:32:57 -0800 (Thu, 27 Dec 2007) $
076: */
077: public class SQLTransaction extends AbstractRequestHandler implements
078: DatastoreTransaction {
079:
080: private static final ILogger LOG = LoggerFactory
081: .getLogger(SQLTransaction.class);
082:
083: /**
084: * Creates a new instance of <code>SQLTransaction</code> from the given parameters.
085: *
086: * @param datastore
087: * @param aliasGenerator
088: * @param conn
089: * @throws DatastoreException
090: */
091: public SQLTransaction(AbstractSQLDatastore datastore,
092: TableAliasGenerator aliasGenerator, Connection conn)
093: throws DatastoreException {
094: super (datastore, aliasGenerator, conn);
095: try {
096: conn.setAutoCommit(false);
097: } catch (SQLException e) {
098: String msg = "Unable to disable auto commit: "
099: + e.getMessage();
100: LOG.logError(msg);
101: throw new DatastoreException(msg, e);
102: }
103: }
104:
105: /**
106: * Returns the underlying <code>AbstractSQLDatastore</code>.
107: *
108: * @return the underlying <code>AbstractSQLDatastore</code>
109: */
110: public AbstractSQLDatastore getDatastore() {
111: return this .datastore;
112: }
113:
114: /**
115: * Returns the underlying JDBC connection.
116: *
117: * @return the underlying JDBC connection
118: */
119: public Connection getConnection() {
120: return this .conn;
121: }
122:
123: /**
124: * Makes the changes persistent that have been performed in this transaction.
125: *
126: * @throws DatastoreException
127: */
128: public void commit() throws DatastoreException {
129: try {
130: conn.commit();
131: } catch (SQLException e) {
132: String msg = "Unable to commit transaction: "
133: + e.getMessage();
134: LOG.logError(msg);
135: throw new DatastoreException(msg, e);
136: }
137: }
138:
139: /**
140: * Aborts the changes that have been performed in this transaction.
141: *
142: * @throws DatastoreException
143: */
144: public void rollback() throws DatastoreException {
145: try {
146: conn.rollback();
147: } catch (SQLException e) {
148: String msg = "Unable to rollback transaction: "
149: + e.getMessage();
150: LOG.logError(msg);
151: throw new DatastoreException(msg, e);
152: }
153: }
154:
155: /**
156: * Returns the transaction instance so other clients may acquire a transaction (and underlying resources, such as
157: * JDBCConnections can be freed).
158: *
159: * @throws DatastoreException
160: */
161: public void release() throws DatastoreException {
162: this .datastore.releaseTransaction(this );
163: }
164:
165: /**
166: * Inserts the given feature instances into the datastore.
167: *
168: * @param features
169: * @return feature ids of the inserted (root) features
170: * @throws DatastoreException
171: */
172: public List<FeatureId> performInsert(List<Feature> features)
173: throws DatastoreException {
174:
175: InsertHandler handler = new InsertHandler(this ,
176: this .aliasGenerator, this .conn);
177: List<FeatureId> fids = handler.performInsert(features);
178: return fids;
179: }
180:
181: /**
182: * Performs an update operation against the datastore.
183: *
184: * @param mappedFeatureType
185: * feature type that is to be updated
186: * @param replacementProps
187: * properties and their replacement values
188: * @param filter
189: * selects the feature instances that are to be updated
190: * @param lockId
191: * optional id of associated lock (may be null)
192: * @return number of updated feature instances
193: * @throws DatastoreException
194: */
195: public int performUpdate(MappedFeatureType mappedFeatureType,
196: Map<PropertyPath, FeatureProperty> replacementProps,
197: Filter filter, String lockId) throws DatastoreException {
198:
199: UpdateHandler handler = new UpdateHandler(this ,
200: this .aliasGenerator, this .conn, lockId);
201: int updatedFeatures = handler.performUpdate(mappedFeatureType,
202: replacementProps, filter);
203: return updatedFeatures;
204: }
205:
206: /**
207: * Performs an update operation against the datastore.
208: * <p>
209: * The filter is expected to match exactly one feature which will be replaced by the specified replacement feature.
210: *
211: * @param mappedFeatureType
212: * feature type that is to be updated
213: * @param replacementFeature
214: * feature instance that will replace the selected feature
215: * @param filter
216: * selects the single feature instances that is to be replaced
217: * @param lockId
218: * optional id of associated lock (may be null)
219: * @return number of updated feature instances (must be 0 or 1)
220: * @throws DatastoreException
221: */
222: public int performUpdate(MappedFeatureType mappedFeatureType,
223: Feature replacementFeature, Filter filter, String lockId)
224: throws DatastoreException {
225:
226: UpdateHandler handler = new UpdateHandler(this ,
227: this .aliasGenerator, this .conn, lockId);
228: int updatedFeatures = handler.performUpdate(mappedFeatureType,
229: replacementFeature, filter);
230: return updatedFeatures;
231: }
232:
233: /**
234: * Deletes the features from the datastore that are matched by the given filter and type.
235: *
236: * @param mappedFeatureType
237: * @param filter
238: * @param lockId
239: * optional id of associated lock (may be null)
240: * @return number of deleted feature instances
241: * @throws DatastoreException
242: */
243: public int performDelete(MappedFeatureType mappedFeatureType,
244: Filter filter, String lockId) throws DatastoreException {
245:
246: DeleteHandler handler = new DeleteHandler(this ,
247: this .aliasGenerator, this .conn, lockId);
248: int deletedFeatures = handler.performDelete(mappedFeatureType,
249: filter);
250: return deletedFeatures;
251: }
252:
253: /**
254: * Performs a 'native' operation against the datastore.
255: *
256: * @param operation
257: * @return number of processed feature instances.
258: * @throws DatastoreException
259: */
260: public int performNative(Native operation)
261: throws DatastoreException {
262:
263: throw new UnsupportedOperationException(
264: "Native operations are not supported.");
265: }
266: }
|