<commit_before>package gov.nih.nci.cabig.caaers.web.ae;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.BooleanUtils;
import org.drools.util.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

import gov.nih.nci.cabig.caaers.dao.AdverseEventReportingPeriodDao;
import gov.nih.nci.cabig.caaers.dao.CtcTermDao;
import gov.nih.nci.cabig.caaers.domain.AdverseEvent;
import gov.nih.nci.cabig.caaers.domain.AdverseEventCtcTerm;
import gov.nih.nci.cabig.caaers.domain.AdverseEventReportingPeriod;
import gov.nih.nci.cabig.caaers.domain.Arm;
import gov.nih.nci.cabig.caaers.domain.Attribution;
import gov.nih.nci.cabig.caaers.domain.CtcGrade;
import gov.nih.nci.cabig.caaers.domain.Epoch;
import gov.nih.nci.cabig.caaers.domain.Grade;
import gov.nih.nci.cabig.caaers.domain.Hospitalization;
import gov.nih.nci.cabig.caaers.domain.SolicitedAdverseEvent;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.Term;
import gov.nih.nci.cabig.caaers.domain.TreatmentAssignment;
import gov.nih.nci.cabig.caaers.domain.UserGroupType;
import gov.nih.nci.cabig.caaers.web.fields.DefaultInputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputField;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldAttributes;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldFactory;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroupMap;
import gov.nih.nci.cabig.caaers.web.fields.MultipleFieldGroupFactory;
import gov.nih.nci.cabig.caaers.web.fields.RepeatingFieldGroupFactory;
import gov.nih.nci.cabig.caaers.web.fields.TabWithFields;
import gov.nih.nci.cabig.caaers.web.participant.AssignParticipantStudyCommand;
import gov.nih.nci.cabig.caaers.web.participant.NewParticipantCommand;

/**
 * 
 * @author <NAME>
 *
 */
public class AdverseEventCaptureTab extends AdverseEventTab{
	
	private static final String MAIN_FIELD_GROUP = "main";
	
	public AdverseEventCaptureTab() {
		super("Enter Adverse Events", "Adverse events", "ae/captureAdverseEvents");
		addHelpKeyExclusion("ctcVersion");
	}
	
	
	/**
	 * This method will create the fields to be displayed on the screen.
	 * Notes<br>
	 * 	1. For solicited adverse events, the "Notes/Verbatim", "Other Meddra" will added to the fields.
	 * 	2. If Study is MedDRA, the "Other MedDRA", will not be added in the fields. 	 
	 *  3. We should run the adverse events against the index fixed list, since that list will have null items in it, we should skip if 'AdverseEvent' is null.
	 */
	
