001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/io/datastore/sql/idgenerator/DBSeqIdGenerator.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2006 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: mschneider $
064 *
065 * @version $Revision: 6588 $, $Date: 2007-04-11 17:31:29 +0200 (Mi, 11 Apr 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 ) throws IdGenerationException {
092 super( params );
093 this.sequenceName = params.getProperty( "sequence" );
094 if ( this.sequenceName == null ) {
095 throw new IdGenerationException( "DBSeqIdGenerator requires 'sequence' parameter." );
096 }
097 }
098
099 /**
100 * Returns a new primary key.
101 *
102 * @param ta
103 * datastore transaction (context)
104 * @return a new primary key.
105 * @throws IdGenerationException
106 * if the generation of the id could not be performed
107 */
108 @Override
109 public Object getNewId( DatastoreTransaction ta )
110 throws IdGenerationException {
111 if ( !( ta instanceof SQLTransaction ) ) {
112 throw new IllegalArgumentException( "DBSeqIdGenerator can only be used with SQL datastores." );
113 }
114
115 Object pk;
116 try {
117 AbstractSQLDatastore ds = (AbstractSQLDatastore) ta.getDatastore();
118 Connection conn = ( (SQLTransaction) ta ).getConnection();
119 pk = ds.getSequenceNextVal( conn, this.sequenceName );
120 } catch ( DatastoreException e ) {
121 throw new IdGenerationException( e.getMessage(), e );
122 }
123 return pk;
124 }
125
126 /**
127 * Returns a new id for a feature of the given type.
128 *
129 * @param ft
130 * (mapped) feature type
131 * @return a new feature id.
132 * @throws IdGenerationException
133 * if the generation of the id could not be performed
134 */
135 @Override
136 public FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta )
137 throws IdGenerationException {
138
139 MappedGMLId fidDefinition = ft.getGMLId();
140 if ( fidDefinition.getKeySize() != 1 ) {
141 throw new IdGenerationException( "Cannot generate feature ids that are mapped to "
142 + fidDefinition.getKeySize() + " columns." );
143 }
144
145 Datastore ds = ft.getGMLSchema().getDatastore();
146 if ( !( ds instanceof AbstractSQLDatastore ) ) {
147 throw new IllegalArgumentException( "DBSeqIdGenerator can only be used with SQL datastores." );
148 }
149
150 Object fidNucleus;
151 try {
152 fidNucleus = ( (AbstractSQLDatastore) ds ).getSequenceNextVal( ( (SQLTransaction) ta ).getConnection(),
153 this.sequenceName );
154 } catch ( DatastoreException e ) {
155 throw new IdGenerationException( e.getMessage(), e );
156 }
157
158 FeatureId fid = new FeatureId( ft, new Object[] { fidNucleus } );
159 return fid;
160 }
161 }