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.DBConnectionFactory;
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.List;
054: import java.util.Map;
055: import java.util.Properties;
056: import net.java.hulp.i18n.Logger;
057: import org.netbeans.modules.etl.codegen.DBConnectionDefinitionTemplate;
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.common.jdbc.SQLDBConnectionDefinition;
064: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
065: import org.netbeans.modules.sql.framework.common.utils.MonitorUtil;
066: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
067: import org.netbeans.modules.sql.framework.model.RuntimeInput;
068: import org.netbeans.modules.sql.framework.model.SQLDefinition;
069: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
070: import org.netbeans.modules.sql.framework.model.SQLObject;
071: import org.netbeans.modules.sql.framework.model.TargetTable;
072: import org.netbeans.modules.sql.framework.ui.SwingWorker;
073: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
074: import org.openide.awt.StatusDisplayer;
075:
076: /**
077: * @author Ahimanikya Satapathy
078: */
079: public class RejectedRowsDataPanel extends DataOutputPanel {
080:
081: private static transient final Logger mLogger = LogUtil
082: .getLogger(RejectedRowsDataPanel.class.getName());
083: private static transient final Localizer mLoc = Localizer.get();
084:
085: public RejectedRowsDataPanel(SQLObject etlObject,
086: SQLDefinition sqlDefinition) {
087: super (etlObject, sqlDefinition, false, false);
088: }
089:
090: public void generateResult() {
091: generateResult(this .table);
092: }
093:
094: public void generateResult(SQLObject aTable) {
095: this .table = aTable;
096: String nbBundle1 = mLoc.t("PRSR001: Rejected Data: {0} ", table
097: .getDisplayName());
098: this .setName(Localizer.parse(nbBundle1));
099: String nbBundle2 = mLoc.t("PRSR001: Loading Data");
100: String title = Localizer.parse(nbBundle2);
101: String nbBundle3 = mLoc
102: .t("PRSR001: Loading from database, please wait...");
103: String msg = Localizer.parse(nbBundle3);
104: UIUtil.startProgressDialog(title, msg);
105: generateRejectionTableData();
106: }
107:
108: private void generateRejectionTableData() {
109: refreshButton.setEnabled(false);
110: refreshField.setEnabled(false);
111: RejectionViewWorkerThread rejectionQueryThread = new RejectionViewWorkerThread(
112: table);
113: rejectionQueryThread.start();
114: }
115:
116: class RejectionViewWorkerThread extends SwingWorker {
117:
118: DBConnectionFactory factory = DBConnectionFactory.getInstance();
119: private SQLObject aTable;
120: private Connection conn;
121: private Throwable ex;
122: private PreparedStatement pstmt;
123: private ResultSet rs;
124: private Statement stmt;
125:
126: public RejectionViewWorkerThread(SQLObject table) {
127: this .aTable = table;
128: }
129:
130: public Object construct() {
131: try {
132: SQLDBConnectionDefinition conDef;
133:
134: TargetTable outTable = (TargetTable) aTable;
135: TargetTable clone = SQLModelObjectFactory.getInstance()
136: .createTargetTable(outTable);
137: clone
138: .setTablePrefix(MonitorUtil.LOG_DETAILS_TABLE_PREFIX);
139:
140: DBConnectionDefinitionTemplate connTemplate = new DBConnectionDefinitionTemplate();
141: conDef = connTemplate
142: .getDBConnectionDefinition("AXIONMEMORYDB");
143:
144: Map connParams = new HashMap();
145: connParams
146: .put(
147: DBConnectionDefinitionTemplate.KEY_DATABASE_NAME,
148: "MonitorDB");
149: conDef.setConnectionURL(StringUtil.replace(conDef
150: .getConnectionURL(), connParams));
151: DB db = DBFactory.getInstance().getDatabase(DB.AXIONDB);
152:
153: Properties connProps = conDef.getConnectionProperties();
154: conn = factory.getConnection(connProps);
155: stmt = conn.createStatement();
156:
157: StatementContext context = new StatementContext();
158: context.setUsingFullyQualifiedTablePrefix(false);
159: context.setUsingUniqueTableName(true);
160: Object limit = (recordToRefresh == 0) ? "" : Integer
161: .toString(recordToRefresh);
162: context.putClientProperty("limit", limit);
163:
164: SQLPart sqlPart = db.getStatements()
165: .getSelectStatement(clone, context);
166: String sql = sqlPart.getSQL();
167:
168: // execute select and get result set
169: List paramList = new ArrayList();
170: Map attribMap = new HashMap();
171: RuntimeDatabaseModel runtimeModel = getRuntimeDbModel();
172: if (runtimeModel != null) {
173: RuntimeInput inputTable = runtimeModel
174: .getRuntimeInput();
175: if (inputTable != null) {
176: attribMap = inputTable.getRuntimeAttributeMap();
177: }
178: }
179:
180: String psSql = SQLUtils.createPreparedStatement(sql,
181: attribMap, paramList);
182: pstmt = conn.prepareStatement(psSql);
183: SQLUtils.populatePreparedStatement(pstmt, attribMap,
184: paramList);
185: mLogger
186: .infoNoloc(mLoc
187: .t(
188: "PRSR175: Select statement used for show data:{0}is{1}",
189: NL, sql));
190: this .rs = pstmt.executeQuery();
191: queryView.setResultSet(rs, maxRows, 0);
192: pstmt.close();
193: queryView.setEditable(false);
194: //get the count of all rows
195: sqlPart = db.getStatements().getRowCountStatement(
196: clone, context);
197: String countSql = db.getStatements()
198: .normalizeSQLForExecution(sqlPart).getSQL();
199: mLogger
200: .infoNoloc(mLoc
201: .t(
202: "PRSR176: Select count(*) statement used for total rows:{0}is{1}",
203: NL, countSql));
204: paramList.clear();
205: psSql = SQLUtils.createPreparedStatement(countSql,
206: attribMap, paramList);
207: pstmt = conn.prepareStatement(psSql);
208: SQLUtils.populatePreparedStatement(pstmt, attribMap,
209: paramList);
210: ResultSet cntRs = pstmt.executeQuery();
211:
212: //set the count
213: setTotalCount(cntRs);
214: cntRs.close();
215: } catch (Exception e) {
216: this .ex = e;
217: mLogger
218: .errorNoloc(
219: mLoc
220: .t(
221: "PRSR177: Can't get contents for table{0}in{1}",
222: ((aTable != null) ? aTable
223: .getDisplayName()
224: : ""),
225: DataOutputPanel.class
226: .getName()), e);
227: queryView.clearView();
228: totalRowsLabel.setText("0");
229: }
230: return "";
231: }
232:
233: //Runs on the event-dispatching thread.
234: @Override
235: public void finished() {
236: if (this .ex != null) {
237: String nbBundle1 = mLoc
238: .t(
239: "PRSR001: Error fetching data for table {0}.\nCause: {1}",
240: aTable.getDisplayName(), ex
241: .getMessage());
242: String errorMsg = Localizer.parse(nbBundle1);
243:
244: // If rejection table does not exist, show a brief user-friendly message
245: // that doesn't include a stack trace.
246: if (ex instanceof SQLException
247: && "42704".equals(((SQLException) ex)
248: .getSQLState())) {
249: String nbBundle4 = mLoc
250: .t(
251: "PRSR001: No rejection rows available for table {0}.",
252: aTable.getDisplayName());
253: errorMsg = Localizer.parse(nbBundle4);
254: }
255: StatusDisplayer.getDefault().setStatusText(errorMsg);
256: }
257:
258: if (truncateButton != null) {
259: truncateButton.setEnabled(true);
260: }
261: refreshButton.setEnabled(true);
262: refreshField.setEnabled(true);
263: if ((nowCount - maxRows) > 0) {
264: first.setEnabled(true);
265: previous.setEnabled(true);
266: }
267: if ((nowCount + maxRows) <= totalCount) {
268: next.setEnabled(true);
269: last.setEnabled(true);
270: }
271: if ((nowCount - maxRows) <= 0) {
272: first.setEnabled(false);
273: previous.setEnabled(false);
274: }
275: if ((nowCount + maxRows) > totalCount) {
276: next.setEnabled(false);
277: last.setEnabled(false);
278: }
279: insert.setEnabled(false);
280: deleteRow.setEnabled(false);
281: commit.setEnabled(false);
282: int endCount = nowCount + maxRows - 1;
283: if ((nowCount + maxRows - 1) > totalCount) {
284: endCount = totalCount;
285: }
286: if (totalCount == 0) {
287: nowCount = 0;
288: endCount = 0;
289: }
290: refreshField.setText("" + maxRows);
291: commit.setEnabled(false);
292: queryView.repaint();
293: try {
294: if (pstmt != null) {
295: pstmt.close();
296: }
297:
298: if (stmt != null) {
299: stmt.close();
300: }
301: } catch (SQLException sqle) {
302: mLogger
303: .errorNoloc(
304: mLoc
305: .t("PRSR178: Could not close statement after retrieving aTable contents."),
306: sqle);
307: } finally {
308: if (conn != null) {
309: factory.closeConnection(conn);
310: conn = null;
311: UIUtil.stopProgressDialog();
312: }
313: }
314: }
315: }
316: }
|