package examples.dbkona;

import java.util.Properties;
import weblogic.common.*;
import weblogic.db.jdbc.*;
import weblogic.event.actions.*;
import java.sql.*;

/**
 *EventfulList and EventfulListClient are a pair of applications that
 * run together to demonstrate how dbKona applications can cooperate
 * to share data and changes to data in a rapid way, using the WebLogic
 * Events built into the EventfulTableDataSet.
 * <p>
 * These examples require an Oracle DBMS and need a clean copy of
 *the Oracle "emp" table (in the DEMO database).
 *
 * <p>
 *<h3>To set up this example:</h3>
 *<ol>
 *<li>Set up your development shell as described in 
 *<a href=../../techstart/environment.html>Setting up your environment</a>.
 *<p>
 *<li> Change connection parameters to correspond to your Oracle configuration.
 * If you need more help, check the section on connecting
 *to a database in the Developers Guide, <a
 *href../../classdocs/API_joci.html>Using WebLogic jDriver for Oracle</a>.
 *<p>
 *
 *<li>Compile this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ javac -d %CLIENT_CLASSES% EventfulList.java</b></font>
 *<p>	
 *
 *<li>To run, first execute the <font face="Courier New" size=-1><a
 *href=examples.dbkona.EventfulListClient.html>EventfulListClient</a></font>
 *from a command line.
 *
 *<p>The application displays some data, and waits for changes made to
 *the eventful data set.
 *
 * <p>
 * <li> At a second command line, start the EventfulList
 *application with the following command: 
 *
 *<pre><font face="Courier New"><b>$ java examples.dbkona.EventfulList t3://<i>WebLogicHost:port
 *  login password server ename</i></b></font></pre>
 *
 *<p>Where <i>WebLogicHost:port</i> is the hostname and port number of
 *your WebLogic Server and <i>port, login, password and server </i> are
 *connection properties for your Oracle DBMS instance. <i>ename</i> is a
 *value in the ename column of the <font face="Courier New"
 *size=-1>emp</font> table.
 *
 *<p>
 *
 *The employee name from a record in the "emp" table, demonstrates that
 *data is passed. You should see the first application report that it
 *received the name from the EventfulList. You can repeatedly execute
 *EventfulList with different names.
 *</ol>
 *
 * @author         Copyright (c) 1997-2000 by BEA Systems, Inc. All Rights Reserved.
 * 
 */
public class EventfulList {

  /**
   * Calls the example.
   */
  public static void main(String argv[]) {
    
    // Check the number of command-line arguments
    if (argv.length != 5) {
      System.out.println("Usage: java examples.dbkona.EventfulList " +
			 "WebLogicURL user password server ename");
      System.out.println("Example: java examples.dbkona.EventfulList " +
			 "t3://localhost:7001 scott tiger DEMO ralph");
      return;	     
    }
    
    String t3url   = argv[0];
    String user    = argv[1];
    String pass    = argv[2];
    String server  = argv[3];
    String ename   = argv[4];
    
    // Open a WebLogic Server connection
    T3Client t3 = null;
    EventfulTableDataSet atds = null;
    Connection conn = null;
    
    try {
      t3=new T3Client(t3url);
      t3.connect();
      java.util.Properties dbprops = new Properties();
      dbprops.put("user",                 user);
      dbprops.put("password",             pass);
      dbprops.put("server",               server);
      
      java.util.Properties t3props = new Properties();
      t3props.put("weblogic.t3",          t3);
      t3props.put("weblogic.t3.dbprops",  dbprops);
      t3props.put("weblogic.t3.driver",   "weblogic.jdbc.oci.Driver");
      t3props.put("weblogic.t3.url",      "jdbc:weblogic:oracle");
      
      Class.forName("weblogic.jdbc.t3.Driver").newInstance();
      conn = DriverManager.getConnection("jdbc:weblogic:t3", t3props);
      
      // Create a primary key def
      KeyDef keydef = new KeyDef();
      keydef.addAttrib("empno");
      
      // EventfulTableDataSet of emp
      atds = new EventfulTableDataSet(t3, conn, "emp", keydef);
      atds.fetchRecords();
      
      Record rec = atds.getRecord(1);
      rec.setValue(2, ename);
      
      atds.save();
      
      atds.fetchRecords();
      
      System.out.println(atds.schema());
      
      for (int i = 0; i < atds.size(); i++) {
	rec = atds.getRecord(i);
	System.out.println(rec.getValue("empno").asString() + " - " + 
			   rec.getValue("ename").asString()  + " - " + 
			   rec.getValue("hiredate").asString());
      }
    } catch(Exception e) {
      System.err.println("Trouble while executing example");
      e.printStackTrace();
      return;
    } finally {
      // You should always close DataSets, ResultSets, and Statements
      // when you have finished working with them.
      try {
	atds.close();
      } catch (Exception e) {}

      // Finally, close the JDBC connection 
      try { 
	conn.close();
      } catch (Exception e) {}

      // and disconnect from the server
      try {
	t3.disconnect();
      } catch (Exception e) {}
    }
  }
}
