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

public class Query extends HttpServlet {
    Connection myCom;
    Statement stmt=null;
    Statement stmt1=null;
    Statement stmt2=null;
    public void doGet (HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
	try {
	HttpSession session = req.getSession(true);
	String name = (String)session.getValue(session.getId());
	//	System.out.println("user name " + name);
	DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
	myCom = DriverManager.getConnection(Configure.HOST_NAME,Configure.LOGIN,Configure.PASSWD);

	PrintWriter out;
	String title;
	res.setContentType("text/html");
	out = res.getWriter();
	if (name == null) {
	    out.println("You are already logged out");
	}
	
	String logout = req.getParameter("logout");
	String query = req.getParameter("query");
	
	
	if (!(logout==null)) {
	    title = "logout db";
	    session.invalidate();
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<h3> You are logged out successfully </br><h3>");
	}
	else if (!(name == null)) {
	
	    title = "query";
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Add\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"add\" VALUE=\"Add Supervisors/analysts\" >");
	    out.println("</FORM>");
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Generic\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"type\" VALUE=\"Add Complaint Types\" >");
	    out.println("</FORM>");
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/AssignWorker\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"assi\" VALUE=\"Assign worker-supervisors\" >");
	    out.println("</FORM>");
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Clear\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"clear\" VALUE=\"Clear resolved Complaints\" >");
	    out.println("</FORM>");
	    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Remove\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"remove\" VALUE=\"Remove anyone\" >");
	    out.println("</FORM>");
	    out.println("<hr>");
	    out.println("<table border=1>");
	    if (query==null) {
		String data = req.getParameter("data");
		String update = req.getParameter("update");
		String doubt = req.getParameter("doubt");
		if (!(doubt==null) && !(data.equals(""))){
		    stmt = myCom.createStatement();
		    ResultSet rs = stmt.executeQuery(data);
		    ResultSetMetaData rsmd = rs.getMetaData();
		    for( int i = 1; i <= rsmd.getColumnCount(); i++ ) {
			out.print("<th>" + "\n" + "  ");
			out.print( rsmd.getColumnName(i)  + "</th>" );
		    }
		    while (rs.next()) {
			out.print("<tr>");
			for( int i = 1; i <= rsmd.getColumnCount(); i++ ) {
			    out.print( "<td>" + rs.getString(i) + "</td>" );
			}
			out.print("</tr>");
		    }
		    out.println("</table><hr><table>");
		}
		else if (!(update==null) && !(data.equals(""))){
		    stmt1 = myCom.createStatement();
		    stmt1.executeUpdate(update);
		}
	    }
	    out.println("<tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Query\">");
	    out.println("<pre> Enter the query <textarea name=\"data\" cols=\"80\" rows=\"5\" maxlength=\"100\"> </textarea></pre>  <pre><input type=\"submit\" value=\"Query\" name=\"doubt\"></pre><pre><input type=\"submit\" value=\"Update\" name=\"update\"></pre>");
	    out.println("<INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" ><br>");
	    out.println("</td></tr></table></body> </html>"); 
	    out.close();
	
	}
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
    }   
}


























