001: package org.apache.ojb.tools.mapping.reversedb;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Iterator;
019: import javax.swing.tree.TreeNode;
020:
021: /**
022: *
023: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
024: * @version $Id: DBMeta.java,v 1.1.2.1 2005/12/21 22:32:04 tomdz Exp $
025: */
026: public class DBMeta implements MetadataNodeInterface, TreeNode,
027: org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel {
028: private boolean enabled = true;
029:
030: private java.sql.DatabaseMetaData dbMeta;
031:
032: // private java.util.TreeMap hmCatalogs = new java.util.TreeMap();
033: private java.util.HashMap hmCatalogs = new java.util.HashMap();
034: private String catalogTerm;
035: private String catalogSeparator;
036: private boolean isCatalogAtStart;
037: private boolean supportsCatalogsInDataManipulation;
038: private boolean supportsCatalogsInIndexDefinitions;
039: private boolean supportsCatalogsInPrivilegeDefinitions;
040: private boolean supportsCatalogsInProcedureCalls;
041: private boolean supportsCatalogsInTableDefinitions;
042: private String schemaTerm;
043:
044: private String catalogPattern;
045: private String schemaPattern;
046:
047: private String databaseProductName = null;
048: private String databaseProductVersion = null;
049:
050: /** Creates a new instance of DBSchema */
051: public DBMeta(String pCatalogPattern, String pSchemaPattern,
052: java.sql.DatabaseMetaData pDbMeta)
053: throws java.sql.SQLException {
054: super ();
055: this .dbMeta = pDbMeta;
056: this .catalogPattern = pCatalogPattern;
057: this .schemaPattern = pSchemaPattern;
058: System.err.println("Using " + dbMeta.getDriverName() + " "
059: + dbMeta.getDriverVersion());
060: }
061:
062: public DBMeta(java.sql.DatabaseMetaData pDbMeta)
063: throws java.sql.SQLException {
064: this (null, null, pDbMeta);
065: }
066:
067: public String getSchemaPattern() {
068: return this .schemaPattern;
069: }
070:
071: public boolean isEnabled() {
072: return this .enabled;
073: }
074:
075: public void setEnabled(boolean b) {
076: this .enabled = b;
077: }
078:
079: public String getDatabaseProductVersion() {
080: return this .databaseProductVersion;
081: }
082:
083: public String getDatabaseProductName() {
084: return this .databaseProductName;
085: }
086:
087: public boolean getSupportsCatalogsInIndexDefinitions() {
088: return this .supportsCatalogsInIndexDefinitions;
089: }
090:
091: public boolean getSupportsCatalogsInDataManipulation() {
092: return this .supportsCatalogsInDataManipulation;
093: }
094:
095: public boolean getSupportsCatalogsInPrivilegeDefinitions() {
096: return this .supportsCatalogsInPrivilegeDefinitions;
097: }
098:
099: public boolean getSupportsCatalogsInProcedureCalls() {
100: return this .supportsCatalogsInProcedureCalls;
101: }
102:
103: public boolean getSupportsCatalogsInTableDefinitions() {
104: return this .supportsCatalogsInTableDefinitions;
105: }
106:
107: public String getCatalogTerm() {
108: return this .catalogTerm;
109: }
110:
111: public String getSchemaTerm() {
112: return this .schemaTerm;
113: }
114:
115: public String getCatalogSeparator() {
116: return this .catalogSeparator;
117: }
118:
119: public boolean getIsCatalogAtStart() {
120: return this .isCatalogAtStart;
121: }
122:
123: public DBCatalog getCatalog(String catalogName) {
124: return (DBCatalog) this .hmCatalogs.get(catalogName);
125: }
126:
127: public void read() throws java.sql.SQLException {
128: this .databaseProductName = dbMeta.getDatabaseProductName();
129: this .databaseProductVersion = dbMeta
130: .getDatabaseProductVersion();
131: catalogTerm = dbMeta.getCatalogTerm();
132: catalogSeparator = dbMeta.getCatalogSeparator();
133: isCatalogAtStart = dbMeta.isCatalogAtStart();
134: supportsCatalogsInDataManipulation = dbMeta
135: .supportsCatalogsInDataManipulation();
136: supportsCatalogsInIndexDefinitions = dbMeta
137: .supportsCatalogsInIndexDefinitions();
138: supportsCatalogsInPrivilegeDefinitions = dbMeta
139: .supportsCatalogsInPrivilegeDefinitions();
140: supportsCatalogsInProcedureCalls = dbMeta
141: .supportsCatalogsInProcedureCalls();
142: supportsCatalogsInTableDefinitions = dbMeta
143: .supportsCatalogsInTableDefinitions();
144: schemaTerm = dbMeta.getSchemaTerm();
145:
146: java.sql.ResultSet rs = dbMeta.getCatalogs();
147: int count = 0;
148: while (rs.next()) {
149: count++;
150: String strCatalogName = rs.getString("TABLE_CAT");
151: try {
152: if (new org.apache.regexp.RE(this .catalogPattern)
153: .match(strCatalogName)) {
154: DBCatalog aDBCatalog = new DBCatalog(dbMeta, this ,
155: strCatalogName);
156: this .hmCatalogs.put(strCatalogName, aDBCatalog);
157: }
158: } catch (org.apache.regexp.RESyntaxException ex) {
159: // This expception should be reported, but this does not fit currently.
160: ex.printStackTrace();
161: }
162: }
163: rs.close();
164: if (count == 0) {
165: DBCatalog aDBCatalog = new DBCatalog(dbMeta, this , null);
166: this .hmCatalogs.put(null, aDBCatalog);
167: }
168:
169: // Read after closing recordset.
170: Iterator it = hmCatalogs.values().iterator();
171: while (it.hasNext())
172: ((DBCatalog) it.next()).read();
173: }
174:
175: public void generateReferences() throws java.sql.SQLException {
176: Iterator it = this .hmCatalogs.values().iterator();
177: while (it.hasNext()) {
178: ((DBCatalog) it.next()).generateReferences();
179: }
180: }
181:
182: public void debug() {
183:
184: }
185:
186: public java.util.Enumeration children() {
187: return java.util.Collections.enumeration(this .hmCatalogs
188: .values());
189: }
190:
191: public boolean getAllowsChildren() {
192: return true;
193: }
194:
195: public TreeNode getChildAt(int param) {
196: return (TreeNode) this .hmCatalogs.values().toArray()[param];
197: }
198:
199: public int getChildCount() {
200: return this .hmCatalogs.size();
201: }
202:
203: public int getIndex(TreeNode treeNode) {
204: return new java.util.Vector(hmCatalogs.values())
205: .indexOf(treeNode);
206: }
207:
208: public TreeNode getParent() {
209: return null;
210: }
211:
212: public boolean isLeaf() {
213: return false;
214: }
215:
216: public String toString() {
217: return "DBMeta";
218: }
219:
220: public Class getPropertySheetClass() {
221: return org.apache.ojb.tools.mapping.reversedb.gui.DBMetaPropertySheet.class;
222: }
223:
224: public String getXML() {
225: java.io.StringWriter sw = new java.io.StringWriter();
226: writeXML(new java.io.PrintWriter(sw));
227: return sw.getBuffer().toString();
228: }
229:
230: public void writeXML(java.io.PrintWriter pw) {
231:
232: pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
233: pw
234: .println("<!DOCTYPE descriptor-repository SYSTEM \"repository.dtd\">");
235: pw.println("<descriptor-repository version=\"0.9.9\">");
236: pw.println(" <jdbc-connection-descriptor");
237: pw.println(" jcd-alias=\"default\"");
238: pw.println(" default-connection=\"true\"");
239: pw.println(" platform=\"XXXX\"");
240: pw.println(" jdbc-level=\"1.0\"");
241: pw.println(" driver=\"XXX\"");
242: pw.println(" protocol=\"XXX\"");
243: pw.println(" subprotocol=\"XXX\"");
244: pw.println(" dbalias=\"XXX\"");
245: pw.println(" username=\"XXX\"");
246: pw.println(" password=\"XXX\">");
247: pw.println(" </jdbc-connection-descriptor>");
248:
249: Iterator i = this .hmCatalogs.values().iterator();
250: while (i.hasNext()) {
251: ((DBCatalog) i.next()).writeXML(pw);
252: }
253: pw.println("</descriptor-repository>");
254: }
255:
256: public void generateJava(java.io.File aFile, String strHeader,
257: String strFooter) throws java.io.IOException,
258: java.io.FileNotFoundException {
259: Iterator it = this .hmCatalogs.values().iterator();
260: while (it.hasNext())
261: ((DBCatalog) it.next()).generateJava(aFile, strHeader,
262: strFooter);
263: }
264:
265: public void setPackage(String packageName) {
266: Iterator it = this .hmCatalogs.values().iterator();
267: while (it.hasNext())
268: ((DBCatalog) it.next()).setPackage(packageName);
269: }
270:
271: public void disableClassesWithRegex(org.apache.regexp.RE aRegexp) {
272: Iterator it = this .hmCatalogs.values().iterator();
273: while (it.hasNext())
274: ((DBCatalog) it.next()).disableClassesWithRegex(aRegexp);
275: }
276:
277: }
278:
279: /***************************** Changelog *****************************
280: // $Log: DBMeta.java,v $
281: // Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
282: // Updated license
283: //
284: // Revision 1.1 2004/05/05 16:39:05 arminw
285: // fix fault
286: // wrong package structure used:
287: // org.apache.ojb.tools.reversdb
288: // org.apache.ojb.tools.reversdb2
289: //
290: // instead of
291: // org.apache.ojb.tools.mapping.reversdb
292: // org.apache.ojb.tools.mapping.reversdb2
293: //
294: // Revision 1.1 2004/05/04 13:45:00 arminw
295: // move reverseDB stuff
296: //
297: // Revision 1.7 2004/04/04 23:53:42 brianm
298: // Fixed initial copyright dates to match cvs repository
299: //
300: // Revision 1.6 2004/03/11 18:16:22 brianm
301: // ASL 2.0
302: //
303: // Revision 1.5 2003/06/21 10:31:45 florianbruckner
304: // implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
305: //
306: // Revision 1.4 2003/01/28 21:42:53 florianbruckner
307: // update XML generation
308: //
309: // Revision 1.3 2003/01/28 19:59:14 florianbruckner
310: // some updates to schema reading to make it a bit more compatible
311: //
312: // Revision 1.2 2002/06/17 19:34:33 jvanzyl
313: // Correcting all the package references.
314: // PR:
315: // Obtained from:
316: // Submitted by:
317: // Reviewed by:
318: //
319: // Revision 1.1.1.1 2002/06/17 18:16:52 jvanzyl
320: // Initial OJB import
321: //
322: // Revision 1.3 2002/05/16 11:47:09 florianbruckner
323: // fix CR/LF issue, change license to ASL
324: //
325: // Revision 1.2 2002/05/16 10:43:59 florianbruckner
326: // use jakarta-regexp instead of gnu-regexp due to the move to jakarta.
327: //
328: // Revision 1.1 2002/04/18 11:44:16 mpoeschl
329: //
330: // move files to new location
331: //
332: // Revision 1.3 2002/04/07 09:05:16 thma
333: // *** empty log message ***
334: //
335: // Revision 1.2 2002/03/11 17:36:05 florianbruckner
336: // fix line break issue for these files that were previously checked in with -kb
337: //
338: // Revision 1.1 2002/03/04 17:19:32 thma
339: // initial checking for Florians Reverse engineering tool
340: //
341: // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
342: // initial import
343: //
344: /***************************** Changelog *****************************/
|