1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.tools.database;
19
20 import java.sql.*;
21 import java.util.*;
22
23 /***
24 * ExecutionException class. This exception is the catch-all exception
25 * that is thrown when anything goes wrong executing a statement using the StatementExecutor.
26 *
27 * @author P.S.D.Reitsma, Finalist IT Group
28 * @version 1.0
29 */
30 public class ExecutionException extends RuntimeException {
31
32 StringBuffer errorReport = new StringBuffer();
33 private static String BLANKS30 = " ";
34
35
36 /***
37 * Constructor that takes a SQLException as an argument.
38 *
39 * @param rootSQLException SQLException
40 */
41 public ExecutionException(SQLException rootSQLException) {
42 StringBuffer rep = new StringBuffer();
43 rep.append("\n\nROOT: SQL EXCEPTION" + rep.toString());
44 rep.append("\n============================================================");
45 rep.append("============================================================");
46 rep.append("\nSQLException :" + rootSQLException.getMessage());
47 rep.append("\nError code :" + rootSQLException.getErrorCode());
48 rep.append("\nSQL State :" + rootSQLException.getSQLState());
49 System.out.println(rep.toString());
50 errorReport.append(rep.toString());
51 }
52
53
54 /***
55 * Constructor that takes a NotMappableException as an argument.
56 *
57 * @param rootNotMappableException SQLException
58 */
59 public ExecutionException(NotMappableException rootNotMappableException) {
60 StringBuffer rep = new StringBuffer();
61 rep.append("\n\nROOT: NOTMAPPABLEEXCEPTION" + rep.toString());
62 rep.append("\n============================================================");
63 rep.append("============================================================");
64 rep.append("\nSQLException :" + rootNotMappableException.getMessage());
65 System.out.println(rep.toString());
66 errorReport.append(rep.toString());
67 }
68
69
70 void createQueryReport(Query query) {
71 StringBuffer rep = new StringBuffer();
72 if (query != null) {
73 rep.append("\n\nQUERY DETAILS" + rep.toString());
74 rep.append("\n============================================================");
75 rep.append("============================================================");
76 rep.append("\nDefinition :" + query.oriqString);
77 rep.append("\nPrepared version :" + query.qString);
78 rep.append("\nBind Names :");
79 Iterator E = query.BindIdentifiers.iterator();
80 while (E.hasNext()) rep.append((String) E.next() + " ");
81 rep.append("\nIs Malformed :" + query.malformed);
82 rep.append("\nReturns resultset :" + query.givesResultSet);
83 rep.append("\nIs Delete :" + query.isDelete);
84 rep.append("\nIs Insert :" + query.isInsert);
85 rep.append("\nIs Update :" + query.isUpdate);
86 rep.append("\nIs Callable :" + query.isCallable);
87 rep.append("\nPerforms Order By :" + query.performsOrderBy);
88 errorReport.append(rep.toString());
89 System.out.println(rep.toString());
90 }
91 }
92
93
94 void createHashMapReport(Map bindVars) {
95 StringBuffer rep = new StringBuffer();
96 rep.append("\n\nBIND HASHMAP DETAILS" + rep.toString());
97 rep.append("\n============================================================");
98 rep.append("============================================================");
99 if (bindVars != null) {
100 Iterator bindVarNames = bindVars.keySet().iterator();
101 while (bindVarNames.hasNext()) {
102 String curVarName = (String) bindVarNames.next();
103 rep.append("\n" + (curVarName + BLANKS30).substring(0, 20) + ":");
104 Object bindVar = bindVars.get(curVarName);
105 String bindVarClassType;
106 if (bindVar != null) {
107 bindVarClassType = bindVar.getClass().getName();
108 }
109 else {
110 bindVarClassType = "undetermined type since bindvariable = null";
111 }
112 rep.append(bindVar + "(" + bindVarClassType + ")");
113 }
114 }
115 else {
116 rep.append("\nNo HashMap set");
117 }
118 errorReport.append(rep.toString());
119 System.out.println(rep.toString());
120 }
121
122
123 void createBindBeanReport(Object bindBean) {
124
125 StringBuffer rep = new StringBuffer();
126 rep.append("\n\nBINDBEAN DETAILS" + rep.toString());
127 rep.append("\n============================================================");
128 rep.append("============================================================");
129 if (bindBean != null) {
130 try {
131 ArrayList properties = MethodInvoker.getGetterNames(bindBean);
132 Iterator propIt = properties.iterator();
133 while (propIt.hasNext()) {
134 String propertyName = (String) propIt.next();
135 String propertyClassType = MethodInvoker.getReturnType(bindBean, propertyName);
136 Object propertyValue = MethodInvoker.getProperty(bindBean, propertyName);
137 rep.append("\n" + (propertyName + BLANKS30).substring(0, 20) + ":");
138 rep.append(propertyValue + "(" + propertyClassType + ")");
139 }
140 }
141 catch (Exception I) {
142 I.printStackTrace();
143 }
144 }
145 else {
146 rep.append("\nNo BindBean set");
147 }
148 errorReport.append(rep.toString());
149 System.out.println(rep.toString());
150
151 }
152
153
154 void reconstructQuery(StatementExecutor patient) {
155
156
157 Object bindVar = null;
158 String bindVarName = null;
159 String bindVarClassType = null;
160 if (patient.query.qString != null) {
161
162 StringBuffer prepString = new StringBuffer(patient.query.qString);
163 StringBuffer rep = new StringBuffer();
164 rep.append("\n\nRECONSTRUCTED QUERY (might not be correctly reconstructed...)");
165 rep.append("\n============================================================");
166 rep.append("============================================================");
167
168 for (Iterator i = patient.query.BindIdentifiers.iterator(); i.hasNext();) {
169 bindVarName = (String) i.next();
170
171 if (patient.bindVars != null && patient.bindVars.containsKey(bindVarName)) {
172 bindVar = patient.bindVars.get(bindVarName.toUpperCase());
173 if (bindVar != null) {
174 bindVarClassType = bindVar.getClass().getName();
175 }
176 else {
177 bindVarClassType = "undetermined";
178 }
179 reconstructBind(prepString, bindVarClassType, bindVar);
180 }
181 else if (patient.bindBean != null && MethodInvoker.getterExists(patient.bindBean, bindVarName)) {
182 try {
183 bindVar = MethodInvoker.getProperty(patient.bindBean, bindVarName);
184 bindVarClassType = MethodInvoker.getReturnType(patient.bindBean, bindVarName);
185 reconstructBind(prepString, bindVarClassType, bindVar);
186
187 }
188 catch (Exception e) {
189
190 }
191 }
192
193 }
194
195 prepString.append(";");
196 rep.append("\n" + prepString.toString());
197 errorReport.append(rep.toString());
198 System.out.println(rep.toString());
199 }
200
201 }
202
203
204 static void reconstructBind(StringBuffer prepString, String type, Object bindVar) {
205 int questionMarkPos = prepString.toString().indexOf("?");
206 prepString.deleteCharAt(questionMarkPos);
207 if (type.indexOf("String") > -1 || type.indexOf("Date") > -1 || type.indexOf("Calendar") > -1) {
208 prepString.insert(questionMarkPos, "'" + bindVar + "'");
209 }
210 else {
211 prepString.insert(questionMarkPos, bindVar);
212 }
213 }
214
215
216 void setPatient(StatementExecutor patient) {
217 createQueryReport(patient.query);
218 createHashMapReport(patient.bindVars);
219 createBindBeanReport(patient.bindBean);
220 reconstructQuery(patient);
221 }
222
223
224 /***
225 *
226 * @return the error message
227 */
228 public String getMessage() {
229 return errorReport.toString();
230 }
231
232
233 }