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-2006 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:
042: package org.netbeans.modules.db.sql.loader;
043:
044: import java.io.IOException;
045: import java.text.NumberFormat;
046: import org.netbeans.modules.db.sql.execute.SQLExecutionLogger;
047: import org.netbeans.modules.db.sql.execute.SQLExecutionResult;
048: import org.netbeans.modules.db.sql.execute.StatementInfo;
049: import org.openide.cookies.LineCookie;
050: import org.openide.text.Line;
051: import org.openide.util.Exceptions;
052: import org.openide.util.NbBundle;
053: import org.openide.windows.IOProvider;
054: import org.openide.windows.InputOutput;
055: import org.openide.windows.OutputEvent;
056: import org.openide.windows.OutputListener;
057: import org.openide.windows.OutputWriter;
058:
059: /**
060: *
061: * @author Andrei Badea
062: */
063: public class SQLExecutionLoggerImpl implements SQLExecutionLogger {
064:
065: private final LineCookie lineCookie;
066: private final InputOutput inputOutput;
067:
068: private boolean inputOutputSelected = false;
069: private int errorCount;
070:
071: public SQLExecutionLoggerImpl(String displayName,
072: LineCookie lineCookie) {
073: this .lineCookie = lineCookie;
074:
075: String ioName = NbBundle.getMessage(SQLEditorSupport.class,
076: "LBL_SQLFileExecution", displayName);
077: inputOutput = IOProvider.getDefault().getIO(ioName, true);
078: }
079:
080: public void log(SQLExecutionResult result) {
081: if (result.getException() != null) {
082: logException(result);
083: } else {
084: logSuccess(result);
085: }
086: }
087:
088: public void finish(long executionTime) {
089: OutputWriter writer = inputOutput.getOut();
090: writer.println(NbBundle.getMessage(SQLEditorSupport.class,
091: "LBL_ExecutionFinished", String
092: .valueOf(millisecondsToSeconds(executionTime)),
093: String.valueOf(errorCount)));
094: writer.println(""); // NOI18N
095: }
096:
097: public void cancel() {
098: OutputWriter writer = inputOutput.getErr();
099: writer.println(NbBundle.getMessage(SQLEditorSupport.class,
100: "LBL_ExecutionCancelled"));
101: writer.println(""); // NOI18N
102: }
103:
104: public void close() {
105: inputOutput.closeInputOutput();
106: }
107:
108: private void logException(SQLExecutionResult result) {
109: errorCount++;
110:
111: if (!inputOutputSelected) {
112: inputOutputSelected = true;
113: inputOutput.select();
114: }
115:
116: OutputWriter writer = inputOutput.getErr();
117:
118: writer.println(NbBundle.getMessage(SQLEditorSupport.class,
119: "LBL_ErrorCodeStateMessage", String.valueOf(result
120: .getException().getErrorCode()), result
121: .getException().getSQLState(), result
122: .getException().getMessage()));
123: printLineColumn(writer, result.getStatementInfo(), true);
124: writer.println(""); // NOI18N
125: }
126:
127: private void logSuccess(SQLExecutionResult result) {
128: OutputWriter writer = inputOutput.getOut();
129:
130: String executionTimeStr = millisecondsToSeconds(result
131: .getExecutionTime());
132: String successLine = null;
133: if (result.getRowCount() >= 0) {
134: successLine = NbBundle.getMessage(SQLEditorSupport.class,
135: "LBL_ExecutedSuccessfullyTimeRows", String
136: .valueOf(executionTimeStr), String
137: .valueOf(result.getRowCount()));
138: } else {
139: successLine = NbBundle.getMessage(SQLEditorSupport.class,
140: "LBL_ExecutedSuccessfullyTime", String
141: .valueOf(executionTimeStr));
142: }
143: writer.println(successLine);
144: printLineColumn(writer, result.getStatementInfo(), false);
145: writer.println(""); // NOI18N
146: }
147:
148: private void printLineColumn(OutputWriter writer,
149: StatementInfo statementInfo, boolean hyperlink) {
150: String lineColumn = NbBundle.getMessage(SQLEditorSupport.class,
151: "LBL_LineColumn", String.valueOf(statementInfo
152: .getStartLine() + 1), String
153: .valueOf(statementInfo.getStartColumn() + 1));
154: try {
155: if (hyperlink) {
156: writer.println(lineColumn,
157: new Hyperlink(statementInfo.getStartLine(),
158: statementInfo.getStartColumn()));
159: } else {
160: writer.println(lineColumn);
161: }
162: } catch (IOException e) {
163: Exceptions.printStackTrace(e);
164: }
165: }
166:
167: private String millisecondsToSeconds(long ms) {
168: NumberFormat fmt = NumberFormat.getInstance();
169: fmt.setMaximumFractionDigits(3);
170: return fmt.format(ms / 1000.0);
171: }
172:
173: public void logResultSetException(Exception e) {
174: inputOutput.select();
175: OutputWriter writer = inputOutput.getErr();
176:
177: writer.println(NbBundle.getMessage(SQLEditorSupport.class,
178: "LBL_ResultSetErrorDetailed", e.getMessage()));
179: }
180:
181: /**
182: * Represents a hyperlinked line in an InputOutput.
183: */
184: private final class Hyperlink implements OutputListener {
185:
186: private final int line;
187: private final int column;
188:
189: public Hyperlink(int line, int column) {
190: this .line = line;
191: this .column = column;
192: }
193:
194: public void outputLineSelected(OutputEvent ev) {
195: goToLine(false);
196: }
197:
198: public void outputLineCleared(OutputEvent ev) {
199: }
200:
201: public void outputLineAction(OutputEvent ev) {
202: goToLine(true);
203: }
204:
205: private void goToLine(boolean focus) {
206: Line l = lineCookie.getLineSet().getOriginal(line);
207: if (!l.isDeleted()) {
208: l.show(focus ? Line.SHOW_GOTO : Line.SHOW_TRY_SHOW,
209: column);
210: }
211: }
212: }
213: }
|