1 package com.explosion.datastream.exql.processes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32
33 import javax.swing.JTextArea;
34
35 import org.apache.log4j.LogManager;
36 import org.apache.log4j.Logger;
37
38 import com.explosion.datastream.exql.gui.EXQLBaseTool;
39 import com.explosion.expfmodules.rdbmsconn.dbom.DataSet;
40 import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLEngine;
41 import com.explosion.utilities.exception.EnhancedException;
42 import com.explosion.utilities.exception.ExceptionManagerFactory;
43 import com.explosion.utilities.process.StackableSimpleProcess;
44 import com.explosion.utilities.process.threads.Finishable;
45
46 /***
47 * @author Stephen Cowx
48 * Date created:@14-Feb-2003
49 * This class executes a query and puts the results in a Dataset object
50 */
51 public class SQLFileProcess extends StackableSimpleProcess
52 {
53 private Connection conn;
54 private DataSet set;
55 private EXQLBaseTool tool;
56 private File file;
57 private static Logger log = LogManager.getLogger(SQLFileProcess.class);
58 private JTextArea logTextArea;
59 private String scriptText;
60
61
62 /***
63 * Constructor for RefreshTopNodesProcess.
64 */
65 public SQLFileProcess(Finishable finishable, Connection conn, File file)
66 {
67 super(finishable, null);
68 this.conn = conn;
69 this.file = file;
70 this.setIsUserProcess(true);
71 }
72
73 /***
74 * Constructor for RefreshTopNodesProcess.
75 */
76 public SQLFileProcess(Finishable finishable, Connection conn, File file, EXQLBaseTool tool)
77 {
78 super(finishable, null);
79 this.conn = conn;
80 this.tool = tool;
81 this.file = file;
82 this.setIsUserProcess(true);
83 }
84
85 /***
86 * Constructor for RefreshTopNodesProcess.
87 */
88 public SQLFileProcess(Finishable finishable, Connection conn, String scriptText, EXQLBaseTool tool, JTextArea logTextArea)
89 {
90 super(finishable, null);
91 this.conn = conn;
92 this.tool = tool;
93 this.scriptText = scriptText;
94 this.setIsUserProcess(true);
95 this.logTextArea = logTextArea;
96 }
97
98 public void process()
99 {
100 try
101 {
102 if (isStopped())
103 return;
104
105 setPercentComplete(5);
106
107 List statements = load();
108
109 if (isStopped())
110 return;
111
112 Iterator it = statements.iterator();
113 int numberOfElements = statements.size();
114
115 while (it.hasNext())
116 {
117 try
118 {
119 String stmt = (String) it.next();
120 SQLEngine engine = new SQLEngine();
121 log("Executing: " + stmt);
122 engine.executeCommand(conn, stmt, -1, this, false);
123 engine.close();
124 setPercentComplete(5 + (90 / numberOfElements) );
125 }
126 catch (java.sql.SQLException e)
127 {
128 log(e,"Error: " + e.getMessage());
129 }
130 }
131
132 if (numberOfElements == 0)
133 log("Script file complete, no statements found in file. (Each SQl statements int the file must end with a ';').");
134 else
135 {
136 log("Script file complete.");
137 }
138
139 setStatusText("Done.");
140
141 this.setPercentComplete(100);
142
143 }
144 catch (Exception e)
145 {
146 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while running script.");
147
148 setStatusText("Script not run successfully.");
149 setPercentComplete(100);
150 }
151
152 }
153
154 public List load() throws IOException, SQLException
155 {
156 BufferedReader reader = null;
157 ArrayList statements;
158 try
159 {
160 statements = new ArrayList();
161 if (file != null)
162 {
163 reader = new BufferedReader(new FileReader(file));
164 }
165 else if (scriptText != null)
166 {
167 reader = new BufferedReader(new StringReader(scriptText));
168 }
169
170 StringBuffer statement = new StringBuffer();
171 String line = new String();
172
173 while (true)
174 {
175
176 if (isStopped())
177 {
178 reader.close();
179 return null;
180 }
181
182 line = reader.readLine();
183
184 if (line == null)
185 {
186 break;
187 }
188 else
189 {
190
191 if (line.startsWith("--"))
192 {
193
194 }
195 else if (line.endsWith(";"))
196 {
197
198 statement.append(line.trim().substring(0, line.trim().length() - 1));
199 statements.add(statement.toString());
200 statement.delete(0, statement.length());
201 }
202 else
203 {
204 String string = line.trim();
205 line.replaceAll("\t","");
206 statement.append(line.trim());
207 }
208 }
209 }
210 }
211 finally
212 {
213 if (reader != null)
214 {
215 try
216 {
217 reader.close();
218 }
219 catch (IOException e)
220 {
221 e.printStackTrace();
222 }
223 }
224 }
225
226 return statements;
227
228 }
229
230 /***
231 * Returns the set.
232 * @return DataSet
233 */
234 public DataSet getSet()
235 {
236 return set;
237 }
238
239
240 public void log(String string)
241 {
242 if (logTextArea != null)
243 {
244 logTextArea.append(string + System.getProperty("line.separator"));
245 logTextArea.setCaretPosition(logTextArea.getText().length());
246 }
247 log.debug(string);
248 }
249
250 public void log(Exception exception, String message)
251 {
252 if (logTextArea != null)
253 {
254 logTextArea.append(message + System.getProperty("line.separator"));
255 logTextArea.append(EnhancedException.getStackTrace(exception) + System.getProperty("line.separator"));
256 logTextArea.setCaretPosition(logTextArea.getText().length());
257 }
258
259 log.error(message + exception.getMessage(), exception);
260 }
261
262 }