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.Enumeration;
28 import java.util.Hashtable;
29 import java.util.Vector;
30 import org.apache.log4j.LogManager;
31 import org.apache.log4j.Logger;
32 import com.explosion.datastream.exql.gui.EXQLBaseTool;
33 import com.explosion.datastream.exql.gui.ExqlTreeNode;
34 import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
35 import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
36 import com.explosion.utilities.exception.ExceptionManagerFactory;
37 import com.explosion.utilities.process.StackableSimpleProcess;
38 import com.explosion.utilities.process.threads.Finishable;
39
40 /***
41 * @author Stephen Cowx Date created:@14-Feb-2003
42 */
43 public class RefreshTableNamesProcess extends StackableSimpleProcess
44 {
45
46 private static Logger log = LogManager.getLogger(RefreshTableNamesProcess.class);
47 private Connection conn;
48 private ExqlTreeNode parentNode;
49 private DBEntity descriptor;
50 private EXQLBaseTool tool;
51 private boolean prefetch = false;
52 private String[] tableTypes;
53
54 /***
55 * Fetches the tables list into a list of table
56 * @param finishable
57 * @param conn
58 * @param parentNode
59 * @param descriptor
60 * @param tool
61 * @param prefetchAllTableInformation
62 */
63 public RefreshTableNamesProcess(Finishable finishable, Connection conn, ExqlTreeNode parentNode, DBEntity descriptor, EXQLBaseTool tool, boolean prefetchAllTableInformation)
64 {
65 super(finishable, null);
66 this.conn = conn;
67 this.parentNode = parentNode;
68 this.descriptor = descriptor;
69 this.tool = tool;
70 this.prefetch = prefetchAllTableInformation;
71 this.setIsUserProcess(true);
72 }
73
74 public void process()
75 {
76 try
77 {
78 Vector collectedNodes = new Vector();
79
80 if (isStopped())
81 return;
82 setStatusText("Obtaining database metadata");
83 setPercentComplete(10);
84
85 DatabaseMetaData dbmd = MetadataUtils.getDBMD(conn);
86 MetadataUtils utils = new MetadataUtils();
87
88
89 if (isStopped())
90 return;
91 log("Fetching table definitions.");
92 setStatusText("Obtaining table types");
93 setPercentComplete(15);
94 Hashtable nodes = getTableTypeNodes(dbmd);
95
96
97 if (isStopped())
98 return;
99 setStatusText("Obtaining list of tables");
100 setPercentComplete(45);
101 Vector tables = utils.getTables(dbmd, descriptor.getCatalogName(), descriptor.getSchemaName(), null, null, this);
102
103 for (int k = 0; k < tables.size(); k++)
104 {
105 Vector tableInfo = (Vector) tables.elementAt(k);
106 String tableName = (String) tableInfo.elementAt(2);
107 String tableType = (String) tableInfo.elementAt(3);
108
109 if (isStopped())
110 return;
111 setStatusText("Parsing table " + tableName);
112 log("Parsing table " + tableName);
113
114 setPercentComplete(80 + 10 / tables.size());
115
116 ExqlTreeNode typeNode = (ExqlTreeNode) nodes.get(getConvertedName(tableType));
117
118 int tableTypeInt = DBEntity.TYPE_OTHER;
119 if (getConvertedName(tableType).toUpperCase().equals("SYSTEM TABLES"))
120 tableTypeInt = DBEntity.TYPE_SYSTEM_TABLE;
121 else if (getConvertedName(tableType).toUpperCase().equals("VIEWS"))
122 tableTypeInt = DBEntity.TYPE_VIEW;
123 else if (getConvertedName(tableType).toUpperCase().equals("TABLES"))
124 tableTypeInt = DBEntity.TYPE_TABLE;
125 else if (getConvertedName(tableType).toUpperCase().equals("SYNONYMS"))
126 tableTypeInt = DBEntity.TYPE_SYNONYM;
127
128 DBEntity tableDescriptor = new DBEntity(descriptor.getCatalogName(), descriptor.getSchemaName(), tableName, tableTypeInt);
129
130 log.debug("Building entity descriptor for table ==> catalog:" + descriptor.getCatalogName() + " schema:" + descriptor.getSchemaName() + " tableName:" + tableName + ".");
131 ExqlTreeNode tableNode = new ExqlTreeNode(tableDescriptor);
132
133 if (prefetch)
134 {
135 utils.getColumns(tableDescriptor, dbmd, tableNode);
136 utils.getTablePrimaryKeys(dbmd, tableDescriptor);
137 utils.getTableForeignKeys(dbmd, tableDescriptor);
138
139 }
140
141 typeNode.add(tableNode);
142 }
143
144 if (isStopped())
145 return;
146
147 setStatusText("Adding new nodes ");
148 setPercentComplete(90);
149
150
151 parentNode.removeAllChildren();
152
153 Enumeration en = nodes.keys();
154 while (en.hasMoreElements())
155 {
156 ExqlTreeNode typeNode = (ExqlTreeNode) nodes.get(en.nextElement());
157 if (typeNode.getChildCount() > 0)
158 {
159 parentNode.add(typeNode);
160 }
161 }
162
163 setStatusText("Done");
164 this.setPercentComplete(100);
165 }
166 catch (Exception e)
167 {
168 ExceptionManagerFactory.getExceptionManager().manageException(e, null);
169 setStatusText("Process not completed successfully");
170 setPercentComplete(100);
171 }
172
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.length; i++)
182 {
183 String tableType = tableTypes[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 String[] 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 }
206 catch (SQLException e)
207 {
208 ExceptionManagerFactory.getExceptionManager().manageException(e, "Error while retrieving table types. Resultset MetaData getTableTypes() has possibly not been implemented.");
209 }
210 finally
211 {
212 try
213 {
214 set.close();
215 }
216 catch (Exception e)
217 {
218 }
219 }
220 String[] tts = new String[tableTypes.size()];
221 return (String[]) tableTypes.toArray(tts);
222 }
223
224 /***
225 * This method converts an uppercase string into a sentence case string.ie
226 * first letter capital, all others lowercase.An s is appended too.
227 */
228 private String getConvertedName(String tableType)
229 {
230 return tableType.substring(0, 1).toUpperCase() + tableType.substring(1, tableType.length()).toLowerCase() + "s";
231 }
232
233 public void log(String string)
234 {
235 tool.log(string);
236 }
237
238 public void log(Exception exception, String message)
239 {
240 tool.log(message + exception.getMessage());
241 }
242
243 }