package gov.nih.nci.cabig.caaers.web.ae;

import gov.nih.nci.cabig.caaers.dao.ParticipantDao;
import gov.nih.nci.cabig.caaers.dao.StudyDao;
import gov.nih.nci.cabig.caaers.dao.StudyParticipantAssignmentDao;
import gov.nih.nci.cabig.caaers.domain.StudyParticipantAssignment;
import gov.nih.nci.cabig.caaers.service.EvaluationService;
import gov.nih.nci.cabig.caaers.web.ControllerTools;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * @author Rhett Sutphin
 */
public class ListAdverseEventsController extends SimpleFormController {
    private StudyParticipantAssignmentDao assignmentDao;
    private ParticipantDao participantDao;
    private StudyDao studyDao;
    protected EvaluationService evaluationService;

    public ListAdverseEventsController() {
        setCommandClass(ListAdverseEventsCommand.class);
        setBindOnNewForm(true);
        setFormView("ae/selectAssignment");
        setSuccessView("ae/list");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        return new ListAdverseEventsCommand(assignmentDao, studyDao, participantDao, evaluationService);
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        ControllerTools.registerGridDomainObjectEditor(binder, "participant", participantDao);
        ControllerTools.registerGridDomainObjectEditor(binder, "study", studyDao);
        ControllerTools.registerGridDomainObjectEditor(binder, "assignment", assignmentDao);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected boolean isFormSubmission(HttpServletRequest request) {
        Set<String> paramNames = request.getParameterMap().keySet();
        boolean hasParticipant
            =  paramNames.contains("participant")
            || paramNames.contains("mrn")
            || paramNames.contains("assignment");
        boolean hasStudy
            =  paramNames.contains("study") 
            || paramNames.contains("nciIdentifier")
            || paramNames.contains("assignment");
        boolean hasStudySubjectGridId = paramNames.contains("studySubjectGridId");
        return (hasParticipant && hasStudy) || hasStudySubjectGridId;
    }
    @Override
    protected void onBind(HttpServletRequest request, Object command,BindException errors) throws Exception {
    	super.onBind(request, command, errors);
    	ListAdverseEventsCommand listAECmd = (ListAdverseEventsCommand) command;
    	String assignmentGridId = request.getParameter("studySubjectGridId");
    	listAECmd.setAssignment(assignmentDao.getByGridId(assignmentGridId));
    	listAECmd.setParticipant(listAECmd.getAssignment().getParticipant());
    	listAECmd.setStudy(listAECmd.getAssignment().getStudySite().getStudy());
    }
    
    @Override
    protected void onBindAndValidate(HttpServletRequest request,Object command, BindException errors) throws Exception {
    	super.onBindAndValidate(request, command, errors);
    	ListAdverseEventsCommand listAECmd = (ListAdverseEventsCommand) command;
    	 boolean noStudy = listAECmd.getStudy() == null;
         boolean noParticipant = listAECmd.getParticipant() == null;
    	if (noStudy) errors.rejectValue("study", "REQUIRED", "Missing study");
        if (noParticipant) errors.rejectValue("participant", "REQUIRED", "Missing participant");
        if (!(noStudy || noParticipant) && listAECmd.getAssignment() == null) {
            errors.reject("REQUIRED", "The participant is not assigned to the provided study");
        }
    }
    
    @Override
    @SuppressWarnings("unchecked")
    protected Map referenceData(
        HttpServletRequest request, Object command, Errors errors
    ) throws Exception {
        Map<String, Object> refdata = new HashMap<String, Object>();
        refdata.put("pageTitle", "Manage AEs: Select Participant & Study");
        refdata.put("bodyTitle", "Manage AEs: Select Participant & Study");
        refdata.put("instructions", "Select a participant and study to see all the AEs for that combination.");
        return refdata;
    }

    ////// CONFIGURATION

    public void setAssignmentDao(StudyParticipantAssignmentDao assignmentDao) {
        this.assignmentDao = assignmentDao;
    }

    public void setParticipantDao(ParticipantDao participantDao) {
        this.participantDao = participantDao;
    }

    public void setStudyDao(StudyDao studyDao) {
        this.studyDao = studyDao;
    }

	public EvaluationService getEvaluationService() {
		return evaluationService;
	}

	public void setEvaluationService(EvaluationService evaluationService) {
		this.evaluationService = evaluationService;
	}
    
    
    
}

@@ -2867,22 +2867,127 @@
    %09
-listAECmd.setA
+//forwarded from external system (eg : Labviewer)%0A    %09if(assignmentGridId != null)%7B%0A    %09%09StudyParticipantAssignment a
 ssig
@@ -2991,17 +2991,19 @@
 signment
-(
+ = 
 assignme
@@ -3037,24 +3037,134 @@
 tGridId)
+;%0A    %09%09//able to load the assignment?%0A    %09%09if(assignment != null)%7B%0A    %09%09%09listAECmd.setAssignment(assignment
 );%0A    %09
 listAECm
@@ -3147,32 +3147,34 @@
 signment);%0A    %09
+%09%09
 listAECmd.setPar
@@ -3232,16 +3232,18 @@
 );%0A    %09
+%09%09
 listAECm
@@ -3303,24 +3303,39 @@
 etStudy());%0A
+    %09%09%7D%0A    %09%7D%0A
     %7D%0A    %0A 

Fix for hot links