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

public class Generic extends HttpServlet {
    Connection myCom;
    Statement stmt=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);
	String query = "select * from type";
	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 type = req.getParameter("type");

	if ((type == null) && (logout==null)) {
	    String tname = req.getParameter("type_name");
	    String description = req.getParameter("description");
	    if ((!(tname==null)) && (!(description==null))) {
		String update="insert into type values ('" + tname + "','" + description + "')";
		System.out.print(update);
		Statement stmt1 =  myCom.createStatement();
		System.out.println("yy");
		stmt1.executeUpdate(update);
	    }
	    else {
		out.println("invalid type name or description");
	    } 
	}
       
	
	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 = "Add users";
	    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 + "/Query\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"query\" VALUE=\"Query\" >");
	    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("<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 + "/Add\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"add\" VALUE=\"Add Supervisors/analysts\" >");
	    out.println("</FORM>");

	    out.println("<table cellpadding=4 border=2>");

	    stmt = myCom.createStatement();
	    ResultSet rs = stmt.executeQuery(query);
	    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><table>");
	out.println("<tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Generic\"><h2> Add Complaint types</h2>");
	out.println("<pre> Enter the type name <input type=\"text\" name=\"type_name\" size=\"20\" maxlength=\"50\"> </pre> <pre> Enter the description <input type=\"text\" name=\"description\" size=\"50\" maxlength=\"50\"> </pre> <pre><input type=\"submit\" value=\"Change\" name=\"generic1\"></pre>");
	out.println("<INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" ><br>");
	out.println("</td></tr></table></form></body> </html>"); 
	out.close();
	
	}
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
    }   
}











