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