<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.StudyDao;
import gov.nih.nci.cabig.caaers.dao.StudySiteDao;
import gov.nih.nci.cabig.caaers.dao.query.OrganizationQuery;
import gov.nih.nci.cabig.caaers.domain.Identifier;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.Participant;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudyParticipantAssignment;
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.Date;
import java.util.List;

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

public class StudyParticipantAssignmentMigrator implements Migrator<Participant> {
	
	private StudySiteDao studySiteDao;
	private final Log log = LogFactory.getLog(getClass());
	private OrganizationDao organizationDao;
	private OrganizationRepository organizationRepository;
	private StudyDao studyDao;
	  
	public void migrate(Participant src, Participant dest,DomainObjectImportOutcome<Participant> outcome) {

        for (StudyParticipantAssignment studyParticipantAssignment : src.getAssignments()) {
            for (Identifier identifier : studyParticipantAssignment.getStudySite().getStudy().getIdentifiers()) {
                log.debug("Size of identifiers : " + studyParticipantAssignment.getStudySite().getStudy().getIdentifiers());
                String identifierValue = identifier.getValue();
                String organizationName = studyParticipantAssignment.getStudySite().getOrganization().getName();
                String organizationNciInstituteCode = studyParticipantAssignment.getStudySite().getOrganization().getNciInstituteCode();
                String identifierType = identifier.getType();
                StudySite studySite = null;
                Organization organization = null;
                if (StringUtilities.isBlank(organizationNciInstituteCode)) {
                	//System.out.println("looking by name");
                	organization = organizationDao.getByName(organizationName);
                	studySite = studySiteDao.matchByStudyAndOrg(organizationName, identifierValue, identifierType);
                } else {
                	//System.out.println("looking by id");
                	organization = fetchOrganization(organizationNciInstituteCode);
                	studySite = studySiteDao.matchByStudyAndOrgNciId(organizationNciInstituteCode, identifierValue, identifierType);
                }
                //If study site is not found add the incoming studysite to the study.
                if(studySite == null){
                	Study study = studyDao.getStudyDesignByIdentifier(identifier);
                	studySite = new StudySite();
                	studySite.setOrganization(organization);
                	studySite.setStartDate(new Date());
                	studySite.setStatus("Active");
                	study.addStudySite(studySite);
                	if(study == null){
                		outcome.ifNullObject(study, DomainObjectImportOutcome.Severity.ERROR,
                                "The Study with Identifier \" " + identifierValue
                                        + " \" is nonexistant");
                	}else{
                		studyDao.save(study);
                	}
                }
                StudyParticipantAssignment studParticipantAssignment = new StudyParticipantAssignment(dest, studySite);
                studParticipantAssignment.setStudySubjectIdentifier(studyParticipantAssignment.getStudySubjectIdentifier());
                
                dest.addAssignment(studParticipantAssignment);

            }

        }
        outcome.ifNullOrEmptyList(src.getAssignments(), DomainObjectImportOutcome.Severity.ERROR,
                "Assignments are either Empty or Not Valid");
	}
	
	///BEAN METHODS
	 @Required
	 public void setStudySiteDao(final StudySiteDao studySiteDao) {
		 this.studySiteDao = studySiteDao;
	 }
	 
    private Organization fetchOrganization(String nciInstituteCode) {
        OrganizationQuery orgQuery = new OrganizationQuery();<commit_msg>fixing  compilation issue<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.StudyDao;
import gov.nih.nci.cabig.caaers.dao.StudySiteDao;
import gov.nih.nci.cabig.caaers.dao.query.OrganizationQuery;
import gov.nih.nci.cabig.caaers.domain.Identifier;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.Participant;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudyParticipantAssignment;
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.Date;
import java.util.List;

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

public class StudyParticipantAssignmentMigrator implements Migrator<Participant> {
	
	private StudySiteDao studySiteDao;
	private final Log log = LogFactory.getLog(getClass());
	private OrganizationDao organizationDao;
	private OrganizationRepository organizationRepository;
	private StudyDao studyDao;
	  
	public void migrate(Participant src, Participant dest,DomainObjectImportOutcome<Participant> outcome) {

        for (StudyParticipantAssignment studyParticipantAssignment : src.getAssignments()) {
        	
        	StudySite srcSite = studyParticipantAssignment.getStudySite();
        	 log.debug("Size of identifiers : " + srcSite.getStudy().getIdentifiers());
        	 
            for (Identifier identifier : studyParticipantAssignment.getStudySite().getStudy().getIdentifiers()) {
               
                String identifierValue = identifier.getValue();
                String identifierType = identifier.getType();
                String organizationName = srcSite.getOrganization().getName();
                String organizationNciInstituteCode = srcSite.getOrganization().getNciInstituteCode();
              
                StudySite studySite = null;
                Organization organization = null;
                if (StringUtilities.isBlank(organizationNciInstituteCode)) {
                	//System.out.println("looking by name");
                	organization = organizationDao.getByName(organizationName);
                	studySite = studySiteDao.matchByStudyAndOrg(organizationName, identifierValue, identifierType);
                } else {
                	//System.out.println("looking by id");
                	organization = fetchOrganization(organizationNciInstituteCode);
                	studySite = studySiteDao.matchByStudyAndOrgNciId(organizationNciInstituteCode, identifierValue, identifierType);
                }
                //If study site is not found add the incoming studysite to the study.
                if(studySite == null){
                	Study study = studyDao.getStudyDesignByIdentifier(identifier);
                	studySite = new StudySite();
                	studySite.setOrganization(organization);
                	studySite.setStartDate(new Date());
                	studySite.setEndDate(srcSite.getEndDate());
                	study.addStudySite(studySite);
                	if(study == null){
                		outcome.ifNullObject(study, DomainObjectImportOutcome.Severity.ERROR,
                                "The Study with Identifier \" " + identifierValue
                                        + " \" is nonexistant");
                	}else{
                		studyDao.save(study);
                	}
                }
                StudyParticipantAssignment studParticipantAssignment = new StudyParticipantAssignment(dest, studySite);
                studParticipantAssignment.setStudySubjectIdentifier(studyParticipantAssignment.getStudySubjectIdentifier());
                
                dest.addAssignment(studParticipantAssignment);

            }

        }
        outcome.ifNullOrEmptyList(src.getAssignments(), DomainObjectImportOutcome.Severity.ERROR,
                "Assignments are either Empty or Not Valid");
	}
	
	///BEAN METHODS
	 @Required
	 public void setStudySiteDao(final StudySiteDao studySiteDao) {
		 this.studySiteDao = studySiteDao;
	 }
	 
    private Organization fetchOrganization(String nciInstituteCode) {
        OrganizationQuery orgQuery = new OrganizationQuery();
{'fraction_non_alphanumeric': 0.08608686042656648, 'fraction_numerical': 0.0, 'mean_word_length': 2.851063829787234, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7397538', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2433, 'n_words': 390}