    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/io/datastore/sql/idgenerator/DBSeqIdGenerator.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.io.datastore.sql.idgenerator;
    037
    038    import java.sql.Connection;
    039    import java.util.Properties;
    040
    041    import org.deegree.io.datastore.Datastore;
    042    import org.deegree.io.datastore.DatastoreException;
    043    import org.deegree.io.datastore.DatastoreTransaction;
    044    import org.deegree.io.datastore.FeatureId;
    045    import org.deegree.io.datastore.idgenerator.IdGenerationException;
    046    import org.deegree.io.datastore.idgenerator.IdGenerator;
    047    import org.deegree.io.datastore.schema.MappedFeatureType;
    048    import org.deegree.io.datastore.schema.MappedGMLId;
    049    import org.deegree.io.datastore.sql.AbstractSQLDatastore;
    050    import org.deegree.io.datastore.sql.transaction.SQLTransaction;
    051
    052    /**
    053     * Feature id generator that uses an SQL sequence to create new values.
    054     *
    055     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
    056     * @author last edited by: $Author: mschneider $
    057     *
    058     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    059     */
    060    public class DBSeqIdGenerator extends IdGenerator {
    061
    062        private String sequenceName;
    063
    064        private int offset = -1;
    065
    066        /**
    067         * Creates a new <code>DBSeqIdGenerator</code> instance.
    068         * <p>
    069         * Supported configuration parameters:
    070         * <table border="1">
    071         * <tr>
    072         * <th>Name</th>
    073         * <th>optional?</th>
    074         * <th>Usage</th>
    075         * </tr>
    076         * <tr>
    077         * <td>sequence</td>
    078         * <td>no</td>
    079         * <td>name of the SQL sequence to be used</td>
    080         * </tr>
    081         * <tr>
    082         * <td>offset</td>
    083         * <td>yes</td>
    084         * <td>integer value, if omitted the next value of the sequence is used (default)<br>
    085         * otherwise the current value + offset is used</td>
    086         * </tr>
    087         * </table>
    088         *
    089         * @param params
    090         *            configuration parameters
    091         * @throws IdGenerationException
    092         */
    093        public DBSeqIdGenerator( Properties params ) throws IdGenerationException {
    094            super( params );
    095            this.sequenceName = params.getProperty( "sequence" );
    096            if ( this.sequenceName == null ) {
    097                throw new IdGenerationException( "DBSeqIdGenerator requires 'sequence' parameter." );
    098            }
    099            String offsetString = params.getProperty( "offset" );
    100            if ( offsetString != null ) {
    101                try {
    102                    offset = Integer.parseInt( offsetString );
    103                } catch ( NumberFormatException e ) {
    104                    throw new IdGenerationException( "DBSeqIdGenerator 'offset' parameter must be an integer value." );
    105                }
    106            }
    107        }
    108
    109        /**
    110         * Returns the name of the used sequence.
    111         *
    112         * @return the name of the used sequence
    113         */
    114        public String getSequenceName() {
    115            return this.sequenceName;
    116        }
    117
    118        /**
    119         * Returns a new primary key.
    120         *
    121         * @param ta
    122         *            datastore transaction (context)
    123         * @return a new primary key.
    124         * @throws IdGenerationException
    125         *             if the generation of the id could not be performed
    126         */
    127        @Override
    128        public Object getNewId( DatastoreTransaction ta )
    129                                throws IdGenerationException {
    130            if ( !( ta instanceof SQLTransaction ) ) {
    131                throw new IllegalArgumentException( "DBSeqIdGenerator can only be used with SQL datastores." );
    132            }
    133
    134            Object pk;
    135            try {
    136                AbstractSQLDatastore ds = (AbstractSQLDatastore) ta.getDatastore();
    137                Connection conn = ( (SQLTransaction) ta ).getConnection();
    138                if (offset == -1) {
    139                    pk = ds.getSequenceNextVal( conn, this.sequenceName );
    140                } else {
    141                    pk = ds.getSequenceCurrValPlusOffset( conn, this.sequenceName, offset );
    142                }
    143            } catch ( DatastoreException e ) {
    144                throw new IdGenerationException( e.getMessage(), e );
    145            }
    146            return pk;
    147        }
    148
    149        /**
    150         * Returns a new id for a feature of the given type.
    151         *
    152         * @param ft
    153         *            (mapped) feature type
    154         * @return a new feature id.
    155         * @throws IdGenerationException
    156         *             if the generation of the id could not be performed
    157         */
    158        @Override
    159        public FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta )
    160                                throws IdGenerationException {
    161
    162            MappedGMLId fidDefinition = ft.getGMLId();
    163            if ( fidDefinition.getKeySize() != 1 ) {
    164                throw new IdGenerationException( "Cannot generate feature ids that are mapped to "
    165                                                 + fidDefinition.getKeySize() + " columns." );
    166            }
    167
    168            Datastore ds = ft.getGMLSchema().getDatastore();
    169            if ( !( ds instanceof AbstractSQLDatastore ) ) {
    170                throw new IllegalArgumentException( "DBSeqIdGenerator can only be used with SQL datastores." );
    171            }
    172
    173            Object fidNucleus;
    174            try {
    175                fidNucleus = ( (AbstractSQLDatastore) ds ).getSequenceNextVal( ( (SQLTransaction) ta ).getConnection(),
    176                                                                               this.sequenceName );
    177            } catch ( DatastoreException e ) {
    178                throw new IdGenerationException( e.getMessage(), e );
    179            }
    180
    181            FeatureId fid = new FeatureId( ft, new Object[] { fidNucleus } );
    182            return fid;
    183        }
    184    }