001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: If you wish your version of this file to be governed by only the CDDL
013: or only the GPL Version 2, indicate your decision by adding
014: "[Contributor] elects to include this software in this distribution
015: under the [CDDL or GPL Version 2] license." If you do not indicate a
016: single choice of license, a recipient has the option to distribute
017: your version of this file under either the CDDL, the GPL Version 2 or
018: to extend the choice of license to its licensees as provided above.
019: However, if you add GPL Version 2 code and therefore, elected the GPL
020: Version 2 license, then the option applies only if the new code is
021: made subject to such option by the copyright holder.
022: If you wish your version of this file to be governed by only the CDDL
023: or only the GPL Version 2, indicate your decision by adding
024: "[Contributor] elects to include this software in this distribution
025: under the [CDDL or GPL Version 2] license." If you do not indicate a
026: single choice of license, a recipient has the option to distribute
027: your version of this file under either the CDDL, the GPL Version 2 or
028: to extend the choice of license to its licensees as provided above.
029: However, if you add GPL Version 2 code and therefore, elected the GPL
030: Version 2 license, then the option applies only if the new code is
031: made subject to such option by the copyright holder.
032: */
033: package org.netbeans.modules.mashup.db.wizard;
034:
035: import java.awt.Component;
036: import java.sql.Connection;
037: import java.sql.DriverManager;
038: import java.sql.ResultSet;
039: import java.sql.Statement;
040: import java.util.HashMap;
041: import java.util.HashSet;
042: import java.util.Iterator;
043: import java.util.Set;
044: import javax.swing.event.ChangeEvent;
045: import javax.swing.event.ChangeListener;
046:
047: import org.netbeans.modules.sql.framework.model.DBMetaDataFactory;
048: import org.netbeans.modules.mashup.db.common.Property;
049: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
050: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
051: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBTableImpl;
052: import org.netbeans.modules.mashup.db.ui.wizard.PreviewDatabaseVisualPanel;
053: import org.netbeans.modules.mashup.tables.wizard.MashupTableWizardIterator;
054: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
055: import org.openide.WizardDescriptor;
056: import org.openide.util.HelpCtx;
057:
058: /**
059: *
060: * @author ks161616
061: */
062: public class FlatfileViewerTreePanel implements WizardDescriptor.Panel {
063:
064: /**
065: * The visual component that displays this panel. If you need to access the
066: * component from this class, just use getComponent().
067: */
068: private PreviewDatabaseVisualPanel component;
069:
070: public Component getComponent() {
071: if (component == null) {
072: component = new PreviewDatabaseVisualPanel();
073: }
074: return component;
075: }
076:
077: public HelpCtx getHelp() {
078: return HelpCtx.DEFAULT_HELP;
079: }
080:
081: public boolean isValid() {
082: return true;
083: }
084:
085: private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(
086: 1);
087:
088: public final void addChangeListener(ChangeListener l) {
089: synchronized (listeners) {
090: listeners.add(l);
091: }
092: }
093:
094: public final void removeChangeListener(ChangeListener l) {
095: synchronized (listeners) {
096: listeners.remove(l);
097: }
098: }
099:
100: public final void fireChangeEvent() {
101: Iterator<ChangeListener> it;
102: synchronized (listeners) {
103: it = new HashSet<ChangeListener>(listeners).iterator();
104: }
105: ChangeEvent ev = new ChangeEvent(this );
106: while (it.hasNext()) {
107: it.next().stateChanged(ev);
108: }
109: }
110:
111: public void readSettings(Object settings) {
112: if (settings instanceof WizardDescriptor) {
113: WizardDescriptor wd = (WizardDescriptor) settings;
114:
115: FlatfileDatabaseModel folder = (FlatfileDatabaseModel) wd
116: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
117: folder = addDBTables(folder);
118: if (folder == null || folder.getTables().size() == 0) {
119: throw new IllegalStateException(
120: "Context must contain a populated FlatfileDatabaseModel.");
121: }
122:
123: component.setModel(folder);
124: fireChangeEvent();
125: }
126: }
127:
128: public void storeSettings(Object settings) {
129: if (settings instanceof WizardDescriptor) {
130: WizardDescriptor wd = (WizardDescriptor) settings;
131: wd.putProperty(
132: MashupTableWizardIterator.PROP_FLATFILEDBMODEL,
133: null);
134: fireChangeEvent();
135: }
136: }
137:
138: private FlatfileDatabaseModel addDBTables(
139: FlatfileDatabaseModel model) {
140: DBMetaDataFactory dbMeta = new DBMetaDataFactory();
141: Connection conn = null;
142: String catalog = null;
143: String[][] tableList = null;
144: String[] types = { "TABLE" };
145: try {
146: conn = model.getJDBCConnection();
147: catalog = (conn.getCatalog() == null) ? "" : conn
148: .getCatalog();
149: dbMeta.connectDB(conn);
150: tableList = dbMeta.getTables(catalog, "", "", types);
151: model = createTable(tableList, model, dbMeta);
152: conn.createStatement().execute("shutdown");
153: } catch (Exception ex) {
154: //ignore
155: } finally {
156: dbMeta.disconnectDB();
157: }
158: return model;
159: }
160:
161: private FlatfileDatabaseModel createTable(String[][] tableList,
162: FlatfileDatabaseModel model, DBMetaDataFactory meta)
163: throws Exception {
164: String[] currTable = null;
165: FlatfileDBTable dbTable = null;
166: DBConnectionDefinition def = model
167: .getFlatfileDBConnectionDefinition(true);
168: if (tableList != null) {
169: for (int i = 0; i < tableList.length; i++) {
170: currTable = tableList[i];
171: dbTable = new FlatfileDBTableImpl(
172: currTable[DBMetaDataFactory.NAME],
173: currTable[DBMetaDataFactory.SCHEMA],
174: currTable[DBMetaDataFactory.CATALOG]);
175: meta.populateColumns(dbTable);
176: HashMap map = getTableMetaData(def, dbTable);
177: dbTable.setProperties(map);
178: model.addTable(dbTable);
179: }
180: }
181: return model;
182: }
183:
184: private HashMap getTableMetaData(DBConnectionDefinition condef,
185: FlatfileDBTable element) {
186: HashMap<String, Property> map = new HashMap<String, Property>();
187: Connection conn = null;
188: Statement stmt = null;
189: ResultSet rs = null;
190: try {
191: conn = DriverManager.getConnection(condef
192: .getConnectionURL());
193: stmt = conn.createStatement();
194: String query = "select PROPERTY_NAME, PROPERTY_VALUE from AXION_TABLE_PROPERTIES "
195: + "where TABLE_NAME = '"
196: + element.getName()
197: + "' ORDER BY PROPERTY_NAME";
198: stmt.execute(query);
199: rs = stmt.getResultSet();
200: while (rs.next()) {
201: rs.getMetaData().getColumnCount();
202: String value1 = rs.getString(1);
203: String value2 = rs.getString(2);
204:
205: Property prop = new Property();
206: if (value2.equals("true") || value2.equals("false")) {
207: prop = new Property(value1, Boolean.class, true);
208: prop.setValue(Boolean.valueOf(value2));
209: map.put(value1, prop);
210: } else {
211: try {
212: Integer.parseInt(value2);
213: prop = new Property(value1, Integer.class, true);
214: prop.setValue(Integer.valueOf(value2));
215: map.put(value1, prop);
216:
217: } catch (NumberFormatException e) {
218: prop = new Property(value1, String.class, true);
219: prop.setValue(value2);
220: map.put(value1, prop);
221: }
222: }
223: }
224: } catch (Exception e) {
225: e.printStackTrace();
226: } finally {
227: try {
228: rs.close();
229: } catch (Exception ex) {
230: //ignore
231: }
232:
233: try {
234: stmt.close();
235: } catch (Exception ex) {
236: //ignore
237: }
238:
239: try {
240: conn.close();
241: } catch (Exception ex) {
242: //ignore
243: }
244: }
245: return map;
246: }
247: }
|