/*
 * Created on Dec 1, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.util;


import gov.va.med.esr.ui.ApplicationConstants;
import gov.va.med.fw.ui.UIConstants;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


/**
 * @author DNS   chenb
 */
public final class SessionManager implements Serializable
{
    private static final long serialVersionUID = -5125239457565356206L;

    // TODO: Need to cleanup how session variables are maintained/referenced.
    // JSP pages should reference a previously defined constant and not a hard coded string.
    // Need to also evaluate using session directly versus using the "sandbox" approach built into the framework.
    private static final String PERSON_LIST = "personList";
	private static final String APP_USER 	= "appUser";
	private static final String SEL_PERSON 	= "selectedPerson";
	private static final String HEADER_BEAN = "veteranHeaderBean";
	private static final String SEL_VETERAN_ID 	= "selectedVeteranId";
	private static final String MESSAGE_LIST = "messageList";
	private static final String SEL_MESSAGE = "selectedMessage";
	private static final String UPD_PERSON	= "updatedPerson";
	private static final String SEL_WKF_CASE = "selectedWorkflowCase";
	private static final String CUR_WKF_LIST_MODE = "currentWorklistMode";
	private static final String WKF_LIST = "worklist";
	private static final String CAL_PERSON	= "calculatedPerson";
    public static final String QRYZ07_FACILITY_LIST_SESSION_KEY = "qryz07FacilityListSessionKey";
	
    public static final String SELECTED_FINANCIAL_INCOME_YEAR = "selectedFinancialIncomeYear";

    public static final String IS_ADD_A_PERSON = "isAddAPerson";
    
	private SessionManager()
	{	
	}

	/**
	 * used when Signout, which will clean all session information
	 * @param request
	 */
	public static void removeAppUser(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		removePersonList(request);
		session.removeAttribute(APP_USER);
	}	
	/**
	 * Used after logon.
	 * @param request
	 * @param obj SecurityContext
	 */
	public static void setAppUser(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
		removePersonList(request);
		session.setAttribute(APP_USER, obj);
	}	
	/**
	 * Used by header to display logon user's name,
	 * also used to set updated user for corrent logon user
	 * @param request
	 * @return the app user
	 */
	public static Object getAppUser(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(APP_USER);
	}

	
	/**
	 * Used by setAppUser or removeAppUser
	 * @param request
	 */
	private static void removePersonList(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		session.removeAttribute(PERSON_LIST);
		removeSelectedPerson(request);
	}
	/**
	 * Used by LogonAction
	 * @param request
	 * @param obj
	 */
	public static void setPersonList(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
		removeSelectedPerson(request);
	    session.setAttribute(PERSON_LIST, obj);
	}	
	/**
	 * Mainly to use this list and given AppUser id to get selecvted Person
	 * @param request
	 * @return the person list
	 */
	public static Object getPersonList(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(PERSON_LIST);
	}

	/**
	 * Used by setPersonList and removePersonList
	 * @param request
	 */
	private static void removeSelectedPerson(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		cleanPerson(request);
	    session.removeAttribute(SEL_PERSON);
	    session.removeAttribute(SEL_VETERAN_ID);
	}	
	/**
	 * TODO: Could be done with just pertyId
	 * @param request
	 * @param obj
	 * @param id
	 */
	public static void setSelectedPerson(HttpServletRequest request, Object obj, Object id)
	{
		HttpSession session = request.getSession();
		cleanPerson(request);
	    session.setAttribute(SEL_PERSON, obj);
	    session.setAttribute(SEL_VETERAN_ID, id);
	}	
	public static void setSelectedPerson(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
		cleanPerson(request);
	    session.setAttribute(SEL_PERSON, obj);
	    //should get partyId and set to session also
	}	
	public static Object getSelectedPerson(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(SEL_PERSON);
	}

	public static void removeVeteranHeaderBean(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    session.removeAttribute(HEADER_BEAN);
	}

	public static void setVeteranHeaderBean(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
	    session.setAttribute(HEADER_BEAN, obj);	    //should get partyId and set to session also
	}

	public static Object getVeteranHeaderBean(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(HEADER_BEAN);
	}

    public static void setSelectedFinancialIncomeYear(HttpServletRequest request, Integer incomeYear)
    {
        HttpSession session = request.getSession();
        session.setAttribute(SELECTED_FINANCIAL_INCOME_YEAR, incomeYear);
    }

    public static Integer getSelectedFinancialIncomeYear(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        return (Integer)session.getAttribute(SELECTED_FINANCIAL_INCOME_YEAR);
    }

    public static void removeSelectedFinancialIncomeYear(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        session.removeAttribute(SELECTED_FINANCIAL_INCOME_YEAR);
    }

