001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/io/GenericSQLIDGenerator.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 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: 6259 $ $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 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, 083 boolean isNumeric ) { 084 this.con = con; 085 this.fieldName = fieldName; 086 this.tableName = tableName; 087 this.fieldType = fieldType; 088 this.isNumeric = isNumeric; 089 } 090 091 /** 092 * Returns the successor to the given id (string). Valid characters in the id-string are: 0-9, 093 * a-z, A-Z. Every other character may result in an exception. 094 * <p> 095 * 096 * @param lastId 097 * @return the successor to the given id (string). 098 */ 099 private String incrementId( String lastId ) { 100 char[] chars = lastId.toCharArray(); 101 for ( int i = chars.length - 1; i >= 0; i-- ) { 102 char c = chars[i]; 103 if ( c >= '0' && c <= '9' ) { 104 if ( c == '9' ) { 105 c = 'a'; 106 } else { 107 c++; 108 } 109 } else if ( c >= 'a' && c <= 'z' ) { 110 if ( c == 'z' ) { 111 c = 'A'; 112 } else { 113 c++; 114 } 115 } else if ( c >= 'A' && c <= 'Z' ) { 116 if ( c == 'Z' ) { 117 c = '0'; 118 } else { 119 c++; 120 } 121 } 122 chars[i] = c; 123 if ( c != '0' ) { 124 break; 125 } 126 } 127 String newId = new String( chars ); 128 if ( chars[0] == '0' ) { 129 newId = '1' + newId; 130 } 131 return newId; 132 } 133 134 /** 135 * Generates a new id, suitable as a primary key for the next dataset. 136 * <p> 137 * 138 * @return Id, the object type depends on the database field used as primary key 139 */ 140 public Object generateUniqueId() 141 throws SQLException { 142 143 Object id = null; 144 145 // retrieve last id 146 Statement stmt = con.createStatement(); 147 ResultSet rs = stmt.executeQuery( "SELECT MAX(" + fieldName + ") FROM " + tableName ); 148 149 if ( rs.next() ) { 150 if ( isNumeric ) { 151 id = new Integer( rs.getInt( 1 ) + 1 ); 152 } else { 153 String oldId = rs.getString( 1 ); 154 if ( oldId != null ) { 155 id = incrementId( oldId ); 156 } else { 157 id = "0"; 158 } 159 } 160 } else { 161 if ( isNumeric ) { 162 id = new Integer( 0 ); 163 } else { 164 id = "0"; 165 } 166 } 167 rs.close(); 168 stmt.close(); 169 return id; 170 } 171 } 172 /*************************************************************************************************** 173 * <code> 174 Changes to this class. What the people have been up to: 175 $Log$ 176 Revision 1.6 2007/01/26 14:42:20 wanhoff 177 fixed Javadoc @return tag and footer 178 179 Revision 1.5 2006/07/12 14:46:18 poth 180 comment footer added 181 182 </code> 183 **************************************************************************************************/