package Juno2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public class CmdLoginHandler extends org.apache.struts.action.Action implements Juno2.RequestHandler {

    public CmdEventFactory factory = new CmdEventFactory();
    Log log = LogFactory.getLog("mylog");
    
    
	public CmdLoginHandler() {
		super();
	}
	
   
    //public ActionForward perform(ActionMapping mapping, ActionForm form,
	public ActionForward execute(ActionMapping mapping, ActionForm form,
                    javax.servlet.http.HttpServletRequest request,
                    javax.servlet.http.HttpServletResponse response) 
    {
        ////System.out.println("CmdLoginHandler perform() invoked");   										

        try {
        	
            log.debug("***** Commons logging!!!");        
            log.info("***** INFO Commons logging!!!");
            log.warn("***** WARN Commons logging!!!");
            log.error("***** ERROR Commons logging!!!");
        	
	   		processRequest(request);
        } catch (Exception e) {
	        System.out.println("CmdLoginHandler processRequest threw exception: " + e.getMessage());
        }
        
        ////System.out.println("CmdLoginHandler about to ActionForward()");   		
   		ActionForward result = mapping.findForward(CmdConstants.SUCCESS_KEY);
   		return result;
	}   		 	
	
	public void processRequest(HttpServletRequest request) throws Exception, CmdException
	{
        CmdCreateEvent event = null;
       	try{
	 		event= (CmdCreateEvent)factory.extractEvent(request);
        } catch (Exception e) {
            System.out.println("CmdCreateEvent exception");
            throw new CmdException("Fatal error creating Login event. " +e);
        }
        HttpSession session = null;
        try {
			session=request.getSession();
        }catch (Exception e) {
           System.out.println("request.getSession() exception");
           throw new CmdException("Fatal error getSession() Login ");
    	}
        CmdVO info = null;
   		try {
        	info=event.getInfo();
    	} catch (Exception e){
           System.out.println("event.getInfo() exception");
           throw new CmdException("Fatal error event.getInfo() Login");
    	}

		//// get the existing userid and password attributes
		String sessionUserId = (String)session.getAttribute("userId");
		String sessionUserPassword = (String)session.getAttribute("userPassword");
		// System.out.println("UserId and Password in existing session |" + sessionUserId +
			// "|" + sessionUserPassword + "|  Login");

		//// Call CmdLoginImpl
		CmdAccountListImpl alImpl = new CmdAccountListImpl();
		AccountListCVO alcvo = alImpl.doAccountList(info.getUserId(), info.getUserPassword());

 		if(alcvo.getLoginState()) {

            ////if( sessionUserId.equals(info.getUserId()))
            ////{
				session.setAttribute("userId",alcvo.getUserId());
				session.setAttribute("userPassword",alcvo.getUserPassword());
				session.setAttribute("firstName",alcvo.getFirstName());
				session.setAttribute("lastName",alcvo.getLastName());
            ////}
            ////else
            ////{
			////	throw new CmdException("Fatal error Login Failure ");
            ////}
        }
        else
        {
            session.invalidate();
            throw new CmdException("Fatal error Session and Request user conflict.");
        }

        session.setAttribute("AccountList",alcvo.getAccountList());
        
 	}
	
}

