// jdbcCommand.java
// $Id: jdbcCommand.java,v 1.5 1997/07/30 14:01:05 ylafon Exp $  
// (c) COPYRIGHT MIT and INRIA, 1997.
// Please first read the full copyright statement in file COPYRIGHT.html

package w3c.jigsaw.ssi.jdbc;

import java.util.*;
import java.sql.*;

import w3c.www.http.*;
import w3c.jigsaw.http.*;
import w3c.jigsaw.resources.*;
import w3c.util.*;
import w3c.jigsaw.ssi.*;

/**
 * Implementation of the SSI <code>jdbc</code> command.  
 */
public class jdbcCommand implements Command {
  private final static String  NAME  = "jdbc";
  private final static boolean debug = true;
    
  private static final String keys[] = {
    "select",
    "url",
    "driver",
    "user",
    "password",
    "name",
    "column",
    "next"
  };

  protected boolean hasmorevalue = true;

  protected Connection getConnection(String driver, 
				     String url, 
				     String user,
				     String password) 
  {
    try {
      Class.forName(driver);
      return DriverManager.getConnection(url, user, password);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  protected ResultSet performSelect(Connection conn, String cmd) {
    try {
      Statement smt = conn.createStatement();
      ResultSet set = smt.executeQuery(cmd);
      hasmorevalue = true;
      return set;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  protected void addSet(Dictionary d, String name, ResultSet set) {
    d.put(getClass().getName()+"."+name, set);
  }

  protected ResultSet getSet(Dictionary d, String name) {
    return (ResultSet) d.get(getClass().getName()+"."+name);
  }

  public String getName() {
    return NAME;
  }

  public String getValue(Dictionary variables, String var) {
      ResultSet set = getSet(variables, var);
      if (!hasmorevalue)
	return "empty";
      else return "not-empty";
  }

  public Reply execute(SSIResource resource
		       , Request request
		       , ArrayDictionary parameters
		       , Dictionary variables) {
    Object values[] = parameters.getMany(keys);
    String select   = (String) values[0];
    String url      = (String) values[1];
    String driver   = (String) values[2];
    String user     = (String) values[3];
    String password = (String) values[4];
    String name     = (String) values[5];
    String column   = (String) values[6];
    String next     = (String) values[7];
    String text     = null;

    if ( select != null ) {
      // user and password are optionnals, they can be null.
      Connection conn = getConnection(driver, url, user, password);
      if ( conn != null )
	addSet(variables, name, performSelect(conn, select));
    } else if (column != null) {
      ResultSet set = getSet(variables, name);
      try {
	if ( set != null ) 
	  text = set.getObject(Integer.parseInt(column)).toString();
      } catch (Exception ex) {
	ex.printStackTrace();
      }
    } else if ( next != null ) {
      ResultSet set = getSet(variables, name);
      if ( set != null ) {
	try {
	  hasmorevalue =  set.next();
	} catch (Exception ex) {
	  ex.printStackTrace();
	}
      }
    }

    // We are NOT doing notMod hack here (tricky and useless ?)
    Reply reply = resource.createCommandReply(request, HTTP.OK);
    if ( text != null )
      reply.setContent(text);
    return reply;
	
  }
}
