import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.sql.*;
import java.util.*;
import Configure;

class EasyPrint{
    public static void printSimilar(ResultSet rs,ResultSetMetaData rsmd,PrintWriter out,Statement stmt) throws IOException,SQLException{
	try{
	    int no_of_cols=rsmd.getColumnCount();
	    //int bldg_col_num = no_of_cols;
	    int loc_col_num = no_of_cols;
	    // String iden_bldg= rs.getString(bldg_col_num);  //idetical location and building.
	    String iden_loc=rs.getString(loc_col_num);
	    String same_cid=rs.getString(1);
	    System.out.println("INSIDE printSimilar");
	    //System.out.println(iden_bldg);
	    System.out.println(iden_loc);
	    
	    /*	    String query = "select  complaint.complaint_id CID,description,status, date_lodged, location, bldg from complaint  where  start_date is null and bldg= " + "'" + iden_bldg + "'"+ " and " + "location=" + "'" + iden_loc + "'" + " and " + " complaint_id <> " + same_cid  ;*/

	    String query = "select  complaint.complaint_id CID,description,status, date_lodged, location from complaint  where  start_date is null and" + " location=" + "'" + iden_loc + "'" + " and " + " complaint_id <> " + same_cid  +" and complaint.complaint_id in (select complaint_id from resolve)";

	    System.out.println(query); //printing the query
	    Statement stmt1= MakeConnection.toDatabase();
	    ResultSet rsTemp = stmt1.executeQuery(query);
	    
	    if(!(rsTemp==null)) {System.out.println("correct qery in similar");}
	    ResultSetMetaData rsmdTemp = rsTemp.getMetaData();
	   
	    out.println("<tr><td>The Complaints similar are</td></tr>");
	  
	    while(rsTemp.next())
		printColumns(rsTemp,rsmdTemp,out);
	    out.println("<tr bgcolor=\"#ffffff\"><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
	    
	    
	}catch(SQLException e){ System.out.println(e);}
    }


    /*--------------    
      public static void printSimilar(ResultSet rs,ResultSetMetaData rsmd,PrintWriter out,Statement stmt) throws IOException,SQLException{
      try{
      int no_of_cols=rsmd.getColumnCount();
      int bldg_col_num = no_of_cols;
      int loc_col_num = no_of_cols-1;
      String iden_bldg= rs.getString(bldg_col_num);  //idetical location and building.
      String iden_loc=rs.getString(loc_col_num);
      String same_cid=rs.getString(1);
      System.out.println("INSIDE printSimilar");
      System.out.println(iden_bldg);
      System.out.println(iden_loc);

      String query = "select  complaint.complaint_id CID,description,status, date_lodged, location, bldg from complaint  where  bldg= " + "'" + iden_bldg + "'"+ " and " + "location=" + "'" + iden_loc + "'" + " and " + " complaint_id <> " + same_cid  ;

	    
      System.out.println(query); //printing the query
      ResultSet rsTemp = stmt.executeQuery(query);
      if(!(rsTemp==null)) {System.out.println("correct qery in similar");}
      ResultSetMetaData rsmdTemp = rsTemp.getMetaData();
	   
      out.println("<tr><td>The Complaints similar are</td></tr>");
	  
      while(rsTemp.next())
      printColumns(rsTemp , rsmdTemp ,out);
	  
      }catch(SQLException e){ System.out.println(e);}
      }*/



    //prints the output in ROW ie <tr> ans </tr>
    public static void printColumns(ResultSet rs, ResultSetMetaData rsmd ,PrintWriter out) throws IOException,SQLException{ 	try{ 
	System.out.println("in printColumns");
	int no_of_cols=rsmd.getColumnCount();
	out.println("<tr>");	    
	for(int i=1 ; i <= no_of_cols; i++){ 
	    out.print( "<td>" + rs.getString(i) + "</td>" + "\n"); 
	}
	out.print("</tr>\n");
	System.out.println("out of PrintClums");}catch(SQLException e){ System.out.println(e);}
    }
    


    //prints the output in ROW ie <tr> ans </tr>
    public static void printHeadings(ResultSetMetaData rsmd ,PrintWriter out) throws IOException,SQLException{ 
	try{ 
	    System.out.println("in printheadings");
	    int no_of_cols=rsmd.getColumnCount();
	    //start of print of headings of cloumns
	    out.print("<tr>");
	    for(int i = 1; i <= no_of_cols; i++ ) {
		out.print("<th>" + "  ");
		out.print(rsmd.getColumnName(i)  + "</th>"+ "\n" );
	    }
	    out.print("</tr>\n");
	    //end of print of headings of columns
	}catch(SQLException e){ System.out.println(e);}
    }



    public static void printButton(String type ,String name , String value ,PrintWriter out) throws IOException,SQLException{ 
      
	System.out.println("in printButton " + name +":" + value);
	out.println("<tr>");
	out.println("<td>");
	out.println("<input type=" + type + " " +  " name=\"" + name + "\"" + " value=\"" + value +  "\">");
	out.println("</td>");
	out.println("</tr>");
	
    }
    


    public  static void errorCode(HttpServletRequest request,HttpServletResponse response)
	throws ServletException, IOException{
	try{
	    PrintWriter  out;
	    String  title = "WELCOME USER";
	    
	    response.setContentType("text/html"); 
	    out = response.getWriter();
	    
	   
	    out.println("<HTML><HEAD><TITLE>");
	    title = "LOGIN FAILURE";
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.print("<a href=\"http://www.cse.iitb.ac.in/~gavaskar/proj/log.html\"> Try Again");
	    out.println("</BODY></HTML>");
	    out.close();
	
	}catch(Exception sqle){
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}   
    }
    
}

 


class MakeConnection{
    public static Statement toDatabase() throws SQLException{ 
	HttpSession  session;
	Connection conn=null;
	Statement stmt=null;
    
	try{
	    System.out.println( " 0" );
	    Class.forName("oracle.jdbc.driver.OracleDriver");
	    System.out.println( " 1" );
	    conn = DriverManager.getConnection (Configure.HOST_NAME,Configure.LOGIN,Configure.PASSWD);
	    stmt = conn.createStatement();
	    System.out.println( " 2 " );
	}catch(SQLException sqe){
	    sqe.printStackTrace();
	    System.out.println( " 3" );
	}catch(ClassNotFoundException ce){
	    ce.getMessage();
	    System.out.println( " 4" );
	}
	return  stmt ;
    }

}