	public static void setSelectedVeteranId(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
		removeSelectedPerson(request);
	    session.setAttribute(SEL_VETERAN_ID, obj);
	}	
	public static Object getSelectedVereranId(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(SEL_VETERAN_ID);
	}

	public static void removeMessageList(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		removeSelectedMessage(request);
	    session.removeAttribute(MESSAGE_LIST);
	}	
	public static void setMessageList(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
		removeSelectedMessage(request);
		session.setAttribute(MESSAGE_LIST, obj);
	}	
	public static Object getMessageList(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(MESSAGE_LIST);
	}

	public static void removeSelectedMessage(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    session.removeAttribute(SEL_MESSAGE);
	}	
	public static void setSelectedMessage(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
	    session.setAttribute(SEL_MESSAGE, obj);
	}	
	public static Object getSelectedMessage(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(SEL_MESSAGE);
	}
	
    public static void setSelectedWorkflowCase(HttpServletRequest request, Object wkfcase)
    {
        HttpSession session = request.getSession();
        session.setAttribute(SEL_WKF_CASE, wkfcase);
    }

    public static Object getSelectedWorkflowCase(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        return session.getAttribute(SEL_WKF_CASE);
    }

    public static void removeSelectedWorkflowCase(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        session.removeAttribute(SEL_WKF_CASE);
    }
	    
    public static void setWorklist(HttpServletRequest request, Object wklist)
    {
        HttpSession session = request.getSession();
        session.setAttribute(WKF_LIST, wklist);
    }

    public static Object getWorklist(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        return session.getAttribute(WKF_LIST);
    }

    public static void removeWorklist(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        session.removeAttribute(WKF_LIST);
    }

    public static void setCurrentWorklistMode(HttpServletRequest request, Object wklistMode)
    {
        HttpSession session = request.getSession();
        session.setAttribute(CUR_WKF_LIST_MODE, wklistMode);
    }

    public static Object getCurrentWorklistMode(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        return session.getAttribute(CUR_WKF_LIST_MODE);
    }

    public static void removeCurrentWorklistMode(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        session.removeAttribute(CUR_WKF_LIST_MODE);
    }
	public static void removeUpdatedPerson(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    session.removeAttribute(UPD_PERSON);
	}	
	public static void setUpdatedPerson(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
	    session.setAttribute(UPD_PERSON, obj);
	}	
	public static Object getUpdatedPerson(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(UPD_PERSON);
	}
	public static void removeCalculatedPerson(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    session.removeAttribute(CAL_PERSON);
	}	
	public static void setCalculatedPerson(HttpServletRequest request, Object obj)
	{
		HttpSession session = request.getSession();
	    session.setAttribute(CAL_PERSON, obj);
	}	
	public static Object getCalculatedPerson(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(CAL_PERSON);
	}

    // Query Z07 Facility List
    public static void removeQRYZ07FacilityList(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        session.removeAttribute(QRYZ07_FACILITY_LIST_SESSION_KEY);
    }
    public static void setQRYZ07FacilityList(HttpServletRequest request, List facilityList)
    {
        HttpSession session = request.getSession();
        session.setAttribute(QRYZ07_FACILITY_LIST_SESSION_KEY, facilityList);
    }
    public static List getQRYZ07FacilityList(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        return (List)session.getAttribute(QRYZ07_FACILITY_LIST_SESSION_KEY);
    }

    private static void cleanPerson(HttpServletRequest request)
	{
		//clean
		removeVeteranHeaderBean(request);
		removeMessageList(request);
		removeUpdatedPerson(request);
	}


    public static void setAttribute(HttpServletRequest request, String name, Object obj)
	{
		HttpSession session = request.getSession();
	    session.setAttribute(name, obj);
	}	
	public static Object getAttribute(HttpServletRequest request, String name)
	{
		HttpSession session = request.getSession();
	    return session.getAttribute(name);
	}
	
	// CCR 10471 Add A Person
	// To make the Add a Person flag session aware, HttpServletRequest object is required
	public static boolean isAddAPerson(HttpServletRequest request)
	{
		Boolean isAddAPerson = new Boolean(false);
		
		// Get the appData out of the session
		HttpSession session = request.getSession();
		Map appData = (Map) session.getAttribute(UIConstants.APP_DATA);
		
		if (appData != null) {
			Boolean addAPersonFromSession = (Boolean) appData.get(ApplicationConstants.SessionData.ADD_A_PERSON);
			if (addAPersonFromSession!= null)
				isAddAPerson = addAPersonFromSession;
		}
		
		return isAddAPerson.booleanValue();
	}
	
}