	@Override
	public Map<String, InputFieldGroup> createFieldGroups(CaptureAdverseEventInputCommand cmd) {
		
		InputFieldGroupMap map = new InputFieldGroupMap();
		MultipleFieldGroupFactory mainFieldFactory;
		List<SolicitedAdverseEvent> saeList;

		// Creating the field groups for the first section of the page
		// which collects the general information from the user (eg., TAC, TAC Description, Start date of first course etc.
		InputFieldGroup reportingPeriodFieldGroup = new DefaultInputFieldGroup("reportingPeriodFG");
		List<InputField> fields = reportingPeriodFieldGroup.getFields();
		InputField reportingPeriodsField = InputFieldFactory.createSelectField("adverseEventReportingPeriod", "Evaluation period", true, fetchReportingPeriodsOptions(cmd));
		fields.add(reportingPeriodsField);
		map.addInputFieldGroup(reportingPeriodFieldGroup);
		
		//create the fields, consisting of reporting period details.
		if(cmd.getAdverseEventReportingPeriod() != null){
			
			InputFieldGroup treatmentAssignmentFieldGroup = new DefaultInputFieldGroup("treatmentAssignmentFG"); 
			InputFieldGroup reportingPeriodDetailsFieldGroup = new DefaultInputFieldGroup("reportingPeriodDetailsFG");
			
			//TAC fields groups
			InputField treatmentAssignmentField = InputFieldFactory.createLabelField("adverseEventReportingPeriod.treatmentAssignment.code", "Treatment assignment");
			InputField treatmentAssignmentDescField = InputFieldFactory.createLabelField("adverseEventReportingPeriod.treatmentAssignment.description", "Treatment description");
			
			//startDateOfFirstCourse - TextField, if it is empty in assignment
			InputField firstCourseDateField = null;
			if(cmd.getAssignment().getStartDateOfFirstCourse() == null){
				firstCourseDateField = InputFieldFactory.createDateField("assignment.startDateOfFirstCourse", "Start date of first course", false);
			}else {
				firstCourseDateField = InputFieldFactory.createLabelField("assignment.startDateOfFirstCourse", "Start date of first course");
			}

			treatmentAssignmentFieldGroup.getFields().add(treatmentAssignmentField);
			treatmentAssignmentFieldGroup.getFields().add(treatmentAssignmentDescField);
			treatmentAssignmentFieldGroup.getFields().add(firstCourseDateField);
		
			// add reportingPeriod details group
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.startDate", "Start date"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.endDate", "End date"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.epoch.name", "Type"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.cycleNumber", "Cycle number"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.description", "Description"));
			
			// add the reportingPeriodFieldGroup to the map.
			map.addInputFieldGroup(treatmentAssignmentFieldGroup);
			map.addInputFieldGroup(reportingPeriodDetailsFieldGroup);
		
			
			/*
			 * AdversEvent related field groups,
			 *  the fields are different for Meddra study, Ctc study and Observed AEs
			 */
			mainFieldFactory = new MultipleFieldGroupFactory(MAIN_FIELD_GROUP, "adverseEvents");
			boolean isMeddraStudy = cmd.getStudy().getAeTerminology().getTerm() == Term.MEDDRA;

			int size = cmd.getAdverseEvents().size();
			for(int i = 0; i < size; i++){
				AdverseEvent ae = cmd.getAdverseEvents().get(i);
				if(ae == null) continue;
				
				mainFieldFactory.addField(InputFieldFactory.createLabelField("adverseEventTerm.universalTerm", "Term", true)); //Term
				if(!ae.getSolicited()){
					if(!isMeddraStudy && ae.getAdverseEventTerm().isOtherRequired()){ //only if other is requrired
						mainFieldFactory.addField(InputFieldFactory.createAutocompleterField("lowLevelTerm", "Other(MedDRA)", false));
					}
					InputField notesField = InputFieldFactory.createTextField("detailsForOther", "Notes/Verbatim");
					InputFieldAttributes.setSize(notesField, 50);
					mainFieldFactory.addField(notesField); //Notes
				}
				//grade	
				if(isMeddraStudy){
					// Make grade mandatory only for observed aes.
					if(ae.getSolicited())
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", false, createGradeOptions(ae, "Meddra")));
					else
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", true, createGradeOptions(ae, "Meddra")));
				}else{
					if(ae.getSolicited())
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", false, createGradeOptions(ae, "Ctc")));
					else
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", true, createGradeOptions(ae, "Ctc")));
				}
				
				if(ae.getSolicited())
					mainFieldFactory.addField(InputFieldFactory.createSelectField("attributionSummary", "Attribution to study", false, createAttributionOptions()));
				else
					mainFieldFactory.addField(InputFieldFactory.createSelectField("attributionSummary", "Attribution to study", true, createAttributionOptions()));
				mainFieldFactory.addField(InputFieldFactory.createSelectField("hospitalization",
						"Hospitalization", false, createHospitalizationOptions()));
				mainFieldFactory.addField(InputFieldFactory.createSelectField("expected", "Expected", false,
						createExpectedOptions()));
				mainFieldFactory.addField(InputFieldFactory.createSelectField("serious", "Serious", false, createSeriousOptions()));
				InputFieldGroup fieldGroup = mainFieldFactory.createGroup(i);
				mainFieldFactory.addFieldGroup(fieldGroup);
				mainFieldFactory.clearFields();
				
			}
			map.addMultipleFieldGroupFactory(mainFieldFactory);
		}
		
		return map;
	}
	
