package examples.dbkona;

import java.util.Properties;
import weblogic.common.*;
import weblogic.common.internal.*;
import weblogic.db.jdbc.*;
import weblogic.event.actions.*;
import weblogic.event.common.*;
import weblogic.event.evaluators.*;
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% EventfulListClient.java</b></font>
 *<p>	
 *
 *<li>To run, execute the <font face="Courier New"
 *size=-1>EventfulListClient</font> first from the
 * command line by executing the following command in your development
 *shell:
 *
 *<p>
 *
 *<pre><font face="Courier New"><b>$ java examples.dbkona.EventfulListClient t3://<i>WebLogicHost:port
 *  login password server</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.
 *
 *
 *<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 <a
 *href=examples.dbkona.EventfulList.html>EventfulList</a>
 *application. It takes the same arguments as EventfulListClient plus an
 *employee name from a record in the "emp" table, to demonstrate 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 EventfulListClient implements ActionDef, Runnable {
  
  static T3Client  t3              = null;
  static EventfulTableDataSet atds = null;
  static int       totalevents     = 10;
  static Object    lock            = new Object();
  
  T3ServicesDef services = null;
  
  /**
   * Saves the services object.
   */
  public void setServices(T3ServicesDef services) {
    this.services = services;
  }

  /**
   * Implements part of the WebLogic Event interface.
   */
  public void registerInit(ParamSet p) {
    System.out.println("Registered ParamSet:\n[" + p + "]");
  }

  /**
   * Implements part of the WebLogic Event interface.
   */
  public void action(EventMessageDef ev) {
    try {
      ParamSet ps=ev.getParameters();
      TimeTrace tt=(TimeTrace)ps.getParam("time").asObject();
      tt.snap("Received");
      tt.print();
      
      Record rec = atds.lookupRecord(ps.getParam("row").asString()); 
      System.out.println(rec.getValue("empno").asString() + " - " + 
			 rec.getValue("ename").asString()  + " - " + 
			 rec.getValue("hiredate").asString());
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  
  static ParamSet h = new ParamSet();

  /**
   * Calls the example.
   */
  public static void main(String argv[]) {
    
    // Check the number of command-line arguments
    if (argv.length != 4) {
      System.out.println("Usage: java examples.dbkona.EventfulListClient " +
                         "WebLogicURL user password server");
      System.out.println("Example: java examples.dbkona.EventfulListClient " +
                         "t3://localhost:7001 scott tiger DEMO");
      return;
    }
    
    String t3url   = argv[0];
    String user    = argv[1];
    String pass    = argv[2];
    String server  = argv[3];
    
    Connection conn=null;
    
    try {
      t3 = new T3Client(t3url);  // required on client
      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.setEventfulObject(new EventfulListClient());
      atds.fetchRecords();
      
      System.out.println(atds.schema());
      Thread thread=new Thread(new EventfulListClient());
      thread.start();
      
      synchronized(lock) {
	lock.wait();
      }
      
    } 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) {}
    }
  }
  
  /**
   * Runs the example.
   */
  public void run() {
    try {
      for (int i = 0; i < atds.size(); i++) {
	Record 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();
    }
  }
}



