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

public class Remove 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);

	String query = "select * from login";
	System.out.println("query is " + query);
	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 remove = req.getParameter("remove");
	if ((remove == null) && (logout == null)) {
	    String person = req.getParameter("person");
	    StringTokenizer st = new StringTokenizer(person,"+");
	    String number = st.nextToken().trim();
	    String number2 = st.nextToken().trim();
	    
	    String update = "delete from login where name='" + number + "' and login_type='" + number2 +"'";
	    System.out.print(update);
	    System.out.println("xx");
	    if ((((logout==null)) && (!(name==null))) && ((!(number.equals(""))) && (!(number2.equals(""))))) {
		stmt1 = myCom.createStatement();
		System.out.println("yy");
		stmt1.executeUpdate(update);
	    }
	}
	
	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 = "Remove 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 + "/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 + "/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++ ) {
		if (!(i==2)){
		out.print("<th>" + "\n" + "  ");
		out.print( rsmd.getColumnName(i)  + "</th>" );
		}
	    }
	    while (rs.next()) {
		out.print("<tr>");
		for( int i = 1; i <= rsmd.getColumnCount(); i++ ) {
		    if (!(i==2)){
		    out.print( "<td>" + rs.getString(i) + "</td>" );
		    }
		}
		out.print("</tr>");
	    }
	    out.println("</table><table>");
	    stmt2 = myCom.createStatement();
	    ResultSet rs2 = stmt2.executeQuery(query);
	    ResultSetMetaData rsmd2 = rs2.getMetaData();
	    String number=null;
	    String number2=null;
	    out.println("<tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Remove\"><h2> Remove Users</h2><pre> Select User <select name=\"person\">");

	    while (rs2.next()) {
		out.print("<option value=\"");
		number=rs2.getString(1);
		number2=rs2.getString(3);
		out.print(number + "+" + number2 + "\">" + number + " " + number2);
		out.print("</option>");
	    }
	    out.println("</select></pre>");

	    out.println("<pre><input type=\"submit\" value=\"Change\" name=\"remove1\"></pre>");
	    out.println("<INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" ><br></form>");
	    out.println("</td></tr>"); 
	    
	    out.println("</table></body> </html>"); 
	    out.close();
	
	}
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
    }   
}


