	@Override
	public void beforeBind(HttpServletRequest request,CaptureAdverseEventInputCommand command) {
		super.beforeBind(request, command);
	}

	
	@Override
    public Map<String, Object> referenceData(HttpServletRequest request, CaptureAdverseEventInputCommand command) {
		// Setup the categories list for aeTermQuery tag.
		command.getCtcCategories();
		
		//initalize the seriousness outcome indicators
		command.initializeOutcome();
		
		return super.referenceData(request, command);
		
	}
	
	@Override
	public void postProcess(HttpServletRequest request,	CaptureAdverseEventInputCommand command, Errors errors) {
		if(findInRequest(request, CaptureAdverseEventController.AJAX_SUBVIEW_PARAMETER) != null) return ; //ignore if this is an ajax request
		//sync the seriousness outcomes
		command.synchronizeOutcome();
	}
	
	@Override
	public void onBind(HttpServletRequest request,CaptureAdverseEventInputCommand command, Errors errors) {
		String rpId = request.getParameter("adverseEventReportingPeriod");
		if(StringUtils.isEmpty(rpId)) {
			command.setAdverseEventReportingPeriod(null);
		}
		Set<String> paramNames = request.getParameterMap().keySet();
		boolean fromListPage = false;
        fromListPage = paramNames.contains("displayReportingPeriod");
        if(fromListPage) 
        	command.refreshAssignment(Integer.decode(rpId));
	}
	
