001: /*
002: * Created on Sep 18, 2003
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package xflow.test;
008:
009: import java.io.*;
010: import java.sql.Connection;
011: import java.sql.DriverManager;
012: import java.sql.ResultSet;
013: import java.sql.Statement;
014: import java.sql.Time;
015: import java.sql.Timestamp;
016: import java.util.Date; //import java.sql.Date;
017:
018: import javax.naming.InitialContext;
019: import javax.sql.DataSource;
020: import javax.xml.parsers.DocumentBuilder;
021: import javax.xml.parsers.DocumentBuilderFactory;
022:
023: import org.apache.xalan.serialize.DOMSerializer;
024: import org.w3c.dom.*;
025: import org.w3c.dom.Node;
026: import org.xml.sax.InputSource;
027:
028: import xflow.common.*;
029: import xflow.events.EventsPersistence;
030: import xflow.events.EventsPublisher;
031: import xflow.messaging.JMSSubscriber;
032: import xflow.util.XflowXMLParser;
033:
034: /**
035: * @author xzma
036: *
037: * To change the template for this generated type comment go to
038: * Window - Preferences - Java - Code Generation - Code and Comments
039: */
040: public class TestParseSOAP {
041:
042: public static void main(String[] args) {
043: try {
044: BufferedReader fin = new BufferedReader(new FileReader(
045: "d:\\eclipse\\workspace\\"
046: + "xflow\\src\\xflow\\test\\testSOAP.xml"));
047: String xml = "";
048: String s = "";
049: while ((s = fin.readLine()) != null) {
050: s += "\n";
051: xml += s;
052: }
053: System.out.println(xml);
054: try {
055: //namespace of SOAP-ENV
056: String docNS = "http://schemas.xmlsoap.org/soap/envelope/";
057:
058: DocumentBuilderFactory factory = DocumentBuilderFactory
059: .newInstance();
060: factory.setNamespaceAware(true);
061: DocumentBuilder builder = factory.newDocumentBuilder();
062:
063: StringReader sreader = new StringReader(xml);
064: InputSource is = new InputSource(sreader);
065: Document doc = builder.parse(is);
066:
067: NodeList els = doc
068: .getElementsByTagNameNS(docNS, "Body");
069: Element body = (Element) els.item(0);
070:
071: els = body.getChildNodes();
072: String eventId;
073: Element event;
074: els = body
075: .getElementsByTagName("WorkflowSuspendedEvent");
076: System.out.println(els);
077: if (els != null && els.item(0) != null) {
078: event = (Element) els.item(0);
079: System.out.println(event.getLocalName());
080: insertSupendedEvent(event);
081: } else if ((els = body
082: .getElementsByTagName("ProcessTimedOutEvent")) != null
083: && els.item(0) != null) {
084: event = (Element) els.item(0);
085: insertTimeOutEvent(event);
086: } else if ((els = body
087: .getElementsByTagName("ModelDeployedEvent")) != null
088: && els.item(0) != null) {
089: event = (Element) els.item(0);
090: insertDeployedEvent(event);
091: } else if ((els = body
092: .getElementsByTagName("VariableUpdatedEvent")) != null
093: && els.item(0) != null) {
094: event = (Element) els.item(0);
095: insertUpdatedEvent(event);
096: } else if ((els = body
097: .getElementsByTagName("WorkflowAbortedEvent")) != null
098: && els.item(0) != null) {
099: event = (Element) els.item(0);
100: insertAbortedEvent(event);
101: } else if ((els = body
102: .getElementsByTagName("WorkflowResumedEvent")) != null
103: && els.item(0) != null) {
104: event = (Element) els.item(0);
105: insertResumedEvent(event);
106: } else if ((els = body
107: .getElementsByTagName("WorkflowCompletedEvent")) != null
108: && els.item(0) != null) {
109: event = (Element) els.item(0);
110: insertCompletedEvent(event);
111: } else if ((els = body
112: .getElementsByTagName("WorkflowStartedEvent")) != null
113: && els.item(0) != null) {
114: event = (Element) els.item(0);
115: insertStartedEvent(event);
116: } else if ((els = body
117: .getElementsByTagName("NodeTransitionEvent")) != null
118: && els.item(0) != null) {
119: event = (Element) els.item(0);
120: insertNodeTransitionEvent(event);
121: } else {
122: System.err.println("unknown event types");
123: }
124:
125: } catch (Exception e) {
126: e.printStackTrace();
127: }
128: } catch (IOException e) {
129: e.printStackTrace();
130: }
131: }
132:
133: private static void insertNodeTransitionEvent(Element event) {
134: int eventId, workItemInternalId;
135: Element workItem = (Element) event.getElementsByTagName(
136: "WorkItem").item(0);
137: String[] info = retriveWorkItemInfo(workItem);
138:
139: int workItemId = Integer.parseInt(info[0]);
140: String payloadType = info[1];
141: String payload = info[2];
142:
143: eventId = insertEventTable(event);
144:
145: workItemInternalId = insertEventWorkItemTable(eventId, -1,
146: workItemId, payloadType, payload);
147: System.out.println("Done with inserting eventworkitem table: "
148: + eventId + " " + workItemInternalId);
149:
150: Element ps = (Element) workItem.getElementsByTagName(
151: "Properties").item(0);
152: NodeList proplist = ps.getElementsByTagName("Property");
153: for (int i = 0; i < proplist.getLength(); i++) {
154: String pname, ptype, pvalue;
155: Element p = (Element) proplist.item(i);
156: info = retriveWorkItemPropertyInfo(p);
157: pname = info[0];
158: ptype = info[1];
159: pvalue = info[2];
160:
161: insertEventWorkItemPropertiesTable(workItemInternalId,
162: pname, ptype, pvalue);
163: System.out
164: .println("Done with inserting eventworkitem properties table: "
165: + workItemInternalId);
166:
167: }
168: // get transtion info
169: info = retriveTransitionInfo(event);
170: insertNodeTranstionEventTable(eventId, info[0], info[1],
171: info[2], info[3]);
172: System.out.println("Done with inserting transition table: "
173: + workItemInternalId);
174:
175: }
176:
177: private static void insertNodeTranstionEventTable(int eventId,
178: String fname, String ftype, String tname, String ttype) {
179: String query = "INSERT INTO NodeTransitionEvent VALUES("
180: + eventId + ",'" + fname + "','" + ftype + "','"
181: + tname + "','" + ttype + "')";
182: // log.info(query);
183: executeQuery(query);
184: }
185:
186: private static void insertStartedEvent(Element event) {
187: int eventId, workItemInternalId;
188: Element workItem = (Element) event.getElementsByTagName(
189: "WorkItem").item(0);
190: String[] info = retriveWorkItemInfo(workItem);
191:
192: int workItemId = Integer.parseInt(info[0]);
193: String payloadType = info[1];
194: String payload = info[2];
195:
196: eventId = insertEventTable(event);
197:
198: workItemInternalId = insertEventWorkItemTable(eventId, -1,
199: workItemId, payloadType, payload);
200: System.out.println("Done with inserting eventworkitem table: "
201: + eventId + " " + workItemInternalId);
202:
203: Element ps = (Element) workItem.getElementsByTagName(
204: "Properties").item(0);
205: NodeList proplist = ps.getElementsByTagName("Property");
206: for (int i = 0; i < proplist.getLength(); i++) {
207: String pname, ptype, pvalue;
208: Element p = (Element) proplist.item(i);
209: info = retriveWorkItemPropertyInfo(p);
210: pname = info[0];
211: ptype = info[1];
212: pvalue = info[2];
213:
214: insertEventWorkItemPropertiesTable(workItemInternalId,
215: pname, ptype, pvalue);
216: System.out
217: .println("Done with inserting eventworkitem properties table: "
218: + workItemInternalId);
219:
220: }
221: }
222:
223: private static String[] retriveWorkItemPropertyInfo(Element property) {
224: String info[] = new String[3];
225: info[0] = property.getElementsByTagName("Name").item(0)
226: .getFirstChild().getNodeValue();
227: info[1] = property.getElementsByTagName("Type").item(0)
228: .getFirstChild().getNodeValue();
229: info[2] = property.getElementsByTagName("Value").item(0)
230: .getFirstChild().getNodeValue();
231: return info;
232: }
233:
234: private static String[] retriveTransitionInfo(Element event) {
235: String info[] = new String[4];
236:
237: Element from = (Element) event.getElementsByTagName("From")
238: .item(0);
239: Element to = (Element) event.getElementsByTagName("To").item(0);
240:
241: info[0] = from.getAttribute("nodeName");
242: info[1] = from.getAttribute("nodeType");
243:
244: info[2] = to.getAttribute("nodeName");
245: info[3] = to.getAttribute("nodeType");
246:
247: return info;
248: }
249:
250: private static String[] retriveWorkItemInfo(Element workItem) {
251: String info[] = new String[3];
252:
253: info[0] = workItem.getElementsByTagName("WorkItemId").item(0)
254: .getFirstChild().getNodeValue();
255: Element pld = (Element) workItem
256: .getElementsByTagName("Payload").item(0);
257:
258: info[1] = pld.getAttribute("type");
259:
260: NodeList plist = pld.getChildNodes();
261: info[2] = "";
262: for (int i = 0; i < plist.getLength(); i++) {
263: info[2] += plist.item(i).toString();
264: }
265: return info;
266: }
267:
268: /**
269: * insert event information into data base
270: * @param event
271: * @return the eventId
272: */
273: private static int insertEventTable(Element event) {
274: String eventId = null;
275: String eventType, workflowName, user;
276: String timestamp;
277: int workflowVersion, workflowInstanceId, parentWorkflowInstanceId;
278: String[] info;
279:
280: info = retriveEventInfo(event);
281: eventType = info[0];
282: timestamp = info[1];
283: workflowName = info[2];
284: workflowVersion = Integer.parseInt(info[3]);
285: workflowInstanceId = (info[4] == null) ? -1 : Integer
286: .parseInt(info[4]);
287: parentWorkflowInstanceId = (info[5] == null) ? -1 : Integer
288: .parseInt(info[5]);
289: user = info[6];
290:
291: String query = "INSERT INTO event VALUES(null, " + "'"
292: + eventType + "','" + timestamp + "','" + workflowName
293: + "'," + workflowVersion + "," + workflowInstanceId
294: + "," + parentWorkflowInstanceId + ","
295: + ((user == null) ? "null)" : ("'" + user + "')"));
296:
297: System.out.println("in insert event method " + query);
298:
299: java.sql.Connection conn = null;
300: try {
301:
302: EventsPersistence.init();
303:
304: //now create a query and update db
305: Class.forName("org.hsql.jdbcDriver");
306:
307: // Connect to the database
308: // It will be create automatically if it does not yet exist
309: // 'testfiles' in the URL is the name of the database
310: // "sa" is the user name and "" is the (empty) password
311: conn = DriverManager.getConnection(
312: "jdbc:HypersonicSQL:testfiles", "sa", "");
313:
314: Statement st = conn.createStatement();
315: st.execute(query);
316: ResultSet rs = st
317: .executeQuery("SELECT max(eventId), eventId from event");
318: while (rs.next()) { //actually there is only one row there
319: try {
320: eventId = rs.getString("eventId").trim();
321: } catch (NumberFormatException e) {
322: System.err.println("bad event ID");
323: }
324: }
325: rs.close();
326: st.close();
327: } catch (Exception e) {
328: System.out.println(e.getMessage());
329: } finally {
330: if (conn != null) {
331: try {
332: conn.close();
333: } catch (Exception e) {
334: }
335: }
336: }
337: return Integer.parseInt(eventId);
338: //return 123;
339: }
340:
341: /**
342: * get information for event from soap body element--event
343: * @param event
344: * @return
345: */
346: private static String[] retriveEventInfo(Element event) {
347: String info[] = new String[7];
348:
349: info[0] = event.getLocalName();
350: info[1] = event.getElementsByTagName("Timestamp").item(0)
351: .getFirstChild().getNodeValue();
352: info[2] = event.getElementsByTagName("WorkflowName").item(0)
353: .getFirstChild().getNodeValue();
354: info[3] = event.getElementsByTagName("WorkflowVersion").item(0)
355: .getFirstChild().getNodeValue();
356: info[4] = event.getElementsByTagName("WorkflowInstanceId")
357: .item(0).getFirstChild().getNodeValue();
358: Node pwi, user;
359: if ((pwi = event.getElementsByTagName(
360: "ParentWorkflowInstanceId").item(0)) != null
361: && pwi.getFirstChild() != null) {
362: info[5] = pwi.getFirstChild().getNodeValue();
363: } else {
364: info[5] = null;
365: }
366:
367: if ((user = event.getElementsByTagName("User").item(0)) != null
368: && user.getFirstChild() != null) {
369: info[6] = user.getFirstChild().getNodeValue();
370: } else {
371: info[6] = null;
372: }
373: return info;
374: }
375:
376: private static void insertCompletedEvent(Element event) {
377: insertEventTable(event);
378: }
379:
380: private static void insertSupendedEvent(Element event) {
381: insertEventTable(event);
382: }
383:
384: private static void insertResumedEvent(Element event) {
385: insertEventTable(event);
386: }
387:
388: private static void insertEventWorkItemPropertiesTable(
389: int workItenInternalId, String pname, String ptype,
390: String pvalue) {
391: String query = "INSERT INTO EventWorkItemProperties VALUES("
392: + workItenInternalId + ",'" + pname + "','" + ptype
393: + "','" + pvalue + "')";
394: // log.info(query);
395: executeQuery(query);
396: }
397:
398: private static void executeQuery(String query) {
399: System.out.println(query);
400: java.sql.Connection conn = null;
401: try {
402: //now create a query and update db
403: Class.forName("org.hsql.jdbcDriver");
404:
405: // Connect to the database
406: // It will be create automatically if it does not yet exist
407: // 'testfiles' in the URL is the name of the database
408: // "sa" is the user name and "" is the (empty) password
409: conn = DriverManager.getConnection(
410: "jdbc:HypersonicSQL:testfiles", "sa", "");
411: Statement st = conn.createStatement();
412: st.execute(query);
413: System.out.println("done inserting in ses-q");
414: st.close();
415: } catch (Exception e) {
416: System.out.println(e.getMessage());
417: } finally {
418: if (conn != null) {
419: try {
420: conn.close();
421: } catch (Exception e) {
422: }
423: }
424: }
425: }
426:
427: /**
428: * execute a query by given query string on table "tableName", return the largest
429: * index for specific colume. It can be use to get the last inserted entry if the
430: * column is auto-incremented
431: * @param query
432: * @param columnName
433: * @param tableName
434: * @return
435: */
436: private static int executeQuery(String query, String columnName,
437: String tableName) {
438: System.out.println(query);
439: java.sql.Connection conn = null;
440: String lastMod = null;
441: try {
442: //now create a query and update db
443: Class.forName("org.hsql.jdbcDriver");
444:
445: // Connect to the database
446: // It will be create automatically if it does not yet exist
447: // 'testfiles' in the URL is the name of the database
448: // "sa" is the user name and "" is the (empty) password
449: conn = DriverManager.getConnection(
450: "jdbc:HypersonicSQL:testfiles", "sa", "");
451:
452: //now create a query and update db
453: // conn = EventsPersistence.getConnection();
454: Statement st = conn.createStatement();
455: st.execute(query);
456:
457: ResultSet rs = st.executeQuery("SELECT max(" + columnName
458: + ")," + columnName + " from " + tableName);
459: while (rs.next()) { // actually only has one row
460: try {
461: lastMod = rs.getString(columnName).trim();
462: } catch (NumberFormatException e) {
463: System.err.println("bad event ID");
464: }
465: }
466: rs.close();
467: st.close();
468: } catch (Exception e) {
469: System.out.println(e.getMessage());
470: } finally {
471: if (conn != null) {
472: try {
473: conn.close();
474: } catch (Exception e) {
475: }
476: }
477: }
478: return Integer.parseInt(lastMod);
479: }
480:
481: private static void insertUpdatedEvent(Element event) {
482: int eventId;
483: Element var = (Element) event.getElementsByTagName("Variable")
484: .item(0);
485:
486: String name = var.getAttribute("name");
487: String type = var.getAttribute("type");
488: String value = var.getFirstChild().getNodeValue();
489:
490: eventId = insertEventTable(event);
491:
492: insertVariableUpdateEventTable(eventId, name, type, value);
493: }
494:
495: private static void insertTimeOutEvent(Element event) {
496: int eventId;
497: String processName = event.getElementsByTagName("ProcessName")
498: .item(0).getFirstChild().getNodeValue();
499:
500: eventId = insertEventTable(event);
501: insertProcessTimedOutTable(eventId, processName);
502: }
503:
504: private static void insertDeployedEvent(Element event) {
505: insertEventTable(event);
506: }
507:
508: private static void insertProcessTimedOutTable(int eventId,
509: String processName) {
510: String query = "INSERT INTO ProcessTimeoutEvent VALUES("
511: + eventId + ",'" + processName + "')";
512: // log.info(query);
513: executeQuery(query);
514: }
515:
516: /**
517: *
518: * @param eventId
519: * @param workItemInternalId: if = -1, auto-invremented
520: * @param workItemId
521: * @param payloadType
522: * @param payload
523: * @return
524: */
525: private static int insertEventWorkItemTable(int eventId,
526: int workItemInternalId, int workItemId, String payloadType,
527: String payload) {
528:
529: Integer internalId = new Integer(workItemInternalId);
530:
531: String query = "INSERT INTO EventWorkItem VALUES("
532: + eventId
533: + ","
534: + ((workItemInternalId == -1) ? "null" : internalId
535: .toString()) + "," + workItemId + ",'"
536: + payloadType + "','" + payload + "')";
537: // log.info(query);
538: return executeQuery(query, "workItemInternalId",
539: "EventWorkItem");
540:
541: }
542:
543: private static void insertAbortedEvent(Element event) {
544: insertEventTable(event);
545: }
546:
547: private static void insertVariableUpdateEventTable(int eventId,
548: String name, String type, String value) {
549: String query = "INSERT INTO VariableUpdatedEvent VALUES("
550: + eventId + ",'" + name + "','" + type + "','" + value
551: + "')";
552: // log.info(query);
553: executeQuery(query);
554: }
555: }
|