<commit_before>package gov.nih.nci.cabig.caaers.service.migrator.adverseevent;

import gov.nih.nci.cabig.caaers.CaaersSystemException;
import gov.nih.nci.cabig.caaers.api.impl.AdverseEventManagementServiceImpl;
import gov.nih.nci.cabig.caaers.dao.CtcTermDao;
import gov.nih.nci.cabig.caaers.dao.meddra.LowLevelTermDao;
import gov.nih.nci.cabig.caaers.domain.AdverseEvent;
import gov.nih.nci.cabig.caaers.domain.AdverseEventCtcTerm;
import gov.nih.nci.cabig.caaers.domain.AdverseEventMeddraLowLevelTerm;
import gov.nih.nci.cabig.caaers.domain.AeTerminology;
import gov.nih.nci.cabig.caaers.domain.Attribution;
import gov.nih.nci.cabig.caaers.domain.Ctc;
import gov.nih.nci.cabig.caaers.domain.CtcGrade;
import gov.nih.nci.cabig.caaers.domain.CtcTerm;
import gov.nih.nci.cabig.caaers.domain.ExternalAdverseEvent;
import gov.nih.nci.cabig.caaers.domain.Grade;
import gov.nih.nci.cabig.caaers.domain.Hospitalization;
import gov.nih.nci.cabig.caaers.domain.Outcome;
import gov.nih.nci.cabig.caaers.domain.TimeValue;
import gov.nih.nci.cabig.caaers.domain.meddra.LowLevelTerm;
import gov.nih.nci.cabig.caaers.integration.schema.adverseevent.AdverseEventMeddraLowLevelTermType;
import gov.nih.nci.cabig.caaers.integration.schema.adverseevent.AdverseEventType;
import gov.nih.nci.cabig.caaers.integration.schema.adverseevent.OutcomeType;
import gov.nih.nci.cabig.caaers.utils.DateUtils;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.context.MessageSource;

public class AdverseEventConverter {
	private CtcTermDao ctcTermDao = null;
	private LowLevelTermDao lowLevelTermDao = null;
	private MessageSource messageSource;