	@Override
    protected void validate(CaptureAdverseEventInputCommand command, BeanWrapper commandBean,
                    Map<String, InputFieldGroup> fieldGroups, Errors errors) {<commit_msg>CAAERS - 474<commit_after>package gov.nih.nci.cabig.caaers.web.ae;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.BooleanUtils;
import org.drools.util.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

import gov.nih.nci.cabig.caaers.dao.AdverseEventReportingPeriodDao;
import gov.nih.nci.cabig.caaers.dao.CtcTermDao;
import gov.nih.nci.cabig.caaers.domain.AdverseEvent;
import gov.nih.nci.cabig.caaers.domain.AdverseEventCtcTerm;
import gov.nih.nci.cabig.caaers.domain.AdverseEventReportingPeriod;
import gov.nih.nci.cabig.caaers.domain.Arm;
import gov.nih.nci.cabig.caaers.domain.Attribution;
import gov.nih.nci.cabig.caaers.domain.CtcGrade;
import gov.nih.nci.cabig.caaers.domain.Epoch;
import gov.nih.nci.cabig.caaers.domain.Grade;
import gov.nih.nci.cabig.caaers.domain.Hospitalization;
import gov.nih.nci.cabig.caaers.domain.SolicitedAdverseEvent;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.Term;
import gov.nih.nci.cabig.caaers.domain.TreatmentAssignment;
import gov.nih.nci.cabig.caaers.domain.UserGroupType;
import gov.nih.nci.cabig.caaers.web.fields.DefaultInputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputField;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldAttributes;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldFactory;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroupMap;
import gov.nih.nci.cabig.caaers.web.fields.MultipleFieldGroupFactory;
import gov.nih.nci.cabig.caaers.web.fields.RepeatingFieldGroupFactory;
import gov.nih.nci.cabig.caaers.web.fields.TabWithFields;
import gov.nih.nci.cabig.caaers.web.participant.AssignParticipantStudyCommand;
import gov.nih.nci.cabig.caaers.web.participant.NewParticipantCommand;

/**
 * 
 * @author <NAME>
 *
 */
public class AdverseEventCaptureTab extends AdverseEventTab{
	
	private static final String MAIN_FIELD_GROUP = "main";
	
	public AdverseEventCaptureTab() {
		super("Enter Adverse Events", "Adverse events", "ae/captureAdverseEvents");
		addHelpKeyExclusion("ctcVersion");
	}
	
	
	/**
	 * This method will create the fields to be displayed on the screen.
	 * Notes<br>
	 * 	1. For solicited adverse events, the "Notes/Verbatim", "Other Meddra" will added to the fields.
	 * 	2. If Study is MedDRA, the "Other MedDRA", will not be added in the fields. 	 
	 *  3. We should run the adverse events against the index fixed list, since that list will have null items in it, we should skip if 'AdverseEvent' is null.
	 */
	
	@Override
	public Map<String, InputFieldGroup> createFieldGroups(CaptureAdverseEventInputCommand cmd) {
		
		InputFieldGroupMap map = new InputFieldGroupMap();
		MultipleFieldGroupFactory mainFieldFactory;
		List<SolicitedAdverseEvent> saeList;

		// Creating the field groups for the first section of the page
		// which collects the general information from the user (eg., TAC, TAC Description, Start date of first course etc.
		InputFieldGroup reportingPeriodFieldGroup = new DefaultInputFieldGroup("reportingPeriodFG");
		List<InputField> fields = reportingPeriodFieldGroup.getFields();
		InputField reportingPeriodsField = InputFieldFactory.createSelectField("adverseEventReportingPeriod", "Evaluation period", true, fetchReportingPeriodsOptions(cmd));
		fields.add(reportingPeriodsField);
		map.addInputFieldGroup(reportingPeriodFieldGroup);
		
		//create the fields, consisting of reporting period details.
		if(cmd.getAdverseEventReportingPeriod() != null){
			
			InputFieldGroup treatmentAssignmentFieldGroup = new DefaultInputFieldGroup("treatmentAssignmentFG"); 
			InputFieldGroup reportingPeriodDetailsFieldGroup = new DefaultInputFieldGroup("reportingPeriodDetailsFG");
			
			//TAC fields groups
			InputField treatmentAssignmentField = InputFieldFactory.createLabelField("adverseEventReportingPeriod.treatmentAssignment.code", "Treatment assignment");
			InputField treatmentAssignmentDescField = InputFieldFactory.createLabelField("adverseEventReportingPeriod.treatmentAssignment.description", "Treatment description");
			
			//startDateOfFirstCourse - TextField, if it is empty in assignment
			InputField firstCourseDateField = null;
			if(cmd.getAssignment().getStartDateOfFirstCourse() == null){
				firstCourseDateField = InputFieldFactory.createDateField("assignment.startDateOfFirstCourse", "Start date of first course", false);
			}else {
				firstCourseDateField = InputFieldFactory.createLabelField("assignment.startDateOfFirstCourse", "Start date of first course");
			}

			treatmentAssignmentFieldGroup.getFields().add(treatmentAssignmentField);
			treatmentAssignmentFieldGroup.getFields().add(treatmentAssignmentDescField);
			treatmentAssignmentFieldGroup.getFields().add(firstCourseDateField);
		
			// add reportingPeriod details group
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.startDate", "Start date"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.endDate", "End date"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.epoch.name", "Type"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.cycleNumber", "Cycle number"));
			reportingPeriodDetailsFieldGroup.getFields().add(InputFieldFactory.createLabelField("adverseEventReportingPeriod.description", "Description"));
			
			// add the reportingPeriodFieldGroup to the map.
			map.addInputFieldGroup(treatmentAssignmentFieldGroup);
			map.addInputFieldGroup(reportingPeriodDetailsFieldGroup);
		
			
			/*
			 * AdversEvent related field groups,
			 *  the fields are different for Meddra study, Ctc study and Observed AEs
			 */
			mainFieldFactory = new MultipleFieldGroupFactory(MAIN_FIELD_GROUP, "adverseEvents");
			boolean isMeddraStudy = cmd.getStudy().getAeTerminology().getTerm() == Term.MEDDRA;

			int size = cmd.getAdverseEvents().size();
			for(int i = 0; i < size; i++){
				AdverseEvent ae = cmd.getAdverseEvents().get(i);
				if(ae == null) continue;
				if(isMeddraStudy)
					mainFieldFactory.addField(InputFieldFactory.createLabelField("lowLevelTerm.meddraTerm", "Term", true));
				else
					mainFieldFactory.addField(InputFieldFactory.createLabelField("adverseEventTerm.universalTerm", "Term", true)); //Term
				
				if(!ae.getSolicited()){
					if(!isMeddraStudy && ae.getAdverseEventTerm().isOtherRequired()){ //only if other is requrired
						mainFieldFactory.addField(InputFieldFactory.createAutocompleterField("lowLevelTerm", "Other(MedDRA)", false));
					}
					InputField notesField = InputFieldFactory.createTextField("detailsForOther", "Notes/Verbatim");
					InputFieldAttributes.setSize(notesField, 50);
					mainFieldFactory.addField(notesField); //Notes
				}
				//grade	
				if(isMeddraStudy){
					// Make grade mandatory only for observed aes.
					if(ae.getSolicited())
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", false, createGradeOptions(ae, "Meddra")));
					else
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", true, createGradeOptions(ae, "Meddra")));
				}else{
					if(ae.getSolicited())
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", false, createGradeOptions(ae, "Ctc")));
					else
						mainFieldFactory.addField(InputFieldFactory.createSelectField("grade", "Grade", true, createGradeOptions(ae, "Ctc")));
				}
				
				if(ae.getSolicited())
					mainFieldFactory.addField(InputFieldFactory.createSelectField("attributionSummary", "Attribution to study", false, createAttributionOptions()));
				else
					mainFieldFactory.addField(InputFieldFactory.createSelectField("attributionSummary", "Attribution to study", true, createAttributionOptions()));
				mainFieldFactory.addField(InputFieldFactory.createSelectField("hospitalization",
						"Hospitalization", false, createHospitalizationOptions()));
				mainFieldFactory.addField(InputFieldFactory.createSelectField("expected", "Expected", false,
						createExpectedOptions()));
				mainFieldFactory.addField(InputFieldFactory.createSelectField("serious", "Serious", false, createSeriousOptions()));
				InputFieldGroup fieldGroup = mainFieldFactory.createGroup(i);
				mainFieldFactory.addFieldGroup(fieldGroup);
				mainFieldFactory.clearFields();
				
			}
			map.addMultipleFieldGroupFactory(mainFieldFactory);
		}
		
		return map;
	}
	
