/**
 * <p>Title: SpecimenEventParametersAction Class>
 * <p>Description:	This class initializes the fields in the SpecimenEventParametersAction Add/Edit webpage.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author Mandar Deshmukh
 * @version 1.00
 * Created on July 28, 2005
 */

package edu.wustl.catissuecore.action;

import java.util.Calendar;
import java.util.Collection;

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

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

import edu.wustl.catissuecore.actionForm.EventParametersForm;
import edu.wustl.catissuecore.bizlogic.BizLogicFactory;
import edu.wustl.catissuecore.bizlogic.UserBizLogic;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.action.BaseAction;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.util.logger.Logger;

/**
 * This class initializes the fields in the SpecimenEventParameters Add/Edit webpage.
 * It is a parent class for some Event Parameter Classes.
 * @author mandar deshmukh
 */

public class SpecimenEventParametersAction  extends BaseAction
{
	/**
	 * This method sets all the common parameters for the SpecimenEventParameter pages
	 * @param request HttpServletRequest instance in which the data will be set. 
	 * @throws Exception Throws Exception. Helps in handling exceptions at one common point.
	 */
	private void setCommonRequestParameters(HttpServletRequest request) throws Exception
	{
        //Gets the value of the operation parameter.
       
		String operation = request.getParameter(Constants.OPERATION);
        //Sets the operation attribute to be used in the Add/Edit FrozenEventParameters Page. 
        request.setAttribute(Constants.OPERATION, operation);
        
        //Sets the minutesList attribute to be used in the Add/Edit FrozenEventParameters Page.
        request.setAttribute("minutesList", Constants.MINUTES_ARRAY);

        //Sets the hourList attribute to be used in the Add/Edit FrozenEventParameters Page.
        request.setAttribute("hourList", Constants.HOUR_ARRAY);
         
        //The id of specimen of this event.
        String specimenId = request.getParameter(Constants.SPECIMEN_ID); 
        request.setAttribute(Constants.SPECIMEN_ID, specimenId);
        Logger.out.debug("\t\t SpecimenEventParametersAction************************************ : "+specimenId );
        
       	UserBizLogic userBizLogic = (UserBizLogic)BizLogicFactory.getInstance().getBizLogic(Constants.USER_FORM_ID);
    	Collection userCollection =  userBizLogic.getUsers(operation);
    	
    	request.setAttribute(Constants.USERLIST, userCollection);
    	
    	
    	
        	
	}
    
    protected ActionForward executeAction(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
    	setCommonRequestParameters(request);

    	EventParametersForm eventParametersForm = (EventParametersForm)form;
    	
    	//	 if operation is add
    	if(eventParametersForm.isAddOperation())
    	{
    		if(eventParametersForm.getUserId()==0)
    		{
    			SessionDataBean sessionData = getSessionData(request);
    			if(sessionData!=null && sessionData.getUserId()!=null)
    			{
    				long userId = sessionData.getUserId().longValue();
    				eventParametersForm.setUserId(userId);
    			}
    		}
    		// set the current Date and Time for the event.
			Calendar cal = Calendar.getInstance();
			if(eventParametersForm.getDateOfEvent()==null)
			{
				eventParametersForm.setDateOfEvent(Utility.parseDateToString(cal.getTime(), Constants.DATE_PATTERN_MM_DD_YYYY));
			}
			if(eventParametersForm.getTimeInHours()==null)
			{
				eventParametersForm.setTimeInHours(Integer.toString(cal.get(Calendar.HOUR_OF_DAY)));
			}
			if(eventParametersForm.getTimeInMinutes()==null)
			{
				eventParametersForm.setTimeInMinutes(Integer.toString(cal.get(Calendar.MINUTE)));
			}
	
    	}
    	//Changes by Anup
    	
        String operation = (String) request.getAttribute(Constants.OPERATION);
        String pageOf=(String)request.getAttribute(Constants.PAGE_OF);
        eventParametersForm.setPageOf(pageOf);
        eventParametersForm.setOperation(operation);
        
        
		String currentEventParametersDate = "";
		currentEventParametersDate = eventParametersForm.getDateOfEvent();
		if(currentEventParametersDate == null)
		{
			currentEventParametersDate = "";
		}
		
		Integer eventParametersYear = new Integer(Utility.getYear(currentEventParametersDate ));
		Integer eventParametersMonth = new Integer(Utility.getMonth(currentEventParametersDate ));
		Integer eventParametersDay = new Integer(Utility.getDay(currentEventParametersDate ));
		request.setAttribute("eventParametersYear", eventParametersYear);
		request.setAttribute("eventParametersDay", eventParametersDay);
		request.setAttribute("eventParametersMonth", eventParametersMonth);
		request.setAttribute("addForJSP", Constants.ADD);
		request.setAttribute("editForJSP", Constants.EDIT);
		request.setAttribute("currentEventParametersDate", currentEventParametersDate);
		request.setAttribute("userListforJSP", Constants.USERLIST);
		// This method will be overridden by the sub classes
		setRequestParameters(request, eventParametersForm);
		
    	return mapping.findForward((String)request.getParameter(Constants.PAGEOF));
    }
    
    /*  This method will be overridden by the sub classes
     * It is called from setCommonRequestParameters().
     * It will be used to set the SubClass specific parameters.
     */  
    protected void setRequestParameters(HttpServletRequest request, EventParametersForm eventParametersForm) throws Exception
	{
	}
    
	
}
@@ -749,24 +749,95 @@
 etersForm;%0D%0A
+import edu.wustl.catissuecore.actionForm.SpecimenEventParametersForm;%0D%0A
 import edu.w
@@ -4353,24 +4353,421 @@
 %0A%09%0D%0A    %09%7D%0D%0A
+    %09else%0D%0A    %09%7B%0D%0A    %09%09String specimenId = (String)request.getAttribute(Constants.SPECIMEN_ID);%0D%0A            if(specimenId == null)%0D%0A            %7B%0D%0A            %09SpecimenEventParametersForm sepF =((SpecimenEventParametersForm)eventParametersForm); %0D%0A            %09specimenId = %22%22+sepF.getSpecimenId();%0D%0A            %09request.setAttribute(Constants.SPECIMEN_ID, specimenId);%0D%0A            %7D%0D%0A    %09%7D%0D%0A
     %09//Chang

set specimenid in request in case of edit of event parameters. For event parameters edit was generating exceptions due to missing specimenid.