package edu.duke.cabig.c3pr.web.registration.tabs;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.Errors;
import org.springframework.web.util.WebUtils;

import edu.duke.cabig.c3pr.domain.Epoch;
import edu.duke.cabig.c3pr.domain.ScheduledEpoch;
import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.domain.SubjectEligibilityAnswer;
import edu.duke.cabig.c3pr.web.registration.RegistrationControllerUtils;
import edu.duke.cabig.c3pr.web.registration.StudySubjectWrapper;

/**
 * Created by IntelliJ IDEA. User: kherm Date: Jun 15, 2007 Time: 3:30:05 PM To change this template
 * use File | Settings | File Templates.
 */
public class EligibilityCriteriaTab extends RegistrationTab<StudySubjectWrapper> {

    public EligibilityCriteriaTab() {
        super("Check Eligibility", "Check Eligibility", "registration/reg_check_eligibility");
    }

    @Override
    public Map<String, Object> referenceData(StudySubjectWrapper command) {
        Map ref = new HashMap();
        boolean requiresEligibility = false;
        if (command.getStudySubject().getScheduledEpoch()!=null) if (( command.getStudySubject()
                        .getScheduledEpoch().getEpoch()).getEligibilityCriteria().size() > 0) requiresEligibility = true;
        ref.put("requiresEligibility", requiresEligibility);
        return ref;
    }

    @Override
    public void postProcess(HttpServletRequest request, StudySubjectWrapper command, Errors error) {
    	StudySubjectWrapper wrapper = (StudySubjectWrapper) command ;
    	StudySubject studySubject = wrapper.getStudySubject();
//        if (studySubject.getScheduledEpoch()!=null) {
//            (studySubject.getScheduledEpoch()).setEligibilityIndicator(registrationControllerUtils.evaluateEligibilityIndicator(command.getStudySubject()));
//        }
        
        // The following code for building scheduled epoch after moving subject to a new epoch.
        if(WebUtils.hasSubmitParameter(request, "epoch")){
        	Integer id;
	        try {
	            id = Integer.parseInt(request.getParameter("epoch"));
	        }
	        catch (RuntimeException e) {
	            return;
	        }
	      
	        Epoch epoch = epochDao.getById(id);
	        epochDao.initialize(epoch);
	        ScheduledEpoch scheduledEpoch;
	        if (epoch.getTreatmentIndicator()) {
	            (epoch).getArms().size();
	            scheduledEpoch = new ScheduledEpoch();
	        }
	        else {
	            scheduledEpoch = new ScheduledEpoch();
	        }
	        scheduledEpoch.setEpoch(epoch);
	        studySubject.getScheduledEpochs().add(scheduledEpoch);
	   //     registrationControllerUtils.buildCommandObject(studySubject);
	        studySiteDao.initialize(studySubject.getStudySite());
        }
    }

    
}

@@ -75,31 +75,8 @@
 ap;%0A
-import java.util.List;%0A
 impo
@@ -93,16 +93,16 @@
 il.Map;%0A
+
 %0Aimport 
@@ -377,141 +377,8 @@
 ct;%0A
-import edu.duke.cabig.c3pr.domain.SubjectEligibilityAnswer;%0Aimport edu.duke.cabig.c3pr.web.registration.RegistrationControllerUtils;%0A
 impo
@@ -1534,244 +1534,8 @@
 ();%0A
-//        if (studySubject.getScheduledEpoch()!=null) %7B%0A//            (studySubject.getScheduledEpoch()).setEligibilityIndicator(registrationControllerUtils.evaluateEligibilityIndicator(command.getStudySubject()));%0A//        %7D%0A        %0A
     
@@ -2259,32 +2259,32 @@
 etEpoch(epoch);%0A
+
 %09        studySu
@@ -2335,81 +2335,8 @@
 h);%0A
-%09   //     registrationControllerUtils.buildCommandObject(studySubject);%0A
 %09   

clean up