View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core.service;
20  
21  
22  import java.sql.*;
23  import java.util.Hashtable;
24  import java.util.Vector;
25  
26  import org.caleigo.core.*;
27  import org.caleigo.toolkit.util.*;
28  
29  /***
30   *
31   * @author  Mattias Hagstrand
32   * @version 1.0
33   */
34  /* 
35  *
36  * WHEN        WHO               WHY & WHAT
37  * ------------------------------------------------------------------------------
38  * 2001-07-20  Mattias Hagstrand    Creation
39  */
40  public class JDBCDataSourceDescriptor extends AbstractDataSourceDescriptor
41  {
42  	// Static methods ---------------------------------------------------------
43  	
44  	private static Object[] createCustomDescriptors(IProgressReceiver progressReceiver, String driverName, String url, String user, String password, String catalog, String schema)
45  	{
46  		Vector entityDescriptors = new Vector();
47  		Connection conn = null;
48  		
49  		try
50          {
51  			Vector tables = new Vector();			
52  			
53              Class.forName(driverName);
54              
55              conn = DriverManager.getConnection(url, user, password);
56              
57  			DatabaseMetaData dbMetaData = conn.getMetaData();
58  			
59  			ResultSet tablesRS = dbMetaData.getTables(catalog, schema, "%", new String[] {"TABLE"});
60  			int nbrOfTables = 0;
61              while (tablesRS.next())
62  			{
63  				System.out.println("Found table: " + tablesRS.getString("TABLE_NAME"));
64  				tables.add(tablesRS.getString("TABLE_NAME"));
65  				nbrOfTables++;
66  			}
67  			tablesRS.close();
68  			
69              for (int i = 0; i < tables.size(); i++)
70              {				
71  				String tableName = (String) tables.get(i);
72  				Vector fieldDescriptors = new Vector();
73  				Hashtable primaryKeys = new Hashtable();
74  				Hashtable indexes = new Hashtable();
75  				Hashtable versionCols = new Hashtable();
76  			
77  				progressReceiver.setProgressMessage("Analyzing table " + tableName + "...");
78  				
79  				ResultSet primaryKeysRS = dbMetaData.getPrimaryKeys(catalog, schema, tableName);
80  				while (primaryKeysRS.next())
81  				{
82  					System.out.println("Found primary key: " + primaryKeysRS.getString("COLUMN_NAME"));
83  					primaryKeys.put(primaryKeysRS.getString("COLUMN_NAME"), "");
84  				}
85  				primaryKeysRS.close();
86  				
87  				ResultSet indexesRS = dbMetaData.getIndexInfo(catalog, schema, tableName, true, false);
88  				while (indexesRS.next())
89  				{
90  					String columnName = indexesRS.getString("COLUMN_NAME");
91  					if (columnName != null)
92  					{
93  						System.out.println("Index column in table " + tableName + ": " + columnName);
94  						indexes.put(columnName, "");
95  					}
96  				}
97  				indexesRS.close();
98  				
99  				ResultSet versionColsRS = dbMetaData.getVersionColumns(catalog, schema, tableName);
100 				while (versionColsRS.next())
101 				{
102 					System.out.println("Found version col: " + versionColsRS.getString("COLUMN_NAME"));
103 					versionCols.put(versionColsRS.getString("COLUMN_NAME"), "");
104 				}
105 				versionColsRS.close();
106 				
107 				ResultSet columnsRS = dbMetaData.getColumns(catalog, schema, tableName, "%");
108 				int index = 0;
109 				while (columnsRS.next())
110 				{
111 					System.out.println("Found column: " + columnsRS.getString("COLUMN_NAME"));
112 					
113 					String sourceName = columnsRS.getString("COLUMN_NAME");
114 					
115 					int flags = 0;		
116 					if (columnsRS.getString("IS_NULLABLE").compareTo("NO") == 0)				
117 						flags |= IFieldDescriptor.REQUIRED;				
118 					if (primaryKeys.containsKey(sourceName))					
119 						flags |= IFieldDescriptor.IDENTITY_FIELD;					
120 					if (indexes.containsKey(sourceName))					
121 						flags |= IFieldDescriptor.INDEXED;					
122 					if (versionCols.contains(sourceName))
123 						flags |= IFieldDescriptor.AUTOGEN;
124 					
125 					fieldDescriptors.add(index, createFieldDescriptor(createCodeName(sourceName), sourceName,
126 													createDisplayName(sourceName), translateType(columnsRS.getShort("DATA_TYPE")),
127 													columnsRS.getInt("COLUMN_SIZE"), flags, columnsRS.getObject("COLUMN_DEF")));
128 					//System.out.println("Column " + sourceName + ": " + ((IFieldDescriptor) fieldDescriptors.get(index)).getDataType().getClass().getName());
129 					index++;
130 				}
131 				
132 				columnsRS.close();
133 				
134                 //TODO nno: Check whether it's ok to send null as parameter
135 				entityDescriptors.add(new CompositeEntityDescriptor(createDisplayName(tableName), null, (IFieldDescriptor[]) fieldDescriptors.toArray(new IFieldDescriptor[0])));
136                 
137 				progressReceiver.setProgress((int) (((i + 1) / (nbrOfTables * 1.0)) * 100));
138             }			
139         }
140         catch (Exception e)
141         {
142             e.printStackTrace();           
143         }
144 		finally
145 		{
146 			try
147             {
148                 conn.close();
149             }
150             catch(SQLException sqle)
151             {
152                 sqle.printStackTrace();
153             }
154 		}
155 		
156 		return entityDescriptors.toArray(new IEntityDescriptor[0]);
157 	}
158 	
159 	private static String createCodeName(String sourceName)
160 	{
161 		return sourceName;
162 	}
163 	
164 	private static String createDisplayName(String sourceName)
165 	{
166 		return sourceName;
167 	}
168 	
169 	private static DataType translateType(short type)
170 	{
171 		switch (type)
172         {
173 			case Types.CHAR:
174 			case Types.VARCHAR:
175 			case Types.LONGVARCHAR:
176                 return DataType.STRING;
177 			case Types.TINYINT:
178                 return DataType.BYTE;
179 			case Types.SMALLINT:
180                 return DataType.SHORT;
181 			case Types.INTEGER:
182                 return DataType.INTEGER;
183 			case Types.BIGINT:
184                 return DataType.LONG;
185 			case Types.REAL:
186                 return DataType.FLOAT;
187 			case Types.FLOAT:
188 			case Types.DOUBLE:
189                 return DataType.DOUBLE;
190 			case Types.DATE:
191                 return DataType.DATE;
192 			case Types.BOOLEAN:
193                 return DataType.BOOLEAN;
194 			case Types.TIME:                
195             case Types.TIMESTAMP:
196                 return DataType.DATE;
197 				
198 				
199             case Types.ARRAY:
200             case Types.NUMERIC:
201 			case Types.DECIMAL:
202             case Types.BINARY:                
203             case Types.BIT:                
204             case Types.BLOB:                
205             case Types.CLOB:                
206 			case Types.DATALINK:                
207             case Types.DISTINCT:              
208             case Types.JAVA_OBJECT:                
209             case Types.LONGVARBINARY:                
210             case Types.NULL:                
211             case Types.OTHER:                
212             case Types.REF:                
213             case Types.STRUCT:
214             case Types.VARBINARY:
215             default:
216 				return DataType.UNKNOWN;
217         }
218 	}
219 
220 	
221     // Constructors -----------------------------------------------------------
222 	
223     public JDBCDataSourceDescriptor(IProgressReceiver progressReceiver, String driverName, String url, String user, String password, String catalog, String schema)
224     {				
225         super(null, catalog + "_" + schema, catalog + "_" + schema, "1.0", false, createCustomDescriptors(progressReceiver, driverName, url, user, password, catalog, schema));
226     }
227 }