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

public class AssignWorker 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 worker_id from worker where worker_id not in (select worker_id from works_for)";
	String query2 =
"select supervisor_id from supervisor";
	//	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 assi = req.getParameter("assi");
	if (assi == null) {
	String cn = req.getParameter("supervisor_no");
       	String worker = req.getParameter("worker_no");
	String update = "insert into works_for values ('" + worker + "','" + cn + "')";
	//	System.out.print(update);
	//	System.out.println("xx");
	if ((((logout==null)) && (!(name==null))) && ((!(cn.equals(""))))) {
	    stmt1 = myCom.createStatement();
	    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 = "View jobs";
	    out.println("<HTML><HEAD><TITLE>");
	    out.println(title);
	    out.println("</TITLE></HEAD><BODY>");
	    out.println("<table cellpadding=4 border=2>");
	    out.println("<tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/AssignWorker\"><h2> Assign Workers</h2><pre> Select Complaint  <select name=\"worker_no\">");
	    stmt = myCom.createStatement();
	    ResultSet rs = stmt.executeQuery(query);
	    ResultSetMetaData rsmd = rs.getMetaData();
	    String number=null;
	    while (rs.next()) {
		out.print("<option value=\"");
		number=rs.getString(1);
		out.print(number + "\">" + number);
		out.print("</option>");
	    }
	    out.println("</select></pre><pre> Select Worker <select name=\"supervisor_no\">");
	    stmt2 = myCom.createStatement();
	    ResultSet rs2 = stmt2.executeQuery(query2);
	    ResultSetMetaData rsmd2 = rs2.getMetaData();
	    String number2=null;
	    while (rs2.next()) {
		out.print("<option value=\"");
		number=rs2.getString(1);
		out.print(number + "\">" + number);
		out.print("</option>");
	    }
	    out.println("</select></pre>");
	    out.println("<pre><input type=\"submit\" value=\"Assign\" name=\"Submit\"></pre>");

	    out.println("<INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" ><br>");
	    out.println("</td></tr></table>");
	    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("<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("</body> </html>"); 
	    out.close();
	
	}
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
    }   
}





