KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > editor > completion > db > Schema


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.editor.completion.db;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeSet JavaDoc;
27 import org.netbeans.modules.dbschema.DBException;
28 import org.netbeans.modules.dbschema.DBIdentifier;
29 import org.netbeans.modules.dbschema.SchemaElement;
30 import org.netbeans.modules.dbschema.TableElement;
31 import org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider;
32 import org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl;
33 import org.openide.ErrorManager;
34
35 /**
36  *
37  * @author Andrei Badea
38  */

39 public class Schema {
40     
41     private DBMetaDataProvider provider;
42     private Catalog catalog;
43     private String JavaDoc name;
44     private Set JavaDoc/*<String>*/ tableNames;
45     
46     private ConnectionProvider cp;
47     private SchemaElementImpl schemaElementImpl;
48     private SchemaElement schemaElement;
49     
50     // XXX views
51

52     Schema(DBMetaDataProvider provider, Catalog catalog, String JavaDoc name) {
53         this.provider = provider;
54         this.catalog = catalog;
55         this.name = name;
56     }
57     
58     public String JavaDoc getName() {
59         return name;
60     }
61     
62     public synchronized String JavaDoc[] getTableNames() throws SQLException JavaDoc {
63         if (tableNames == null) {
64             tableNames = getTableNamesByType("TABLE"); // NOI18N
65
}
66         
67         return (String JavaDoc[])tableNames.toArray(new String JavaDoc[tableNames.size()]);
68     }
69     
70     public TableElement getTable(String JavaDoc tableName) throws SQLException JavaDoc {
71         SchemaElement schemaElement;
72         synchronized (this) {
73             schemaElement = this.schemaElement;
74         }
75         if (schemaElement == null) {
76             cp = new ConnectionProvider(provider.getConnection(), provider.getDriverClass());
77             cp.setSchema(name);
78             schemaElementImpl = new SchemaElementImpl(cp);
79             try {
80                 schemaElementImpl.setName(DBIdentifier.create("foo")); // XXX set a proper name
81
} catch (DBException e) {
82                 ErrorManager.getDefault().notify(e);
83             }
84             schemaElement = new SchemaElement(schemaElementImpl);
85             synchronized (this) {
86                 this.schemaElement = schemaElement;
87             }
88         }
89         
90         DBIdentifier tableId = DBIdentifier.create(tableName);
91         TableElement tableElement = schemaElement.getTable(tableId);
92         if (tableElement == null) {
93             LinkedList JavaDoc tableList = new LinkedList JavaDoc();
94             tableList.add(tableName);
95             LinkedList JavaDoc viewList = new LinkedList JavaDoc();
96             schemaElementImpl.initTables(cp, tableList, viewList, false);
97             
98             tableElement = schemaElement.getTable(tableId);
99         }
100         
101         return tableElement;
102     }
103     
104     public synchronized void refresh() {
105         schemaElement = null;
106         tableNames = null;
107     }
108     
109     private Set JavaDoc/*<String>*/ getTableNamesByType(String JavaDoc type) throws SQLException JavaDoc {
110         Set JavaDoc/*<String>*/ result = new TreeSet JavaDoc();
111
112         ResultSet JavaDoc rs = provider.getMetaData().getTables(catalog.getName(), name, "%", new String JavaDoc[] { type }); // NOI18N
113
try {
114             while (rs.next()) {
115                 String JavaDoc tableName = rs.getString("TABLE_NAME"); // NOI18N
116
result.add(tableName);
117             }
118         } finally {
119             rs.close();
120         }
121
122         return result;
123     }
124     
125     public String JavaDoc toString() {
126         return "Schema[catalog=" + catalog + ",name='" + name + "']"; // NOI18N
127
}
128 }
129
Popular Tags