// this servlet will be called by the login page. after the user give the login password, it will be checked here. If the login password is found to be correct, a session would be started for that user else the user would bve prom[ted again for the login.

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

/**
 * Control passes to the doGet method of this class from the main login page. All users are authenticated through the method of this class. 
 * @author      Nilesh Agarwal
 * @version     1.00, 09/10/2001
 */ 

public class LoginServlet extends HttpServlet
{ 

    /**
     * <code>doGet</code> - This method uses methods of class QueryEngine to authenticate a registered user. 
     * It starts a session and binds 'username' after the user is authenticated. 
     * @param HttpServletRequest request
     * @param HttpServletResponse response
     */      
   
    
    //    public HttpSession session=null;
    public boolean isUserIn; 
    
    /*    public void doGet (HttpServletRequest request, 
		       HttpServletResponse response) 
	throws ServletException, IOException {
	PrintWriter out;
	response.setContentType("text/html");
	out=response.getWriter();
	System.out.println("calling UserPageGenerator.loginPage(out)");
	UserPageGenerator.loginPage(out);  // this is a function of UserPageGenerator which will generate User Page
	System.out.println("outta here");
	}*/
    
    public void doGet (HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
	System.out.println("inside dopost method");
	PrintWriter out;
	response.setContentType("text/html");
	out=response.getWriter();
	
	//nahin yaar i tried doing this too earlier nahin wait i will show	out.close();
	System.out.println("last line of dopost method");
	
	HttpSession session=request.getSession(false);
	
	try{   	// invalidating any existing session. 
	    if (session==null){
		System.out.println("session is null");
	    }
	    else{
		System.out.println("invalidating null after check (session!=null)");
		session.invalidate();
		session=null;
	    }
	}catch(IllegalStateException se){
	    System.out.println("trying to invalidate an invalid session:"+se);
	    session=null;
	}

	String login = request.getParameter("userLogin");     // userLogin field of   the form
	String password = request.getParameter("password"); // password field from the form
	System.out.println("login:"+login ); // printing for checking, later to be commented
	System.out.println("password:"+password ); // ----do------
		
	if (!(login==null)) { // when login field in not null
	    //NOTE A FUNCTION FOR UserQueryEngine CLASS HERE  DONE!!!
	    System.out.println("entered");
	    boolean checkVal=(SharedObjects.qe).isValidUser(login, password); // calling the isValidUser function of the   UserQueryEngine to be made by me. will check if a login name and password exist in the list, will return ture if it does else false
	    System.out.println("outt here");
	    if (checkVal){System.out.println("checkval=true");}
	    else{System.out.println("checkval=false");}
	    if (checkVal){
		//function of UserQueryEngine TO BE MADE
		boolean isSuspended=(SharedObjects.qe).isSuspendedUser(login);
		if (!(isSuspended)){
		    try{   	// invalidating any existing session. 
			if (session!=null){
			    System.out.println("not a null session, invalidating it");
			    session.invalidate();
			    session=null;
			}
		    }catch(IllegalStateException se){System.out.println("trying to invalidate an invalid session:"+se);}
		    System.out.println("yess");   
		    session = request.getSession(true);  //starting the session for the user
		    try{
			System.out.println("binding user login and password");
			session.putValue("userLogin", login);
			session.putValue("password", password);
			System.out.println("binding done successfully");
		    }catch(IllegalStateException e){System.out.println("binding an attribute to an invalidated session:"+ e);}
		    System.out.println("generating user's main page");
		    if (out==null){
			System.out.println("out=NULL");
		    }
		    else{
			System.out.println("out=not NULL");			
		    }

		    UserPageGenerator.userMainPage(out, login); // this function generates a user's personal page with all the options a user has. effectively, it will return a string which will be the html code for the page to be generated. for all users it will be the same except that there will be the user's login name here.
		}
		else{
		    // function of UserPageGenerator class TO BE MADE..
		    UserPageGenerator.userIsSuspended(out);
		}
	    }
	    else{
		//NOTE A FUNCTION FOR UserPageGenerator CLASS HERE  DONE!!!
		UserPageGenerator.incorrectLogin(out);  // this is a function of UserPageGenerator which will print the appropriate message and provide a link to login page
	    }
	}
	else{
	    //NOTE A FUNCTION FOR UserPageGenerator CLASS HERE   DONE!!!
	    UserPageGenerator.incorrectLogin(out);// this is a function of UserPageGenerator which will print the appropriate message and provide a link to login page
	    }
	
    }
    
}









