package uk.co.patrickhaston.wiki;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

public class User 
{
  private String dbUser = "";
  private String dbPassword = "";
  private String dbUrl = "";
  private OracleDataSource ods = null;
  
  public User()
  {
    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 users in the WIKI_USERS_VIEW
  public String getSelect(String selectName, String network_id)
  {
    String select = "<select name='" + selectName + "'>";
    if( network_id == null ) network_id = "X";
    //staffSelect = staffSelect + "<option value='%'>All</option>";
    String SQL = "";

    try
    {
      SQL = "select user_id, username from wiki_users_view"
        + " 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() )
      {
        String id = rs.getString("user_id");
        if( id == null ) id = "Z";
        
        if( network_id.toUpperCase().equals( id.toUpperCase() ) )
        {
          select = select + "<option value='" + id + 
            "' selected >" + rs.getString("username") + "</option>";
        }
        else
        {
          select = select + "<option value='" + id + "'>" +
            rs.getString("username") + "</option>";
        }
      }
      
      select = select + "</select>";
    
      rs.close();
      conn.close();
    }
    catch (Exception e)
    {
      // to do
      return select = select + "<option value='Error'>Error in User.getSelect(): " + e.getMessage() + "</option></select>";
    }

    return select;
  }
  
}