1 package com.explosion.datastream.exql.processes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.sql.Connection;
24 import java.sql.DatabaseMetaData;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.util.Hashtable;
28 import java.util.Vector;
29
30 import com.explosion.datastream.exql.gui.EXQLBaseTool;
31 import com.explosion.datastream.exql.gui.ExqlTreeNode;
32 import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
33 import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
34 import com.explosion.utilities.exception.ExceptionManagerFactory;
35 import com.explosion.utilities.process.StackableSimpleProcess;
36 import com.explosion.utilities.process.threads.Finishable;
37
38 /***
39 * @author Stephen Cowx Date created:@14-Feb-2003
40 */
41 public class RefreshTableInformationProcess extends StackableSimpleProcess
42 {
43
44 private Connection conn;
45 private ExqlTreeNode parentNode;
46 private DBEntity descriptor;
47 private EXQLBaseTool tool;
48
49 private Vector tableTypes;
50
51 /***
52 * Constructor for RefreshTopNodesProcess.
53 */
54 public RefreshTableInformationProcess(Finishable finishable, Connection conn, ExqlTreeNode parentNode, DBEntity descriptor, EXQLBaseTool tool)
55 {
56 super(finishable, null);
57 this.conn = conn;
58 this.parentNode = parentNode;
59 this.descriptor = descriptor;
60 this.tool = tool;
61 this.setIsUserProcess(true);
62 }
63
64 /***
65 * Constructor for RefreshTopNodesProcess.
66 */
67 public RefreshTableInformationProcess(StackableSimpleProcess parentProcess, Finishable finishable, Connection conn, ExqlTreeNode parentNode, DBEntity descriptor, EXQLBaseTool tool)
68 {
69 super(finishable, parentProcess);
70 this.conn = conn;
71 this.parentNode = parentNode;
72 this.descriptor = descriptor;
73 this.tool = tool;
74 this.setIsUserProcess(true);
75 }
76
77 public void process()
78 {
79 try
80 {
81 this.parentNode.removeAllChildren();
82
83 if (isStopped())
84 return;
85
86 setPercentComplete(10);
87
88 DatabaseMetaData dbmd = MetadataUtils.getDBMD(conn);
89
90
91 if (isStopped())
92 return;
93
94 setPercentComplete(15);
95
96
97 parentNode.removeAllChildren();
98 descriptor.clearMetadata();
99
100 if (descriptor.getEntityType() == DBEntity.TYPE_TABLE || descriptor.getEntityType() == DBEntity.TYPE_SYNONYM || descriptor.getEntityType() == DBEntity.TYPE_SYSTEM_TABLE
101 || descriptor.getEntityType() == DBEntity.TYPE_VIEW)
102 {
103 log("Fetching column information for " + descriptor.getEntityName());
104 setStatusText("Fetching column information for " + descriptor.getEntityName());
105
106 MetadataUtils utils = new MetadataUtils();
107
108 utils.getColumns(descriptor, dbmd, parentNode);
109 utils.getTablePrimaryKeys(dbmd, descriptor);
110 utils.getTableForeignKeys(dbmd, descriptor);
111
112
113
114 }
115
116
117 if (isStopped())
118 return;
119
120 log("Finished fetching column information.");
121 setStatusText("Finished fetching column information.");
122
123 setStatusText("Done");
124 this.setPercentComplete(100);
125 } catch (Exception e)
126 {
127 ExceptionManagerFactory.getExceptionManager().manageException(e, null);
128 setStatusText("Process not completed successfully");
129 setPercentComplete(100);
130 }
131
132 }
133
134 /***
135 * This method will return a list of the tables in this database. Each row
136 * of this list contains table data in the folowing order: Catalog, Schema,
137 * Name, Type, Remarks <br>
138 * If there aren't any tables or the driver does not support this method
139 * then an empty Vector will be returned.
140 */
141 private Vector getTables(DatabaseMetaData dbmd, String catalogName, String schemaName, String tableName, String[] types)
142 {
143 Vector tables = new Vector();
144 ResultSet set = null;
145 try
146 {
147 set = dbmd.getTables(catalogName, schemaName, tableName, types);
148 while (set.next())
149 {
150 if (isStopped())
151 break;
152
153 Vector tableData = new Vector();
154 tableData.addElement(set.getString(1));
155 tableData.addElement(set.getString(2));
156 tableData.addElement(set.getString(3));
157 tableData.addElement(set.getString(4));
158 tableData.addElement(set.getString(5));
159 tables.addElement(tableData);
160 }
161 } catch (SQLException e)
162 {
163 ExceptionManagerFactory.getExceptionManager().manageException(e, "Error while retrieving tables. Resultset MetaData getTables() has possibly not been implemented.");
164 } finally
165 {
166 try
167 {
168 set.close();
169 } catch (Exception e)
170 {}
171 }
172 return tables;
173 }
174
175 private Hashtable getTableTypeNodes(DatabaseMetaData dbmd) throws Exception
176 {
177 Hashtable nodes = new Hashtable();
178 if (tableTypes == null)
179 tableTypes = getTableTypes(dbmd);
180
181 for (int i = 0; i < tableTypes.size(); i++)
182 {
183 String tableType = (String) tableTypes.elementAt(i);
184 String convertedName = getConvertedName(tableType);
185 ExqlTreeNode tableTypeNode = new ExqlTreeNode(convertedName);
186 nodes.put(convertedName, tableTypeNode);
187 }
188 return nodes;
189 }
190
191 /***
192 * This method will return a set of Table Types. If there aren't any or the
193 * driver does not support this method then an empty Vector will be
194 * returned.
195 */
196 private Vector getTableTypes(DatabaseMetaData dbmd)
197 {
198 Vector tableTypes = new Vector();
199 ResultSet set = null;
200 try
201 {
202 set = dbmd.getTableTypes();
203 while (set.next())
204 tableTypes.addElement(set.getString(1));
205 } catch (SQLException e)
206 {
207 ExceptionManagerFactory.getExceptionManager().manageException(e, "Error while retrieving table types. Resultset MetaData getTableTypes() has possibly not been implemented.");
208 } finally
209 {
210 try
211 {
212 set.close();
213 } catch (Exception e)
214 {}
215 }
216 return tableTypes;
217 }
218
219 /***
220 * This method converts an uppercase string into a sentence case string.ie
221 * first letter capital, all others lowercase.An s is appended too.
222 */
223 private String getConvertedName(String tableType)
224 {
225 return tableType.substring(0, 1).toUpperCase() + tableType.substring(1, tableType.length()).toLowerCase() + "s";
226 }
227
228 public void log(String string)
229 {
230 tool.log(string);
231 }
232
233 public void log(Exception exception, String message)
234 {
235 tool.log(message + exception.getMessage());
236 }
237
238 }