001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.output.dataview;
042:
043: import com.sun.sql.framework.jdbc.DBConstants;
044: import com.sun.sql.framework.jdbc.SQLPart;
045: import com.sun.sql.framework.utils.StringUtil;
046: import java.sql.Connection;
047: import java.sql.PreparedStatement;
048: import java.sql.ResultSet;
049: import java.sql.SQLException;
050: import java.sql.Statement;
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.Iterator;
054: import java.util.List;
055: import java.util.Map;
056: import net.java.hulp.i18n.Logger;
057: import org.axiondb.ExternalConnectionProvider;
058: import org.netbeans.modules.etl.logger.Localizer;
059: import org.netbeans.modules.etl.logger.LogUtil;
060: import org.netbeans.modules.sql.framework.codegen.DB;
061: import org.netbeans.modules.sql.framework.codegen.DBFactory;
062: import org.netbeans.modules.sql.framework.codegen.StatementContext;
063: import org.netbeans.modules.sql.framework.codegen.Statements;
064: import org.netbeans.modules.sql.framework.codegen.axion.AxionDB;
065: import org.netbeans.modules.sql.framework.codegen.axion.AxionPipelineStatements;
066: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
067: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
068: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
069: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
070: import org.netbeans.modules.sql.framework.model.RuntimeInput;
071: import org.netbeans.modules.sql.framework.model.SQLConstants;
072: import org.netbeans.modules.sql.framework.model.SQLDBTable;
073: import org.netbeans.modules.sql.framework.model.SQLJoinOperator;
074: import org.netbeans.modules.sql.framework.model.SQLJoinView;
075: import org.netbeans.modules.sql.framework.model.SQLObject;
076: import org.netbeans.modules.sql.framework.model.SourceTable;
077: import org.netbeans.modules.sql.framework.model.TargetTable;
078: import org.netbeans.modules.sql.framework.ui.SwingWorker;
079: import org.netbeans.modules.sql.framework.ui.utils.AxionExternalConnectionProvider;
080: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
081: import org.openide.DialogDisplayer;
082: import org.openide.NotifyDescriptor;
083: import org.openide.NotifyDescriptor.Message;
084:
085: /**
086: * @author Ahimanikya Satapathy
087: */
088: class DataViewWorkerThread extends SwingWorker {
089:
090: private SQLObject dbTable;
091: private String errMsg;
092: DataOutputPanel dataOutputPanel;
093: private static transient final Logger mLogger = LogUtil
094: .getLogger(DataViewWorkerThread.class.getName());
095: private static transient final Localizer mLoc = Localizer.get();
096:
097: public DataViewWorkerThread(SQLObject table,
098: DataOutputPanel dataOutputPanel) {
099: super ();
100: this .dataOutputPanel = dataOutputPanel;
101: this .dbTable = table; // May be a SQLDBTable or a JoinView
102: }
103:
104: public Object construct() {
105: if (dbTable instanceof SQLDBTable) {
106: showDataForDBTable();
107: } else if (dbTable instanceof SQLJoinView) {
108: showDataForJoinView();
109: } else if (dbTable instanceof SQLJoinOperator) {
110: showDataForJoinOperator();
111: }
112: return "";
113: }
114:
115: @Override
116: public void finished() {
117:
118: if (this .errMsg != null) {
119: String nbBundle = mLoc
120: .t(
121: "PRSR001: Error fetching data for table {0}.Cause:{1}",
122: dbTable.getDisplayName(), this .errMsg);
123: String errorMsg = Localizer.parse(nbBundle);
124: DialogDisplayer.getDefault().notify(
125: new Message(errorMsg,
126: NotifyDescriptor.ERROR_MESSAGE));
127: }
128: UIUtil.stopProgressDialog();
129: if (dataOutputPanel.truncateButton != null) {
130: dataOutputPanel.truncateButton.setEnabled(true);
131: }
132: dataOutputPanel.refreshButton.setEnabled(true);
133: dataOutputPanel.refreshField.setEnabled(true);
134: if (this .dbTable instanceof SQLDBTable) {
135: if (((dataOutputPanel.nowCount - dataOutputPanel.maxRows) > 0)
136: && (dataOutputPanel.totalCount != 0)) {
137: dataOutputPanel.first.setEnabled(true);
138: dataOutputPanel.previous.setEnabled(true);
139: }
140: if (((dataOutputPanel.nowCount + dataOutputPanel.maxRows) <= dataOutputPanel.totalCount)
141: && (dataOutputPanel.totalCount != 0)) {
142: dataOutputPanel.next.setEnabled(true);
143: dataOutputPanel.last.setEnabled(true);
144: }
145: if ((dataOutputPanel.nowCount - dataOutputPanel.maxRows) <= 0) {
146: dataOutputPanel.first.setEnabled(false);
147: dataOutputPanel.previous.setEnabled(false);
148: }
149: if ((dataOutputPanel.nowCount + dataOutputPanel.maxRows) > dataOutputPanel.totalCount) {
150: dataOutputPanel.next.setEnabled(false);
151: dataOutputPanel.last.setEnabled(false);
152: }
153: if (dataOutputPanel.maxRows > dataOutputPanel.totalCount) {
154: dataOutputPanel.maxRows = dataOutputPanel.totalCount;
155: dataOutputPanel.refreshField.setText(String
156: .valueOf(dataOutputPanel.maxRows));
157: }
158: if (dataOutputPanel.totalCount != 0
159: && dataOutputPanel.maxRows != 0) {
160: dataOutputPanel.deleteRow.setEnabled(true);
161: } else {
162: dataOutputPanel.deleteRow.setEnabled(false);
163: }
164: dataOutputPanel.insert.setEnabled(true);
165: } else {
166: dataOutputPanel.first.setEnabled(false);
167: dataOutputPanel.next.setEnabled(false);
168: dataOutputPanel.last.setEnabled(false);
169: dataOutputPanel.previous.setEnabled(false);
170: dataOutputPanel.commit.setEnabled(false);
171: dataOutputPanel.deleteRow.setEnabled(false);
172: dataOutputPanel.insert.setEnabled(false);
173: }
174:
175: if (dataOutputPanel.totalCount == 0) {
176: dataOutputPanel.nowCount = 0;
177: }
178:
179: dataOutputPanel.refreshField.setText(""
180: + dataOutputPanel.maxRows);
181: dataOutputPanel.queryView.revalidate();
182: dataOutputPanel.queryView.repaint();
183: }
184:
185: private void closeConnection(Connection conn) {
186: if (conn != null) {
187: try {
188: conn.close();
189: } catch (SQLException e) {
190: }
191: }
192: }
193:
194: private void showDataForDBTable() {
195: Statement stmt = null;
196: Connection conn = null;
197:
198: try {
199: DBTableMetadata meta = dataOutputPanel.meta;
200: DB db = DBFactory.getInstance().getDatabase(
201: meta.getDBType());
202: conn = meta.createConnection();
203:
204: String resetFetchSizeSQL = null;
205: if (meta.isDBType(DBConstants.SYBASE)) {
206: conn.setAutoCommit(false);
207: stmt = conn.createStatement();
208: stmt.execute("SET ROWCOUNT "
209: + dataOutputPanel.recordToRefresh);
210: resetFetchSizeSQL = "SET ROWCOUNT 0";
211: }
212:
213: StatementContext context = new StatementContext();
214: Object limit = (dataOutputPanel.recordToRefresh == 0) ? ""
215: : dataOutputPanel.recordToRefresh;
216: context.putClientProperty("limit", limit);
217: Statements stmts = db.getStatements();
218:
219: String sql = null;
220: if (dbTable.getObjectType() == SQLConstants.SOURCE_TABLE) {
221: SQLPart sqlPart = stmts.getSelectStatement(
222: (SourceTable) dbTable, context);
223: sql = sqlPart.getSQL();
224: } else {
225: SQLPart sqlPart = stmts.getSelectStatement(
226: (TargetTable) dbTable, context);
227: sql = sqlPart.getSQL();
228: }
229:
230: List paramList = new ArrayList();
231: Map attribMap = new HashMap();
232: RuntimeDatabaseModel runtimeModel = dataOutputPanel
233: .getRuntimeDbModel();
234: if (runtimeModel != null) {
235: RuntimeInput inputTable = runtimeModel
236: .getRuntimeInput();
237: if (inputTable != null) {
238: attribMap = inputTable.getRuntimeAttributeMap();
239: }
240: }
241:
242: String psSql = SQLUtils.createPreparedStatement(sql,
243: attribMap, paramList);
244: PreparedStatement pstmt = conn.prepareStatement(psSql);
245: SQLUtils.populatePreparedStatement(pstmt, attribMap,
246: paramList);
247: mLogger
248: .infoNoloc(mLoc
249: .t(
250: "PRSR155: Select statement used for show data:{0}for {1}",
251: DataOutputPanel.NL, sql));
252: ResultSet rs = pstmt.executeQuery();
253:
254: dataOutputPanel.queryView.setEditable(true);
255: dataOutputPanel.queryView.setResultSet(rs,
256: dataOutputPanel.maxRows,
257: dataOutputPanel.nowCount - 1);
258:
259: rs.close();
260: pstmt.close();
261:
262: context.putClientProperty("limit", "");
263: SQLPart sqlPart = db.getStatements().getRowCountStatement(
264: meta.getTable(), context);
265: String countSql = db.getStatements()
266: .normalizeSQLForExecution(sqlPart).getSQL();
267: mLogger
268: .infoNoloc(mLoc
269: .t(
270: "PRSR156: Select count(*) statement used for total rows:{0}for {1}",
271: DataOutputPanel.NL, countSql));
272: paramList.clear();
273: psSql = SQLUtils.createPreparedStatement(countSql,
274: attribMap, paramList);
275: pstmt = conn.prepareStatement(psSql);
276: SQLUtils.populatePreparedStatement(pstmt, attribMap,
277: paramList);
278: ResultSet cntRs = pstmt.executeQuery();
279: dataOutputPanel.setTotalCount(cntRs);
280:
281: cntRs.close();
282: pstmt.close();
283:
284: if (resetFetchSizeSQL != null) {
285: stmt.execute(resetFetchSizeSQL);
286: }
287:
288: } catch (Exception e) {
289: this .errMsg = e.getMessage();
290: mLogger.errorNoloc(mLoc
291: .t("PRSR157: Can\'t get contents for table{0}",
292: ((dbTable != null) ? dbTable
293: .getDisplayName() : "")), e);
294: dataOutputPanel.queryView.clearView();
295: dataOutputPanel.totalRowsLabel.setText("0");
296: } finally {
297: if (stmt != null) {
298: try {
299: stmt.close();
300: } catch (SQLException e) {
301: }
302: }
303: closeConnection(conn);
304: }
305: }
306:
307: private void showDataForJoinOperator() {
308: ResultSet rs = null;
309: Statement stmt = null;
310: Connection conn = null;
311:
312: try {
313: SQLJoinOperator joinOperator = (SQLJoinOperator) dbTable;
314: Iterator it = joinOperator.getAllSourceTables().iterator();
315: DBConnectionDefinition connDef = null;
316: DBConnectionDefinition tempDef = null;
317: boolean isSameDB = true;
318: while (it.hasNext()) {
319: SourceTable tbl = (SourceTable) it.next();
320: connDef = tbl.getParent().getConnectionDefinition();
321: if (tempDef == null) {
322: tempDef = connDef;
323: }
324: if (!connDef.getConnectionURL().equals(
325: tempDef.getConnectionURL())) {
326: isSameDB = false;
327: break;
328: }
329: }
330: String joinSql = "";
331: StringBuilder buf = null;
332: if (isSameDB) {
333: conn = DBExplorerUtil.createConnection(connDef
334: .getDriverClass(), connDef.getConnectionURL(),
335: connDef.getUserName(), connDef.getPassword());
336: StatementContext context = new StatementContext();
337: DB db = DBFactory.getInstance()
338: .getDatabase(
339: SQLUtils.getSupportedDBType(connDef
340: .getDBType()));
341: context.setUseSourceTableAliasName(true);
342: buf = new StringBuilder(db.getStatements()
343: .getSelectStatement(joinOperator, context)
344: .getSQL());
345: joinSql = joinSql
346: + db.getGeneratorFactory().generate(
347: joinOperator, context);
348: } else {
349: try {
350: Thread
351: .currentThread()
352: .getContextClassLoader()
353: .loadClass(
354: AxionExternalConnectionProvider.class
355: .getName());
356: System
357: .setProperty(
358: ExternalConnectionProvider.EXTERNAL_CONNECTION_PROVIDER_PROPERTY_NAME,
359: AxionExternalConnectionProvider.class
360: .getName());
361: } catch (ClassNotFoundException e) {
362: }
363: conn = DBExplorerUtil.createConnection(
364: "org.axiondb.jdbc.AxionDriver",
365: "jdbc:axiondb:joinview", "sa", "sa");
366: stmt = conn.createStatement();
367: it = joinOperator.getAllSourceTables().iterator();
368: StatementContext joinContext = new StatementContext();
369: while (it.hasNext()) {
370: SourceTable table = (SourceTable) it.next();
371: AxionDB db = (AxionDB) DBFactory.getInstance()
372: .getDatabase(DBConstants.AXION);
373: DBConnectionDefinition connDefn = table.getParent()
374: .getConnectionDefinition();
375: AxionPipelineStatements stmts = db
376: .getAxionPipelineStatements();
377: String linkName = StringUtil
378: .createSQLIdentifier(connDefn.getName());
379: String dbLinkSql = stmts.getCreateDBLinkStatement(
380: connDefn, linkName).getSQL();
381: String dropDBLinkSql = stmts
382: .getDropDBLinkStatement(linkName).getSQL();
383: try {
384: stmt.execute(dropDBLinkSql);
385: } catch (SQLException e) {
386: }
387: stmt.execute(dbLinkSql);
388: StatementContext context = new StatementContext();
389: context.setUsingUniqueTableName(table, true);
390: context.setUsingFullyQualifiedTablePrefix(false);
391: String localName = db.getUnescapedName(db
392: .getGeneratorFactory().generate(table,
393: context));
394: String remoteTableSql = stmts
395: .getCreateRemoteTableStatement(table,
396: localName, linkName).getSQL();
397: try {
398: stmt.execute(remoteTableSql);
399: } catch (SQLException e) {
400: }
401:
402: // add table properties to statementcontext.
403: joinContext
404: .setUsingFullyQualifiedTablePrefix(false);
405: joinContext.setUsingUniqueTableName(table, true);
406: }
407: DB db = DBFactory.getInstance().getDatabase(
408: DBConstants.AXION);
409:
410: joinContext.setUsingUniqueTableName(true);
411: buf = new StringBuilder(db.getStatements()
412: .getSelectStatement(joinOperator, joinContext)
413: .getSQL());
414: joinSql = joinSql
415: + db.getGeneratorFactory().generate(
416: joinOperator, joinContext);
417: }
418:
419: stmt = conn.createStatement();
420: rs = stmt.executeQuery(buf.toString().trim());
421: dataOutputPanel.queryView.setEditable(false);
422: dataOutputPanel.queryView.setResultSet(rs,
423: dataOutputPanel.recordToRefresh, 0);
424: rs.close();
425: stmt.close();
426: try {
427: // set total count
428: ResultSet cntRs = conn.createStatement().executeQuery(
429: "SELECT COUNT(*) FROM " + joinSql.trim());
430: dataOutputPanel.setTotalCount(cntRs);
431: cntRs.close();
432: } catch (SQLException e) {
433: }
434: } catch (Exception ex1) {
435: mLogger.errorNoloc(mLoc
436: .t("PRSR158: Can\'t get contents for table{0}",
437: ((dbTable != null) ? dbTable
438: .getDisplayName() : "")), ex1);
439: dataOutputPanel.queryView.clearView();
440: dataOutputPanel.totalRowsLabel.setText("0");
441: } finally {
442: closeConnection(conn);
443: }
444: }
445:
446: private void showDataForJoinView() {
447: ResultSet rs = null;
448: Statement stmt = null;
449: Connection conn = null;
450:
451: try {
452: SQLJoinView joinView = (SQLJoinView) dbTable;
453: Iterator it = joinView.getSourceTables().iterator();
454: DBConnectionDefinition connDef = null;
455: DBConnectionDefinition tempDef = null;
456: boolean isSameDB = true;
457: while (it.hasNext()) {
458: SourceTable tbl = (SourceTable) it.next();
459: connDef = tbl.getParent().getConnectionDefinition();
460: if (tempDef == null) {
461: tempDef = connDef;
462: }
463: if (!connDef.getConnectionURL().equals(
464: tempDef.getConnectionURL())) {
465: isSameDB = false;
466: break;
467: }
468: }
469: String joinSql = "";
470: StringBuilder buf = null;
471: if (isSameDB) {
472: conn = DBExplorerUtil.createConnection(connDef
473: .getDriverClass(), connDef.getConnectionURL(),
474: connDef.getUserName(), connDef.getPassword());
475: StatementContext context = new StatementContext();
476: DB db = DBFactory.getInstance()
477: .getDatabase(
478: SQLUtils.getSupportedDBType(connDef
479: .getDBType()));
480: context.setUseSourceTableAliasName(true);
481: buf = new StringBuilder(db.getStatements()
482: .getSelectStatement(joinView, context).getSQL());
483: joinSql = joinSql
484: + db.getGeneratorFactory().generate(
485: joinView.getRootJoin(), context);
486: } else {
487: try {
488: Thread
489: .currentThread()
490: .getContextClassLoader()
491: .loadClass(
492: AxionExternalConnectionProvider.class
493: .getName());
494: System
495: .setProperty(
496: ExternalConnectionProvider.EXTERNAL_CONNECTION_PROVIDER_PROPERTY_NAME,
497: AxionExternalConnectionProvider.class
498: .getName());
499: } catch (ClassNotFoundException e) {
500: }
501: conn = DBExplorerUtil.createConnection(
502: "org.axiondb.jdbc.AxionDriver",
503: "jdbc:axiondb:joinview", "sa", "sa");
504: stmt = conn.createStatement();
505: it = joinView.getSourceTables().iterator();
506: StatementContext joinContext = new StatementContext();
507: while (it.hasNext()) {
508: SourceTable table = (SourceTable) it.next();
509: AxionDB db = (AxionDB) DBFactory.getInstance()
510: .getDatabase(DBConstants.AXION);
511: DBConnectionDefinition connDefn = table.getParent()
512: .getConnectionDefinition();
513: AxionPipelineStatements stmts = db
514: .getAxionPipelineStatements();
515: String linkName = StringUtil
516: .createSQLIdentifier(connDefn.getName());
517: String dbLinkSql = stmts.getCreateDBLinkStatement(
518: connDefn, linkName).getSQL();
519: String dropDBLinkSql = stmts
520: .getDropDBLinkStatement(linkName).getSQL();
521: try {
522: stmt.execute(dropDBLinkSql);
523: } catch (SQLException e) {
524: }
525: stmt.execute(dbLinkSql);
526: StatementContext context = new StatementContext();
527: context.setUsingUniqueTableName(table, true);
528: context.setUsingFullyQualifiedTablePrefix(false);
529: String localName = db.getUnescapedName(db
530: .getGeneratorFactory().generate(table,
531: context));
532: String remoteTableSql = stmts
533: .getCreateRemoteTableStatement(table,
534: localName, linkName).getSQL();
535: try {
536: stmt.execute(remoteTableSql);
537: } catch (SQLException e) {
538: }
539: joinContext
540: .setUsingFullyQualifiedTablePrefix(false);
541: joinContext.setUsingUniqueTableName(table, true);
542: }
543: DB db = DBFactory.getInstance().getDatabase(
544: DBConstants.AXION);
545: joinContext.setUsingUniqueTableName(true);
546: buf = new StringBuilder(db.getStatements()
547: .getSelectStatement(joinView, joinContext)
548: .getSQL());
549: joinSql = joinSql
550: + db.getGeneratorFactory().generate(
551: joinView.getRootJoin(), joinContext);
552: }
553: stmt = conn.createStatement();
554: rs = stmt.executeQuery(buf.toString().trim());
555: dataOutputPanel.queryView.setEditable(false);
556: dataOutputPanel.queryView.setResultSet(rs,
557: dataOutputPanel.recordToRefresh, 0);
558: rs.close();
559: stmt.close();
560: try {
561: ResultSet cntRs = conn.createStatement().executeQuery(
562: "SELECT COUNT(*) FROM " + joinSql.trim());
563: dataOutputPanel.setTotalCount(cntRs);
564: cntRs.close();
565: } catch (SQLException e) {
566: }
567: } catch (Exception e) {
568: mLogger.errorNoloc(mLoc
569: .t("PRSR159: Can\'t get contents for table{0}",
570: ((dbTable != null) ? dbTable
571: .getDisplayName() : "")), e);
572: dataOutputPanel.queryView.clearView();
573: dataOutputPanel.totalRowsLabel.setText("0");
574: } finally {
575: closeConnection(conn);
576: }
577: }
578: }
|