package requestHandlers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Beans.R2UserBean;
import Beans.R2UserPassBean;
import DAOs.R2UserDAO;

public class loginH implements RequestHandler
{

	@Override
	public void processRequest(Object user, HttpServletRequest req,
			HttpServletResponse res, Object[] services, Object log)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
		     R2UserPassBean userpass = new R2UserPassBean();
		     userpass.setUserName(req.getParameter("username"));
		     userpass.setPassword(req.getParameter("password"));

		     R2UserBean userbean = R2UserDAO.login(userpass);
			   		    
		     if (userbean.isValid())
		     {
		          HttpSession session = req.getSession(true);	    
		          session.setAttribute("currentSessionUser",userbean); 
		          res.sendRedirect("MainUI.jsp"); //logged-in page      		
		     }
		     else 
		    	 res.sendRedirect("InvalidLogin.jsp"); //error page 
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		}
	}

}
