Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

B2B XML Application: Step by Step , 30 of 32

## Retailer Scripts

The Retailer uses the following scripts:

- Java Example 17: Retailer Waits for Status Update Sent from Supplier -- UpdateMaster.java

### Java Example 17: Retailer Waits for Status Update Sent from Supplier -- UpdateMaster.java

    package B2BDemo.Retailer;
    /**
     *
     * This class implements the component waiting on the retailer side for
     * the status update made by the Supplier after shipping.
     * The recieved document is parsed and its content is used to make
     * the convenient update in the database.
     *
     * @author Olivier LE DIOURIS - Partner Technical Services - Oracle Copr.
     *
     */
    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.*;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    //import oracle.bali.ewt.border.UIBorderFactory;
    //import oracle.bali.ewt.olaf.OracleLookAndFeel;

    public class UpdateMaster extends Object
    {
      private BufferedReader stdin = new BufferedReader(new
    InputStreamReader(System.in));

      private static boolean stepByStep = false;
      private static boolean verbose    = false;
      private static Integer pauseTime  = null;

      AQReader aqr;

      private static final String userName = "retailer";
      private static final String password = "retailer";
      private static String url      = "jdbc:oracle:thin:@localhost:1521:ORCL"; //
    This is the default value !
      private static Connection conn = null;

      String currOrdId = "";

      DOMParser parser = new DOMParser();
      /**
       * Constructor
       */
      public UpdateMaster()
      {
        XMLFrame frame = new XMLFrame("Retailer");
        /**
        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
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height)
        {
          frameSize.height = screenSize.height;
        }
        /**
        if (frameSize.width > screenSize.width)
        {
          frameSize.width = screenSize.width;
        }
        */
        frameSize.width = screenSize.width / 3;

    //  frame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height
    - frameSize.height)/2);
        frame.setLocation(0, (screenSize.height - frameSize.height)/2);
    //  frame.addWindowListener(new WindowAdapter() { public void
    windowClosing(WindowEvent e) { System.exit(0); } });
        frame.setVisible(true);

          // Initialize AQ reader
        aqr = new AQReader(AppCste.AQuser,
                           AppCste.AQpswd,
                           AppCste.AQDBUrl,
                           "AppFour_QTab",
                           "AppFourMsgQueue");
        boolean go = true;
        while (go)
        {
          String ordIdValue = "";
          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 { frame.setXMLDocument(sm.getContent()); }
            catch (Exception e)
            {
              e.printStackTrace();
            }
          }
          if (stepByStep)
          {
            if (pauseTime != null)
            {
              System.out.println("Waiting for " + pauseTime.longValue() + "
    milliseconds");
              try { Thread.sleep(pauseTime.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("Updating");
            try
            {
              parser.parse(new InputSource(new
    ByteArrayInputStream(sm.getContent().getBytes())));
              XMLDocument xml = parser.getDocument();
              XMLElement elmt = (XMLElement)xml.getDocumentElement();
              NodeList nl = elmt.getElementsByTagName("SHIP"); // ORD ID
              for (int i=0; i<nl.getLength(); i++)
              {
                XMLElement ordId = (XMLElement)nl.item(i);
                XMLNode theText = (XMLNode)ordId.getFirstChild();
                currOrdId = theText.getNodeValue();
                System.out.println("Gonna update " + currOrdId);
                try
                {
                  if (conn == null)
                    getConnected(url, userName, password);
                  String strStmt = "update ORD set STATUS = 'Shipped' where ID = ?";
                  PreparedStatement pStmt = conn.prepareStatement(strStmt);
                  pStmt.setString(1, currOrdId);
                  pStmt.execute();
                  conn.commit();
                  pStmt.close();
                  System.out.println("Done !");
                }
                catch (SQLException e)
                {
                  System.out.println("Pb updating the ORD\n" + e.toString());
                }

              }
            }
            catch (SAXParseException e)
            {
              System.out.println(e.getMessage());
            }
            catch (SAXException e)
            {
              System.out.println(e.getMessage());
            }
            catch (Exception e)
            {
              System.out.println(e.getMessage());
            }
          }
        }
        frame.setVisible(false);
        System.exit(0);
      }

      private static void getConnected(String connURL,
                                       String userName,
                                       String password)
      {
        try
        {
          DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
          conn = DriverManager.getConnection(connURL, userName, password);
        }
        catch (Exception e)
        {
          System.err.println(e);
          System.exit(1);
        }
      }

      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;
      }

      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 is:");
            System.out.println("\tjava B2BDemo.Retailer.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);
        UpdateMaster updateMaster = new UpdateMaster();
      }
    }

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index