001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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 13:32:57 +0100 (Do, 27 Dez 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 the name of the used sequence.
101 *
102 * @return the name of the used sequence
103 */
104 public String getSequenceName() {
105 return this.sequenceName;
106 }
107
108 /**
109 * Returns a new primary key.
110 *
111 * @param ta
112 * datastore transaction (context)
113 * @return a new primary key.
114 * @throws IdGenerationException
115 * if the generation of the id could not be performed
116 */
117 @Override
118 public Object getNewId( DatastoreTransaction ta )
119 throws IdGenerationException {
120 if ( !( ta instanceof SQLTransaction ) ) {
121 throw new IllegalArgumentException( "DBSeqIdGenerator can only be used with SQL datastores." );
122 }
123
124 Object pk;
125 try {
126 AbstractSQLDatastore ds = (AbstractSQLDatastore) ta.getDatastore();
127 Connection conn = ( (SQLTransaction) ta ).getConnection();
128 pk = ds.getSequenceNextVal( conn, this.sequenceName );
129 } catch ( DatastoreException e ) {
130 throw new IdGenerationException( e.getMessage(), e );
131 }
132 return pk;
133 }
134
135 /**
136 * Returns a new id for a feature of the given type.
137 *
138 * @param ft
139 * (mapped) feature type
140 * @return a new feature id.
141 * @throws IdGenerationException
142 * if the generation of the id could not be performed
143 */
144 @Override
145 public FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta )
146 throws IdGenerationException {
147
148 MappedGMLId fidDefinition = ft.getGMLId();
149 if ( fidDefinition.getKeySize() != 1 ) {
150 throw new IdGenerationException( "Cannot generate feature ids that are mapped to "
151 + fidDefinition.getKeySize() + " columns." );
152 }
153
154 Datastore ds = ft.getGMLSchema().getDatastore();
155 if ( !( ds instanceof AbstractSQLDatastore ) ) {
156 throw new IllegalArgumentException( "DBSeqIdGenerator can only be used with SQL datastores." );
157 }
158
159 Object fidNucleus;
160 try {
161 fidNucleus = ( (AbstractSQLDatastore) ds ).getSequenceNextVal( ( (SQLTransaction) ta ).getConnection(),
162 this.sequenceName );
163 } catch ( DatastoreException e ) {
164 throw new IdGenerationException( e.getMessage(), e );
165 }
166
167 FeatureId fid = new FeatureId( ft, new Object[] { fidNucleus } );
168 return fid;
169 }
170 }