KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb > DBSchema


1 package org.apache.ojb.tools.mapping.reversedb;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.Iterator JavaDoc;
19 import javax.swing.tree.TreeNode JavaDoc;
20
21 /**
22  *
23  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
24  * @version $Id: DBSchema.java,v 1.1.2.1 2005/12/21 22:32:04 tomdz Exp $
25  */

26
27 public class DBSchema implements MetadataNodeInterface, TreeNode JavaDoc, org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel
28 {
29   private java.sql.DatabaseMetaData JavaDoc dbMeta;
30   private DBCatalog aDBCatalog;
31   
32   private boolean enabled = true;
33
34   private java.util.TreeMap JavaDoc tmTables = new java.util.TreeMap JavaDoc();
35   
36   private String JavaDoc m_strSchemaName;
37   
38   
39   /** Creates a new instance of DBSchema */
40   public DBSchema( java.sql.DatabaseMetaData JavaDoc pdbMeta, DBCatalog paDBCatalog,
41                    String JavaDoc pstrSchemaName)
42   {
43     aDBCatalog = paDBCatalog;
44     dbMeta = pdbMeta;
45     m_strSchemaName = pstrSchemaName;
46   }
47
48   public boolean isEnabled()
49   {
50     return this.enabled;
51   }
52   
53   public void setEnabled(boolean b)
54   {
55     this.enabled = b;
56   }
57   
58   
59   public DBCatalog getDBCatalog()
60   {
61     return this.aDBCatalog;
62   }
63   
64   public String JavaDoc getSchemaName()
65   {
66     return this.m_strSchemaName;
67   }
68   
69   public boolean isTreeEnabled()
70   {
71     return this.getDBCatalog().isTreeEnabled() && this.isEnabled();
72   }
73   
74   
75   public void read()
76     throws java.sql.SQLException JavaDoc
77   {
78     java.sql.ResultSet JavaDoc rs = dbMeta.getTables(this.getDBCatalog().getCatalogName(), this.getSchemaName(), "%", null);
79     while (rs.next())
80     {
81       String JavaDoc strTableCat = rs.getString("TABLE_CAT");
82       String JavaDoc strSchemaName = rs.getString("TABLE_SCHEM");
83       String JavaDoc strTableName = rs.getString("TABLE_NAME");
84       String JavaDoc strTableType = rs.getString("TABLE_TYPE");
85       // Pointbase returns the catalog name in uppercase here and in mixed
86
// case in getCatalogs(). Therefore we have to use toUpper().
87
if (
88           (strTableCat!=null && strTableCat.equalsIgnoreCase(this.getDBCatalog().getCatalogName()) || strTableCat==this.getDBCatalog().getCatalogName())
89           &&
90           (strSchemaName!=null && strSchemaName.equals(this.getSchemaName()) || strSchemaName==this.getSchemaName())
91          )
92         this.addTable(strTableName, strTableType);
93       
94     }
95     rs.close();
96     
97     rs = dbMeta.getColumns(this.getDBCatalog().getCatalogName(), this.getSchemaName(), "%", "%");
98     while (rs.next())
99     {
100       String JavaDoc strSchemaName = rs.getString("TABLE_SCHEM");
101       String JavaDoc strTableName = rs.getString("TABLE_NAME");
102       String JavaDoc strColumnName = rs.getString("COLUMN_NAME");
103       int iDataType = rs.getInt("DATA_TYPE");
104       String JavaDoc strTypeName = rs.getString("TYPE_NAME");
105       int iColumnSize = rs.getInt("COLUMN_SIZE");
106       int iNullable = rs.getInt("NULLABLE");
107       this.addColumn(strTableName, strColumnName,
108       iDataType, strTypeName, iColumnSize, iNullable);
109     }
110     rs.close();
111   }
112   
113   public void addTable(String JavaDoc strTableName, String JavaDoc strTableType)
114     throws java.sql.SQLException JavaDoc
115   {
116     DBTable aDBTable = new DBTable(dbMeta, this, strTableName);
117     this.tmTables.put(strTableName, aDBTable);
118     aDBTable.read();
119   }
120
121   public void addColumn(String JavaDoc strTableName, String JavaDoc strColumnName,
122             int iDataType, String JavaDoc strTypeName, int iColumnSize, int iNullable)
123   {
124     DBTable aDBTable= this.getTable(strTableName);
125     if (aDBTable != null)
126     {
127       aDBTable.addColumn( strColumnName,
128                           iDataType, strTypeName, iColumnSize, iNullable);
129     }
130   }
131   
132   public void addPrimaryKeyColumn(String JavaDoc strTableName, String JavaDoc strColumnName)
133   {
134     DBTable aDBTable = this.getTable(strTableName);
135     if (aDBTable != null)
136     {
137       aDBTable.addPrimaryKeyColumn(strColumnName);
138     }
139   }
140   
141   
142   public DBTable getTable(String JavaDoc strTableName)
143   {
144     return (DBTable)tmTables.get(strTableName);
145   }
146   
147   public void generateReferences()
148     throws java.sql.SQLException JavaDoc
149   {
150     Iterator JavaDoc it = tmTables.values().iterator();
151     while (it.hasNext())
152     {
153       ((DBTable)it.next()).generateReferences();
154     }
155   }
156   
157   public java.util.Enumeration JavaDoc children ()
158   {
159     return java.util.Collections.enumeration(this.tmTables.values());
160   }
161   
162   public boolean getAllowsChildren ()
163   {
164     return true;
165   }
166   
167   public TreeNode JavaDoc getChildAt(int param)
168   {
169     TreeNode JavaDoc tn = (TreeNode JavaDoc)tmTables.values().toArray()[param];
170     return tn;
171   }
172   
173   public int getChildCount ()
174   {
175     return this.tmTables.size();
176   }
177   
178   public int getIndex(TreeNode JavaDoc treeNode)
179   {
180     int indexOf = new java.util.Vector JavaDoc(tmTables.values()).indexOf(treeNode);
181     return indexOf;
182   }
183   
184   public TreeNode JavaDoc getParent()
185   {
186     return this.aDBCatalog;
187   }
188   
189   public boolean isLeaf ()
190   {
191     return false;
192   }
193   
194   public String JavaDoc toString()
195   {
196     if (m_strSchemaName == null || m_strSchemaName.trim().length() == 0)
197        return "<empty schema>";
198     else return this.m_strSchemaName;
199   }
200     
201   public Class JavaDoc getPropertySheetClass ()
202   {
203     return org.apache.ojb.tools.mapping.reversedb.gui.DBSchemaPropertySheet.class;
204   }
205   
206   public String JavaDoc getXML()
207   {
208       java.io.StringWriter JavaDoc swr = new java.io.StringWriter JavaDoc();
209       writeXML(new java.io.PrintWriter JavaDoc(swr));
210       return swr.getBuffer().toString();
211   }
212
213   public void writeXML(java.io.PrintWriter JavaDoc pw)
214   {
215       Iterator JavaDoc i = this.tmTables.values().iterator();
216       while (i.hasNext())
217       {
218         ((DBTable)i.next()).writeXML(pw);
219       }
220   }
221   
222   public void generateJava (java.io.File JavaDoc aFile, String JavaDoc strHeader, String JavaDoc strFooter) throws java.io.IOException JavaDoc, java.io.FileNotFoundException JavaDoc
223   {
224     Iterator JavaDoc i = this.tmTables.values().iterator();
225     while (i.hasNext()) ((DBTable)i.next()).generateJava(aFile, strHeader, strFooter);
226   }
227   
228   public void setPackage (String JavaDoc packageName)
229   {
230     Iterator JavaDoc i = this.tmTables.values().iterator();
231     while (i.hasNext()) ((DBTable)i.next()).setPackage(packageName);
232   }
233
234   public void disableClassesWithRegex(org.apache.regexp.RE aRegexp)
235   {
236     Iterator JavaDoc it = this.tmTables.values().iterator();
237     while (it.hasNext()) ((DBTable)it.next()).disableClassesWithRegex(aRegexp);
238   }
239     
240   
241 }
242
243 /***************************** Changelog *****************************
244 // $Log: DBSchema.java,v $
245 // Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
246 // Updated license
247 //
248 // Revision 1.1 2004/05/05 16:39:05 arminw
249 // fix fault
250 // wrong package structure used:
251 // org.apache.ojb.tools.reversdb
252 // org.apache.ojb.tools.reversdb2
253 //
254 // instead of
255 // org.apache.ojb.tools.mapping.reversdb
256 // org.apache.ojb.tools.mapping.reversdb2
257 //
258 // Revision 1.1 2004/05/04 13:45:01 arminw
259 // move reverseDB stuff
260 //
261 // Revision 1.11 2004/04/04 23:53:42 brianm
262 // Fixed initial copyright dates to match cvs repository
263 //
264 // Revision 1.10 2004/03/11 18:16:22 brianm
265 // ASL 2.0
266 //
267 // Revision 1.9 2003/09/13 16:59:44 brj
268 // prevent NPE
269 // patch by Jason Pyeron
270 //
271 // Revision 1.8 2003/06/29 14:28:07 florianbruckner
272 // fix a bug that could cause problems with all databases that use mixed or lower case catalog names (e.g. Informix, MySQL)
273 //
274 // Revision 1.7 2003/06/21 10:34:01 florianbruckner
275 // implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
276 //
277 // Revision 1.6 2003/06/07 10:10:04 brj
278 // some style fixes
279 //
280 // Revision 1.5 2003/02/21 12:44:10 florianbruckner
281 // add support for PointBase. The version distributed with Sun ONE AppServer
282 // returns a mixed case catalog name ("PointBase") in getCatalogs() and an upper
283 // case catalog name in getTables().
284 //
285 // Revision 1.4 2003/01/28 21:42:53 florianbruckner
286 // update XML generation
287 //
288 // Revision 1.3 2003/01/28 19:59:14 florianbruckner
289 // some updates to schema reading to make it a bit more compatible
290 //
291 // Revision 1.2 2002/06/17 19:34:33 jvanzyl
292 // Correcting all the package references.
293 // PR:
294 // Obtained from:
295 // Submitted by:
296 // Reviewed by:
297 //
298 // Revision 1.1.1.1 2002/06/17 18:16:52 jvanzyl
299 // Initial OJB import
300 //
301 // Revision 1.3 2002/05/16 11:47:09 florianbruckner
302 // fix CR/LF issue, change license to ASL
303 //
304 // Revision 1.2 2002/05/16 10:43:59 florianbruckner
305 // use jakarta-regexp instead of gnu-regexp due to the move to jakarta.
306 //
307 // Revision 1.1 2002/04/18 11:44:16 mpoeschl
308 //
309 // move files to new location
310 //
311 // Revision 1.3 2002/04/07 09:05:16 thma
312 // *** empty log message ***
313 //
314 // Revision 1.2 2002/03/11 17:36:05 florianbruckner
315 // fix line break issue for these files that were previously checked in with -kb
316 //
317 // Revision 1.1 2002/03/04 17:19:32 thma
318 // initial checking for Florians Reverse engineering tool
319 //
320 // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
321 // initial import
322 //
323 /***************************** Changelog *****************************/

324
Popular Tags