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

import gov.nih.nci.cabig.caaers.domain.*;
import gov.nih.nci.cabig.caaers.utils.DateUtils;
import gov.nih.nci.cabig.caaers.web.fields.*;
import gov.nih.nci.cabig.caaers.web.utils.WebUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author <NAME>
 * @author <NAME>
 * 
 */
public class AdverseEventCaptureTab extends AdverseEventTab {
	
	// The names of the field groups
    private static final String MAIN_FIELD_GROUP = "main";
    private static final String OUTCOME_FIELD_GROUP ="outcomes";
    private static final String TAB_NAME = AdverseEventCaptureTab.class.getName();
    
    //max characters allowed for Verbatim
    private static final Integer VERBATIM_MAX_SIZE = 65;
    
    public AdverseEventCaptureTab() {
        super("Enter Adverse Events", "Adverse Events", "ae/captureAdverseEvents");
        setAutoPopulateHelpKey(true);
    }


    /**
     * This method will create the fields to be displayed on the screen.
     *  For CTC/MedDRA Study the following fields will be created:
     *    0. Other(MedDRA) - [conditional, only appear for CTC studies, if term is otherspecify].
     *    1. Verbatim
     *    2. Grade
     *    3. StartDate
     *    4. End Date
     *    5. Attribution To Study
     *    6. Time Of Event
     *    7. Event Location
     *    8. Hospitalization
     *    9. Expectedness
     *   10. Outcome
     *   
     * Note:- 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 fieldGrpMap = new InputFieldGroupMap();
        MultipleFieldGroupFactory mainFieldFactory;
        
        if(cmd.getAdverseEventReportingPeriod() != null){
        	
        	Study study = cmd.getAdverseEventReportingPeriod().getStudy();
        	boolean isMeddraStudy = study.getAeTerminology().getTerm() == Term.MEDDRA;
        	int size = cmd.getAdverseEvents().size();
        	
        	//create one field group - One row of main groups
            mainFieldFactory = new MultipleFieldGroupFactory(MAIN_FIELD_GROUP, "adverseEvents");
            
        	for (int i = 0; i < size; i++) {
        		
                AdverseEvent ae = cmd.getAdverseEvents().get(i);
                if (ae == null) continue;
                
                boolean unRetired = !ae.isRetired();
                
                //other MedDRA
        		InputField otherMeddraField = (ae.getSolicited()) ? InputFieldFactory.createLabelField("lowLevelTerm.meddraTerm", "Other (MedDRA)", false & unRetired) :
        															InputFieldFactory.createAutocompleterField("lowLevelTerm", "Other (MedDRA)", true & unRetired);
        		//only add otherMedDRA on non MedDRA and otherRequired=true
                if(ae.getAdverseEventTerm().isOtherRequired() && study.getOtherMeddra() != null){
                	mainFieldFactory.addField(otherMeddraField);
                }
                
            	//verbatim - Is required when there is no other MedDRA
                boolean verbatimMandatory = (study.getOtherMeddra() == null) && (ae.getAdverseEventTerm().isOtherRequired());
                boolean verbatimApplicable = caaersFieldConfigurationManager.isFieldApplicable(TAB_NAME, "aeReport.adverseEvents.detailsForOther");
                boolean verbatimRequired =  verbatimApplicable && (verbatimMandatory || isFieldRequired(ae, "adverseEvents[].detailsForOther"));
            	InputField verbatimField = InputFieldFactory.createTextField("detailsForOther", "Verbatim", "aeReport.adverseEvents.detailsForOther", verbatimRequired);
                InputFieldAttributes.setSize(verbatimField, 25);
                mainFieldFactory.addField(verbatimField);
                
                //grade
                InputField gradeField = InputFieldFactory.createLongSelectField("grade","Grade", "aeReport.adverseEvents.grade", isFieldRequired(ae, "adverseEvents[].grade"), createGradeOptions(ae, isMeddraStudy ? "Meddra" : "Ctc"));
                mainFieldFactory.addField(gradeField);
                
                //startDate
                InputField startDateField = InputFieldFactory.createPastDateField("startDate", "Start date", "aeReport.adverseEvents.startDate", isFieldRequired(ae, "adverseEvents[].startDate"));
                mainFieldFactory.addField(startDateField);
                
                //endDate
                InputField endDateField = InputFieldFactory.createPastDateField("endDate", "End date", "aeReport.adverseEvents.endDate", isFieldRequired(ae, "adverseEvents[].endDate"));
                mainFieldFactory.addField(endDateField);
                
                //attribution
                InputField attributionField = InputFieldFactory.createSelectField("attributionSummary", "Attribution to study intervention", "aeReport.adverseEvents.attributionSummary", isFieldRequired(ae, "adverseEvents[].attributionSummary"), createAttributionOptions());
                mainFieldFactory.addField(attributionField);
                
                //Hospitalization
                InputField hospitalizationField = InputFieldFactory.createSelectField("hospitalization", "Did AE cause hospitalization?", "aeReport.adverseEvents.hospitalization", isFieldRequired(ae, "adverseEvents[].hospitalization"), createHospitalizationOptions());
                mainFieldFactory.addField(hospitalizationField);
                
                //expectedness
                InputField expectednessField = InputFieldFactory.createSelectField("expected", "Expected", "aeReport.adverseEvents.expected", isFieldRequired(ae, "adverseEvents[].expected"), createExpectedOptions());
                mainFieldFactory.addField(expectednessField);
                
                //Time of event
                InputField timeOfEventField = createTimeField("eventApproximateTime", "Event time", "aeReport.adverseEvents.eventApproximateTime.hourString");
                mainFieldFactory.addField(timeOfEventField);
                
                //Participant at risk
                InputField riskField = InputFieldFactory.createBooleanSelectField("participantAtRisk", "Does this place participant at increased risk?", "aeReport.adverseEvents.participantAtRisk", isFieldRequired(ae, "adverseEvents[].participantAtRisk"));
                mainFieldFactory.addField(riskField);
                
                //Event location
                InputField eventLocationField = InputFieldFactory.createTextField("eventLocation", "Where was the patient when the event occurred?", "aeReport.adverseEvents.eventLocation", isFieldRequired(ae, "adverseEvents[].eventLocation"));
                mainFieldFactory.addField(eventLocationField);

                InputFieldGroup fieldGroup = mainFieldFactory.createGroup(i);
                mainFieldFactory.addFieldGroup(fieldGroup);
                mainFieldFactory.clearFields();
                
                
                
                //now add the fields related to outcomes
                InputFieldGroup outcomeFieldGrp = new DefaultInputFieldGroup(OUTCOME_FIELD_GROUP + i);
                List<InputField> outcomeFields = outcomeFieldGrp.getFields();
                Map<Integer, Boolean> oneOutcomeMap = cmd.getOutcomes().get(i);

                for (Integer code : oneOutcomeMap.keySet()) {
                    OutcomeType outcomeType = OutcomeType.getByCode(code);

                    outcomeFields.add(InputFieldFactory.createCheckboxField("outcomes[" + i + "][" + code + "]", outcomeType.getDisplayName()));

                    if (outcomeType == OutcomeType.OTHER_SERIOUS) {
                        outcomeFields.add(InputFieldFactory.createTextField("outcomeOtherDetails[" + i + "]", ""));
                    }
                }
                fieldGrpMap.addInputFieldGroup(outcomeFieldGrp);
        	}
        	
        	 fieldGrpMap.addMultipleFieldGroupFactory(mainFieldFactory);
        }
        
        return fieldGrpMap;
    }
    
    private boolean isFieldRequired(AdverseEvent ae, String fieldPath){
    	if(ae.getSolicited() || ae.isRetired())
    		return false;
    	else{
    		return caaersFieldConfigurationManager.isFieldMandatory(TAB_NAME, fieldPath);
    	}
    }

    @Override
    public void beforeBind(HttpServletRequest request, CaptureAdverseEventInputCommand command) {
        super.beforeBind(request, command);
        
        command.reassociate();
    }

    @Override
    public Map<String, Object> referenceData(CaptureAdverseEventInputCommand command) {
        //initalize the seriousness outcome indicators
        command.initializeOutcomes();<commit_msg>Mandatory field configuration and business rules not enforced on Verbatim field in Capture AE CAAERS-4020<commit_after>package gov.nih.nci.cabig.caaers.web.ae;

import gov.nih.nci.cabig.caaers.domain.*;
import gov.nih.nci.cabig.caaers.utils.DateUtils;
import gov.nih.nci.cabig.caaers.web.fields.*;
import gov.nih.nci.cabig.caaers.web.utils.WebUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author <NAME>
 * @author <NAME>
 * 
 */
public class AdverseEventCaptureTab extends AdverseEventTab {
	
