001: package org.enhydra.shark.toolagent;
002:
003: import java.sql.Connection;
004: import java.sql.PreparedStatement;
005: import java.sql.Statement;
006: import java.sql.Time;
007: import java.sql.Timestamp;
008: import java.sql.Types;
009: import java.util.Calendar;
010: import java.util.Date;
011: import java.util.HashMap;
012: import java.util.Map;
013:
014: import javax.naming.Context;
015: import javax.naming.InitialContext;
016: import javax.sql.DataSource;
017:
018: import org.enhydra.shark.Shark;
019: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
020: import org.enhydra.shark.api.client.wfmodel.WfActivity;
021: import org.enhydra.shark.api.client.wfservice.SharkConnection;
022: import org.enhydra.shark.api.client.wfservice.WMEntity;
023: import org.enhydra.shark.api.internal.toolagent.AppParameter;
024: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
025: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
026: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
027: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
028: import org.enhydra.shark.toolagent.AbstractToolAgent;
029:
030: /**
031: * Tool agent for SQL statements execution. Parameters are mapped by name and there are
032: * two possible combinations: first: statement(full expression for execute)+ data_source +
033: * row_count ( updated rows number - optional) OR second: prepared_statement + data_source +
034: * row_count (optional) + parameters for prepared_statement In tne case when only the
035: * "statement" is given, tool doesn't take care about unknown paramerers. "statement" must
036: * be full and valid. When "prepered_statement" is used all needed parameters must be set
037: * in the same order as "prepared_statement" expects them.
038: *
039: * @author Svjetlana Milidrag
040: * @version 1.0
041: */
042: public class ExecuteSqlTool extends AbstractToolAgent {
043:
044: public static final String STATEMENT = "statement"; // xsl name
045:
046: public static final String PREPARED_STATEMENT = "prepared_statement"; // xsl name
047:
048: public static final String DATA_SOURCE = "data_source"; // xsl name
049:
050: public static final String ROW_COUNT = "row_count"; // xsl path
051:
052: protected String statement = null;
053:
054: protected String dataSrc = null;
055:
056: protected String rowCount = null;
057:
058: Statement stmt = null;
059:
060: public void invokeApplication(WMSessionHandle _shandle,
061: long _handle, WMEntity _appInfo, WMEntity _toolInfo,
062: String _applicationName, String _procInstId, String _assId,
063: AppParameter[] _parameters, Integer _appMode)
064: throws ApplicationNotStarted, ApplicationNotDefined,
065: ApplicationBusy, ToolAgentGeneralException {
066: super .invokeApplication(_shandle, _handle, _appInfo, _toolInfo,
067: _applicationName, _procInstId, _assId, _parameters,
068: _appMode);
069: status = APP_STATUS_RUNNING;
070: try {
071: SharkConnection sc = Shark.getInstance()
072: .getSharkConnection();
073:
074: sc.attachToHandle(shandle);
075: WfActivity act = sc.getActivity(procInstId,
076: Shark.getInstance().getAdminMisc()
077: .getAssignmentActivityId(shandle,
078: procInstId, assId));
079:
080: Map ret = new HashMap();
081:
082: AppParameter param = getParameterByName(this .parameters,
083: DATA_SOURCE);
084:
085: if (param != null) {
086: dataSrc = (String) param.the_value;
087: } else {
088: throw new Exception("Data source isn't defined !");
089: }
090: DataSource dataSource;
091: Context initialContext = new InitialContext();
092:
093: dataSource = (DataSource) initialContext.lookup(dataSrc);
094:
095: Connection conn = dataSource.getConnection();
096:
097: initStatement(conn);
098:
099: try {
100: if (stmt instanceof PreparedStatement) {
101: ((PreparedStatement) stmt).execute();
102: } else {
103: stmt.execute(statement);
104: }
105:
106: if (null != rowCount) {
107:
108: int i = stmt.getUpdateCount();
109: ret.put(rowCount, new Integer(i));
110: }
111: } finally {
112: try {
113: if (null != stmt)
114: stmt.close();
115: } finally {
116: if (null != conn)
117: conn.close();
118: }
119: }
120:
121: act.set_result(ret);
122:
123: status = APP_STATUS_FINISHED;
124: } catch (Throwable e) {
125: status = APP_STATUS_TERMINATED;
126: if (e instanceof ApplicationNotStarted) {
127: throw (ApplicationNotStarted) e;
128: } else if (e instanceof ApplicationNotDefined) {
129: throw (ApplicationNotDefined) e;
130: } else if (e instanceof ApplicationBusy) {
131: throw (ApplicationBusy) e;
132: } else if (e instanceof ToolAgentGeneralException) {
133: throw (ToolAgentGeneralException) e;
134: } else {
135: throw new ToolAgentGeneralException(e);
136: }
137: }
138: }
139:
140: protected void initStatement(Connection conn) throws Exception {
141:
142: AppParameter param = getParameterByName(this .parameters,
143: STATEMENT);
144:
145: if (param != null) {
146: statement = (String) param.the_value;
147: stmt = conn.createStatement();
148: } else {
149: param = getParameterByName(this .parameters,
150: PREPARED_STATEMENT);
151: if (param != null) {
152: statement = (String) param.the_value;
153: stmt = conn.prepareStatement(statement);
154:
155: initParameters((PreparedStatement) stmt);
156: } else {
157: throw new Exception("Prepare statement isn't defined !");
158: }
159: }
160: param = getParameterByName(this .parameters, ROW_COUNT);
161:
162: if (param != null) {
163: rowCount = (String) param.the_value;
164: }
165:
166: }
167:
168: protected void initParameters(PreparedStatement stmt)
169: throws Exception {
170:
171: if (this .parameters != null) {
172: int count = 1;
173: for (int i = 1; i < this .parameters.length; i++) {
174: String name = this .parameters[i].the_formal_name;
175: if (!name.equals(STATEMENT)
176: && !name.equals(PREPARED_STATEMENT)
177: && !name.equals(ROW_COUNT)
178: && !name.equals(DATA_SOURCE)) {
179:
180: Class paramClass = this .parameters[i].the_class;
181: Object paramValue = this .parameters[i].the_value;
182:
183: if (paramClass.equals(String.class)) {
184: if (null == paramValue) {
185: stmt.setNull(count++, Types.VARCHAR);
186: } else {
187: stmt
188: .setString(count++,
189: (String) paramValue);
190: }
191:
192: }
193: if (paramClass.equals(Boolean.class)) {
194: if (null == paramValue) {
195: stmt.setNull(count++, Types.BOOLEAN);
196: } else {
197: stmt.setBoolean(count++,
198: ((Boolean) paramValue)
199: .booleanValue());
200: }
201:
202: } else if (paramClass.equals(Long.class)) {
203: if (null == paramValue) {
204: stmt.setNull(count++, Types.BIGINT);
205: } else {
206: stmt.setLong(count++, ((Long) paramValue)
207: .longValue());
208: }
209:
210: } else if (paramClass.equals(Integer.class)) {
211: if (null == paramValue) {
212: stmt.setNull(count++, Types.INTEGER);
213: } else {
214: stmt.setInt(count++, ((Integer) paramValue)
215: .intValue());
216: }
217:
218: } else if (paramClass.equals(Float.class)) {
219: if (null == paramValue) {
220: stmt.setNull(count++, Types.FLOAT);
221: } else {
222: stmt.setFloat(count++, ((Float) paramValue)
223: .floatValue());
224: }
225:
226: } else if (paramClass.equals(Double.class)) {
227: if (null == paramValue) {
228: stmt.setNull(count++, Types.DOUBLE);
229: } else {
230: stmt
231: .setDouble(count++,
232: ((Double) paramValue)
233: .doubleValue());
234: }
235:
236: } else if (paramClass.equals(Date.class)) {
237:
238: if (null == paramValue) {
239: stmt.setNull(count++, Types.DATE);
240: } else {
241: if ((paramClass.getName())
242: .equalsIgnoreCase("java.util.Date")) {
243: paramValue = getDate(paramValue);
244: }
245: stmt.setDate(count++,
246: (java.sql.Date) paramValue);
247: }
248:
249: } else if (paramClass.equals(Time.class)) {
250: if (null == paramValue) {
251: stmt.setNull(count++, Types.TIME);
252: } else {
253: stmt.setTime(count++,
254: (java.sql.Time) paramValue);
255: }
256:
257: } else if (paramClass.equals(Timestamp.class)) {
258: if (null == paramValue) {
259: stmt.setNull(count++, Types.TIMESTAMP);
260: } else {
261: stmt.setTimestamp(count++,
262: (java.sql.Timestamp) paramValue);
263: }
264:
265: } else if (paramClass.equals(byte[].class)) {
266: if (null == paramValue) {
267: stmt.setNull(count++, Types.ARRAY);
268: } else {
269: stmt.setBytes(count++, (byte[]) paramValue);
270: }
271: } else if (paramClass.equals(Object.class)) {
272: if (null == paramValue) {
273: stmt.setNull(count++, Types.JAVA_OBJECT);
274: } else {
275: stmt.setObject(count++, paramValue);
276: }
277:
278: }
279:
280: }
281: }
282: }
283:
284: }
285:
286: private Date getDate(Object obj) {
287: Calendar aa = Calendar.getInstance();
288: aa.setTime((java.util.Date) obj);
289: Date ret = new Date(aa.getTimeInMillis());
290: return ret;
291: }
292:
293: }
|