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

import gov.nih.nci.cabig.caaers.dao.ConditionDao;
import gov.nih.nci.cabig.caaers.dao.DiseaseTermDao;
import gov.nih.nci.cabig.caaers.dao.meddra.LowLevelTermDao;
import gov.nih.nci.cabig.caaers.domain.*;
import gov.nih.nci.cabig.caaers.domain.meddra.LowLevelTerm;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class DiseaseTab extends StudyTab {
    private static Log log = LogFactory.getLog(DiseaseTab.class);

    private DiseaseTermDao diseaseTermDao;
    private LowLevelTermDao lowLevelTermDao;
    private ConditionDao conditionDao;
    private HashMap<String, Condition> conditionMap;
    
    public DiseaseTab() {
        super("Disease", "Disease", "study/study_diseases");
    }

    /*
     * If CTEP Study Disease 1. Check if the existing CTEP disease ID is mentioned in
     * study.getDiseaseTermIds() 2. Throw error, saying that the selected disease term already
     * exists.
     * 
     * If Medra Study Disease 1. Check if the existing MEDRA disease (LowLevelTerm) is mentioned in
     * study.getDiseaseLlt() 2. Throw error, saying that the selected disease already present.
     * 
     * If Study Condition, if already existing will throw error. 
     */
    @Override
    protected void validate(StudyCommand command, BeanWrapper commandBean, Map<String, InputFieldGroup> fieldGroups, Errors errors) {

        HashMap<String, DiseaseTerm> ctepTermMap = new HashMap<String, DiseaseTerm>();
        for (CtepStudyDisease ctepDisease : command.getStudy().getCtepStudyDiseases()) {
        	if(ctepDisease.isRetired()) continue;
            ctepTermMap.put(ctepDisease.getTerm().getId().toString(), ctepDisease.getDiseaseTerm());
        }

        String[] newCTEPTermIds = command.getDiseaseTermIds();
        if (newCTEPTermIds != null) {
            for (String newCTEPTermId : newCTEPTermIds) {
                if (ctepTermMap.containsKey(newCTEPTermId)) {
                    errors.reject("STU_005", new Object[]{ctepTermMap.get(newCTEPTermId).getFullName()}, "'" + ctepTermMap.get(newCTEPTermId).getFullName() + "' is already associated to this study");
                }
            }
        }

        HashMap<String, LowLevelTerm> medraTermMap = new HashMap<String, LowLevelTerm>();
        for (MeddraStudyDisease meddraStudyDisease : command.getStudy().getMeddraStudyDiseases()) {
        	if(meddraStudyDisease.isRetired()) continue;
            medraTermMap.put(meddraStudyDisease.getTerm().getId().toString(), meddraStudyDisease.getTerm());
        }

        if (command.getDiseaseLlt() != null) {
            if (medraTermMap.containsKey(command.getDiseaseLlt())) {
                errors.reject("STU_005",new Object[]{medraTermMap.get(command.getDiseaseLlt()).getFullName()}, "'" + medraTermMap.get(command.getDiseaseLlt()).getFullName() + "' is already associated to this study");
                command.setDiseaseLlt(null);
            }
        }

        if (checkDuplicateConditionById(conditionMap, command.getCondition())) {
            errors.reject("DUPLICATE_STUDY_CONDITION", new Object[] {(conditionMap.get(command.getCondition())).getConditionName()}, "");
        }
    }

    @Override
    public void postProcess(HttpServletRequest request, StudyCommand command, Errors errors) {
        super.postProcess(request, command, errors);
        if (!errors.hasErrors()) {
            handleStudyDiseaseAction(errors, command, request.getParameter("_action"), request.getParameter("_selected"), request);
            command.setDiseaseLlt(null);
        }
    }

    @Override
    public void onBind(HttpServletRequest request, StudyCommand command, Errors errors) {
        super.onBind(request, command, errors);
        if (command.getPrimaryStudyDisease() == null) return;

        List<? extends AbstractStudyDisease> ls = command.getStudy().getActiveStudyDiseases();
        if (ls != null && ls.size() > 0) {
            for (byte i=0; i< ls.size(); i++) {
                AbstractStudyDisease d = ls.get(i);
                d.setLeadDisease(i == command.getPrimaryStudyDisease().intValue());
            }
        }
    }

    @Override
    public Map<String, Object> referenceData(HttpServletRequest request, StudyCommand command) {
        Map<String, Object> refdata = super.referenceData(command);
        Study study = command.getStudy();

        // this will hold the Study Conditions' IDs as keys
        conditionMap = new HashMap<String, Condition>();
        for (StudyCondition studyCondition : study.getStudyConditions()) {
        	if(studyCondition.isRetired()) continue;
            conditionMap.put(studyCondition.getTerm().getId().toString(), studyCondition.getTerm());
        }

        List<? extends AbstractStudyDisease> ls = command.getStudy().getActiveStudyDiseases();
        if (ls != null && ls.size() > 0) {
            for (byte i=0; i< ls.size(); i++) {
                AbstractStudyDisease d = ls.get(i);
                if (d.getLeadDisease() != null && d.getLeadDisease()) command.setPrimaryStudyDisease(new Integer(i));
            }
        }

        String diseaseTerminology = "MEDDRA";
        if (study.getDiseaseTerminology().getDiseaseCodeTerm() == DiseaseCodeTerm.CTEP) diseaseTerminology = "CTEP";
        else if (study.getDiseaseTerminology().getDiseaseCodeTerm() == DiseaseCodeTerm.OTHER) diseaseTerminology = "OTHER";
        refdata.put("diseaseTerminology", diseaseTerminology);

        if (study.getDiseaseTerminology().getDiseaseCodeTerm().equals(DiseaseCodeTerm.MEDDRA)) {
            refdata.put("meddraVersionId", study.getDiseaseTerminology().getMeddraVersion().getId());<commit_msg>CAAERS-3792<commit_after>package gov.nih.nci.cabig.caaers.web.study;

import gov.nih.nci.cabig.caaers.dao.ConditionDao;
import gov.nih.nci.cabig.caaers.dao.DiseaseTermDao;
import gov.nih.nci.cabig.caaers.dao.meddra.LowLevelTermDao;
import gov.nih.nci.cabig.caaers.domain.*;
import gov.nih.nci.cabig.caaers.domain.meddra.LowLevelTerm;
import gov.nih.nci.cabig.caaers.web.fields.InputFieldGroup;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.validation.Errors;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class DiseaseTab extends StudyTab {
    private static Log log = LogFactory.getLog(DiseaseTab.class);

    private DiseaseTermDao diseaseTermDao;
    private LowLevelTermDao lowLevelTermDao;
    private ConditionDao conditionDao;
    private HashMap<String, Condition> conditionMap;
    
    public DiseaseTab() {
        super("Disease", "Disease", "study/study_diseases");
    }

    /*
     * If CTEP Study Disease 1. Check if the existing CTEP disease ID is mentioned in
     * study.getDiseaseTermIds() 2. Throw error, saying that the selected disease term already
     * exists.
     * 
     * If Medra Study Disease 1. Check if the existing MEDRA disease (LowLevelTerm) is mentioned in
     * study.getDiseaseLlt() 2. Throw error, saying that the selected disease already present.
     * 
     * If Study Condition, if already existing will throw error. 
     */
    @Override
    protected void validate(StudyCommand command, BeanWrapper commandBean, Map<String, InputFieldGroup> fieldGroups, Errors errors) {

        HashMap<String, DiseaseTerm> ctepTermMap = new HashMap<String, DiseaseTerm>();
        for (CtepStudyDisease ctepDisease : command.getStudy().getCtepStudyDiseases()) {
        	if(ctepDisease.isRetired()) continue;
            ctepTermMap.put(ctepDisease.getTerm().getId().toString(), ctepDisease.getDiseaseTerm());
        }

        String[] newCTEPTermIds = command.getDiseaseTermIds();
        if (newCTEPTermIds != null) {
            for (String newCTEPTermId : newCTEPTermIds) {
                if (ctepTermMap.containsKey(newCTEPTermId)) {
                    errors.reject("STU_005", new Object[]{ctepTermMap.get(newCTEPTermId).getFullName()}, "'" + ctepTermMap.get(newCTEPTermId).getFullName() + "' is already associated to this study");
                }
            }
        }

        HashMap<String, LowLevelTerm> medraTermMap = new HashMap<String, LowLevelTerm>();
        for (MeddraStudyDisease meddraStudyDisease : command.getStudy().getMeddraStudyDiseases()) {
        	if(meddraStudyDisease.isRetired()) continue;
            medraTermMap.put(meddraStudyDisease.getTerm().getId().toString(), meddraStudyDisease.getTerm());
        }

        if (command.getDiseaseLlt() != null) {
            if (medraTermMap.containsKey(command.getDiseaseLlt())) {
                errors.reject("STU_005",new Object[]{medraTermMap.get(command.getDiseaseLlt()).getFullName()}, "'" + medraTermMap.get(command.getDiseaseLlt()).getFullName() + "' is already associated to this study");
                command.setDiseaseLlt(null);
            }
        }

        if (checkDuplicateConditionById(conditionMap, command.getCondition())) {
            errors.reject("DUPLICATE_STUDY_CONDITION", new Object[] {(conditionMap.get(command.getCondition())).getConditionName()}, "");
        }
    }

    @Override
    public void postProcess(HttpServletRequest request, StudyCommand command, Errors errors) {
        super.postProcess(request, command, errors);
        if (!errors.hasErrors()) {
            handleStudyDiseaseAction(errors, command, request.getParameter("_action"), request.getParameter("_selected"), request);
            command.setDiseaseLlt(null);
        }
    }

    @Override
    public void onBind(HttpServletRequest request, StudyCommand command, Errors errors) {
        super.onBind(request, command, errors);
        if (command.getPrimaryStudyDisease() == null) return;

        List<? extends AbstractStudyDisease> ls = command.getStudy().getActiveStudyDiseases();
        if (ls != null && ls.size() > 0) {
            for (byte i=0; i< ls.size(); i++) {
                AbstractStudyDisease d = ls.get(i);
                d.setLeadDisease(i == command.getPrimaryStudyDisease().intValue());
            }
        }
    }

    @Override
    public Map<String, Object> referenceData(HttpServletRequest request, StudyCommand command) {
        Map<String, Object> refdata = super.referenceData(command);
        Study study = command.getStudy();

        if (study.getDiseaseTerminology() == null || study.getDiseaseTerminology().getDiseaseCodeTerm() == null) return refdata;

        // this will hold the Study Conditions' IDs as keys
        conditionMap = new HashMap<String, Condition>();
        for (StudyCondition studyCondition : study.getStudyConditions()) {
        	if(studyCondition.isRetired()) continue;
            conditionMap.put(studyCondition.getTerm().getId().toString(), studyCondition.getTerm());
        }

        List<? extends AbstractStudyDisease> ls = command.getStudy().getActiveStudyDiseases();
        if (ls != null && ls.size() > 0) {
            for (byte i=0; i< ls.size(); i++) {
                AbstractStudyDisease d = ls.get(i);
                if (d.getLeadDisease() != null && d.getLeadDisease()) command.setPrimaryStudyDisease(new Integer(i));
            }
        }

        String diseaseTerminology = "MEDDRA";
        if (study.getDiseaseTerminology().getDiseaseCodeTerm() == DiseaseCodeTerm.CTEP) diseaseTerminology = "CTEP";
        else if (study.getDiseaseTerminology().getDiseaseCodeTerm() == DiseaseCodeTerm.OTHER) diseaseTerminology = "OTHER";
        refdata.put("diseaseTerminology", diseaseTerminology);

        if (study.getDiseaseTerminology().getDiseaseCodeTerm().equals(DiseaseCodeTerm.MEDDRA)) {
            refdata.put("meddraVersionId", study.getDiseaseTerminology().getMeddraVersion().getId());
{'fraction_non_alphanumeric': 0.1199583866837388, 'fraction_numerical': 0.002560819462227913, 'mean_word_length': 3.33021483021483, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '7491443', 'n_tokens_mistral': 3937, 'n_tokens_neox': 3679, 'n_words': 718}