KickJava   Java API By Example, From Geeks To Geeks.

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


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: DBCatalog.java,v 1.1.2.1 2005/12/21 22:32:04 tomdz Exp $
25  */

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

318
Popular Tags