package examples.t3client;

import weblogic.common.*;
import weblogic.remote.common.*;
import weblogic.workspace.common.*;
import java.util.*;
import java.io.*;
import java.sql.*;

/**
 * The StartupQuery example illustrates three different facets of the
 * WebLogic Server architecture:
 * <ul>
 * <li><i>Startup classes</i> are automatically loaded
 * and executed when the WebLogic Server starts up. You can use startup
 * classes to perform functions that need to run when a
 * WebLogic-based application is launched. In this example we
 * perform JDBC queries that will be used to populate listboxes
 * on GUI.
 * <p>
 * <li><i>Workspaces</i> are hierarchical storage areas in
 * WebLogic that can be shared among WebLogic components,
 * such as WebLogic Event services action and evaluate methods, WebLogic 
 * Remote T3Servlets, and T3Startup classes. In this example, we use a
 * Workspace to store the results of a JDBC query.
 * <p>
 * <li><i>T3Servlets</i> are methods that can be invoked by
 * T3Clients within the WebLogic Server. In this example, we use a
 * T3Servlet to retrieve the results of the JDBC query from the
 * Workspace.
 * </ul>
 * <p>
 * This example requres an Oracle database instance. You'll need to
 * register this class as a startup class in your <tt>weblogic.properties</tt>
 * file, as detailed in the description of the <tt>startup()</tt>
 * method.
 *
 * @author Copyright (c)1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class StartupQuery implements T3ServletDef, T3StartupDef {
  
  /**
   * The <tt>startup()</tt> method is called at WebLogic startup,
   * as a result of placing the following lines in the
   * <tt>weblogic.properties</tt> file:
   * <pre>weblogic.system.startupClass.doquery=examples.t3client.StartupQuery
   *    weblogic.system.startupArgs.doquery=\
   *    query=select * from emp,\
   *    db=jdbc:weblogic:pool:demoPool</pre>
   * <p>
   * where "db" is the URL of your database pool that
   * contains a table that you include in the arg "query".
   * <p>
   * When startup is invoked as a result of these two lines, the
   * "name" argument will be "doquery" (from the left-hand of the
   * property), and the args hashtable will contain the name-value
   * pair "query", "select * from emp".
   */
  public String startup(String name, Hashtable args) throws Exception {
    // Grab startup arguments
    String query    = (String)args.get("query");
    String db   = (String)args.get("db");
    
    Class.forName("weblogic.jdbc.pool.Driver").newInstance();
    Connection conn = 
      DriverManager.getConnection(db);
    
    weblogic.db.jdbc.QueryDataSet qds = 
      new weblogic.db.jdbc.QueryDataSet(conn, query);
    qds.fetchRecords();
    
    ParamSet ps = new ParamSet();
    ps.setParam("name",    name);
    ps.setParam("query",   query);
    ps.setParam("dataset", qds);
    
    services.workspace().store(name, ps, WorkspaceDef.SCOPE_SERVER);
    
    qds.close();
    conn.close();
    
    return name + " started";
  }
  
  private T3ServicesDef services;
  
  /**
   * This method implements the WebLogic T3Servlet interface.
   * <p>
   * The <tt>setServices()</tt> method is called to pass to the
   * T3Servlet the handle of an object that can be used to access WebLogic
   * services. This makes another reference to the services object so
   * it can be used later.
   *
   * @param services          Services stub
   */
  public void setServices(T3ServicesDef services) {
    this.services = services;
  }
  
  /**
   * This method implements the WebLogic T3Servlet interface.
   * <p>
   * The <tt>declareParams()</tt> method is called to specify the
   * parameters that this T3Servlet will accept. WebLogic
   * calls this method when the server-side object is
   * instantiated.
   *
   * @param ps                ParamSet
   * @exception               weblogic.common.ParamSetException
   *                          if there is an error in the ParamSet
   */
  public void declareParams(ParamSet ps) throws ParamSetException {
    ps.declareParam("name",    ParamTypes.STRING);
    ps.declareParam("dataset", ParamTypes.OBJECT);
  }
  
  /**
   * This method implements the WebLogic T3Servlet interface.
   * <p>
   * The <tt>execute()</tt> method is called when the client
   * requests the invocation of this servlet. the ParamSet
   * contains the arguments for the <tt>execute()</tt> method.
   *
   * @param ps                ParamSet
   * @exception               java.lang.Exception
   *                          if there is an error in execution
   */
  public void execute(ParamSet ps) throws Exception {
    String name = ps.getParam("name").asString();
    
    // Fetch the object out of the server scoped workspace
    ParamSet wsps = (ParamSet)
      services.workspace().fetch(name, WorkspaceDef.SCOPE_SERVER);
    
    // This takes the corresponding items out of <tt>wsps</tt> and
    // puts them into <tt>ps</tt>.
    ps.setParams(wsps);
  }
}

