001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/io/IDGeneratorFactory.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.DatabaseMetaData;
048    import java.sql.ResultSet;
049    import java.sql.ResultSetMetaData;
050    import java.sql.SQLException;
051    import java.sql.Statement;
052    import java.sql.Types;
053    
054    import org.deegree.framework.log.ILogger;
055    import org.deegree.framework.log.LoggerFactory;
056    import org.deegree.framework.util.DataBaseIDGenerator;
057    
058    /**
059     * Factory for <tt>IDGenerator</tt>-instances. The generated instance is
060     * suitable for the database used and for the configuration (i.e. table and
061     * field type).
062     * <p>
063     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
064     * @version $Revision: 6259 $ $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
065     */
066    public class IDGeneratorFactory {
067        
068        private static final ILogger LOG = LoggerFactory.getLogger( IDGeneratorFactory.class );
069            
070            public static DataBaseIDGenerator createIDGenerator (Connection con, String tableName,
071                                                             String fieldName)
072                    throws SQLException {
073    
074                    // retrieve type of field
075                    Statement stmt = con.createStatement ();
076                    ResultSet rs = stmt.executeQuery ("SELECT " + fieldName + " FROM " + tableName);
077                    ResultSetMetaData rsmd = rs.getMetaData();
078                    int type = rsmd.getColumnType (1);
079                    rs.close ();
080                    stmt.close ();
081                    boolean isNumeric = false;
082    
083                    switch (type) {
084                            case Types.INTEGER:
085                            case Types.BIGINT:
086                            case Types.DECIMAL:
087                            case Types.NUMERIC:
088                            case Types.SMALLINT:
089                            case Types.TINYINT: {
090                                    isNumeric = true;
091                                    break;
092                            }
093                            case Types.CHAR:
094                            case Types.LONGVARCHAR:
095                            case Types.VARCHAR: {
096                                    isNumeric = false;
097                                    break;
098                            }                               
099                            default: {
100                                    throw new SQLException (
101                                            "Cannot create DataBaseIDGenerator for table '" + tableName +
102                                            "' and field '" + fieldName + "'. Only integer and alphanumeric " +
103                                            "fields are supported");
104                            }
105                    }
106    
107                    // find suitable instance of IDGenerator                
108                    DatabaseMetaData dbmd = con.getMetaData ();
109                    String dbName         = dbmd.getDatabaseProductName();
110                    String dbVersion      = dbmd.getDatabaseProductVersion();
111                    String driverName     = dbmd.getDriverName();
112                    String driverVersion  = dbmd.getDriverVersion();
113                    
114                    LOG.logDebug( "dbName       : " + dbName);
115                    LOG.logDebug( "dbVersion    : " + dbVersion);
116                    LOG.logDebug( "driverName   : " + driverName);
117                    LOG.logDebug( "driverVersion: " + driverVersion);
118                    
119                    DataBaseIDGenerator idGenerator = new GenericSQLIDGenerator (con, tableName, fieldName, type, isNumeric);
120                    return idGenerator;
121            }
122    }
123    /* ********************************************************************
124    Changes to this class. What the people have been up to:
125    $Log$
126    Revision 1.6  2006/07/12 14:46:18  poth
127    comment footer added
128    
129    ********************************************************************** */