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

import gov.nih.nci.cabig.caaers.dao.OrganizationDao;
import gov.nih.nci.cabig.caaers.dao.ResearchStaffDao;
import gov.nih.nci.cabig.caaers.dao.SiteInvestigatorDao;
import gov.nih.nci.cabig.caaers.dao.query.OrganizationQuery;
import gov.nih.nci.cabig.caaers.domain.CoordinatingCenter;
import gov.nih.nci.cabig.caaers.domain.FundingSponsor;
import gov.nih.nci.cabig.caaers.domain.Investigator;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.OrganizationAssignedIdentifier;
import gov.nih.nci.cabig.caaers.domain.ResearchStaff;
import gov.nih.nci.cabig.caaers.domain.SiteInvestigator;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudyCoordinatingCenter;
import gov.nih.nci.cabig.caaers.domain.StudyFundingSponsor;
import gov.nih.nci.cabig.caaers.domain.StudyInvestigator;
import gov.nih.nci.cabig.caaers.domain.StudyOrganization;
import gov.nih.nci.cabig.caaers.domain.StudyPersonnel;
import gov.nih.nci.cabig.caaers.domain.StudySite;
import gov.nih.nci.cabig.caaers.service.DomainObjectImportOutcome;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;


public class StudyOrganizationMigrator implements Migrator<Study>{
	
	  private OrganizationDao organizationDao;
	  private SiteInvestigatorDao siteInvestigatorDao;
	  private ResearchStaffDao researchStaffDao;
	  
	/**
	 * This method will copy the {@link StudyOrganization}s from source to destination
	 */
	public void migrate(Study source, Study destination,DomainObjectImportOutcome<Study> outcome) {
		
		//migrate funding sponsor
		migrateFundingSponsor(source, destination, outcome);
        
        //migrate coordinating center
		migrateCoordinatingCenter(source, destination, outcome);
		
        //migrate studyOrganization.
		migrateStudySite(source, destination, outcome);
	}
	
	private void migrateStudySite(Study source, Study destination,DomainObjectImportOutcome<Study> outcome) {
		if(source.getStudyOrganizations() != null && source.getStudyOrganizations().size() > 0){
			for (StudyOrganization studyOrganization : source.getStudyOrganizations()) {
				Organization organization = null;
				if(studyOrganization.getOrganization().getNciInstituteCode() != null && studyOrganization.getOrganization().getNciInstituteCode().length() > 0){
					String nciInstituteCode = studyOrganization.getOrganization().getNciInstituteCode();
			        organization = fetchOrganization(nciInstituteCode);
				}else{
					String orgName = studyOrganization.getOrganization().getName();
			        organization = organizationDao.getByName(orgName);
				}
		        outcome.ifNullObject(organization, DomainObjectImportOutcome.Severity.ERROR, 
				"The organization specified in studySite is invalid");
		        studyOrganization.setOrganization(organization);

		        // Migrate Study investigators and Study Personnels
		        migrateStudyInvestigators(studyOrganization, organization, outcome);
		        migrateStudyPersonnels(studyOrganization, organization, outcome);
	        	destination.addStudySite((StudySite) studyOrganization);
		   }
	
		}
		
	}
	
	private void migrateFundingSponsor(Study source, Study destination, DomainObjectImportOutcome<Study> outcome ){
		FundingSponsor sponsor = source.getFundingSponsor();
		if(sponsor == null) return;
		
		
		StudyFundingSponsor studySponsor = sponsor.getStudyFundingSponsor();
		Organization organization = null;
		if(studySponsor.getOrganization().getNciInstituteCode() != null && studySponsor.getOrganization().getNciInstituteCode().length() > 0){
			String nciInstituteCode = studySponsor.getOrganization().getNciInstituteCode();
	        organization = fetchOrganization(nciInstituteCode);
		}else{
			String orgName = studySponsor.getOrganization().getName();
			organization = organizationDao.getByName(orgName);
		}
		outcome.ifNullObject(organization, DomainObjectImportOutcome.Severity.ERROR, 
				"The organization specified in fundingSponsor is invalid");
		studySponsor.setOrganization(organization);
		
		OrganizationAssignedIdentifier orgIdentifier = sponsor.getOrganizationAssignedIdentifier();
		orgIdentifier.setType(OrganizationAssignedIdentifier.SPONSOR_IDENTIFIER_TYPE);
		orgIdentifier.setOrganization(organization);
        
        //	Migrate Study investigators and Study Personnels
        migrateStudyInvestigators(studySponsor, organization, outcome);
        migrateStudyPersonnels(studySponsor, organization, outcome);
        
        destination.getIdentifiers().add(orgIdentifier);
        destination.addStudyFundingSponsor(studySponsor);
		
	}
	