	// The names of the field groups
    private static final String MAIN_FIELD_GROUP = "main";
    private static final String OUTCOME_FIELD_GROUP ="outcomes";
    private static final String TAB_NAME = AdverseEventCaptureTab.class.getName();
    
    //max characters allowed for Verbatim
    private static final Integer VERBATIM_MAX_SIZE = 65;
    
    public AdverseEventCaptureTab() {
        super("Enter Adverse Events", "Adverse Events", "ae/captureAdverseEvents");
        setAutoPopulateHelpKey(true);
    }


    /**
     * This method will create the fields to be displayed on the screen.
     *  For CTC/MedDRA Study the following fields will be created:
     *    0. Other(MedDRA) - [conditional, only appear for CTC studies, if term is otherspecify].
     *    1. Verbatim
     *    2. Grade
     *    3. StartDate
     *    4. End Date
     *    5. Attribution To Study
     *    6. Time Of Event
     *    7. Event Location
     *    8. Hospitalization
     *    9. Expectedness
     *   10. Outcome
     *   
     * Note:- 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 fieldGrpMap = new InputFieldGroupMap();
        MultipleFieldGroupFactory mainFieldFactory;
        
        if(cmd.getAdverseEventReportingPeriod() != null){
        	
        	Study study = cmd.getAdverseEventReportingPeriod().getStudy();
        	boolean isMeddraStudy = study.getAeTerminology().getTerm() == Term.MEDDRA;
        	int size = cmd.getAdverseEvents().size();
        	
        	//create one field group - One row of main groups
            mainFieldFactory = new MultipleFieldGroupFactory(MAIN_FIELD_GROUP, "adverseEvents");
            
        	for (int i = 0; i < size; i++) {
        		
                AdverseEvent ae = cmd.getAdverseEvents().get(i);
                if (ae == null) continue;
                
                boolean unRetired = !ae.isRetired();
                
                //other MedDRA
        		InputField otherMeddraField = (ae.getSolicited()) ? InputFieldFactory.createLabelField("lowLevelTerm.meddraTerm", "Other (MedDRA)", false & unRetired) :
        															InputFieldFactory.createAutocompleterField("lowLevelTerm", "Other (MedDRA)", true & unRetired);
        		//only add otherMedDRA on non MedDRA and otherRequired=true
                if(ae.getAdverseEventTerm().isOtherRequired() && study.getOtherMeddra() != null){
                	mainFieldFactory.addField(otherMeddraField);
                }
                
            	//verbatim - Is required when there is no other MedDRA
                boolean verbatimMandatory = (study.getOtherMeddra() == null) && (ae.getAdverseEventTerm().isOtherRequired());
                boolean verbatimApplicable = caaersFieldConfigurationManager.isFieldApplicable(TAB_NAME, "adverseEvents.detailsForOther");
                boolean verbatimRequired =  verbatimApplicable && (verbatimMandatory || isFieldRequired(ae, "adverseEvents[].detailsForOther"));
            	InputField verbatimField = InputFieldFactory.createTextField("detailsForOther", "Verbatim", "aeReport.adverseEvents.detailsForOther", verbatimRequired);
                InputFieldAttributes.setSize(verbatimField, 25);
                mainFieldFactory.addField(verbatimField);
                
                //grade
                InputField gradeField = InputFieldFactory.createLongSelectField("grade","Grade", "aeReport.adverseEvents.grade", isFieldRequired(ae, "adverseEvents[].grade"), createGradeOptions(ae, isMeddraStudy ? "Meddra" : "Ctc"));
                mainFieldFactory.addField(gradeField);
                
                //startDate
                InputField startDateField = InputFieldFactory.createPastDateField("startDate", "Start date", "aeReport.adverseEvents.startDate", isFieldRequired(ae, "adverseEvents[].startDate"));
                mainFieldFactory.addField(startDateField);
                
                //endDate
                InputField endDateField = InputFieldFactory.createPastDateField("endDate", "End date", "aeReport.adverseEvents.endDate", isFieldRequired(ae, "adverseEvents[].endDate"));
                mainFieldFactory.addField(endDateField);
                
                //attribution
                InputField attributionField = InputFieldFactory.createSelectField("attributionSummary", "Attribution to study intervention", "aeReport.adverseEvents.attributionSummary", isFieldRequired(ae, "adverseEvents[].attributionSummary"), createAttributionOptions());
                mainFieldFactory.addField(attributionField);
                
                //Hospitalization
                InputField hospitalizationField = InputFieldFactory.createSelectField("hospitalization", "Did AE cause hospitalization?", "aeReport.adverseEvents.hospitalization", isFieldRequired(ae, "adverseEvents[].hospitalization"), createHospitalizationOptions());
                mainFieldFactory.addField(hospitalizationField);
                
                //expectedness
                InputField expectednessField = InputFieldFactory.createSelectField("expected", "Expected", "aeReport.adverseEvents.expected", isFieldRequired(ae, "adverseEvents[].expected"), createExpectedOptions());
                mainFieldFactory.addField(expectednessField);
                
                //Time of event
                InputField timeOfEventField = createTimeField("eventApproximateTime", "Event time", "aeReport.adverseEvents.eventApproximateTime.hourString");
                mainFieldFactory.addField(timeOfEventField);
                
                //Participant at risk
                InputField riskField = InputFieldFactory.createBooleanSelectField("participantAtRisk", "Does this place participant at increased risk?", "aeReport.adverseEvents.participantAtRisk", isFieldRequired(ae, "adverseEvents[].participantAtRisk"));
                mainFieldFactory.addField(riskField);
                
                //Event location
                InputField eventLocationField = InputFieldFactory.createTextField("eventLocation", "Where was the patient when the event occurred?", "aeReport.adverseEvents.eventLocation", isFieldRequired(ae, "adverseEvents[].eventLocation"));
                mainFieldFactory.addField(eventLocationField);

                InputFieldGroup fieldGroup = mainFieldFactory.createGroup(i);
                mainFieldFactory.addFieldGroup(fieldGroup);
                mainFieldFactory.clearFields();
                
                
                
                //now add the fields related to outcomes
                InputFieldGroup outcomeFieldGrp = new DefaultInputFieldGroup(OUTCOME_FIELD_GROUP + i);
                List<InputField> outcomeFields = outcomeFieldGrp.getFields();
                Map<Integer, Boolean> oneOutcomeMap = cmd.getOutcomes().get(i);

                for (Integer code : oneOutcomeMap.keySet()) {
                    OutcomeType outcomeType = OutcomeType.getByCode(code);

                    outcomeFields.add(InputFieldFactory.createCheckboxField("outcomes[" + i + "][" + code + "]", outcomeType.getDisplayName()));

                    if (outcomeType == OutcomeType.OTHER_SERIOUS) {
                        outcomeFields.add(InputFieldFactory.createTextField("outcomeOtherDetails[" + i + "]", ""));
                    }
                }
                fieldGrpMap.addInputFieldGroup(outcomeFieldGrp);
        	}
        	
        	 fieldGrpMap.addMultipleFieldGroupFactory(mainFieldFactory);
        }
        
        return fieldGrpMap;
    }
    
    private boolean isFieldRequired(AdverseEvent ae, String fieldPath){
    	if(ae.getSolicited() || ae.isRetired())
    		return false;
    	else{
    		return caaersFieldConfigurationManager.isFieldMandatory(TAB_NAME, fieldPath);
    	}
    }

    @Override
    public Map<String, Object> referenceData(CaptureAdverseEventInputCommand command) {
        //initalize the seriousness outcome indicators
        command.initializeOutcomes();
{'fraction_non_alphanumeric': 0.09220679012345678, 'fraction_numerical': 0.0020943562610229276, 'mean_word_length': 2.6545820745216515, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '2502580', 'n_tokens_mistral': 4934, 'n_tokens_neox': 4630, 'n_words': 951}