001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/datastore/sql/idgenerator/DBSeqIdGenerator.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.idgenerator;
044:
045: import java.sql.Connection;
046: import java.util.Properties;
047:
048: import org.deegree.io.datastore.Datastore;
049: import org.deegree.io.datastore.DatastoreException;
050: import org.deegree.io.datastore.DatastoreTransaction;
051: import org.deegree.io.datastore.FeatureId;
052: import org.deegree.io.datastore.idgenerator.IdGenerationException;
053: import org.deegree.io.datastore.idgenerator.IdGenerator;
054: import org.deegree.io.datastore.schema.MappedFeatureType;
055: import org.deegree.io.datastore.schema.MappedGMLId;
056: import org.deegree.io.datastore.sql.AbstractSQLDatastore;
057: import org.deegree.io.datastore.sql.transaction.SQLTransaction;
058:
059: /**
060: * Feature id generator that uses an SQL sequence to create new values.
061: *
062: * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
063: * @author last edited by: $Author: apoth $
064: *
065: * @version $Revision: 9342 $, $Date: 2007-12-27 04:32:57 -0800 (Thu, 27 Dec 2007) $
066: */
067: public class DBSeqIdGenerator extends IdGenerator {
068:
069: private String sequenceName;
070:
071: /**
072: * Creates a new <code>DBSeqIdGenerator</code> instance.
073: * <p>
074: * Supported configuration parameters: <table>
075: * <tr>
076: * <th>Name</th>
077: * <th>optional?</th>
078: * <th>Usage</th>
079: * </tr>
080: * <tr>
081: * <td>sequence</td>
082: * <td>no</td>
083: * <td>name of the SQL sequence to be used</td>
084: * </tr>
085: * </table>
086: *
087: * @param params
088: * configuration parameters
089: * @throws IdGenerationException
090: */
091: public DBSeqIdGenerator(Properties params)
092: throws IdGenerationException {
093: super (params);
094: this .sequenceName = params.getProperty("sequence");
095: if (this .sequenceName == null) {
096: throw new IdGenerationException(
097: "DBSeqIdGenerator requires 'sequence' parameter.");
098: }
099: }
100:
101: /**
102: * Returns the name of the used sequence.
103: *
104: * @return the name of the used sequence
105: */
106: public String getSequenceName() {
107: return this .sequenceName;
108: }
109:
110: /**
111: * Returns a new primary key.
112: *
113: * @param ta
114: * datastore transaction (context)
115: * @return a new primary key.
116: * @throws IdGenerationException
117: * if the generation of the id could not be performed
118: */
119: @Override
120: public Object getNewId(DatastoreTransaction ta)
121: throws IdGenerationException {
122: if (!(ta instanceof SQLTransaction)) {
123: throw new IllegalArgumentException(
124: "DBSeqIdGenerator can only be used with SQL datastores.");
125: }
126:
127: Object pk;
128: try {
129: AbstractSQLDatastore ds = (AbstractSQLDatastore) ta
130: .getDatastore();
131: Connection conn = ((SQLTransaction) ta).getConnection();
132: pk = ds.getSequenceNextVal(conn, this .sequenceName);
133: } catch (DatastoreException e) {
134: throw new IdGenerationException(e.getMessage(), e);
135: }
136: return pk;
137: }
138:
139: /**
140: * Returns a new id for a feature of the given type.
141: *
142: * @param ft
143: * (mapped) feature type
144: * @return a new feature id.
145: * @throws IdGenerationException
146: * if the generation of the id could not be performed
147: */
148: @Override
149: public FeatureId getNewId(MappedFeatureType ft,
150: DatastoreTransaction ta) throws IdGenerationException {
151:
152: MappedGMLId fidDefinition = ft.getGMLId();
153: if (fidDefinition.getKeySize() != 1) {
154: throw new IdGenerationException(
155: "Cannot generate feature ids that are mapped to "
156: + fidDefinition.getKeySize() + " columns.");
157: }
158:
159: Datastore ds = ft.getGMLSchema().getDatastore();
160: if (!(ds instanceof AbstractSQLDatastore)) {
161: throw new IllegalArgumentException(
162: "DBSeqIdGenerator can only be used with SQL datastores.");
163: }
164:
165: Object fidNucleus;
166: try {
167: fidNucleus = ((AbstractSQLDatastore) ds)
168: .getSequenceNextVal(((SQLTransaction) ta)
169: .getConnection(), this .sequenceName);
170: } catch (DatastoreException e) {
171: throw new IdGenerationException(e.getMessage(), e);
172: }
173:
174: FeatureId fid = new FeatureId(ft, new Object[] { fidNucleus });
175: return fid;
176: }
177: }
|