KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 public class DBMeta implements MetadataNodeInterface, TreeNode JavaDoc, org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel
27 {
28   private boolean enabled = true;
29   
30   private java.sql.DatabaseMetaData JavaDoc dbMeta;
31   
32   // private java.util.TreeMap hmCatalogs = new java.util.TreeMap();
33
private java.util.HashMap JavaDoc hmCatalogs = new java.util.HashMap JavaDoc();
34   private String JavaDoc catalogTerm;
35   private String JavaDoc catalogSeparator;
36   private boolean isCatalogAtStart;
37   private boolean supportsCatalogsInDataManipulation;
38   private boolean supportsCatalogsInIndexDefinitions;
39   private boolean supportsCatalogsInPrivilegeDefinitions;
40   private boolean supportsCatalogsInProcedureCalls;
41   private boolean supportsCatalogsInTableDefinitions;
42   private String JavaDoc schemaTerm;
43   
44   private String JavaDoc catalogPattern;
45   private String JavaDoc schemaPattern;
46   
47   private String JavaDoc databaseProductName = null;
48   private String JavaDoc databaseProductVersion = null;
49   
50   
51   /** Creates a new instance of DBSchema */
52   public DBMeta (String JavaDoc pCatalogPattern, String JavaDoc pSchemaPattern, java.sql.DatabaseMetaData JavaDoc pDbMeta) throws java.sql.SQLException JavaDoc
53   {
54     super();
55     this.dbMeta = pDbMeta;
56     this.catalogPattern = pCatalogPattern;
57     this.schemaPattern = pSchemaPattern;
58     System.err.println("Using " + dbMeta.getDriverName() + " " + dbMeta.getDriverVersion());
59   }
60   
61   public DBMeta (java.sql.DatabaseMetaData JavaDoc pDbMeta) throws java.sql.SQLException JavaDoc
62   {
63       this(null, null, pDbMeta);
64   }
65   
66   public String JavaDoc getSchemaPattern()
67   {
68     return this.schemaPattern;
69   }
70   
71   public boolean isEnabled()
72   {
73     return this.enabled;
74   }
75   
76   public void setEnabled(boolean b)
77   {
78     this.enabled = b;
79   }
80
81   public String JavaDoc getDatabaseProductVersion()
82   {
83     return this.databaseProductVersion;
84   }
85   
86   public String JavaDoc getDatabaseProductName()
87   {
88     return this.databaseProductName;
89   }
90   
91   public boolean getSupportsCatalogsInIndexDefinitions()
92   {
93     return this.supportsCatalogsInIndexDefinitions;
94   }
95   
96   public boolean getSupportsCatalogsInDataManipulation()
97   {
98     return this.supportsCatalogsInDataManipulation;
99   }
100   
101   public boolean getSupportsCatalogsInPrivilegeDefinitions()
102   {
103     return this.supportsCatalogsInPrivilegeDefinitions;
104   }
105   
106   public boolean getSupportsCatalogsInProcedureCalls()
107   {
108     return this.supportsCatalogsInProcedureCalls;
109   }
110   
111   public boolean getSupportsCatalogsInTableDefinitions()
112   {
113     return this.supportsCatalogsInTableDefinitions;
114   }
115   
116   public String JavaDoc getCatalogTerm()
117   {
118     return this.catalogTerm;
119   }
120   
121   public String JavaDoc getSchemaTerm()
122   {
123     return this.schemaTerm;
124   }
125   
126   public String JavaDoc getCatalogSeparator()
127   {
128     return this.catalogSeparator;
129   }
130   
131   public boolean getIsCatalogAtStart()
132   {
133     return this.isCatalogAtStart;
134   }
135   
136   public DBCatalog getCatalog(String JavaDoc catalogName)
137   {
138     return (DBCatalog)this.hmCatalogs.get(catalogName);
139   }
140   
141   public void read()
142     throws java.sql.SQLException JavaDoc
143   {
144     this.databaseProductName = dbMeta.getDatabaseProductName ();
145     this.databaseProductVersion = dbMeta.getDatabaseProductVersion ();
146     catalogTerm = dbMeta.getCatalogTerm();
147     catalogSeparator = dbMeta.getCatalogSeparator();
148     isCatalogAtStart = dbMeta.isCatalogAtStart();
149     supportsCatalogsInDataManipulation = dbMeta.supportsCatalogsInDataManipulation();
150     supportsCatalogsInIndexDefinitions = dbMeta.supportsCatalogsInIndexDefinitions();
151     supportsCatalogsInPrivilegeDefinitions = dbMeta.supportsCatalogsInPrivilegeDefinitions();
152     supportsCatalogsInProcedureCalls = dbMeta.supportsCatalogsInProcedureCalls();
153     supportsCatalogsInTableDefinitions = dbMeta.supportsCatalogsInTableDefinitions();
154     schemaTerm = dbMeta.getSchemaTerm();
155     
156     java.sql.ResultSet JavaDoc rs = dbMeta.getCatalogs();
157     int count = 0;
158     while(rs.next())
159     {
160         count++;
161         String JavaDoc strCatalogName = rs.getString("TABLE_CAT");
162         try
163         {
164           if (new org.apache.regexp.RE(this.catalogPattern).match(strCatalogName))
165           {
166             DBCatalog aDBCatalog = new DBCatalog(dbMeta, this, strCatalogName);
167             this.hmCatalogs.put(strCatalogName, aDBCatalog);
168           }
169         }
170         catch (org.apache.regexp.RESyntaxException ex)
171         {
172           // This expception should be reported, but this does not fit currently.
173
ex.printStackTrace();
174         }
175     }
176     rs.close();
177     if (count==0)
178     {
179         DBCatalog aDBCatalog = new DBCatalog(dbMeta, this, null);
180         this.hmCatalogs.put(null, aDBCatalog);
181     }
182     
183     // Read after closing recordset.
184
Iterator JavaDoc it = hmCatalogs.values().iterator();
185     while (it.hasNext()) ((DBCatalog)it.next()).read();
186   }
187   
188   public void generateReferences()
189     throws java.sql.SQLException JavaDoc
190   {
191     Iterator JavaDoc it = this.hmCatalogs.values().iterator();
192     while (it.hasNext())
193     {
194       ((DBCatalog)it.next()).generateReferences();
195     }
196   }
197   
198   public void debug()
199   {
200
201   }
202   
203   public java.util.Enumeration JavaDoc children ()
204   {
205     return java.util.Collections.enumeration(this.hmCatalogs.values());
206   }
207   
208   public boolean getAllowsChildren ()
209   {
210     return true;
211   }
212   
213   public TreeNode JavaDoc getChildAt(int param)
214   {
215     return (TreeNode JavaDoc)this.hmCatalogs.values().toArray()[param];
216   }
217   
218   public int getChildCount ()
219   {
220     return this.hmCatalogs.size();
221   }
222   
223   public int getIndex(TreeNode JavaDoc treeNode)
224   {
225     return new java.util.Vector JavaDoc(hmCatalogs.values()).indexOf(treeNode);
226   }
227   
228   public TreeNode JavaDoc getParent()
229   {
230     return null;
231   }
232   
233   public boolean isLeaf ()
234   {
235     return false;
236   }
237   
238   public String JavaDoc toString()
239   {
240     return "DBMeta";
241   }
242     
243   public Class JavaDoc getPropertySheetClass ()
244   {
245     return org.apache.ojb.tools.mapping.reversedb.gui.DBMetaPropertySheet.class;
246   }
247
248   public String JavaDoc getXML()
249   {
250         java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc();
251         writeXML(new java.io.PrintWriter JavaDoc(sw));
252         return sw.getBuffer().toString();
253   }
254   
255   public void writeXML(java.io.PrintWriter JavaDoc pw)
256   {
257       
258     
259     pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
260     pw.println("<!DOCTYPE descriptor-repository SYSTEM \"repository.dtd\">" );
261     pw.println("<descriptor-repository version=\"0.9.9\">" );
262     pw.println(" <jdbc-connection-descriptor" );
263     pw.println(" jcd-alias=\"default\"" );
264     pw.println(" default-connection=\"true\"" );
265     pw.println(" platform=\"XXXX\"" );
266     pw.println(" jdbc-level=\"1.0\"");
267     pw.println(" driver=\"XXX\"" );
268     pw.println(" protocol=\"XXX\"" );
269     pw.println(" subprotocol=\"XXX\"");
270     pw.println(" dbalias=\"XXX\"" );
271     pw.println(" username=\"XXX\"");
272     pw.println(" password=\"XXX\">");
273     pw.println(" </jdbc-connection-descriptor>");
274     
275     Iterator JavaDoc i = this.hmCatalogs.values().iterator();
276     while (i.hasNext())
277     {
278       ((DBCatalog)i.next()).writeXML(pw);
279     }
280     pw.println("</descriptor-repository>");
281   }
282   
283   public void generateJava (java.io.File JavaDoc aFile, String JavaDoc strHeader, String JavaDoc strFooter) throws java.io.IOException JavaDoc, java.io.FileNotFoundException JavaDoc
284   {
285     Iterator JavaDoc it = this.hmCatalogs.values().iterator();
286     while (it.hasNext()) ((DBCatalog)it.next()).generateJava(aFile, strHeader, strFooter);
287   }
288   
289   public void setPackage (String JavaDoc packageName)
290   {
291     Iterator JavaDoc it = this.hmCatalogs.values().iterator();
292     while (it.hasNext()) ((DBCatalog)it.next()).setPackage(packageName);
293   }
294   
295   public void disableClassesWithRegex(org.apache.regexp.RE aRegexp)
296   {
297     Iterator JavaDoc it = this.hmCatalogs.values().iterator();
298     while (it.hasNext()) ((DBCatalog)it.next()).disableClassesWithRegex(aRegexp);
299   }
300   
301   
302 }
303
304
305 /***************************** Changelog *****************************
306 // $Log: DBMeta.java,v $
307 // Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
308 // Updated license
309 //
310 // Revision 1.1 2004/05/05 16:39:05 arminw
311 // fix fault
312 // wrong package structure used:
313 // org.apache.ojb.tools.reversdb
314 // org.apache.ojb.tools.reversdb2
315 //
316 // instead of
317 // org.apache.ojb.tools.mapping.reversdb
318 // org.apache.ojb.tools.mapping.reversdb2
319 //
320 // Revision 1.1 2004/05/04 13:45:00 arminw
321 // move reverseDB stuff
322 //
323 // Revision 1.7 2004/04/04 23:53:42 brianm
324 // Fixed initial copyright dates to match cvs repository
325 //
326 // Revision 1.6 2004/03/11 18:16:22 brianm
327 // ASL 2.0
328 //
329 // Revision 1.5 2003/06/21 10:31:45 florianbruckner
330 // implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
331 //
332 // Revision 1.4 2003/01/28 21:42:53 florianbruckner
333 // update XML generation
334 //
335 // Revision 1.3 2003/01/28 19:59:14 florianbruckner
336 // some updates to schema reading to make it a bit more compatible
337 //
338 // Revision 1.2 2002/06/17 19:34:33 jvanzyl
339 // Correcting all the package references.
340 // PR:
341 // Obtained from:
342 // Submitted by:
343 // Reviewed by:
344 //
345 // Revision 1.1.1.1 2002/06/17 18:16:52 jvanzyl
346 // Initial OJB import
347 //
348 // Revision 1.3 2002/05/16 11:47:09 florianbruckner
349 // fix CR/LF issue, change license to ASL
350 //
351 // Revision 1.2 2002/05/16 10:43:59 florianbruckner
352 // use jakarta-regexp instead of gnu-regexp due to the move to jakarta.
353 //
354 // Revision 1.1 2002/04/18 11:44:16 mpoeschl
355 //
356 // move files to new location
357 //
358 // Revision 1.3 2002/04/07 09:05:16 thma
359 // *** empty log message ***
360 //
361 // Revision 1.2 2002/03/11 17:36:05 florianbruckner
362 // fix line break issue for these files that were previously checked in with -kb
363 //
364 // Revision 1.1 2002/03/04 17:19:32 thma
365 // initial checking for Florians Reverse engineering tool
366 //
367 // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
368 // initial import
369 //
370 /***************************** Changelog *****************************/

371
Popular Tags