	private void migrateCoordinatingCenter(Study source, Study destination, DomainObjectImportOutcome<Study> outcome ){
		CoordinatingCenter coCenter = source.getCoordinatingCenter();
		if(coCenter == null) return;
		
		StudyCoordinatingCenter studyCoordinatingCenter = coCenter.getStudyCoordinatingCenter();
		Organization organization = null;
		if(studyCoordinatingCenter.getOrganization().getNciInstituteCode() != null && studyCoordinatingCenter.getOrganization().getNciInstituteCode().length() > 0){
			String nciInstituteCode = studyCoordinatingCenter.getOrganization().getNciInstituteCode();
	        organization = fetchOrganization(nciInstituteCode);
		}else{
			String orgName = studyCoordinatingCenter.getOrganization().getName();
			organization = organizationDao.getByName(orgName);
		}
		outcome.ifNullObject(organization, DomainObjectImportOutcome.Severity.ERROR, "The organization specified in coordinatingCenter is invalid");
		studyCoordinatingCenter.setOrganization(organization);
		
		OrganizationAssignedIdentifier orgIdentifier = coCenter.getOrganizationAssignedIdentifier();
		orgIdentifier.setType(OrganizationAssignedIdentifier.COORDINATING_CENTER_IDENTIFIER_TYPE);
		orgIdentifier.setOrganization(organization);
        
        //	Migrate Study investigators and Study Personnels
        migrateStudyInvestigators(studyCoordinatingCenter, organization, outcome);
        migrateStudyPersonnels(studyCoordinatingCenter, organization, outcome);
        
        destination.getIdentifiers().add(orgIdentifier);
        destination.addStudyOrganization(studyCoordinatingCenter);
		
	}	
 

