001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.awt.Component;
004: import java.sql.Connection;
005: import java.util.ArrayList;
006: import java.util.HashSet;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011: import javax.swing.event.ChangeEvent;
012: import javax.swing.event.ChangeListener;
013: import javax.swing.table.DefaultTableModel;
014:
015: import org.netbeans.modules.sql.framework.model.DBMetaDataFactory;
016: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
017: import org.netbeans.modules.sql.framework.codegen.DB;
018: import org.netbeans.modules.sql.framework.codegen.DBFactory;
019: import org.netbeans.modules.sql.framework.codegen.StatementContext;
020: import org.netbeans.modules.sql.framework.codegen.axion.AxionDB;
021: import org.netbeans.modules.sql.framework.codegen.axion.AxionStatements;
022: import org.netbeans.modules.sql.framework.model.SQLConstants;
023: import org.netbeans.modules.sql.framework.model.SQLDBModel;
024: import org.netbeans.modules.sql.framework.model.SQLDBTable;
025: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
026: import org.netbeans.modules.sql.framework.model.impl.SourceTableImpl;
027: import org.openide.WizardDescriptor;
028: import org.openide.util.HelpCtx;
029:
030: import com.sun.sql.framework.exception.BaseException;
031: import com.sun.sql.framework.utils.StringUtil;
032: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
033:
034: public class JDBCTablePanel implements WizardDescriptor.FinishablePanel {
035:
036: /**
037: * The visual component that displays this panel. If you need to access the
038: * component from this class, just use getComponent().
039: */
040: private Component component;
041: private List<String> dblinks = new ArrayList<String>();
042: private List<String> statements = new ArrayList<String>();
043: private String linkname;
044:
045: public Component getComponent() {
046: if (component == null) {
047: component = new JDBCTableVisualPanel(this );
048: }
049: return component;
050: }
051:
052: public HelpCtx getHelp() {
053: return HelpCtx.DEFAULT_HELP;
054: }
055:
056: public boolean isValid() {
057: return canAdvance();
058: }
059:
060: private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(
061: 1);
062:
063: public final void addChangeListener(ChangeListener l) {
064: synchronized (listeners) {
065: listeners.add(l);
066: }
067: }
068:
069: public final void removeChangeListener(ChangeListener l) {
070: synchronized (listeners) {
071: listeners.remove(l);
072: }
073: }
074:
075: protected final void fireChangeEvent() {
076: Iterator<ChangeListener> it;
077: synchronized (listeners) {
078: it = new HashSet<ChangeListener>(listeners).iterator();
079: }
080: ChangeEvent ev = new ChangeEvent(this );
081: while (it.hasNext()) {
082: it.next().stateChanged(ev);
083: }
084: }
085:
086: public void readSettings(Object settings) {
087: if (settings instanceof WizardDescriptor) {
088: WizardDescriptor wd = (WizardDescriptor) settings;
089: }
090: }
091:
092: public void storeSettings(Object settings) {
093: if (settings instanceof WizardDescriptor) {
094: WizardDescriptor wd = (WizardDescriptor) settings;
095: Map<String, String> userMap = ((JDBCTableVisualPanel) getComponent())
096: .getUserMap();
097: Map<String, String> passwdMap = ((JDBCTableVisualPanel) getComponent())
098: .getPasswordMap();
099: Map<String, String> driverMap = ((JDBCTableVisualPanel) getComponent())
100: .getDriverMap();
101: populateStatements(((JDBCTableVisualPanel) getComponent())
102: .getTables(), userMap, passwdMap, driverMap);
103: wd.putProperty("dblinks", dblinks);
104: wd.putProperty("statements", statements);
105: }
106: }
107:
108: private SQLDBModel getModel(String jdbcUrl, String user,
109: String pass, String driver, String schema, String catalog,
110: String table) throws Exception {
111: DBMetaDataFactory meta = new DBMetaDataFactory();
112: Connection conn = DBExplorerUtil.createConnection(driver,
113: jdbcUrl, user, pass);
114: meta.connectDB(conn);
115: DBConnectionDefinition def = null;
116: SQLDBModel model = SQLModelObjectFactory.getInstance()
117: .createDBModel(SQLConstants.SOURCE_DBMODEL);
118: try {
119: def = SQLModelObjectFactory.getInstance()
120: .createDBConnectionDefinition(jdbcUrl,
121: meta.getDBType(), driver, jdbcUrl, user,
122: pass, "Descriptive info here");
123: } catch (Exception ex) {
124: // ignore
125: }
126: model.setModelName(jdbcUrl);
127: model.setConnectionDefinition(def);
128: SourceTableImpl newTable = getTable(meta, schema, catalog,
129: table);
130: meta.populateColumns(newTable);
131:
132: newTable.setEditable(true);
133: newTable.setSelected(true);
134: newTable.setAliasName("MASHUPDB");
135:
136: model.addTable(newTable);
137: meta.disconnectDB();
138: return model;
139: }
140:
141: private SourceTableImpl getTable(DBMetaDataFactory dbMeta,
142: String schemaName, String catalogName, String tableName)
143: throws Exception {
144: String[][] tableList = dbMeta.getTablesAndViews(catalogName,
145: schemaName, "", false);
146: SourceTableImpl aTable = null;
147: String[] currTable = null;
148: if (tableList != null) {
149: for (int i = 0; i < tableList.length; i++) {
150: currTable = tableList[i];
151: if (currTable[DBMetaDataFactory.NAME].equals(tableName)) {
152: aTable = new SourceTableImpl(
153: currTable[DBMetaDataFactory.NAME].trim(),
154: currTable[DBMetaDataFactory.SCHEMA],
155: currTable[DBMetaDataFactory.CATALOG]);
156: break;
157: }
158: }
159: }
160: return aTable;
161: }
162:
163: private void getCreateStatement(String schema, String table,
164: String jdbcUrl, String user, String pass, String driver) {
165: StringBuilder createStatement = new StringBuilder();
166: String catalog = "";
167: try {
168: SQLDBModel model = getModel(jdbcUrl, user, pass, driver,
169: schema, catalog, table);
170: SQLDBTable tbl = (SQLDBTable) model.getTables().get(0);
171: AxionDB db = (AxionDB) DBFactory.getInstance().getDatabase(
172: DB.AXIONDB);
173: AxionStatements stmts = (AxionStatements) db
174: .getStatements();
175: linkname = StringUtil.createSQLIdentifier(model
176: .getConnectionDefinition().getName());
177: StatementContext context = new StatementContext();
178: context.setUsingUniqueTableName(tbl, true);
179: context.setUsingFullyQualifiedTablePrefix(false);
180: String localName = db.getUnescapedName(db
181: .getGeneratorFactory().generate(tbl, context));
182: createStatement.append(getCreateRemoteTableSQL(stmts, tbl,
183: localName, linkname));
184: dblinks.add(getCreateDBLinkSQL(stmts, model
185: .getConnectionDefinition(), linkname, pass));
186: statements.add(createStatement.toString());
187: } catch (Exception ex) {
188: ex.printStackTrace();
189: }
190: }
191:
192: private String getCreateDBLinkSQL(AxionStatements stmts,
193: DBConnectionDefinition connDef, String linkName, String pass)
194: throws BaseException {
195: StringBuilder stmtBuf = new StringBuilder(50);
196:
197: stmtBuf.append(stmts
198: .getCreateDBLinkStatement(connDef, linkName).getSQL());
199: int start = stmtBuf.indexOf("PASSWORD='")
200: + "PASSWORD='".length();
201: int end = stmtBuf.indexOf("'", start);
202: stmtBuf.replace(start, end, pass);
203: return stmtBuf.toString();
204: }
205:
206: private String getCreateRemoteTableSQL(AxionStatements stmts,
207: SQLDBTable table, String localName, String linkName)
208: throws BaseException {
209: StringBuilder stmtBuf = new StringBuilder(50);
210: if (StringUtil.isNullString(localName)) {
211: localName = table.getName();
212: }
213:
214: // Generate a "create external table" statement that references its DB link
215: stmtBuf.append(stmts.getCreateRemoteTableStatement(table,
216: localName, linkName).getSQL());
217: return stmtBuf.toString();
218: }
219:
220: public boolean isFinishPanel() {
221: return canAdvance();
222: }
223:
224: private void populateStatements(DefaultTableModel model,
225: Map<String, String> userMap, Map<String, String> passwdMap,
226: Map<String, String> driverMap) {
227: dblinks.clear();
228: statements.clear();
229: for (int i = 0; i < model.getRowCount(); i++) {
230: String table = (String) model.getValueAt(i, 0);
231: String schema = (String) model.getValueAt(i, 1);
232: String jdbcUrl = (String) model.getValueAt(i, 2);
233: getCreateStatement(schema, table, jdbcUrl, userMap
234: .get(jdbcUrl), passwdMap.get(jdbcUrl), driverMap
235: .get(jdbcUrl));
236: }
237: }
238:
239: private boolean canAdvance() {
240: return ((JDBCTableVisualPanel) getComponent()).canAdvance();
241: }
242: }
|