Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
|
B2B XML Application: Step by Step , 20 of 32
The following scripts are executed by the B2B application (demo):
<?xml version="1.0"?> <!-- | This is the fifth and last, but not least, script called. | This script actually fires the whole B2B process. | It uses the Action Handler facility of XSQL Servlet. | | $Author: olediour@us $ | $Revision: 1.1 $ +--> <?xml-stylesheet type="text/xsl" media="HandHTTP" href="PP.xsl"?> <?xml-stylesheet type="text/xsl" media="Mozilla" href="HTML.xsl"?> <placeOrder xmlns:xsql="urn:oracle-xsql" connection="retail" dbUrl ="jdbc:oracle:thin:@atp-1.us.oracle.com:1521:ORCL" username ="retailer" password ="retailer" entity ="Ord" operation ="insert" custId ="" ordId ="" prodId ="" qty =""> <xsql:include-request-params/> <pageTitle>Hi-Tech Mall</pageTitle> <pageSeparator/> <xsql:action handler ="B2BDemo.XSQLActionHandler.RetailActionHandler" dbUrl ="{@dbUrl}" username ="{@username}" password ="{@password}" entity ="{@entity}" operation ="{@operation}" custId ="{@custId}" ordId ="{@ordId}" prodId ="{@prodId}" qty ="{@qty}"/> <pageSeparator/> <bottomLinks> <aLink href="placeOrder.xsql?operation=rollback">Rollback</aLink> </bottomLinks> <returnHome>index.xsql</returnHome> </placeOrder>
package B2BDemo.XSQLActionHandler; /** * Action Handler called by the placeOrder.xsql script. * Actually fires the B2B process itself. * Uses SessionHolder to maintain transaction state. * * @see SessionHolder * @see placeOrder.xsql * @author Olivier LE DIOURIS - Partner Technical Services - Oracle Corp. */ import oracle.xml.xsql.*; import oracle.xml.xsql.actions.XSQLIncludeXSQLHandler; import javax.servlet.http.*; import javax.servlet.*; import org.w3c.dom.*; import java.sql.*; import java.io.*; import oracle.xml.parser.v2.*; import B2BDemo.AQUtil.*; import B2BDemo.*; import B2BDemo.XMLUtil.*; public class RetailActionHandler extends XSQLActionHandlerImpl { private static final boolean verbose = false; private static final boolean debugFile = false; private Connection actionConnection = null; private String appUrl = ""; private String appUser = ""; private String appPassword = ""; public static final String DBURL = "dbUrl"; public static final String USERNAME = "username"; public static final String PASSWORD = "password"; public static final String OPERATION = "operation"; public static final String ENTITY = "entity"; public static final String ORDID = "ordId"; public static final String ORDERDATE = "orderDate"; public static final String CONTACTNAME = "contactName"; public static final String TRACKINGNO = "trackingNo"; public static final String STATUS = "status"; public static final String CUSTID = "custId"; public static final String QTY = "qty"; public static final String PRODID = "prodId"; public static final String SELECT = "select"; public static final String INSERT = "insert"; public static final String BEGIN = "begin"; public static final String COMMIT = "commit"; public static final String ROLLBACK = "rollback"; XSQLActionHandler nestedHandler = null; String operation = null; String entity = null; String ordId = null; String orderDate = null; String contactName = null; String trackingNo = null; String status = null; String custId = null; String qty = null; String prodId = null; HttpServletRequest request = null; HttpServletResponse response = null; HttpSession session = null; public void init(XSQLPageRequest xspRequest, Element action) { super.init(xspRequest, action); // Retrieve the parameters if (verbose) System.out.println("init Action Handler..................."); appUrl = getAttributeAllowingParam(DBURL, action); appUser = getAttributeAllowingParam(USERNAME, action); appPassword = getAttributeAllowingParam(PASSWORD, action); operation = getAttributeAllowingParam(OPERATION, action); entity = getAttributeAllowingParam(ENTITY, action); ordId = getAttributeAllowingParam(ORDID, action); orderDate = getAttributeAllowingParam(ORDERDATE, action); contactName = getAttributeAllowingParam(CONTACTNAME, action); trackingNo = getAttributeAllowingParam(TRACKINGNO, action); status = getAttributeAllowingParam(STATUS, action); custId = getAttributeAllowingParam(CUSTID, action); prodId = getAttributeAllowingParam(PRODID, action); qty = getAttributeAllowingParam(QTY, action); // if (verbose) { System.out.println("OrdID > " + ordId); System.out.println("CustID > " + custId); System.out.println("ProdID > " + prodId); } final String HOLDER_NAME = "XSQLActionHandler.connection"; try { if (xspRequest.getRequestType().equals("Servlet")) { XSQLServletPageRequest xspr = (XSQLServletPageRequest)xspRequest; HttpServletRequest req = xspr.getHttpServletRequest(); session = req.getSession(true); // true : Create if missing !!! if (verbose) System.out.println("Session Id = " + session.getId() + " - new : " + session.isNew()); SessionHolder sh = (SessionHolder) session.getValue(HOLDER_NAME); if (sh == null) { if (verbose) System.out.println("New SessionHandler > Getting connected at " + (new java.util.Date())); actionConnection = getConnected(appUrl, appUser, appPassword); sh = new SessionHolder(actionConnection); session.putValue(HOLDER_NAME, sh); } actionConnection = sh.getConnection(); if (verbose) { System.out.println("Reusing Connection at " + (new java.util.Date()) + " - Opened at " + sh.getOpenDate().toString()); System.out.println("Driver : " + actionConnection.getMetaData().getDriverName()); System.out.println("SessionId : " + session.getId()); System.out.println("AutoCommit : " + actionConnection.getAutoCommit()); } } } catch (Exception e) { System.err.println("Error in retrieving session context \n" + e); e.printStackTrace(); } } // The result is the out parameter public void handleAction(Node result) throws SQLException { XSQLPageRequest xpr = getPageRequest(); if (xpr.getRequestType().equals("Servlet")) { // Get the servlet context and components XSQLServletPageRequest xspr = (XSQLServletPageRequest)xpr; request = xspr.getHttpServletRequest(); response = xspr.getHttpServletResponse(); Document doc = null; // Display CLASSPATH XMLDocument myDoc = new XMLDocument(); try { Element root = myDoc.createElement("root"); myDoc.appendChild(root); Element cp = myDoc.createElement("ClassPath"); root.appendChild(cp); // The text is a descendant of its node Node cpTxt = myDoc.createTextNode("text#"); cpTxt.setNodeValue(System.getProperty("java.class.path")); cp.appendChild(cpTxt); Element e = myDoc.getDocumentElement(); e.getParentNode().removeChild(e); result.appendChild(e); // Append child to result before returning it. } catch (Exception e) { System.err.println("Building XMLDoc"); e.printStackTrace(); } try { // Add a node to hold operation value XMLDocument xmlDoc = new XMLDocument(); Element elmt = xmlDoc.createElement("requiredOperation"); xmlDoc.appendChild(elmt); Node theText = xmlDoc.createTextNode("text#"); theText.setNodeValue(operation); elmt.appendChild(theText); // Append to result Element e = xmlDoc.getDocumentElement(); e.getParentNode().removeChild(e); result.appendChild(e); // Append child to result before returning it. } catch (Exception e) { System.err.println("Building XMLDoc (2)"); e.printStackTrace(); } try { // Dispatch if (operation.equals(SELECT)) /* doc = manageSelect() */; else if (operation.equals(INSERT)) doc = manageInsert(); else if (operation.equals(BEGIN)) doc = doBegin(); else if (operation.equals(COMMIT)) doc = doCommit(); else if (operation.equals(ROLLBACK)) doc = doRollback(); else // Wrong operation { XMLDocument xmlDoc = new XMLDocument(); Element elmt = xmlDoc.createElement("unknownOperation"); xmlDoc.appendChild(elmt); Node theText = xmlDoc.createTextNode("text#"); theText.setNodeValue(operation); elmt.appendChild(theText); // Append to result Element e = xmlDoc.getDocumentElement(); e.getParentNode().removeChild(e); result.appendChild(e); // Append child to result before returning it. } } catch (Exception ex) { // file://this.reportError(e); XMLDocument xmlDoc = new XMLDocument(); Element elmt = xmlDoc.createElement("operationProblem"); xmlDoc.appendChild(elmt); Node theText = xmlDoc.createTextNode("text#"); theText.setNodeValue(ex.toString()); elmt.appendChild(theText); // Append to result Element e = xmlDoc.getDocumentElement(); e.getParentNode().removeChild(e); result.appendChild(e); // Append child to result before returning it. } try { if (doc != null) { Element e = doc.getDocumentElement(); e.getParentNode().removeChild(e); result.appendChild(e); // Append child to result before returning it. } } catch (Exception e) { try { ServletOutputStream out = response.getOutputStream(); out.println(e.toString()); } catch (Exception ex) {} } } else // Command line ? { System.out.println("Request type is [" + xpr.getRequestType() + "]"); } } /** * Removed because uselezss in this demo. * private Document manageSelect() throws Exception { Document doc = null; String cStmt = ""; if (custId != null && custId.length() > 0) vo.setWhereClause("Customer_Id = '" + custId + "'"); else vo.setWhereClause(null); vo.executeQuery(); doc = data.getXMLDocument(); // Query implicitly executed ! return doc; } */ private Document manageInsert() throws Exception { Document doc = null; if (entity.equals("Ord")) doc = insertInOrd(); else if (entity.equals("LineItem")) doc = insertInLine(); else { doc = new XMLDocument(); Element elmt = doc.createElement("operationQuestion"); Attr attr = doc.createAttribute("opType"); attr.setValue("insert"); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue("Don't know what to do with " + entity); } return doc; } private Document insertInOrd() { Document doc = null; if (custId == null || custId.length() == 0) { doc = new XMLDocument(); Element elmt = doc.createElement("operationProblem"); Attr attr = doc.createAttribute("opType"); attr.setValue("OrdInsert"); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue("Some element(s) missing for ord insert (custId)"); } else { String seqStmt = "select Ord_Seq.nextVal from dual"; String seqVal = ""; try { Statement stmt = actionConnection.createStatement(); ResultSet rSet = stmt.executeQuery(seqStmt); while (rSet.next()) seqVal = rSet.getString(1); rSet.close(); stmt.close(); } catch (SQLException e) { System.err.println("Error reading ORD_SEQ Sequence : " + e.toString()); } // 1 2 3 4 String cStmt = "insert into ORD values (?, sysdate, ?, 'AX' || ?, 'Pending', ?)"; try { if (verbose) System.out.println("Inserting Order # " + seqVal); PreparedStatement pStmt = actionConnection.prepareStatement(cStmt); pStmt.setString(1, seqVal); pStmt.setString(2, "Ora817"); // Default value ! pStmt.setString(3, seqVal); pStmt.setString(4, custId); pStmt.execute(); pStmt.close(); /** try { Statement stmt = actionConnection.createStatement(); ResultSet rSet = stmt.executeQuery("SELECT * FROM ORD WHERE ID = " + seqVal); int i = 0; while (rSet.next()) i++; if (verbose) System.out.println(i + " record found for " + seqVal); rSet.close(); stmt.close(); } catch (SQLException e) { System.err.println("Error : " + e.toString()); } */ doc = new XMLDocument(); Element elmt = doc.createElement("operationResult"); Attr attr = doc.createAttribute("opType"); attr.setValue("insert"); elmt.setAttributeNode(attr); attr = doc.createAttribute("Step"); attr.setValue(entity); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue("About to insert your Order for " + qty + " item(s)"); Element nextElmt = doc.createElement("nextStep"); elmt.appendChild(nextElmt); attr = doc.createAttribute("Label"); attr.setValue("Go on"); nextElmt.setAttributeNode(attr); attr = doc.createAttribute("Action"); nextElmt.setAttributeNode(attr); attr.setValue("placeOrder.xsql"); Element pList = doc.createElement("prmList"); nextElmt.appendChild(pList); // viewobject Element prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("entity"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue("LineItem"); prm.setAttributeNode(attr); // custId prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("custId"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue(custId); prm.setAttributeNode(attr); // prodId prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("prodId"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue(prodId); prm.setAttributeNode(attr); // qty prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("qty"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue(qty); prm.setAttributeNode(attr); // ordId prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("ordId"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue(seqVal); prm.setAttributeNode(attr); nextElmt = doc.createElement("nextStep"); elmt.appendChild(nextElmt); attr = doc.createAttribute("Label"); attr.setValue("Give up"); nextElmt.setAttributeNode(attr); attr = doc.createAttribute("Action"); nextElmt.setAttributeNode(attr); attr.setValue("placeOrder.xsql"); pList = doc.createElement("prmList"); nextElmt.appendChild(pList); // viewobject prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("operation"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue("rollback"); prm.setAttributeNode(attr); } catch (Exception e) { doc = new XMLDocument(); Element elmt = doc.createElement("operationProblem"); Attr attr = doc.createAttribute("opType"); attr.setValue("insert"); elmt.setAttributeNode(attr); attr = doc.createAttribute("Step"); attr.setValue(entity); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue(e.toString()); if (verbose) System.out.println("Error : " + e.toString()); Element prm = doc.createElement("parameters"); elmt.appendChild(prm); // ID Element prmVal = doc.createElement("ID"); prm.appendChild(prmVal); txt = doc.createTextNode("text#"); prmVal.appendChild(txt); txt.setNodeValue(ordId); // CUSTOMER_ID prmVal = doc.createElement("CUSTOMER_ID"); prm.appendChild(prmVal); txt = doc.createTextNode("text#"); prmVal.appendChild(txt); txt.setNodeValue(custId); } } return doc; } private Document insertInLine() { Document doc = null; if (custId == null || custId.length() == 0 || qty == null || qty.length() == 0 || prodId == null || prodId.length() == 0 || ordId == null || ordId.length() == 0) { doc = new XMLDocument(); Element elmt = doc.createElement("operationProblem"); Attr attr = doc.createAttribute("opType"); attr.setValue("lineInsert"); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue("Some element(s) missing for line insert (" + ((custId == null || custId.length() == 0)?"custId ":"") + ((qty == null || qty.length() == 0)?"qty ":"") + ((prodId == null || prodId.length() == 0)?"prodId ":"") + ((ordId == null || ordId.length() == 0)?"ordId ":"") +")" ); Element subElmt = doc.createElement("custId"); elmt.appendChild(subElmt); txt = doc.createTextNode("text#"); subElmt.appendChild(txt); txt.setNodeValue(custId); subElmt = doc.createElement("qty"); elmt.appendChild(subElmt); txt = doc.createTextNode("text#"); subElmt.appendChild(txt); txt.setNodeValue(qty); subElmt = doc.createElement("prodId"); elmt.appendChild(subElmt); txt = doc.createTextNode("text#"); subElmt.appendChild(txt); txt.setNodeValue(prodId); subElmt = doc.createElement("ordId"); elmt.appendChild(subElmt); txt = doc.createTextNode("text#"); subElmt.appendChild(txt); txt.setNodeValue(ordId); } else { if (verbose) System.out.println("Inserting line : Ord>" + ordId + ", Prod>" + prodId + ", Qty>" + qty); /** try { Statement stmt = actionConnection.createStatement(); ResultSet rSet = stmt.executeQuery("SELECT * FROM ORD WHERE ID = " + ordId); int i = 0; while (rSet.next()) i++; System.out.println(i + " record found for " + ordId); rSet.close(); stmt.close(); } catch (SQLException e) { System.err.println("Error : " + e.toString()); } */ String cStmt = "insert into line_item values (Line_item_seq.nextVal, ?, ?, ?, 0)"; try { PreparedStatement pStmt = actionConnection.prepareStatement(cStmt); pStmt.setString(1, qty); pStmt.setString(2, prodId); pStmt.setString(3, ordId); pStmt.execute(); pStmt.close(); doc = new XMLDocument(); Element elmt = doc.createElement("operationResult"); Attr attr = doc.createAttribute("opType"); attr.setValue("insert"); elmt.setAttributeNode(attr); attr = doc.createAttribute("Step"); attr.setValue(entity); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue("Insert Successful"); Element nextElmt = doc.createElement("nextStep"); elmt.appendChild(nextElmt); attr = doc.createAttribute("Label"); attr.setValue("Validate"); nextElmt.setAttributeNode(attr); attr = doc.createAttribute("Action"); nextElmt.setAttributeNode(attr); attr.setValue("placeOrder.xsql"); Element pList = doc.createElement("prmList"); nextElmt.appendChild(pList); // operation Element prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("operation"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue("commit"); prm.setAttributeNode(attr); // ordId prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("ordId"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue(ordId); prm.setAttributeNode(attr); nextElmt = doc.createElement("nextStep"); elmt.appendChild(nextElmt); attr = doc.createAttribute("Label"); attr.setValue("Cancel"); nextElmt.setAttributeNode(attr); attr = doc.createAttribute("Action"); nextElmt.setAttributeNode(attr); attr.setValue("placeOrder.xsql"); pList = doc.createElement("prmList"); nextElmt.appendChild(pList); // operation prm = doc.createElement("prm"); pList.appendChild(prm); attr = doc.createAttribute("name"); attr.setValue("operation"); prm.setAttributeNode(attr); attr = doc.createAttribute("value"); attr.setValue("rollback"); prm.setAttributeNode(attr); } catch (Exception e) { if (verbose) System.out.println("Error when inserting " + e.toString()); doc = new XMLDocument(); Element elmt = doc.createElement("operationProblem"); Attr attr = doc.createAttribute("opType"); attr.setValue("insert"); elmt.setAttributeNode(attr); attr = doc.createAttribute("Step"); attr.setValue(entity); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("text#"); elmt.appendChild(txt); txt.setNodeValue(e.toString()); Element prm = doc.createElement("parameters"); elmt.appendChild(prm); // ID Element prmVal = doc.createElement("ORD_ID"); prm.appendChild(prmVal); txt = doc.createTextNode("text#"); prmVal.appendChild(txt); txt.setNodeValue(ordId); // QTY prmVal = doc.createElement("QTY"); prm.appendChild(prmVal); txt = doc.createTextNode("text#"); prmVal.appendChild(txt); txt.setNodeValue(qty); // ITEM_ID prmVal = doc.createElement("ITEM_ID"); prm.appendChild(prmVal); txt = doc.createTextNode("text#"); prmVal.appendChild(txt); txt.setNodeValue(prodId); } } return doc; } private Document doCommit() throws Exception { Document doc = null; actionConnection.commit(); doc = new XMLDocument(); Element elmt = doc.createElement("operationResult"); Attr attr = doc.createAttribute("opType"); attr.setValue("commit"); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("dummy"); elmt.appendChild(txt); txt.setNodeValue("Commit successfull for order #" + ordId + " from " + entity); if (ordId != null && ordId.length() > 0) { // Generate XML Document to send to AQ // Start from Ord with OrdId value - AQWriter aqw = null; aqw = new AQWriter(AppCste.AQuser, AppCste.AQpswd, AppCste.AQDBUrl, "AppOne_QTab", "AppOneMsgQueue"); String doc2send = XMLGen.returnDocument(actionConnection, ordId); // sending XMLDoc in the Queue try { if (verbose) System.out.println("Doc : " + doc2send); if (debugFile) { BufferedWriter bw = new BufferedWriter(new FileWriter("debug.txt")); bw.write("Rows in " + entity); bw.write(doc2send); bw.flush(); bw.close(); } } catch (Exception ex) {} aqw.writeQ(new B2BMessage(MessageHeaders.APP_A, MessageHeaders.APP_B, MessageHeaders.NEW_ORDER, doc2send)); aqw.flushQ(); // Commit ! } return doc; } private Document doRollback() throws Exception { Document doc = null; actionConnection.rollback(); doc = new XMLDocument(); Element elmt = doc.createElement("operationResult"); Attr attr = doc.createAttribute("opType"); attr.setValue("rollback"); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("dummy"); elmt.appendChild(txt); txt.setNodeValue("Rollback successfull"); return doc; } private Document doBegin() throws Exception { Document doc = null; actionConnection.setAutoCommit(false); doc = new XMLDocument(); Element elmt = doc.createElement("operationResult"); Attr attr = doc.createAttribute("opType"); attr.setValue("begin"); elmt.setAttributeNode(attr); doc.appendChild(elmt); Node txt = doc.createTextNode("dummy"); elmt.appendChild(txt); txt.setNodeValue("Begin successfull"); return doc; } private static Connection getConnected(String connURL, String userName, String password) { Connection conn = null; try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); conn = DriverManager.getConnection(connURL, userName, password); conn.setAutoCommit(false); } catch (Exception e) { System.err.println(e); System.exit(1); } return conn; } }
// Copyright (c) 2000 Oracle Corporation package B2BDemo.XSQLActionHandler; /** * Used to maintain the connection context from the XSQL Action Handler. * Also closes the connection when servlet expires. * * @see RetailActionHandler */ import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class SessionHolder implements HttpSessionBindingListener { private Connection c; private java.util.Date d = null; public SessionHolder(Connection conn) { System.out.println("New SessionHandler"); this.c = conn; this.d = new java.util.Date(); } public Connection getConnection() { return this.c; } public java.util.Date getOpenDate() { return this.d; } public void valueBound(HttpSessionBindingEvent event) { System.out.println("\nvalueBound ! " + event.getName() + "\nat " + (new java.util.Date()) + "\nfor " + event.getSession().getId()); } public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("\nvalueUnbound ! " + event.getName() + "\nat " + (new java.util.Date()) + "\nfor " + event.getSession().getId()); event.getSession().removeValue("XSQLActionHandler.connection"); if (this.c != null) { try { this.c.close(); } catch (Exception e) { System.out.println("Problem when closing the connection from " + event.getName() + " for " + event.getSession().getId() + " :\n" + e); } } } }
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|