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