Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
|
B2B XML Application: Step by Step , 32 of 32
The Supplier uses the following scripts:
package B2BDemo.Supplier; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.net.*; import java.sql.*; import oracle.xml.sql.query.*; import oracle.xml.sql.dml.*; import org.w3c.dom.*; import oracle.xml.parser.v2.*; import org.xml.sax.*; import B2BDemo.AQUtil.*; import B2BDemo.*; import B2BDemo.XMLUtil.*; /** * This class implements the Frame suggesting to ship the order.' * * @see SupplierWatcher in the same package. * @author Olivier LE DIOURIS - Partner Technical Services - Oracle Copr. */ public class SupplierFrame extends JFrame { private BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); private static boolean stepByStep = false; private static boolean verbose = false; private static Integer pause = null; private XMLFrame frame = null; AQReader aqr; XMLtoDMLv2 x2d = null; String userName = "supplier"; String password = "supplier"; String url = null; String currOrdId = ""; DOMParser parser = new DOMParser(); AQWriter aqw = null; BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); BorderLayout borderLayout2 = new BorderLayout(); JPanel southPanel = new JPanel(); JButton shipButton = new JButton(); JPanel centerPanel = new JPanel(); JLabel ordMessage = new JLabel(); /** * Constructs a new instance. */ public SupplierFrame(boolean v, boolean s, Integer p, XMLFrame f, String url) { super(); this.verbose = v; this.stepByStep = s; this.pause = p; this.frame = f; this.url = url; try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } /** * Initializes the state of this instance. */ private void jbInit() throws Exception { this.getContentPane().setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); shipButton.setText("Ship Order"); shipButton.setEnabled(false); shipButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { shipButton_actionPerformed(e); } }); ordMessage.setText("Waiting for Orders"); ordMessage.setFont(new Font("Dialog", 1, 20)); jPanel1.setLayout(borderLayout2); this.setTitle("Supplier Watcher"); this.getContentPane().add(jPanel1, BorderLayout.CENTER); jPanel1.add(southPanel, BorderLayout.SOUTH); southPanel.add(shipButton, null); jPanel1.add(centerPanel, BorderLayout.CENTER); centerPanel.add(ordMessage, null); } public void enterTheLoop() { // Initialize AQ reader aqr = new AQReader(AppCste.AQuser, AppCste.AQpswd, AppCste.AQDBUrl, "AppTwo_QTab", "AppTwoMsgQueue"); // Initialize XSL Transformer x2d = new XMLtoDMLv2(userName, password, url); // Initialize the AQ Writer aqw = new AQWriter(AppCste.AQuser, AppCste.AQpswd, AppCste.AQDBUrl, "AppThree_QTab", "AppThreeMsgQueue"); boolean go = true; while (go) { String ordIdValue = ""; String custIdValue = ""; B2BMessage sm = aqr.readQ(); if (verbose) System.out.println("Recieved\nFrom > " + sm.getFrom() + "\nTo > " + sm.getTo() + "\nType > " + sm.getType() + "\nContent >\n" + sm.getContent()); else System.out.println("Recieved\nFrom > " + sm.getFrom() + "\nTo > " + sm.getTo() + "\nType > " + sm.getType()); String xmlDoc = sm.getContent(); if (xmlDoc != null && xmlDoc.length() > 0) { try { this.frame.setXMLDocument(sm.getContent()); } catch (Exception e) { e.printStackTrace(); } } if (stepByStep) { if (pause != null) { System.out.println("Waiting for " + pause.longValue() + " milliseconds"); try { Thread.sleep(pause.longValue()); } catch (InterruptedException e) {} } else try { String s = _userInput("[Hit return to continue]"); } catch (Exception e) {} } if (sm.getType().equals(MessageHeaders.EXIT)) go = false; else { System.out.println("Inserting"); TableInDocument d[] = new TableInDocument[2]; d[0] = new TableInDocument("ROWSET", "ROW", "ORD"); d[1] = new TableInDocument("ITEMS", "ITEM_ROW", "LINE_ITEM"); try { String XMLDoc = sm.getContent(); x2d.insertFromXML(d, XMLDoc); System.out.println("Document processed."); // We want to read elements parser.setValidationMode(false); try { parser.parse(new InputSource(new ByteArrayInputStream(XMLDoc.getBytes()))); XMLDocument xml = parser.getDocument(); XMLElement elmt = (XMLElement)xml.getDocumentElement(); NodeList nl = elmt.getElementsByTagName("ID"); // ORD ID for (int i=0; i<nl.getLength(); i++) { XMLElement ordId = (XMLElement)nl.item(i); XMLNode theText = (XMLNode)ordId.getFirstChild(); ordIdValue = theText.getNodeValue(); currOrdId = ordIdValue; break; // Just the first one !!! } nl = elmt.getElementsByTagName("CUSTOMER_ID"); // CUSTOMER ID for (int i=0; i<nl.getLength(); i++) { XMLElement ordId = (XMLElement)nl.item(i); XMLNode theText = (XMLNode)ordId.getFirstChild(); custIdValue = theText.getNodeValue(); } } catch (SAXParseException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } } catch (Exception e) { System.err.println("Ooops:\n" + e); } this.shipButton.setEnabled(true); String custName = ""; try { PreparedStatement pStmt = x2d.getConnection().prepareStatement("Select C.NAME from CUSTOMER C where C.ID = ?"); pStmt.setString(1, custIdValue); ResultSet rSet = pStmt.executeQuery(); while (rSet.next()) custName = rSet.getString(1); rSet.close(); pStmt.close(); } catch (SQLException e) {} this.ordMessage.setText("Order [" + ordIdValue + "] to process for [" + custName + "]"); JOptionPane.showMessageDialog(this, "New Order Pending !", "Wake Up !", JOptionPane.INFORMATION_MESSAGE); } } frame.setVisible(false); } void shipButton_actionPerformed(ActionEvent e) { // Send message String doc2send = "<SHIP>" + currOrdId + "</SHIP>"; // sending XMLDoc in the Queue aqw.writeQ(new B2BMessage(MessageHeaders.APP_B, MessageHeaders.APP_A, MessageHeaders.UPDATE_ORDER, doc2send)); aqw.flushQ(); // Commit ! // Disable Button this.shipButton.setEnabled(false); // display wait message this.ordMessage.setText("Waiting for orders..."); } private String _userInput(String prompt) throws Exception { String retString; System.out.print(prompt); try { retString = stdin.readLine(); } catch (Exception e) { System.out.println(e); throw(e); } return retString; } }
package B2BDemo.Supplier; import java.awt.*; import java.awt.event.*; import javax.swing.*; //import oracle.bali.ewt.border.UIBorderFactory; //import oracle.bali.ewt.olaf.OracleLookAndFeel; import B2BDemo.XMLUtil.*; /** * This class implements the agent waiting on the queue where the orders are delivered. * When a message is read, the agent "wakes up" and suggests to ship the order. * Shipping the order will then fire a new B2B process to update the status of * the order in the Retailer database. * * @author Olivier LE DIOURIS - Partner Technical Services - Oracle Copr. */ public class SupplierWatcher { private static boolean stepByStep = false; private static boolean verbose = false; private static Integer pauseTime = null; private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; // Default value /** * Constructor */ public SupplierWatcher() { XMLFrame xmlFrame = new XMLFrame("Supplier"); /* try { OracleLookAndFeel.setColorScheme(Color.cyan); // OracleLookAndFeel.setColorScheme("Titanium"); UIManager.setLookAndFeel(new OracleLookAndFeel()); SwingUtilities.updateComponentTreeUI(xmlFrame); xmlFrame.setBackground(UIManager.getColor("darkIntensity")); } catch (Exception e) { System.err.println("Exception for Oracle Look and Feel:" + e ); } */ //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = xmlFrame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } /** if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } */ frameSize.width = screenSize.width / 3; // xmlFrame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2); xmlFrame.setLocation((2 * frameSize.width), (screenSize.height - frameSize.height)/2); // xmlFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); xmlFrame.setVisible(true); SupplierFrame frame = new SupplierFrame(verbose, stepByStep, pauseTime, xmlFrame, url); /* try { OracleLookAndFeel.setColorScheme(Color.cyan); // OracleLookAndFeel.setColorScheme("Titanium"); UIManager.setLookAndFeel(new OracleLookAndFeel()); SwingUtilities.updateComponentTreeUI(frame); frame.setBackground(UIManager.getColor("darkIntensity")); } catch (Exception e) { System.err.println("Exception for Oracle Look and Feel:" + e ); } */ //Center the window screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2); // frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setVisible(true); frame.enterTheLoop(); frame.setVisible(false); xmlFrame.setVisible(false); System.exit(1); } private static void setRunPrm(String[] prm) { for (int i=0; i<prm.length; i++) { if (prm[i].toLowerCase().startsWith("-verbose")) verbose = isolatePrmValue(prm[i], "-verbose"); else if (prm[i].toLowerCase().startsWith("-help")) { System.out.println("Usage iB2BDemo.Supplier.MessageBroker"); System.out.println("\tparameters can be -dbURL -verbose, -step, -help"); System.out.println("\tdbURL contains a string like jdbc:oracle:thin:@localhost:1521:ORCL"); System.out.println("\tparameters values can be (except for -help):"); System.out.println("\t\tnone - equivalent to 'y'"); System.out.println("\t\ty"); System.out.println("\t\ttrue - equivalent to 'y'"); System.out.println("\t\tn"); System.out.println("\t\tfalse - equivalent to 'n'"); System.out.println("\t\t-step can take a value in milliseconds"); System.exit(0); } else if (prm[i].toLowerCase().startsWith("-step")) { String s = getPrmValue(prm[i], "-step"); try { pauseTime = new Integer(s); System.out.println("Timeout " + pauseTime); stepByStep = true; } catch (NumberFormatException nfe) { pauseTime = null; if (s.toUpperCase().equals("Y") || s.toUpperCase().equals("TRUE")) stepByStep = true; else stepByStep = false; } } else if (prm[i].toLowerCase().startsWith("-dburl")) { url = getPrmValue(prm[i], "-dbURL"); } else System.err.println("Unknown parameter [" + prm[i] + "], ignored."); } } private static boolean isolatePrmValue(String s, String p) { boolean ret = true; if (s.length() > (p.length() + 1)) // +1 : "=" { if (s.indexOf("=") > -1) { String val = s.substring(s.indexOf("=") + 1); if (val.toUpperCase().equals("Y") || val.toUpperCase().equals("TRUE")) ret = true; else if (val.toUpperCase().equals("N") || val.toUpperCase().equals("FALSE")) ret = false; else { System.err.println("Unrecognized value for " + p + ", set to y"); ret = true; } } } return ret; } private static String getPrmValue(String s, String p) { String ret = ""; if (s.length() > (p.length() + 1)) // +1 : "=" { if (s.indexOf("=") > -1) { ret = s.substring(s.indexOf("=") + 1); } } return ret; } /** * main * @param args */ public static void main(String[] args) { if (args.length > 0) setRunPrm(args); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } new SupplierWatcher(); } }
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|