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

public class Login extends HttpServlet
{
    HttpSession session;
    Connection conn=null;
    Statement stmt=null;
    
    public void init(ServletConfig config) throws ServletException {
	try{
	    Class.forName("oracle.jdbc.driver.OracleDriver");
	    conn = DriverManager.getConnection (Configure.HOST_NAME,Configure.LOGIN,Configure.PASSWD);
	    stmt = conn.createStatement();
	}catch(SQLException sqe){
	    sqe.printStackTrace();
	}catch(ClassNotFoundException ce){
	    ce.getMessage();
	}
	System.out.println("Init successful");
    }
    
    public void doGet (HttpServletRequest request, 
			HttpServletResponse response) 
	throws ServletException, IOException {
	
	HttpSession session = null;
	PrintWriter  out;
	String  user;
	String  name = request.getParameter("login");
	String  user_type = request.getParameter("login_type");
	
	String  userpasswd = request.getParameter("passwd");
	String  title = "WELCOME WORKER";
	String title2 = "WELCOME ADMIN";
	response.setContentType("text/html"); 
	out = response.getWriter();
	String type = "worker";
	String type2 = "admin";
	user = "";
	System.out.println(name + user_type + userpasswd);
	try
	    {
		ResultSet rs;
		rs = stmt.executeQuery("select passwd from LOGIN where name= "+"'"+ name +"'");
		while(rs.next())
		    {
			user = rs.getString(1);
		    }
		if(user.equals(userpasswd) && (user_type.equals(type))){
		    
		    session=request.getSession(true);
		    session.putValue(session.getId(), name);

		    out.println("<HTML><HEAD><TITLE>");
		    out.println(title);
		    out.println("</TITLE></HEAD><BODY>");
		    out.println("<B>WELCOME WORKER </BLINK><B>");
		    out.println("<h3>You logged in successfully </br><h3>");
		    out.println("<FORM ACTION=\"" + Configure.SERVLET_SERVER + ":" + Configure.SERVLET_PORT + "/" + Configure.SERVLET_PATH + "/Worker\" method =\"GET\">");
		    out.println("<br><INPUT TYPE=\"submit\" NAME=\"view\" VALUE=\"View all jobs\" >");
		    out.println("<br><INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" >");
		    out.println("</FORM>");
		}
		else if (user.equals(userpasswd) && (user_type.equals(type2))){
		    session=request.getSession(true);
		    session.putValue(session.getId(), name);

		    out.println("<HTML><HEAD><TITLE>");
		    out.println(title);
		    out.println("</TITLE></HEAD><BODY>");
		    out.println("<B>WELCOME ADMIN </BLINK><B>");
		    out.println("<h3>You logged in successfully </br><h3>");
		    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 + "/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 + "/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("<br><INPUT TYPE=\"submit\" NAME=\"logout\" VALUE=\"Logout\" >");
		    out.println("</FORM>");
		}
		else{
		    System.out.println(name + " " + userpasswd);
		    out.println("<HTML><HEAD><TITLE>");
		    title = "LOGIN FAILURE";
		    out.println(title);
		    out.println("</TITLE></HEAD><BODY>");
		    out.print("Login failure please ");
		    out.print("<a href=\"file:///home2/ug99/gavaskar/proj/login.html\"> Try Again");
		}
		out.println("</BODY></HTML>");
		out.close();
	    }
	catch (Exception e){}
    }
}	    
	





