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

import gov.nih.nci.cabig.caaers.dao.DiseaseTermDao;
import gov.nih.nci.cabig.caaers.dao.MeddraVersionDao;
import gov.nih.nci.cabig.caaers.dao.meddra.LowLevelTermDao;
import gov.nih.nci.cabig.caaers.domain.CtepStudyDisease;
import gov.nih.nci.cabig.caaers.domain.DiseaseCodeTerm;
import gov.nih.nci.cabig.caaers.domain.DiseaseTerm;
import gov.nih.nci.cabig.caaers.domain.MeddraStudyDisease;
import gov.nih.nci.cabig.caaers.domain.Study;
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.Map;

import javax.servlet.http.HttpServletRequest;

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>
 */
public class DiseaseTab extends StudyTab {
    private static Log log = LogFactory.getLog(DiseaseTab.class);

    private DiseaseTermDao diseaseTermDao;
    private LowLevelTermDao lowLevelTermDao;
    private MeddraVersionDao meddraVersionDao;

    public DiseaseTab() {
        super("Study 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.
     */
    @Override
    protected void validate(Study command, BeanWrapper commandBean,
    		Map<String, InputFieldGroup> fieldGroups, Errors errors) {
    	
    	HashMap<String, DiseaseTerm> ctepTermMap = new HashMap<String, DiseaseTerm>();
    	for(CtepStudyDisease ctepDisease : command.getCtepStudyDiseases()){
    		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("DUPLICATE", "'" + ctepTermMap.get(newCTEPTermId).getFullName() + "' is already associated to this study");
    			}
    		}
    	}
    	
    	HashMap<String, LowLevelTerm> medraTermMap = new HashMap<String, LowLevelTerm>();
    	for(MeddraStudyDisease meddraStudyDisease : command.getMeddraStudyDiseases()){
    		medraTermMap.put(meddraStudyDisease.getTerm().getId().toString(), meddraStudyDisease.getTerm());
    	}
    	if(command.getDiseaseLlt() != null){
    		if(medraTermMap.containsKey(command.getDiseaseLlt())) {
    			errors.reject("DUPLICATE", "'" + medraTermMap.get(command.getDiseaseLlt()).getFullName()+ "' is already associated to this study");
    		}
    	}
    }
    
    @Override
    public void postProcess(HttpServletRequest request, Study command, Errors errors) {
    	super.postProcess(request, command, errors);
    	if(!errors.hasErrors()){
    		handleStudyDiseaseAction(command, request.getParameter("_action"),
    				request.getParameter("_selected"));
    	}
    }
    
    @Override
    public Map<String, Object> referenceData(Study command) {<commit_msg>fix for  : [#12074] Problem adding disease to study using MedDRA terminology<commit_after>package gov.nih.nci.cabig.caaers.web.study;

import gov.nih.nci.cabig.caaers.dao.DiseaseTermDao;
import gov.nih.nci.cabig.caaers.dao.MeddraVersionDao;
import gov.nih.nci.cabig.caaers.dao.meddra.LowLevelTermDao;
import gov.nih.nci.cabig.caaers.domain.CtepStudyDisease;
import gov.nih.nci.cabig.caaers.domain.DiseaseCodeTerm;
import gov.nih.nci.cabig.caaers.domain.DiseaseTerm;
import gov.nih.nci.cabig.caaers.domain.MeddraStudyDisease;
import gov.nih.nci.cabig.caaers.domain.Study;
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.Map;

import javax.servlet.http.HttpServletRequest;

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>
 */
public class DiseaseTab extends StudyTab {
    private static Log log = LogFactory.getLog(DiseaseTab.class);

    private DiseaseTermDao diseaseTermDao;
    private LowLevelTermDao lowLevelTermDao;
    private MeddraVersionDao meddraVersionDao;

    public DiseaseTab() {
        super("Study 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.
     */
    @Override
    protected void validate(Study command, BeanWrapper commandBean,
    		Map<String, InputFieldGroup> fieldGroups, Errors errors) {
    	
    	HashMap<String, DiseaseTerm> ctepTermMap = new HashMap<String, DiseaseTerm>();
    	for(CtepStudyDisease ctepDisease : command.getCtepStudyDiseases()){
    		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("DUPLICATE", "'" + ctepTermMap.get(newCTEPTermId).getFullName() + "' is already associated to this study");
    			}
    		}
    	}
    	
    	HashMap<String, LowLevelTerm> medraTermMap = new HashMap<String, LowLevelTerm>();
    	for(MeddraStudyDisease meddraStudyDisease : command.getMeddraStudyDiseases()){
    		medraTermMap.put(meddraStudyDisease.getTerm().getId().toString(), meddraStudyDisease.getTerm());
    	}
    	if(command.getDiseaseLlt() != null){
    		if(medraTermMap.containsKey(command.getDiseaseLlt())) {
    			errors.reject("DUPLICATE", "'" + medraTermMap.get(command.getDiseaseLlt()).getFullName()+ "' is already associated to this study");
    			command.setDiseaseLlt(null);
    		}
    	}
    }
    
    @Override
    public void postProcess(HttpServletRequest request, Study command, Errors errors) {
    	super.postProcess(request, command, errors);
    	if(!errors.hasErrors()){
    		handleStudyDiseaseAction(command, request.getParameter("_action"),
    				request.getParameter("_selected"));
    		command.setDiseaseLlt(null);
    	}
    }
    
    @Override
    public Map<String, Object> referenceData(Study command) {
{'fraction_non_alphanumeric': 0.112981455064194, 'fraction_numerical': 0.001854493580599144, 'mean_word_length': 3.9512711864406778, 'pattern_counts': {'":': 0, '<': 19, '<?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': '3143367', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2399, 'n_words': 416}