package uk.co.patrickhaston.wiki;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

public class CommentType 
{
  private String dbUser = "";
  private String dbPassword = "";
  private String dbUrl = "";
  private OracleDataSource ods = null; 

  public CommentType()
  {
    Database db = new Database();
    dbUser = db.getDbUser();
    dbPassword = db.getDbPassword();
    dbUrl = db.getDbUrl();
  }

  // Returns an html string containing a select drop-down box
  // of all active comment types in the WIKI_COMMENT_TYPE table
  // excluding TOPIC as that is not used for replies
  public String getSelect(String selectName)
  {
    String select = "<select name='" + selectName + "'>";
    String SQL = "";

    try
    {
      SQL = "select comment_type_code, comment_type_name from wiki_comment_type "
        + "where active_flag = 'Y' "
        + "   and comment_type_code != 'TOPIC' "
        + " order by 2";

      OracleDataSource ods = new OracleDataSource();
      ods.setURL( dbUrl );
      ods.setUser( dbUser );
      ods.setPassword( dbPassword );

      Connection conn = ods.getConnection();

      //Connection conn = snh.getConnection();
      Statement state = conn.createStatement();
      ResultSet rs = state.executeQuery(SQL);
      while( rs.next() )
      {
          select = select + "<option value='" + rs.getString("comment_type_code") + 
            "' >" + rs.getString("comment_type_name") + "</option>";
      }
      
      select = select + "</select>";
      rs.close();
      
      conn.close();

    }
    catch (Exception e)
    {
      // to do
      return select = select + "<option value='Error'>Error in CommentType.getSelect(): " + e.getMessage() + "</option></select>";
    }

    return select;
  }
  
}