    private void migrateStudyInvestigators(StudyOrganization studyOrganization, Organization organization, DomainObjectImportOutcome studyImportOutcome) {

        for (StudyInvestigator studyInvestigator : studyOrganization.getStudyInvestigators()) {

            Investigator investigator = studyInvestigator.getSiteInvestigator().getInvestigator();
            List<SiteInvestigator> siteInvestigators = null;
            if(investigator.getNciIdentifier() != null && investigator.getNciIdentifier().length() > 0){
            	String[] nciIdentifier = {investigator.getNciIdentifier()};
                siteInvestigators = siteInvestigatorDao.getByNciIdentifier(nciIdentifier, organization.getId());
            }else{
            	String[] investigatorFirstAndLast = {investigator.getFirstName(), investigator.getLastName()};
                siteInvestigators = siteInvestigatorDao.getBySubnames(investigatorFirstAndLast, organization.getId());<commit_msg>Null check made on organization before migrating studyInvestigators and StudyPersonells.<commit_after>package gov.nih.nci.cabig.caaers.service.migrator;

import gov.nih.nci.cabig.caaers.dao.OrganizationDao;
import gov.nih.nci.cabig.caaers.dao.ResearchStaffDao;
import gov.nih.nci.cabig.caaers.dao.SiteInvestigatorDao;
import gov.nih.nci.cabig.caaers.dao.query.OrganizationQuery;
import gov.nih.nci.cabig.caaers.domain.CoordinatingCenter;
import gov.nih.nci.cabig.caaers.domain.FundingSponsor;
import gov.nih.nci.cabig.caaers.domain.Investigator;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.OrganizationAssignedIdentifier;
import gov.nih.nci.cabig.caaers.domain.ResearchStaff;
import gov.nih.nci.cabig.caaers.domain.SiteInvestigator;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudyCoordinatingCenter;
import gov.nih.nci.cabig.caaers.domain.StudyFundingSponsor;
import gov.nih.nci.cabig.caaers.domain.StudyInvestigator;
import gov.nih.nci.cabig.caaers.domain.StudyOrganization;
import gov.nih.nci.cabig.caaers.domain.StudyPersonnel;
import gov.nih.nci.cabig.caaers.domain.StudySite;
import gov.nih.nci.cabig.caaers.service.DomainObjectImportOutcome;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;


public class StudyOrganizationMigrator implements Migrator<Study>{
	
	  private OrganizationDao organizationDao;
	  private SiteInvestigatorDao siteInvestigatorDao;
	  private ResearchStaffDao researchStaffDao;
	  
	/**
	 * This method will copy the {@link StudyOrganization}s from source to destination
	 */
	public void migrate(Study source, Study destination,DomainObjectImportOutcome<Study> outcome) {
		
		//migrate funding sponsor
		migrateFundingSponsor(source, destination, outcome);
        
        //migrate coordinating center
		migrateCoordinatingCenter(source, destination, outcome);
		
        //migrate studyOrganization.
		migrateStudySite(source, destination, outcome);
	}
	
	private void migrateStudySite(Study source, Study destination,DomainObjectImportOutcome<Study> outcome) {
		if(source.getStudyOrganizations() != null && source.getStudyOrganizations().size() > 0){
			for (StudyOrganization studyOrganization : source.getStudyOrganizations()) {
				Organization organization = null;
				if(studyOrganization.getOrganization().getNciInstituteCode() != null && studyOrganization.getOrganization().getNciInstituteCode().length() > 0){
					String nciInstituteCode = studyOrganization.getOrganization().getNciInstituteCode();
			        organization = fetchOrganization(nciInstituteCode);
				}else{
					String orgName = studyOrganization.getOrganization().getName();
			        organization = organizationDao.getByName(orgName);
				}
		        outcome.ifNullObject(organization, DomainObjectImportOutcome.Severity.ERROR, 
				"The organization specified in studySite is invalid");
		        studyOrganization.setOrganization(organization);

		        // Migrate Study investigators and Study Personnels
		        if(organization != null){
		        	migrateStudyInvestigators(studyOrganization, organization, outcome);
			        migrateStudyPersonnels(studyOrganization, organization, outcome);
		        }
		        
	        	destination.addStudySite((StudySite) studyOrganization);
		   }
	
		}
		
	}
	
	private void migrateFundingSponsor(Study source, Study destination, DomainObjectImportOutcome<Study> outcome ){
		FundingSponsor sponsor = source.getFundingSponsor();
		if(sponsor == null) return;
		
		
		StudyFundingSponsor studySponsor = sponsor.getStudyFundingSponsor();
		Organization organization = null;
		if(studySponsor.getOrganization().getNciInstituteCode() != null && studySponsor.getOrganization().getNciInstituteCode().length() > 0){
			String nciInstituteCode = studySponsor.getOrganization().getNciInstituteCode();
	        organization = fetchOrganization(nciInstituteCode);
		}else{
			String orgName = studySponsor.getOrganization().getName();
			organization = organizationDao.getByName(orgName);
		}
		outcome.ifNullObject(organization, DomainObjectImportOutcome.Severity.ERROR, 
				"The organization specified in fundingSponsor is invalid");
		studySponsor.setOrganization(organization);
		
		OrganizationAssignedIdentifier orgIdentifier = sponsor.getOrganizationAssignedIdentifier();
		orgIdentifier.setType(OrganizationAssignedIdentifier.SPONSOR_IDENTIFIER_TYPE);
		orgIdentifier.setOrganization(organization);
        
        //	Migrate Study investigators and Study Personnels
		if(organization != null){
			migrateStudyInvestigators(studySponsor, organization, outcome);
			migrateStudyPersonnels(studySponsor, organization, outcome);
		}
        
        destination.getIdentifiers().add(orgIdentifier);
        destination.addStudyFundingSponsor(studySponsor);
		
	}
	
	private void migrateCoordinatingCenter(Study source, Study destination, DomainObjectImportOutcome<Study> outcome ){
		CoordinatingCenter coCenter = source.getCoordinatingCenter();
		if(coCenter == null) return;
		
		StudyCoordinatingCenter studyCoordinatingCenter = coCenter.getStudyCoordinatingCenter();
		Organization organization = null;
		if(studyCoordinatingCenter.getOrganization().getNciInstituteCode() != null && studyCoordinatingCenter.getOrganization().getNciInstituteCode().length() > 0){
			String nciInstituteCode = studyCoordinatingCenter.getOrganization().getNciInstituteCode();
	        organization = fetchOrganization(nciInstituteCode);
		}else{
			String orgName = studyCoordinatingCenter.getOrganization().getName();
			organization = organizationDao.getByName(orgName);
		}
		outcome.ifNullObject(organization, DomainObjectImportOutcome.Severity.ERROR, "The organization specified in coordinatingCenter is invalid");
		studyCoordinatingCenter.setOrganization(organization);
		
		OrganizationAssignedIdentifier orgIdentifier = coCenter.getOrganizationAssignedIdentifier();
		orgIdentifier.setType(OrganizationAssignedIdentifier.COORDINATING_CENTER_IDENTIFIER_TYPE);
		orgIdentifier.setOrganization(organization);
        
        //	Migrate Study investigators and Study Personnels
		if(organization != null){
			migrateStudyInvestigators(studyCoordinatingCenter, organization, outcome);
			migrateStudyPersonnels(studyCoordinatingCenter, organization, outcome);
		}
        
        destination.getIdentifiers().add(orgIdentifier);
        destination.addStudyOrganization(studyCoordinatingCenter);
		
	}	
 

    private void migrateStudyInvestigators(StudyOrganization studyOrganization, Organization organization, DomainObjectImportOutcome studyImportOutcome) {

        for (StudyInvestigator studyInvestigator : studyOrganization.getStudyInvestigators()) {

            Investigator investigator = studyInvestigator.getSiteInvestigator().getInvestigator();
            List<SiteInvestigator> siteInvestigators = null;
            if(investigator.getNciIdentifier() != null && investigator.getNciIdentifier().length() > 0){
            	String[] nciIdentifier = {investigator.getNciIdentifier()};
                siteInvestigators = siteInvestigatorDao.getByNciIdentifier(nciIdentifier, organization.getId());
            }else{
            	String[] investigatorFirstAndLast = {investigator.getFirstName(), investigator.getLastName()};
                siteInvestigators = siteInvestigatorDao.getBySubnames(investigatorFirstAndLast, organization.getId());
{'fraction_non_alphanumeric': 0.09041114417094942, 'fraction_numerical': 0.000676223965377333, 'mean_word_length': 5.219091673675358, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, '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': '6082899', 'n_tokens_mistral': 4661, 'n_tokens_neox': 4154, 'n_words': 646}