KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > loader > SQLExecutionLoggerImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.loader;
21
22 import java.io.IOException JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24 import org.netbeans.modules.db.sql.execute.SQLExecutionLogger;
25 import org.netbeans.modules.db.sql.execute.SQLExecutionResult;
26 import org.netbeans.modules.db.sql.execute.StatementInfo;
27 import org.openide.ErrorManager;
28 import org.openide.cookies.LineCookie;
29 import org.openide.text.Line;
30 import org.openide.util.NbBundle;
31 import org.openide.windows.IOProvider;
32 import org.openide.windows.InputOutput;
33 import org.openide.windows.OutputEvent;
34 import org.openide.windows.OutputListener;
35 import org.openide.windows.OutputWriter;
36
37 /**
38  *
39  * @author Andrei Badea
40  */

41 public class SQLExecutionLoggerImpl implements SQLExecutionLogger {
42
43     private final LineCookie lineCookie;
44     private final InputOutput inputOutput;
45
46     private boolean inputOutputSelected = false;
47     private int errorCount;
48
49     public SQLExecutionLoggerImpl(String JavaDoc displayName, LineCookie lineCookie) {
50         this.lineCookie = lineCookie;
51
52         String JavaDoc ioName = NbBundle.getMessage(SQLEditorSupport.class, "LBL_SQLFileExecution", displayName);
53         inputOutput = IOProvider.getDefault().getIO(ioName, true);
54     }
55
56     public void log(SQLExecutionResult result) {
57         if (result.getException() != null) {
58             logException(result);
59         } else {
60             logSuccess(result);
61         }
62     }
63
64     public void finish(long executionTime) {
65         OutputWriter writer = inputOutput.getOut();
66         writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutionFinished",
67                 String.valueOf(millisecondsToSeconds(executionTime)),
68                 String.valueOf(errorCount)));
69         writer.println(""); // NOI18N
70
}
71
72     public void cancel() {
73         OutputWriter writer = inputOutput.getErr();
74         writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutionCancelled"));
75         writer.println(""); // NOI18N
76
}
77
78     public void close() {
79         inputOutput.closeInputOutput();
80     }
81
82     private void logException(SQLExecutionResult result) {
83         errorCount++;
84
85         if (!inputOutputSelected) {
86             inputOutputSelected = true;
87             inputOutput.select();
88         }
89
90         OutputWriter writer = inputOutput.getErr();
91
92         writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ErrorCodeStateMessage",
93                 String.valueOf(result.getException().getErrorCode()),
94                 result.getException().getSQLState(),
95                 result.getException().getMessage()));
96         printLineColumn(writer, result.getStatementInfo(), true);
97         writer.println(""); // NOI18N
98
}
99
100     private void logSuccess(SQLExecutionResult result) {
101         OutputWriter writer = inputOutput.getOut();
102
103         String JavaDoc executionTimeStr = millisecondsToSeconds(result.getExecutionTime());
104         String JavaDoc successLine = null;
105         if (result.getRowCount() >= 0) {
106             successLine = NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutedSuccessfullyTimeRows",
107                     String.valueOf(executionTimeStr),
108                     String.valueOf(result.getRowCount()));
109         } else {
110             successLine = NbBundle.getMessage(SQLEditorSupport.class, "LBL_ExecutedSuccessfullyTime",
111                     String.valueOf(executionTimeStr));
112         }
113         writer.println(successLine);
114         printLineColumn(writer, result.getStatementInfo(), false);
115         writer.println(""); // NOI18N
116
}
117
118     private void printLineColumn(OutputWriter writer, StatementInfo statementInfo, boolean hyperlink) {
119         String JavaDoc lineColumn = NbBundle.getMessage(SQLEditorSupport.class, "LBL_LineColumn",
120                 String.valueOf(statementInfo.getStartLine() + 1),
121                 String.valueOf(statementInfo.getStartColumn() + 1));
122         try {
123             if (hyperlink) {
124                 writer.println(lineColumn, new Hyperlink(statementInfo.getStartLine(), statementInfo.getStartColumn()));
125             } else {
126                 writer.println(lineColumn);
127             }
128         } catch (IOException JavaDoc e) {
129             ErrorManager.getDefault().notify(e);
130         }
131     }
132
133     private String JavaDoc millisecondsToSeconds(long ms) {
134         NumberFormat JavaDoc fmt = NumberFormat.getInstance();
135         fmt.setMaximumFractionDigits(3);
136         return fmt.format(ms / 1000.0);
137     }
138
139     public void logResultSetException(Exception JavaDoc e) {
140         inputOutput.select();
141         OutputWriter writer = inputOutput.getErr();
142
143         writer.println(NbBundle.getMessage(SQLEditorSupport.class, "LBL_ResultSetErrorDetailed",
144                 e.getMessage()));
145     }
146
147     /**
148      * Represents a hyperlinked line in an InputOutput.
149      */

150     private final class Hyperlink implements OutputListener {
151
152         private final int line;
153         private final int column;
154
155         public Hyperlink(int line, int column) {
156             this.line = line;
157             this.column = column;
158         }
159
160         public void outputLineSelected(OutputEvent ev) {
161             goToLine(false);
162         }
163
164         public void outputLineCleared(OutputEvent ev) {
165         }
166
167         public void outputLineAction(OutputEvent ev) {
168             goToLine(true);
169         }
170
171         private void goToLine(boolean focus) {
172             Line l = lineCookie.getLineSet().getOriginal(line);
173             if (!l.isDeleted()) {
174                 l.show(focus ? Line.SHOW_GOTO : Line.SHOW_TRY_SHOW, column);
175             }
176         }
177     }
178 }
179
Popular Tags