	public void convertAdverseEventDtoToAdverseEventDomain(AdverseEventType adverseEventDto, 
			AdverseEvent adverseEvent, AeTerminology terminology  , Date startDateOfFirstCourse, String operation) throws CaaersSystemException{
		if(adverseEvent == null){
			adverseEvent = new AdverseEvent();
		}
		
		try{
			if (adverseEventDto.getVerbatim() != null) { 
				adverseEvent.setDetailsForOther(adverseEventDto.getVerbatim());
			}
			adverseEvent.setExternalId(adverseEventDto.getExternalId());
			populateGrade(adverseEventDto,adverseEvent);
			populateHospitalization(adverseEventDto,adverseEvent);
			adverseEvent.setExpected(adverseEventDto.isExpected());
	
			populateAttributionSummary(adverseEventDto,adverseEvent);
			adverseEvent.setComments(adverseEventDto.getComments());
			if(adverseEventDto.getStartDate() != null){
				//check for future date .
				int dateCompare = DateUtils.compareDate(new Date(), adverseEventDto.getStartDate().toGregorianCalendar().getTime());
				if (dateCompare == -1) {
					throw new CaaersSystemException (messageSource.getMessage("WS_AEMS_031", new String[]{adverseEventDto.getStartDate()+""},"",Locale.getDefault()));
				}
				adverseEvent.setStartDate(adverseEventDto.getStartDate().toGregorianCalendar().getTime());
			}
			if(adverseEventDto.getEndDate() != null){
				int dateCompare = DateUtils.compareDate(new Date(), adverseEventDto.getEndDate().toGregorianCalendar().getTime());
				if (dateCompare == -1) {
					throw new CaaersSystemException (messageSource.getMessage("WS_AEMS_031", new String[]{adverseEventDto.getEndDate()+""},"",Locale.getDefault()));
				}
				adverseEvent.setEndDate(adverseEventDto.getEndDate().toGregorianCalendar().getTime());
			}	
	        // Check if the start date is equal to or before the end date.
			if(adverseEventDto.getStartDate() != null && startDateOfFirstCourse != null){
				int dateCompare = DateUtils.compareDate(adverseEventDto.getStartDate().toGregorianCalendar().getTime(),startDateOfFirstCourse);
				if (dateCompare < 0) {
					throw new CaaersSystemException (messageSource.getMessage("WS_AEMS_059", new String[]{adverseEventDto.getStartDate()+"",startDateOfFirstCourse+""},"",Locale.getDefault()));
				}
			}
	  

			if (operation.equals(AdverseEventManagementServiceImpl.CREATE) || operation.equals(AdverseEventManagementServiceImpl.UPDATE)) {
				if (terminology.getCtcVersion() != null && adverseEventDto.getAdverseEventCtepTerm() != null && adverseEventDto.getAdverseEventCtepTerm().getCtepCode()!=null) {
					populateCtcTerm(adverseEventDto,adverseEvent,terminology.getCtcVersion());
				}
				
				if (terminology.getMeddraVersion() != null && adverseEventDto.getAdverseEventMeddraLowLevelTerm() != null) {
					populateLowLevelTerm(adverseEventDto.getAdverseEventMeddraLowLevelTerm() ,adverseEvent); 
				}				
			}
			
			if (adverseEventDto.getEventApproximateTime() != null) { 
				TimeValue tv = new TimeValue();
				tv.setHour(adverseEventDto.getEventApproximateTime().getHour().intValue());
				tv.setMinute(adverseEventDto.getEventApproximateTime().getMinute().intValue());
				if (adverseEventDto.getEventApproximateTime().getAmpm().equals("AM")) {
					tv.setType(0);
				} else {
					tv.setType(1);
				}
				adverseEvent.setEventApproximateTime(tv);
			}

			if (adverseEventDto.getEventLocation() != null) { 
				adverseEvent.setEventLocation(adverseEventDto.getEventLocation());
			}
			if (adverseEventDto.getOutcome() != null) {
				populateOutcomes(adverseEventDto,adverseEvent);
			}
			/*
			if(adverseEventDto.getGradedDate() != null){
				adverseEvent.setGradedDate(adverseEventDto.getGradedDate().toGregorianCalendar().getTime());
			}
						
			if (adverseEventDto.isReported() != null) {
				adverseEvent.setReported(adverseEventDto.isReported());
			}
			
			if (adverseEventDto.isSolicited() != null){
				adverseEvent.setSolicited(adverseEventDto.isSolicited());
			}			
			
			if (adverseEventDto.isRequiresReporting() != null) {
				adverseEvent.setRequiresReporting(adverseEventDto.isRequiresReporting());
			}*/			
			
		}catch(Exception e){
			throw new CaaersSystemException(e);
		}
	}
	
	
	public void convertAdverseEventDtoToExternalAdverseEventDTO(AdverseEventType adverseEventDto, 
			ExternalAdverseEvent adverseEvent, AeTerminology terminology  , Date startDateOfFirstCourse) throws CaaersSystemException{
		if(adverseEvent == null){
			throw new IllegalArgumentException("adverse event cannot be null");
		}
		
		try{
			// populate verbatim
			adverseEvent.setVerbatim(adverseEventDto.getVerbatim());
			
			// populate attribution
			if (adverseEventDto.getAttributionSummary() != null) {
                Attribution attribution = Attribution.valueOf(adverseEventDto.getAttributionSummary().name());
				adverseEvent.setAttribution(attribution.getDisplayName());
			}
			
			// populate howSerious
			gov.nih.nci.cabig.caaers.domain.OutcomeType mostSeriousOutcome = null; 
			
			for (OutcomeType xmlOutcome:adverseEventDto.getOutcome()) {
				String xmlOc = xmlOutcome.getOutComeEnumType().name();
				gov.nih.nci.cabig.caaers.domain.OutcomeType oct = gov.nih.nci.cabig.caaers.domain.OutcomeType.valueOf(xmlOc);
				if(mostSeriousOutcome == null || mostSeriousOutcome.compareTo(oct) > 1){
					mostSeriousOutcome = oct;
				} 
			}
			
			if(mostSeriousOutcome != null) adverseEvent.setHowSerious(mostSeriousOutcome.getShortName());
				
			// populate grade
			Grade grade = Grade.getByCode(adverseEventDto.getGrade());
			adverseEvent.setGrade(grade);	
			
			// populate start date
			if(adverseEventDto.getStartDate() != null){
				adverseEvent.setStartDate(adverseEventDto.getStartDate().toGregorianCalendar().getTime());
			}
			
			// populate end date
			if(adverseEventDto.getEndDate() != null){
				adverseEvent.setEndDate(adverseEventDto.getEndDate().toGregorianCalendar().getTime());
			}	

			// populate adverse event term, code and other value

			if (terminology.getCtcVersion() != null && adverseEventDto.getAdverseEventCtepTerm()!=null && adverseEventDto.getAdverseEventCtepTerm().getCtepCode() != null) {<commit_msg>Fixed for CAAERS-5905 to save all outcome values<commit_after>package gov.nih.nci.cabig.caaers.service.migrator.adverseevent;

import gov.nih.nci.cabig.caaers.CaaersSystemException;
import gov.nih.nci.cabig.caaers.api.impl.AdverseEventManagementServiceImpl;
import gov.nih.nci.cabig.caaers.dao.CtcTermDao;
import gov.nih.nci.cabig.caaers.dao.meddra.LowLevelTermDao;
import gov.nih.nci.cabig.caaers.domain.AdverseEvent;
import gov.nih.nci.cabig.caaers.domain.AdverseEventCtcTerm;
import gov.nih.nci.cabig.caaers.domain.AdverseEventMeddraLowLevelTerm;
import gov.nih.nci.cabig.caaers.domain.AeTerminology;
import gov.nih.nci.cabig.caaers.domain.Attribution;
import gov.nih.nci.cabig.caaers.domain.Ctc;
import gov.nih.nci.cabig.caaers.domain.CtcGrade;
import gov.nih.nci.cabig.caaers.domain.CtcTerm;
import gov.nih.nci.cabig.caaers.domain.ExternalAdverseEvent;
import gov.nih.nci.cabig.caaers.domain.Grade;
import gov.nih.nci.cabig.caaers.domain.Hospitalization;
import gov.nih.nci.cabig.caaers.domain.Outcome;
import gov.nih.nci.cabig.caaers.domain.TimeValue;
import gov.nih.nci.cabig.caaers.domain.meddra.LowLevelTerm;
import gov.nih.nci.cabig.caaers.integration.schema.adverseevent.AdverseEventMeddraLowLevelTermType;
import gov.nih.nci.cabig.caaers.integration.schema.adverseevent.AdverseEventType;
import gov.nih.nci.cabig.caaers.integration.schema.adverseevent.OutcomeType;
import gov.nih.nci.cabig.caaers.utils.DateUtils;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.MessageSource;

public class AdverseEventConverter {
	private CtcTermDao ctcTermDao = null;
	private LowLevelTermDao lowLevelTermDao = null;
	private MessageSource messageSource;

	public void convertAdverseEventDtoToAdverseEventDomain(AdverseEventType adverseEventDto, 
			AdverseEvent adverseEvent, AeTerminology terminology  , Date startDateOfFirstCourse, String operation) throws CaaersSystemException{
		if(adverseEvent == null){
			adverseEvent = new AdverseEvent();
		}
		
		try{
			if (adverseEventDto.getVerbatim() != null) { 
				adverseEvent.setDetailsForOther(adverseEventDto.getVerbatim());
			}
			adverseEvent.setExternalId(adverseEventDto.getExternalId());
			populateGrade(adverseEventDto,adverseEvent);
			populateHospitalization(adverseEventDto,adverseEvent);
			adverseEvent.setExpected(adverseEventDto.isExpected());
	
			populateAttributionSummary(adverseEventDto,adverseEvent);
			adverseEvent.setComments(adverseEventDto.getComments());
			if(adverseEventDto.getStartDate() != null){
				//check for future date .
				int dateCompare = DateUtils.compareDate(new Date(), adverseEventDto.getStartDate().toGregorianCalendar().getTime());
				if (dateCompare == -1) {
					throw new CaaersSystemException (messageSource.getMessage("WS_AEMS_031", new String[]{adverseEventDto.getStartDate()+""},"",Locale.getDefault()));
				}
				adverseEvent.setStartDate(adverseEventDto.getStartDate().toGregorianCalendar().getTime());
			}
			if(adverseEventDto.getEndDate() != null){
				int dateCompare = DateUtils.compareDate(new Date(), adverseEventDto.getEndDate().toGregorianCalendar().getTime());
				if (dateCompare == -1) {
					throw new CaaersSystemException (messageSource.getMessage("WS_AEMS_031", new String[]{adverseEventDto.getEndDate()+""},"",Locale.getDefault()));
				}
				adverseEvent.setEndDate(adverseEventDto.getEndDate().toGregorianCalendar().getTime());
			}	
	        // Check if the start date is equal to or before the end date.
			if(adverseEventDto.getStartDate() != null && startDateOfFirstCourse != null){
				int dateCompare = DateUtils.compareDate(adverseEventDto.getStartDate().toGregorianCalendar().getTime(),startDateOfFirstCourse);
				if (dateCompare < 0) {
					throw new CaaersSystemException (messageSource.getMessage("WS_AEMS_059", new String[]{adverseEventDto.getStartDate()+"",startDateOfFirstCourse+""},"",Locale.getDefault()));
				}
			}
	  

			if (operation.equals(AdverseEventManagementServiceImpl.CREATE) || operation.equals(AdverseEventManagementServiceImpl.UPDATE)) {
				if (terminology.getCtcVersion() != null && adverseEventDto.getAdverseEventCtepTerm() != null && adverseEventDto.getAdverseEventCtepTerm().getCtepCode()!=null) {
					populateCtcTerm(adverseEventDto,adverseEvent,terminology.getCtcVersion());
				}
				
				if (terminology.getMeddraVersion() != null && adverseEventDto.getAdverseEventMeddraLowLevelTerm() != null) {
					populateLowLevelTerm(adverseEventDto.getAdverseEventMeddraLowLevelTerm() ,adverseEvent); 
				}				
			}
			
			if (adverseEventDto.getEventApproximateTime() != null) { 
				TimeValue tv = new TimeValue();
				tv.setHour(adverseEventDto.getEventApproximateTime().getHour().intValue());
				tv.setMinute(adverseEventDto.getEventApproximateTime().getMinute().intValue());
				if (adverseEventDto.getEventApproximateTime().getAmpm().equals("AM")) {
					tv.setType(0);
				} else {
					tv.setType(1);
				}
				adverseEvent.setEventApproximateTime(tv);
			}

			if (adverseEventDto.getEventLocation() != null) { 
				adverseEvent.setEventLocation(adverseEventDto.getEventLocation());
			}
			if (adverseEventDto.getOutcome() != null) {
				populateOutcomes(adverseEventDto,adverseEvent);
			}
			/*
			if(adverseEventDto.getGradedDate() != null){
				adverseEvent.setGradedDate(adverseEventDto.getGradedDate().toGregorianCalendar().getTime());
			}
						
			if (adverseEventDto.isReported() != null) {
				adverseEvent.setReported(adverseEventDto.isReported());
			}
			
			if (adverseEventDto.isSolicited() != null){
				adverseEvent.setSolicited(adverseEventDto.isSolicited());
			}			
			
			if (adverseEventDto.isRequiresReporting() != null) {
				adverseEvent.setRequiresReporting(adverseEventDto.isRequiresReporting());
			}*/			
			
		}catch(Exception e){
			throw new CaaersSystemException(e);
		}
	}
	
	
	public void convertAdverseEventDtoToExternalAdverseEventDTO(AdverseEventType adverseEventDto, 
			ExternalAdverseEvent adverseEvent, AeTerminology terminology  , Date startDateOfFirstCourse) throws CaaersSystemException{
		if(adverseEvent == null){
			throw new IllegalArgumentException("adverse event cannot be null");
		}
		
		try{
			// populate verbatim
			adverseEvent.setVerbatim(adverseEventDto.getVerbatim());
			
			// populate attribution
			if (adverseEventDto.getAttributionSummary() != null) {
                Attribution attribution = Attribution.valueOf(adverseEventDto.getAttributionSummary().name());
				adverseEvent.setAttribution(attribution.getDisplayName());
			}
			
			// populate howSerious
			StringBuilder howSeriousSb = new StringBuilder();
			
			for (OutcomeType xmlOutcome:adverseEventDto.getOutcome()) {
				String xmlOc = xmlOutcome.getOutComeEnumType().name();
				gov.nih.nci.cabig.caaers.domain.OutcomeType oct = gov.nih.nci.cabig.caaers.domain.OutcomeType.valueOf(xmlOc);
				howSeriousSb.append(oct.getShortName()).append(",");
			}
			String howSerious = howSeriousSb.toString();
			if(StringUtils.isNotEmpty(howSerious)) {
				adverseEvent.setHowSerious(howSerious);
			}
				
			// populate grade
			Grade grade = Grade.getByCode(adverseEventDto.getGrade());
			adverseEvent.setGrade(grade);	
			
			// populate start date
			if(adverseEventDto.getStartDate() != null){
				adverseEvent.setStartDate(adverseEventDto.getStartDate().toGregorianCalendar().getTime());
			}
			
			// populate end date
			if(adverseEventDto.getEndDate() != null){
				adverseEvent.setEndDate(adverseEventDto.getEndDate().toGregorianCalendar().getTime());
			}	

			// populate adverse event term, code and other value

			if (terminology.getCtcVersion() != null && adverseEventDto.getAdverseEventCtepTerm()!=null && adverseEventDto.getAdverseEventCtepTerm().getCtepCode() != null) {
{'fraction_non_alphanumeric': 0.12087700122162927, 'fraction_numerical': 0.0021217771491030667, 'mean_word_length': 5.22658126501201, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '425833', 'n_tokens_mistral': 5946, 'n_tokens_neox': 5256, 'n_words': 637}