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


public class MainServlet extends HttpServlet{
    public void init(ServletConfig config) throws ServletException {
	
	System.out.println( " 0" );
	System.out.println( " 3" );
	System.out.println( " 4" );
	
	System.out.println("Init successful");
    }
    

    //System.out.println( " before get" );
    

    public void doPost (HttpServletRequest request ,HttpServletResponse response) 
	throws ServletException, IOException {
	try{
	    Enumeration all_parameters=request.getParameterNames();
	    String  hidden_mainpage =request.getParameter("frommainpage"); 
	    String login_type=request.getParameter("login_type"); 
	
	    
	  	     
	    System.out.println("IN MAINSERVLET.JAVA");
	    
	  
	    while(all_parameters.hasMoreElements()){
		System.out.println(all_parameters.nextElement());
	    }
	     
	    if(hidden_mainpage==null){
		System.out.println("request not from main page ");
	    }
	    if( !(hidden_mainpage==null) && (hidden_mainpage.equals("frommainpage"))){
		System.out.println("");
		System.out.println("calling callappropiate");
		MainPage.callValidate(request,response,login_type);
	    }
	}catch (Exception sqle){
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}
    }
}


//This is special class..fron first page call this.. this shud take care of the stufff :)
//----------------------------------------------------------------------------------------

class MainPage{
    public static void callValidate(HttpServletRequest request,HttpServletResponse response, String login_type)throws ServletException, IOException {
	try{
	    //PrintWriter  out;
	    String  authentic_passwd=null;
	    String  entered_name = request.getParameter("login");
	    String  entered_passwd = request.getParameter("passwd");
	    String user_type=null ;
	    Statement stmt;
	    HttpSession session;
	    stmt=MakeConnection.toDatabase();
	    System.out.println("made connection");
	    ResultSet rs;
	    rs = stmt.executeQuery("select passwd,login_type from LOGIN where name= "+"'"+ entered_name +"'");
	    while(rs.next())
		{
		    authentic_passwd = rs.getString(1);       
		    user_type=rs.getString(2);
		}
	    System.out.println("aftere exe query");
	 	    
	    System.out.println(user_type+";");
	    
	    if (!(user_type==null)) {
	    if(!(rs==null)&&(authentic_passwd.equals(entered_passwd)) && (user_type.startsWith(login_type))){
		if(login_type.startsWith("analyst")){
		    session=request.getSession(true);
		    session.putValue(session.getId(),entered_name);
		    Analyst.printAnalystLoginCode(request,response);
		}
		else if(login_type.startsWith("supervisor")){
		    session=request.getSession(true);
		    session.putValue(session.getId(),entered_name);
		    System.out.println("ddddddddd:::::");
		    Supervisor.printSupervisorLoginCode(request,response);
		}    
		else if(login_type.startsWith("worker")){
		    session=request.getSession(true);
		    session.putValue(session.getId(),entered_name);
		    System.out.println("ddddddddd:::::");
		    WorkerLogin.printWorkerLoginCode(request,response);
		}    
		else if(login_type.startsWith("administrator")){
		    session=request.getSession(true);
		    session.putValue(session.getId(),entered_name);
		    System.out.println("ddddddddd:::::");
		    Admin.printAdminLoginCode(request,response);
		}    
	    }
	    else{
		System.out.println("aaaaaaaaaaaa");
		Analyst.errorCode(request,response);
		EasyPrint.errorCode(request,response);
	    }
	    }
	    else{
		System.out.println("aaaaaaaaaaaa");
		Analyst.errorCode(request,response);
		EasyPrint.errorCode(request,response);
	    }
	
	}catch(Exception sqle){
	    System.out.println( "exception in get " );
	    System.out.println(sqle);
	}
    }
}	        












