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

import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import gov.nih.nci.cabig.caaers.domain.ReportStatus;
import gov.nih.nci.cabig.caaers.domain.expeditedfields.ExpeditedReportSection;
import gov.nih.nci.cabig.caaers.domain.report.Report;
import gov.nih.nci.cabig.caaers.service.ErrorMessages;
import gov.nih.nci.cabig.caaers.service.EvaluationService;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroupMap;

/**
 * @author Krikor Krumlian
 */
public class ViewReportTab extends AeTab {
	private EvaluationService evaluationService;

    public ViewReportTab() {
        super("Submission", "Submit", "ae/submit");

    }
    
    @Override
    public void postProcess(HttpServletRequest request, ExpeditedAdverseEventInputCommand command, Errors errors) {

        handleWithdrawAction(command, request.getParameter("_action"),
            request.getParameter("_selected"));
    }
    
    @Override
    public void onDisplay(HttpServletRequest request, ExpeditedAdverseEventInputCommand command) {
       updateReports(command);
    }
    
    //  Set a property in the Report object to provide a clean way of accessing this on the JSP
    private void updateReports(ExpeditedAdverseEventInputCommand command){
    	
    		for (Report report : command.getAeReport().getReports()) {
    			ErrorMessages errorMessages = evaluationService.isSubmittable(report);
    			report.setDataMissing(errorMessages.hasErrors());
    			System.out.println(errorMessages.hasErrors());
			}
    }
    
    private void handleWithdrawAction(ExpeditedAdverseEventInputCommand command, String action, String selected) {
        if ("withdraw".equals(action) ) {
        	
        	for (Report report : command.getAeReport().getReports()) {
            	if (report.getId().equals(selected) && !report.getLastVersion().getReportStatus().equals(ReportStatus.COMPLETED)){
            		reportService.withdrawLastReportVersion(report);
         	        break;
         		}
    		}
        }
    }
    
    

    @Override
    @SuppressWarnings("unchecked")
    public InputFieldGroupMap createFieldGroups(ExpeditedAdverseEventInputCommand command) {
        InputFieldGroupMap groups = new InputFieldGroupMap();
        return groups;
    }

    @Override
    protected void validate(
        ExpeditedAdverseEventInputCommand command, BeanWrapper commandBean,
        Map<String, InputFieldGroup> fieldGroups, Errors errors
    ) {
    }
    
    ////// CONFIGURATION

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

    @Override
    public ExpeditedReportSection section() {
    	return ExpeditedReportSection.SUBMIT_REPORT_SECTION;
    }
}

@@ -39,171 +39,8 @@
 e;%0A%0A
-import org.springframework.beans.BeanWrapper;%0Aimport org.springframework.validation.Errors;%0A%0Aimport java.util.Map;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0A%0A
 impo
@@ -393,16 +393,19 @@
 eldGroup
+Map
 ;%0Aimport
@@ -409,62 +409,92 @@
 ort 
-gov.nih.nci.cabig.caaers.web.fields.InputFieldGroupMap
+org.springframework.validation.Errors;%0A%0Aimport javax.servlet.http.HttpServletRequest
 ;%0A%0A/
@@ -2310,210 +2310,8 @@
  %7D%0A%0A
-    @Override%0A    protected void validate(%0A        ExpeditedAdverseEventInputCommand command, BeanWrapper commandBean,%0A        Map%3CString, InputFieldGroup%3E fieldGroups, Errors errors%0A    ) %7B%0A    %7D%0A    %0A
     

Remove empty override of empty template method