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

public class Clear extends HttpServlet {
    Connection myCom;
    Statement stmt0=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 clear = req.getParameter("clear");
	
	
	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 = "clear";
	    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 + "/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("<table>");
	    if (clear==null) {
		String update0 = "delete from complaint_type where complaint_id in (select complaint_id from complaint where end_date is not null)";
		String update1 = "delete from resolve where complaint_id in (select complaint_id from complaint where end_date is not null)";
		String update2 = "delete from complaint where end_date is not null";
		stmt0 = myCom.createStatement();
		stmt0.executeUpdate(update0);
		stmt1 = myCom.createStatement();
		stmt1.executeUpdate(update1);
		stmt2 = myCom.createStatement();
		stmt2.executeUpdate(update2);
	    }
	    out.println("To clear data click Clear <tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Clear\">");
	    out.println("<pre><input type=\"submit\" value=\"Clear\" name=\"clear1\"></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();
	}
    }   
}


























