/*
 * Created on May 22, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.ee.action;

import java.util.Map;
import java.util.HashMap;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import gov.va.med.esr.common.model.lookup.AgentOrangeExposureLocation;
import gov.va.med.esr.common.model.lookup.MonetaryBenefitType;
import gov.va.med.esr.common.model.person.Person;

import gov.va.med.esr.ui.util.ConvertUtils;
import gov.va.med.esr.ui.util.SessionManager;
import gov.va.med.esr.ui.conversion.UIConversionService;

/**
 * @author DNS   chenb
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ServicePeriodFormAction extends EligibilityAction {

	/**
	 * 
	 */
	public ServicePeriodFormAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}
	
	public ActionForward update( ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response )
	throws Exception 
	{

		ServicePeriodForm servicePeriodForm = (form instanceof ServicePeriodForm) ? (ServicePeriodForm)form : null;
		Person updatedPerson = getUpdatedPerson( request );
		
		
		if( updatedPerson != null ) 
		{
			UIConversionService uiConversionService = (UIConversionService)conversionService;
			uiConversionService.convert(servicePeriodForm, updatedPerson);
			uiConversionService.merge((Person)getSelectedPerson( request ), updatedPerson);
			
			boolean isAddAPerson = false;
	        isAddAPerson = SessionManager.isAddAPerson(request);
	        if (!isAddAPerson)
	        	updatedPerson = getEnrollmentService().updateEligibilityData(updatedPerson);
			if (updatedPerson != null)
			{
				updateSelectedPerson(request, updatedPerson);
				setUpdatedPerson(request, null);					
			}
		}
		return mapping.findForward("eligibilityView");
	}
	
	public ActionForward cancel( ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response )
	throws Exception 
	{
		return mapping.findForward("cancel");
	}
	
	
	/**
     * handle one form have multiple submit button.
     * 
     * @see org.apache.struts.actions.LookupDispatchAction#getKeyMethodMap()
     * @return
     */
   protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.update","update");
        map.put("button.cancel","cancel");
        
        return map;
    }

}