	@Override
	public void beforeBind(HttpServletRequest request,CaptureAdverseEventInputCommand command) {
		super.beforeBind(request, command);
	}

	
	@Override
    public Map<String, Object> referenceData(HttpServletRequest request, CaptureAdverseEventInputCommand command) {
		// Setup the categories list for aeTermQuery tag.
		boolean isCTCStudy = command.getStudy().getAeTerminology().getTerm() == Term.CTC;
		if(isCTCStudy)
			command.getCtcCategories();
		
		//initalize the seriousness outcome indicators
		command.initializeOutcome();
		
		return super.referenceData(request, command);
		
	}
	
	@Override
	public void postProcess(HttpServletRequest request,	CaptureAdverseEventInputCommand command, Errors errors) {
		if(findInRequest(request, CaptureAdverseEventController.AJAX_SUBVIEW_PARAMETER) != null) return ; //ignore if this is an ajax request
		//sync the seriousness outcomes
		command.synchronizeOutcome();
	}
	
	@Override
	public void onBind(HttpServletRequest request,CaptureAdverseEventInputCommand command, Errors errors) {
		String rpId = request.getParameter("adverseEventReportingPeriod");
		if(StringUtils.isEmpty(rpId)) {
			command.setAdverseEventReportingPeriod(null);
		}
		Set<String> paramNames = request.getParameterMap().keySet();
		boolean fromListPage = false;
        fromListPage = paramNames.contains("displayReportingPeriod");
        if(fromListPage) 
        	command.refreshAssignment(Integer.decode(rpId));
	}
	
	@Override
    protected void validate(CaptureAdverseEventInputCommand command, BeanWrapper commandBean,
                    Map<String, InputFieldGroup> fieldGroups, Errors errors) {
{'fraction_non_alphanumeric': 0.10338020030816641, 'fraction_numerical': 0.0007222650231124807, 'mean_word_length': 5.756343526350032, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '21562', 'n_tokens_mistral': 6890, 'n_tokens_neox': 6087, 'n_words': 1099}