001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/GenericSQLIDGenerator.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;
037    
038    import java.sql.Connection;
039    import java.sql.ResultSet;
040    import java.sql.SQLException;
041    import java.sql.Statement;
042    
043    import org.deegree.framework.util.DataBaseIDGenerator;
044    
045    /**
046     * Primary key generator for generic JDBC-connections.
047     * <p>
048     * NOTE: At the moment, every application has to take care of locking the table to prevent problems
049     * in multithreaded or multihosted applications.
050     * <p>
051     *
052     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
053     * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
054     */
055    class GenericSQLIDGenerator implements DataBaseIDGenerator {
056    
057        Connection con;
058    
059        int fieldType;
060    
061        boolean isNumeric;
062    
063        String tableName;
064    
065        String fieldName;
066    
067        /**
068         * Constructs a new GenericSQLIDGenerator.
069         * <p>
070         *
071         * @param con
072         * @param fieldType
073         */
074        GenericSQLIDGenerator( Connection con, String tableName, String fieldName, int fieldType, boolean isNumeric ) {
075            this.con = con;
076            this.fieldName = fieldName;
077            this.tableName = tableName;
078            this.fieldType = fieldType;
079            this.isNumeric = isNumeric;
080        }
081    
082        /**
083         * Returns the successor to the given id (string). Valid characters in the id-string are: 0-9,
084         * a-z, A-Z. Every other character may result in an exception.
085         * <p>
086         *
087         * @param lastId
088         * @return the successor to the given id (string).
089         */
090        private String incrementId( String lastId ) {
091            char[] chars = lastId.toCharArray();
092            for ( int i = chars.length - 1; i >= 0; i-- ) {
093                char c = chars[i];
094                if ( c >= '0' && c <= '9' ) {
095                    if ( c == '9' ) {
096                        c = 'a';
097                    } else {
098                        c++;
099                    }
100                } else if ( c >= 'a' && c <= 'z' ) {
101                    if ( c == 'z' ) {
102                        c = 'A';
103                    } else {
104                        c++;
105                    }
106                } else if ( c >= 'A' && c <= 'Z' ) {
107                    if ( c == 'Z' ) {
108                        c = '0';
109                    } else {
110                        c++;
111                    }
112                }
113                chars[i] = c;
114                if ( c != '0' ) {
115                    break;
116                }
117            }
118            String newId = new String( chars );
119            if ( chars[0] == '0' ) {
120                newId = '1' + newId;
121            }
122            return newId;
123        }
124    
125        /**
126         * Generates a new id, suitable as a primary key for the next dataset.
127         * <p>
128         *
129         * @return Id, the object type depends on the database field used as primary key
130         */
131        public Object generateUniqueId()
132                                throws SQLException {
133    
134            Object id = null;
135    
136            // retrieve last id
137            Statement stmt = con.createStatement();
138            ResultSet rs = stmt.executeQuery( "SELECT MAX(" + fieldName + ") FROM " + tableName );
139    
140            if ( rs.next() ) {
141                if ( isNumeric ) {
142                    id = new Integer( rs.getInt( 1 ) + 1 );
143                } else {
144                    String oldId = rs.getString( 1 );
145                    if ( oldId != null ) {
146                        id = incrementId( oldId );
147                    } else {
148                        id = "0";
149                    }
150                }
151            } else {
152                if ( isNumeric ) {
153                    id = new Integer( 0 );
154                } else {
155                    id = "0";
156                }
157            }
158            rs.close();
159            stmt.close();
160            return id;
161        }
162    }