<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.SiteInvestigatorDao;
import gov.nih.nci.cabig.caaers.dao.SiteResearchStaffDao;
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.SiteResearchStaff;
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.domain.repository.OrganizationRepository;
import gov.nih.nci.cabig.caaers.service.DomainObjectImportOutcome;
import gov.nih.nci.security.util.StringUtilities;

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 OrganizationRepository organizationRepository;
	  private SiteInvestigatorDao siteInvestigatorDao;
	  private SiteResearchStaffDao siteResearchStaffDao;
	  private OrganizationMigrator organizationMigrator;

	/**
	 * 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()) {
				if(studyOrganization instanceof StudySite){
					Organization organization = null;
					Organization inStreamOrganization = studyOrganization.getOrganization();
					String nciInstituteCode = inStreamOrganization.getNciInstituteCode();
					String orgName = inStreamOrganization.getName();

                    if (!StringUtilities.isBlank(nciInstituteCode)) {
                        organization = fetchOrganization(nciInstituteCode);
                    } else {
                        organization = organizationDao.getByName(orgName);
                    }

                    organizationMigrator.migrate(studyOrganization.getOrganization(), organization, null);
                    organizationDao.save(organization);
                    // create organization if it does not exist.
					/*
					if (organization == null) {
						organizationRepository.create(inStreamOrganization);
						if (!StringUtilities.isBlank(nciInstituteCode)) {							
							organization = fetchOrganization(nciInstituteCode);
						} else {
							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);
		}

        organizationMigrator.migrate(studySponsor.getOrganization(), organization, null);
        organizationDao.save(organization);

        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);
		orgIdentifier.setPrimaryIndicator(Boolean.TRUE);
		
        //	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();<commit_msg>CAAERS-5449 - Unable to import a CTEP study with ID = CALGB-70305<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.SiteInvestigatorDao;
import gov.nih.nci.cabig.caaers.dao.SiteResearchStaffDao;
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.SiteResearchStaff;
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.domain.repository.OrganizationRepository;
import gov.nih.nci.cabig.caaers.service.DomainObjectImportOutcome;
import gov.nih.nci.security.util.StringUtilities;

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 OrganizationRepository organizationRepository;
	  private SiteInvestigatorDao siteInvestigatorDao;
	  private SiteResearchStaffDao siteResearchStaffDao;
	  private OrganizationMigrator organizationMigrator;

	/**
	 * 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()) {
				if(studyOrganization instanceof StudySite){
					Organization organization = null;
					Organization inStreamOrganization = studyOrganization.getOrganization();
					String nciInstituteCode = inStreamOrganization.getNciInstituteCode();
					String orgName = inStreamOrganization.getName();

                    if (!StringUtilities.isBlank(nciInstituteCode)) {
                        organization = fetchOrganization(nciInstituteCode);
                    } else {
                        organization = organizationDao.getByName(orgName);
                    }

                    organizationMigrator.migrate(studyOrganization.getOrganization(), organization, null);
                    organizationDao.save(organization);
                    // create organization if it does not exist.
					/*
					if (organization == null) {
						organizationRepository.create(inStreamOrganization);
						if (!StringUtilities.isBlank(nciInstituteCode)) {							
							organization = fetchOrganization(nciInstituteCode);
						} else {
							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(StringUtils.isNotEmpty(studySponsor.getOrganization().getNciInstituteCode())){
			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);
		orgIdentifier.setPrimaryIndicator(Boolean.TRUE);
		
        //	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();
{'fraction_non_alphanumeric': 0.08921196101106889, 'fraction_numerical': 0.0009912440112340988, 'mean_word_length': 4.388072986203827, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, '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': '3026824', 'n_tokens_mistral': 4000, 'n_tokens_neox': 3494, 'n_words': 553}