import java.io.*;
import java.sql.*;
import java.net.*;
import java.math.*;
import java.util.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This servlet will display a list of current Program Profiles
**/
public class currentServlet extends HttpServlet 
  
{
    private String fileName = "current.htm"; 
    private String _baseDir;
    private String Gifcurr;
    private String Gifback;

    // Database specific parameters
    private String dbName        = "";
    private String subProtocol   = "";

    // properties file for this servlet
    private String prop_file = "../../PPDservlets/classes/PPD.properties";
    private Properties props;
 
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {   
System.err.println("started currentServlet");
        // Database specific parameters for reoccurences of database
   
        String user          = "stastny1";
        String pass          = "oracle7";
        String query         = "";

        Vector html = new Vector();
        Vector ID   = new Vector();

        // Load the Properties File
        props = new Properties();
        try
        {
          File file = new File(prop_file);
          FileInputStream fi = new FileInputStream(file);
          props.load(fi);
          fi.close();
        }
        catch(IOException x)
        {
          System.err.println("Error occurred loading file - " + x);
          System.err.println("in programIDformServlet");
          return;
        }
        StringBuffer buf = new StringBuffer();
        String pName, pVal;
        for ( Enumeration enum = props.propertyNames();enum.hasMoreElements();){
                pName = enum.nextElement().toString();
                pVal  = props.getProperty( pName );
                if (pName.equals("FormDir")){_baseDir = pVal;}
                if (pName.equals("gifcurr")){Gifcurr = pVal;}
                if (pName.equals("gifback")){Gifback = pVal;}
                if (pName.equals("databaseName")){dbName = pVal;}
                if (pName.equals("subProtocol")){subProtocol = pVal;}
        }

        ServletOutputStream out = res.getOutputStream();
        res.setContentType("text/html");

        //*********************************************************************
        // Set up vectors with database information for displaying
        // form
        try { 

           // This driver may need to be changed to the one being used
           // on the platform this servlet is running.

           DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
           
           String DB_URL = "jdbc:" + subProtocol + ":" + dbName;
           // Connect to db       
           Connection dbConnection = DriverManager.getConnection(DB_URL, user, pass );
           // Get programID values from program Table in database
           // Make and execute query and place in Vector
           query = "select programID from program";
           Statement sqlStatement = dbConnection.createStatement();
           ResultSet results = sqlStatement.executeQuery(query);
           // Get ID values
           while( results.next() ) {
              ID.addElement(results.getString("programID"));
           }
           Collections.sort(ID);
           // Close 
           results.close();
           sqlStatement.close();
           dbConnection.close();
        }
        catch(SQLException sql) {
           out.println("<body background=\"" + Gifback + "\"><br><br><pre>"+sql.toString()+"</pre>"); 
           out.println("DatabaseName= " + dbName + "<br>");
           out.println("Protocol= " + subProtocol + "<br><br>");
           out.println("Please contact your database administrator<br>");
           out.println("<center><form><input type=button value=Return onclick=history.back();></form></center><br><br>");
           // Close the connection to client; the response is done.
           out.close();
        }
        try{
           //  Get "current.htm" file created from MS Frontpage 98
           //  and place in vector                                              
           String fileLine;
           String InputFile = _baseDir + "/" + fileName;
           // Get the  file specified by InputFile
           BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(InputFile)));                          
           //while there are still lines in the file, get-em
           while((fileLine = br.readLine())!= null)
           {
             //add each line to the vector, each line will have a CRLF
             html.addElement(fileLine);
           }
           //IMPORTANT!!!! - CLOSE THE STREAM!!!!!
           br.close();
        }
        catch(IOException e)
        {
           out.println("<body background=\"" + Gifback + "\">");
           out.println("<br><br>An error occurred reading the file<br>" + e + "<br>");
           out.println("Please contact your webmaster.<br>");
           out.println("<center><form><input type=button value=Return onclick=history.back();></form></center><br><br>");
           out.close();
        }

        //Here we will just loop through the vector and print the
        //lines of the file out to the client.
        int num = html.size();
        for(int times = 0; times < num; times++)
        {
          String  s;
          String imageCurr = "<p><img src=\"images/BannerCurrent.gif";
          String imageBack = "<body background=\"images/blegtext2.gif";
          String dummy     = "    <option value=";
          s = html.elementAt(times).toString();
          if ( s.regionMatches(0, imageCurr, 0, 37) ){
             out.println("<p><img src=\"" + Gifcurr + "\"" +
               " alt=\"BannerCurrent.gif \"" +
               " WIDTH=\"600\" HEIGHT=\"60\"></p>");
          }else if ( s.regionMatches(0, imageBack, 0, 37)){
             out.println("<body background=\"" + Gifback + "\">");
          }else if ( s.regionMatches(0, dummy, 0, 18)){
             int num1 = ID.size();
             String d1;
             for(int times1 = 0; times1 < num1; times1++){
                d1 = ID.elementAt(times1).toString();
                out.println("<option value=\"" + d1 + "\">" + d1 + "</option>"); 
             }
          }else out.println(html.elementAt(times).toString());
        }
        out.close();
System.err.println("Ended currentServlet");
    }
}