package examples.event.clockquery;

import weblogic.common.*;
import weblogic.event.actions.ActionDef;
import weblogic.event.common.*;
import weblogic.db.jdbc.*;
import weblogic.workspace.common.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;

/**
 * This example shows a time-triggered action that periodically makes
 * a query to a DBMS and stores the current results in a T3 Workspace
 * for common access to a set of clients. It works with the Register
 * class in this directory.
 * <p>
 * This example is set up for use with a Cloudscape database. Your
 * database may require different parameters in the ParamSet passed to
 * the <tt>registerInit()</tt> method. Check the Developers Guide for
 * your JDBC driver for information about required environment
 * variables and parameters for connecting.
 *
 * @author Copyright (c) 1997-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class QueryOnTime implements ActionDef {
  
  // Initializes the services variable
  T3ServicesDef services = null;
  // Initializes the DBMS username
  String user = "";
  // Initializes the DBMS password
  String passwd = "";
  // Initializes the DBMS database name
  String dbserver = "";
  
  /**
   * Sets the access to services within the WebLogic 
   * Server for a WebLogic client.
   *
   * @param services          T3ServicesDef object
   */ 
  public void setServices(T3ServicesDef services) {
    this.services = services;
  }
  
  /**
   * Registers initialization parameters for the
   * event message. The parameters of interest
   * are the username, password, and servername
   * for connecting to the DBMS.
   *
   * @param params            ParamSet object
   */
  public void registerInit(ParamSet params) {
    user     = params.getValue("user"     ).toString();
    passwd   = params.getValue("passwd"   ).toString();
    dbserver = params.getValue("dbserver" ).toString();
  }
  
  /**
   * The action method contains the actual work for this
   * class. In this method, we set up a java.util.Properties
   * object and create a JDBC Connection with the appropriate
   * JDBC driver and the Properties object. Then we execute
   * an SQL statement and create a QueryDataSet with the
   * results. We create a ParamSet and add the QueryDataSet
   * to it with the key "dataset," and then we store the
   * QueryDataSet in the server workspace, which gives access
   * to all clients with workspaces within that server.
   * <p>
   * This action method is called each time an event that
   * evaluates to true is received by an interested
   * registration. When you register an interest in this
   * topic with the Register class also in this directory,
   * this action is called according to a regular time
   * schedule.
   *
   * @param ev                EventMessage
   */
  public void action(EventMessageDef ev) {

    try {
      Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    java.util.Properties props = new java.util.Properties();
    props.put("user",     user);
    props.put("password", passwd);
    props.put("server",   dbserver);

    java.sql.Connection conn = null;
    weblogic.db.jdbc.QueryDataSet qds = null;

    try {
      conn = DriverManager.getConnection("jdbc:cloudscape:demo;create=true", props);
      Statement stmt = conn.createStatement();
      
      stmt.execute("insert into webEventTest (Line, Now) " +
                   "values ('row inserted on ', CURRENT_TIMESTAMP)");
      
      qds = new weblogic.db.jdbc.QueryDataSet(conn, "select * from webEventTest");
      qds.fetchRecords();
      
      ParamSet ps = new ParamSet();
      ps.setParam("dataset", qds);

      
      services.workspace().store("myworkspace", ps, WorkspaceDef.SCOPE_SERVER);

      qds.close();
    } catch (SQLException sqle) {
      System.err.println("Trouble interacting with database");
      sqle.printStackTrace();
      return;
    } catch (DataSetException dse) {
      System.err.println("Trouble using dataset");
      dse.printStackTrace();
      return;
    } catch (ParamSetException pse) {
      System.err.println("Trouble using paramset");
      pse.printStackTrace();
      return;   
    } catch (T3Exception t3e) {
      System.err.println("Trouble with client or server infrastructure");
      t3e.printStackTrace();
      return;
    } finally {
      try {
	conn.close();
      } catch (Exception e) {}
    }
  }

}











