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: DBCatalog.java,v 1.1.2.1 2005/12/21 22:32:04 tomdz Exp $
025: */
026: public class DBCatalog implements MetadataNodeInterface, TreeNode,
027: org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel {
028: private DBMeta parsedMetaData;
029: private java.sql.DatabaseMetaData dbMeta;
030:
031: private boolean enabled = true;
032:
033: private String strCatalogName;
034: private java.util.TreeMap hmSchemas = new java.util.TreeMap();
035:
036: /** Creates a new instance of DBCatalog */
037: public DBCatalog(java.sql.DatabaseMetaData pdbMeta, DBMeta paMeta,
038: String pstrCatalogName) {
039: this .dbMeta = pdbMeta;
040: this .parsedMetaData = paMeta;
041: this .strCatalogName = pstrCatalogName;
042: }
043:
044: public boolean isEnabled() {
045: return this .enabled;
046: }
047:
048: public void setEnabled(boolean b) {
049: this .enabled = b;
050: }
051:
052: public DBSchema getSchema(String strSchemaName) {
053: return (DBSchema) this .hmSchemas.get(strSchemaName);
054: }
055:
056: public void putSchema(String key, DBSchema schema) {
057: this .hmSchemas.put(key, schema);
058: }
059:
060: public String getCatalogName() {
061: return this .strCatalogName;
062: }
063:
064: public DBMeta getDBMeta() {
065: return parsedMetaData;
066: }
067:
068: public boolean isTreeEnabled() {
069: return getDBMeta().isEnabled() && this .isEnabled();
070: }
071:
072: public void generateReferences() throws java.sql.SQLException {
073: // Set the catalog name of the connection before accessing the metadata object
074: dbMeta.getConnection().setCatalog(this .getCatalogName());
075: Iterator it = this .hmSchemas.values().iterator();
076: while (it.hasNext()) {
077: ((DBSchema) it.next()).generateReferences();
078: }
079: }
080:
081: public void read() throws java.sql.SQLException {
082: // Set the catalog name of the connection before accessing the metadata object
083: java.sql.ResultSet rs = dbMeta.getSchemas();
084: int count = 0;
085: while (rs.next()) {
086: count++;
087: String strSchemaName = rs.getString("TABLE_SCHEM");
088: // Fix for IBM Informix JDBC 2.21JC2; Schema is padded with spaces, needs to be trimmed
089: strSchemaName = strSchemaName.trim();
090: try {
091: if (new org.apache.regexp.RE(this .getDBMeta()
092: .getSchemaPattern()).match(strSchemaName)) {
093: this .hmSchemas.put(strSchemaName, new DBSchema(
094: dbMeta, this , strSchemaName));
095: }
096: } catch (org.apache.regexp.RESyntaxException ex) {
097: // This expception should be reported, but this does not fit currently.
098: ex.printStackTrace();
099: }
100:
101: }
102: // Fix for MySQL: Create an empty schema
103: if (count == 0) {
104: this .hmSchemas.put("", new DBSchema(dbMeta, this , ""));
105: }
106:
107: rs.close();
108: Iterator it = hmSchemas.values().iterator();
109: while (it.hasNext())
110: ((DBSchema) it.next()).read();
111:
112: }
113:
114: public void addTable(String strSchemaName, String strTableName,
115: String strTableType) throws java.sql.SQLException {
116: DBSchema aDBSchema = this .getSchema(strSchemaName);
117: if (aDBSchema == null) {
118: aDBSchema = new DBSchema(dbMeta, this , strSchemaName);
119: this .putSchema(strSchemaName, aDBSchema);
120: aDBSchema.read();
121: }
122: aDBSchema.addTable(strTableName, strTableType);
123: }
124:
125: public void addColumn(String strSchemaName, String strTableName,
126: String strColumnName, int iDataType, String strTypeName,
127: int iColumnSize, int iNullable) {
128: DBSchema aDBSchema = this .getSchema(strSchemaName);
129: if (aDBSchema != null) {
130: aDBSchema.addColumn(strTableName, strColumnName, iDataType,
131: strTypeName, iColumnSize, iNullable);
132: }
133: }
134:
135: public void addPrimaryKeyColumn(String strSchemaName,
136: String strTableName, String strColumnName) {
137: DBSchema aDBSchema = this .getSchema(strSchemaName);
138: if (aDBSchema != null) {
139: aDBSchema.addPrimaryKeyColumn(strTableName, strColumnName);
140: }
141: }
142:
143: public java.util.Enumeration children() {
144: return java.util.Collections.enumeration(this .hmSchemas
145: .values());
146: }
147:
148: public boolean getAllowsChildren() {
149: return true;
150: }
151:
152: public TreeNode getChildAt(int param) {
153: TreeNode tn = (TreeNode) this .hmSchemas.values().toArray()[param];
154: return tn;
155: }
156:
157: public int getChildCount() {
158: return this .hmSchemas.size();
159: }
160:
161: public int getIndex(TreeNode treeNode) {
162: int indexOf = new java.util.Vector(this .hmSchemas.values())
163: .indexOf(treeNode);
164: return indexOf;
165: }
166:
167: public TreeNode getParent() {
168: return this .parsedMetaData;
169: }
170:
171: public boolean isLeaf() {
172: return false;
173: }
174:
175: public String toString() {
176: if (this .strCatalogName == null
177: || this .strCatalogName.trim().length() == 0)
178: return "<empty catalog>";
179: else
180: return this .strCatalogName;
181: }
182:
183: public Class getPropertySheetClass() {
184: return org.apache.ojb.tools.mapping.reversedb.gui.DBCatalogPropertySheet.class;
185: }
186:
187: public String getXML() {
188: java.io.StringWriter swr = new java.io.StringWriter();
189: writeXML(new java.io.PrintWriter(swr));
190: return swr.getBuffer().toString();
191: }
192:
193: public void writeXML(java.io.PrintWriter pw) {
194: Iterator i = this .hmSchemas.values().iterator();
195: while (i.hasNext()) {
196: ((DBSchema) i.next()).writeXML(pw);
197: }
198: }
199:
200: public void generateJava(java.io.File aFile, String strHeader,
201: String strFooter) throws java.io.IOException,
202: java.io.FileNotFoundException {
203: Iterator i = this .hmSchemas.values().iterator();
204: while (i.hasNext())
205: ((DBSchema) i.next()).generateJava(aFile, strHeader,
206: strFooter);
207: }
208:
209: public void setPackage(String packageName) {
210: Iterator i = this .hmSchemas.values().iterator();
211: while (i.hasNext())
212: ((DBSchema) i.next()).setPackage(packageName);
213: }
214:
215: public void disableClassesWithRegex(org.apache.regexp.RE aRegexp) {
216: Iterator it = this .hmSchemas.values().iterator();
217: while (it.hasNext())
218: ((DBSchema) it.next()).disableClassesWithRegex(aRegexp);
219: }
220:
221: }
222:
223: /***************************** Changelog *****************************
224: * // $Log: DBCatalog.java,v $
225: * // Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
226: * // Updated license
227: * //
228: * // Revision 1.1 2004/05/05 16:39:05 arminw
229: * // fix fault
230: * // wrong package structure used:
231: * // org.apache.ojb.tools.reversdb
232: * // org.apache.ojb.tools.reversdb2
233: * //
234: * // instead of
235: * // org.apache.ojb.tools.mapping.reversdb
236: * // org.apache.ojb.tools.mapping.reversdb2
237: * //
238: * // Revision 1.1 2004/05/04 13:45:00 arminw
239: * // move reverseDB stuff
240: * //
241: * // Revision 1.7 2004/04/04 23:53:42 brianm
242: * // Fixed initial copyright dates to match cvs repository
243: * //
244: * // Revision 1.6 2004/03/11 18:16:22 brianm
245: * // ASL 2.0
246: * //
247: * // Revision 1.5 2003/06/21 10:23:25 florianbruckner
248: * // implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
249: * //
250: * // Revision 1.4 2003/01/28 21:42:53 florianbruckner
251: * // update XML generation
252: * //
253: * // Revision 1.3 2003/01/28 19:59:14 florianbruckner
254: * // some updates to schema reading to make it a bit more compatible
255: * //
256: * // Revision 1.2 2002/06/17 19:34:33 jvanzyl
257: * // Correcting all the package references.
258: * // PR:
259: * // Obtained from:
260: * // Submitted by:
261: * // Reviewed by:
262: * //
263: * // Revision 1.1.1.1 2002/06/17 18:16:51 jvanzyl
264: * // Initial OJB import
265: * //
266: * // Revision 1.3 2002/05/16 11:47:09 florianbruckner
267: * // fix CR/LF issue, change license to ASL
268: * //
269: * // Revision 1.2 2002/05/16 10:43:59 florianbruckner
270: * // use jakarta-regexp instead of gnu-regexp due to the move to jakarta.
271: * //
272: * // Revision 1.1 2002/04/18 11:44:16 mpoeschl
273: * //
274: * // move files to new location
275: * //
276: * // Revision 1.3 2002/04/07 09:05:16 thma
277: * // *** empty log message ***
278: * //
279: * // Revision 1.2 2002/03/11 17:36:04 florianbruckner
280: * // fix line break issue for these files that were previously checked in with -kb
281: * //
282: * // Revision 1.1 2002/03/04 17:19:32 thma
283: * // initial checking for Florians Reverse engineering tool
284: * //
285: * // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
286: * // initial import
287: * //
288: * /***************************** Changelog *****************************/
|