package Juno2;
import java.io.*;
import java.util.HashMap;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


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

    public CmdEventFactory factory = new CmdEventFactory();

    //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("CmdAccountListHandler perform() invoked");   										
        try {
	   		processRequest(request);
        } catch (Exception e) {
	        System.out.println("CmdAccountListHandler processRequest threw exception: " + e.getMessage());
        }
        
        ////System.out.println("CmdAccountListHandler 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 AccountList event. " +e);
        }
        HttpSession session = null;
        try {
			session=request.getSession();
        }catch (Exception e) {
           System.out.println("request.getSession() AccountList exception");
			throw new CmdException("Fatal error getting session. " +e);
    	}
        CmdVO info = null;
   		try {
        	info=event.getInfo();
    	} catch (Exception e){
           System.out.println("event.getInfo() AccountList exception");
			throw new CmdException("Fatal error getting event. " +e);
    	}

		//// 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 + "|");

		CmdAccountListImpl alImpl = new CmdAccountListImpl();
		AccountListCVO alcvo = alImpl.doAccountList(info.getUserId(), info.getUserPassword());
		
		if(alcvo.getLoginState()) {
			if(!sessionUserId.equals(alcvo.getUserId()))
                System.out.println("ALH: Session (" + sessionUserId +
				") and request (" + alcvo.getUserId() +
				    ") user out of sync!");

			//// do same test for password
            //// on error invalidate session and redirect to login
        }

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

