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

public class Add 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";
	PrintWriter out;
	String title;
	res.setContentType("text/html");
	out = res.getWriter();
	if (name == null) {
	    out.println("You are already logged out");
	}
	boolean bal=true;
	String logout = req.getParameter("logout");
	String add = req.getParameter("add");
	if (add == null) {
	String type = req.getParameter("login_type");
	String naam = req.getParameter("naam");
	String passwd = req.getParameter("passwd");
	String passwd2 = req.getParameter("passwd2");
	if (logout==null) {
	if ((passwd.equals(passwd2)) && (!(naam.equals(""))) && (!(naam==null))) {
	    
	    String qw = "select name from login where name='" + naam + "'";
	    Statement stmt3 = myCom.createStatement();
	    ResultSet rs3 = stmt.executeQuery(qw);
	    if (rs3.next()) {
		bal = false;
	    }
	    String update = "insert into login values ('" + naam + "','" + passwd + "','" + type +"')";
	    String update1 = "insert into login values ('" + naam + "','','','','')";
	    System.out.print(update);
	    System.out.println("xx");
	    boolean bol = (((logout==null)) && (!(name==null))) && ((!(type.equals(""))) && ((!(naam.equals(""))) && (!(passwd.equals("")))));
	    if (bol && bal) {
		stmt1 = myCom.createStatement();
		System.out.println("yy");
		stmt1.executeUpdate(update);
		if (type.equals("supervisor")) {
		stmt2 = myCom.createStatement();
		stmt2.executeUpdate(update1);
		}
	    }
	} 
	else {
	    out.println("passwords doesn't match or login name is invalid,user not added");
	}
	}
	}
	
	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 + "/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 + "/Remove\" method =\"GET\">");
	    out.println("<br><INPUT TYPE=\"submit\" NAME=\"remove\" VALUE=\"Remove anyone\" >");
	    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>");
	    out.println("<tr><td valign=\"top\" height=\"360\"><form name=\"mainform\" method=\"GET\" action=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Add\"><h2> Add Supervisors/Analysts</h2>");
	    out.println("<pre>");
	    out.print("<select name=\"login_type\"><option value=\"supervisor\">Supervisor</option><option value=\"analyst\">Analyst</option>");
	    out.println("</select></pre>");
	    out.println("<pre> Enter the name <input type=\"text\" name=\"naam\" size=\"20\" maxlength=\"50\"> </pre> <pre> Enter the password <input type=\"password\" name=\"passwd\" size=\"20\" maxlength=\"20\"> </pre> <pre> Retype password <input type=\"password\" name=\"passwd2\" size=\"20\" maxlength=\"20\"> </pre> <pre><input type=\"submit\" value=\"Change\" name=\"add1\"></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();
	}
    }   
}


























