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

public class Worker 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 complaint.complaint_id,title,location,description,date_lodged,start_date,status,end_date from complaint,worker,resolve where complaint.complaint_id=resolve.complaint_id and worker.worker_id=resolve.worker_id and worker.worker_id=" + "'" + name + "'";
	//	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 view = req.getParameter("view");
	if (view == null) {
	String complaint_no = req.getParameter("complaint_no");
	String day = req.getParameter("day");
	String month = req.getParameter("month");
	String year = req.getParameter("year");
	String day1 = req.getParameter("day1");
	String month1 = req.getParameter("month1");
	String year1 = req.getParameter("year1");
       	String status = req.getParameter("status");
	String start = req.getParameter("start");
	String end = req.getParameter("end");

	String update = "update complaint set status='" + status + "',start_date='" + day + "-" + month + "-" + year + "',end_date='" + day1 + "-" + month1 + "-" + year1 + "' where complaint_id=" + complaint_no;
	String update1 = "update complaint set status='" + status + "',start_date='" + day + "-" + month + "-" + year + "' where complaint_id=" + complaint_no;
	String update2 = "update complaint set status='" + status + "',end_date='" + day1 + "-" + month1 + "-" + year1 + "' where complaint_id=" + complaint_no;
	//	System.out.print(update);
	//	System.out.println("xx");
	if ((((logout==null)) && (!(name==null))) && ((!(complaint_no.equals(""))))) {
	    stmt1 = myCom.createStatement();
	    if (start==null) {
		stmt1.executeUpdate(update2);
	    }
	    else if (end==null) {
		stmt1.executeUpdate(update1);
	    }
	    else {
		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("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/EditWorker\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"edit\" VALUE=\"Edit Preferences\" >");
	    out.println("</FORM>");
	    out.println("<h3>Complaints for you</h3>");
		    
	    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>");
	    stmt2 = myCom.createStatement();
	    ResultSet rs2 = stmt2.executeQuery(query);
	    ResultSetMetaData rsmd2 = rs2.getMetaData();
	    String number=null;
	    out.println("<tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Worker\"><h2> Change Status</h2><pre> Select Complaint  <select name=\"complaint_no\">");

	    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>");
	    out.print("Enter the start date  "); 
	    out.print("<select name=\"day\"><option value=\"01\">01</option><option value=\"02\">02</option><option value=\"03\">03</option><option value=\"04\">04</option><option value=\"05\">05</option><option value=\"06\">06</option><option value=\"07\">07</option><option value=\"08\">08</option><option value=\"09\">09</option><option value=\"10\">10</option><option value=\"11\">11</option><option value=\"12\">12</option><option value=\"13\">13</option><option value=\"14\">14</option><option value=\"15\">15</option><option value=\"16\">16</option><option value=\"17\">17</option><option value=\"18\">18</option><option value=\"19\">19</option><option value=\"20\">20</option><option value=\"21\">21</option><option value=\"22\">22</option><option value=\"23\">23</option><option value=\"24\">24</option><option value=\"25\">25</option><option value=\"26\">26</option><option value=\"27\">27</option><option value=\"28\">28</option><option value=\"29\">29</option><option value=\"30\">30</option><option value=\"31\">31</option></select>");
	    out.print("<select name=\"month\"><option value=\"JAN\">January</option><option value=\"FEB\">February</option><option value=\"MAR\">March</option><option value=\"APR\">April</option><option value=\"MAY\">May</option><option value=\"JUN\">June</option><option value=\"JUL\">July</option><option value=\"AUG\">August</option><option value=\"SEP\">September</option><option value=\"OCT\">October</option><option value=\"NOV\">November</option><option value=\"DEC\">December</option></select>");
	    out.print("<select name=\"year\"><option value=\"2001\">2001</option><option value=\"2002\">2002</option><option value=\"2003\">2003</option><option value=\"2004\">2004</option><option value=\"2005\">2005</option></select>");
	    out.println("</pre>");
	    out.println("<pre>");
	    out.print("Enter the end date  "); 
	    out.print("<select name=\"day1\"><option value=\"01\">01</option><option value=\"02\">02</option><option value=\"03\">03</option><option value=\"04\">04</option><option value=\"05\">05</option><option value=\"06\">06</option><option value=\"07\">07</option><option value=\"08\">08</option><option value=\"09\">09</option><option value=\"10\">10</option><option value=\"11\">11</option><option value=\"12\">12</option><option value=\"13\">13</option><option value=\"14\">14</option><option value=\"15\">15</option><option value=\"16\">16</option><option value=\"17\">17</option><option value=\"18\">18</option><option value=\"19\">19</option><option value=\"20\">20</option><option value=\"21\">21</option><option value=\"22\">22</option><option value=\"23\">23</option><option value=\"24\">24</option><option value=\"25\">25</option><option value=\"26\">26</option><option value=\"27\">27</option><option value=\"28\">28</option><option value=\"29\">29</option><option value=\"30\">30</option><option value=\"31\">31</option></select>");
	    out.print("<select name=\"month1\"><option value=\"JAN\">January</option><option value=\"FEB\">February</option><option value=\"MAR\">March</option><option value=\"APR\">April</option><option value=\"MAY\">May</option><option value=\"JUN\">June</option><option value=\"JUL\">July</option><option value=\"AUG\">August</option><option value=\"SEP\">September</option><option value=\"OCT\">October</option><option value=\"NOV\">November</option><option value=\"DEC\">December</option></select>");
	    out.print("<select name=\"year1\"><option value=\"2001\">2001</option><option value=\"2002\">2002</option><option value=\"2003\">2003</option><option value=\"2004\">2004</option><option value=\"2005\">2005</option></select>");
	    out.println("</pre>");
	    out.print("<pre>Change Start date<input type=checkbox name=\"start\" value=\"change start\"></pre>");
	    out.print("<pre>Change End date<input type=checkbox name=\"end\" value=\"change end\"></pre>");
	    
	    out.println("<pre> Enter the new status  <input type=\"text\" name=\"status\" size=\"20\" maxlength=\"50\"> </pre>  <pre><input type=\"submit\" value=\"Change\" name=\"Submit\"></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();
	}
    }   
}


























