<commit_before>package edu.duke.cabig.c3pr.domain.factory;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.MessageSource;

import edu.duke.cabig.c3pr.constants.ContactMechanismType;
import edu.duke.cabig.c3pr.constants.CoordinatingCenterStudyStatus;
import edu.duke.cabig.c3pr.constants.OrganizationIdentifierTypeEnum;
import edu.duke.cabig.c3pr.constants.SiteStudyStatus;
import edu.duke.cabig.c3pr.dao.HealthcareSiteDao;
import edu.duke.cabig.c3pr.dao.ICD9DiseaseSiteDao;
import edu.duke.cabig.c3pr.dao.ParticipantDao;
import edu.duke.cabig.c3pr.dao.StudySubjectDao;
import edu.duke.cabig.c3pr.domain.Arm;
import edu.duke.cabig.c3pr.domain.Consent;
import edu.duke.cabig.c3pr.domain.ContactMechanism;
import edu.duke.cabig.c3pr.domain.Epoch;
import edu.duke.cabig.c3pr.domain.HealthcareSite;
import edu.duke.cabig.c3pr.domain.ICD9DiseaseSite;
import edu.duke.cabig.c3pr.domain.LocalContactMechanism;
import edu.duke.cabig.c3pr.domain.OrganizationAssignedIdentifier;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.duke.cabig.c3pr.domain.ScheduledEpoch;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.domain.StudyDisease;
import edu.duke.cabig.c3pr.domain.StudyInvestigator;
import edu.duke.cabig.c3pr.domain.StudySite;
import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.domain.StudySubjectConsentVersion;
import edu.duke.cabig.c3pr.domain.repository.StudyRepository;
import edu.duke.cabig.c3pr.exception.C3PRCodedException;
import edu.duke.cabig.c3pr.exception.C3PRExceptionHelper;
import edu.duke.cabig.c3pr.service.ParticipantService;

public class StudySubjectFactory {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(StudySubjectFactory.class);

    private C3PRExceptionHelper exceptionHelper;

    private MessageSource c3prErrorMessages;

    private final String prtIdentifierTypeValueStr = "MRN";

    private ParticipantService participantService;

    private StudyRepository studyRepository;

    private StudySubjectDao studySubjectDao;

    private ParticipantDao participantDao;
    
    private ICD9DiseaseSiteDao icd9DiseaseSiteDao;
    
    private HealthcareSiteDao healthcareSiteDao;

    public void setHealthcareSiteDao(HealthcareSiteDao healthcareSiteDao) {
		this.healthcareSiteDao = healthcareSiteDao;
	}

	public ICD9DiseaseSiteDao getIcd9DiseaseSiteDao() {
		return icd9DiseaseSiteDao;
	}

	public void setIcd9DiseaseSiteDao(ICD9DiseaseSiteDao icd9DiseaseSiteDao) {
		this.icd9DiseaseSiteDao = icd9DiseaseSiteDao;
	}

	public void setParticipantDao(ParticipantDao participantDao) {
        this.participantDao = participantDao;
    }

    public void setStudySubjectDao(StudySubjectDao studySubjectDao) {
        this.studySubjectDao = studySubjectDao;
    }

    public StudySubject buildStudySubject(StudySubject deserializedStudySubject)
                    throws C3PRCodedException {
        StudySubject built = new StudySubject();
        StudySite studySite = buildStudySite(deserializedStudySubject.getStudySite(),
                        buildStudy(deserializedStudySubject.getStudySite().getStudy()));
        Participant participant = buildParticipant(deserializedStudySubject.getParticipant());
        if (participant.getId() != null) {
            List<StudySubject> registrations = studySubjectDao
                            .searchBySubjectAndStudyIdentifiers(participant.getPrimaryIdentifier(),
                            		studySite.getStudy().getCoordinatingCenterAssignedIdentifier());
            if (registrations.size() > 0) {
                throw this.exceptionHelper
                                .getException(getCode("C3PR.EXCEPTION.REGISTRATION.STUDYSUBJECTS_ALREADY_EXISTS.CODE"));
            }
        }
        else {
            if (participant.validateParticipant()){ }//participantDao.save(participant);
            else {
                throw this.exceptionHelper
                                .getException(getCode("C3PR.EXCEPTION.REGISTRATION.SUBJECTS_INVALID_DETAILS.CODE"));
            }
        }
        buildAndAddStudySubjectConsentVersions(built, studySite);
        built.setStudySite(studySite);
        built.setParticipant(participant);
        Epoch epoch = buildEpoch(studySite.getStudy().getEpochs(), deserializedStudySubject
                        .getScheduledEpoch());
        ScheduledEpoch scheduledEpoch = buildScheduledEpoch(deserializedStudySubject
                        .getScheduledEpoch(), epoch);
        built.getScheduledEpochs().add(0, scheduledEpoch);
        fillStudySubjectDetails(built, deserializedStudySubject);
        return built;
    }
    
    public void buildAndAddStudySubjectConsentVersions(StudySubject built,StudySite studySite){
    	for (Consent consent :studySite.getStudy().getStudyVersion().getConsents()){
    		StudySubjectConsentVersion studySubjectConsentVersion = new StudySubjectConsentVersion();
    		studySubjectConsentVersion.setConsent(consent);
    		built.getStudySubjectStudyVersion().addStudySubjectConsentVersion(studySubjectConsentVersion);
    		built.getStudySubjectStudyVersion().setStudySiteStudyVersion(studySite.getStudySiteStudyVersion());
    		studySite.getStudySiteStudyVersion().addStudySubjectStudyVersion(built.getStudySubjectStudyVersion());
    	}
    }

    public StudySubject buildReferencedStudySubject(StudySubject deserializedStudySubject)
                    throws C3PRCodedException {
        StudySubject built = new StudySubject();
        StudySite studySite = buildStudySite(deserializedStudySubject.getStudySite(),
                        buildStudy(deserializedStudySubject.getStudySite().getStudy()));
        Participant participant = buildParticipant(deserializedStudySubject.getParticipant());
        built.setStudySite(studySite);
        built.setParticipant(participant);
        Epoch epoch = buildEpoch(studySite.getStudy().getEpochs(), deserializedStudySubject
                        .getScheduledEpoch());
        ScheduledEpoch scheduledEpoch = buildScheduledEpoch(deserializedStudySubject
                        .getScheduledEpoch(), epoch);
        built.getScheduledEpochs().add(0, scheduledEpoch);
        fillStudySubjectDetails(built, deserializedStudySubject);
        return built;
    }

    public Participant buildParticipant(Participant participant) throws C3PRCodedException {
        if (participant.getIdentifiers() == null || participant.getIdentifiers().size() == 0) {
            throw exceptionHelper
                            .getException(getCode("C3PR.EXCEPTION.REGISTRATION.MISSING.SUBJECT_IDENTIFIER.CODE"));
        }
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : participant
                        .getOrganizationAssignedIdentifiers()) {
            if (organizationAssignedIdentifier.getType().getName().equals(this.prtIdentifierTypeValueStr)) {
                List<Participant> paList = participantService
                                .searchByMRN(organizationAssignedIdentifier);
                if (paList.size() > 1) {
                    throw exceptionHelper
                                    .getException(
                                                    getCode("C3PR.EXCEPTION.REGISTRATION.MULTIPLE.SUBJECTS_SAME_MRN.CODE"),
                                                    new String[] { organizationAssignedIdentifier
                                                                    .getValue() });
                }
                else if (paList.size() == 1) {
                    if (logger.isDebugEnabled()) {
                        logger
                                        .debug("buildParticipant(Participant) - Participant with the same MRN found in the database");
                    }
                    Participant temp = paList.get(0);
                    if (temp.getFirstName().equals(participant.getFirstName())
                                    && temp.getLastName().equals(participant.getLastName())
                                    && temp.getBirthDate().getTime() == participant.getBirthDate()
                                                    .getTime()) {
                        return temp;
                    }
                    else {
                        throw exceptionHelper
                                        .getException(
                                                        getCode("C3PR.EXCEPTION.REGISTRATION.INVALID.ANOTHER_SUBJECT_SAME_MRN.CODE"),
                                                        new String[] { organizationAssignedIdentifier
                                                                        .getValue() });
                    }
                }
            }
        }
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : participant
                .getOrganizationAssignedIdentifiers()) {
        	HealthcareSite healthcareSite= healthcareSiteDao.getByPrimaryIdentifier(organizationAssignedIdentifier.getHealthcareSite().getPrimaryIdentifier());
        	organizationAssignedIdentifier.setHealthcareSite(healthcareSite);
        }
//        addContactsToParticipant(participant);
        return participant;
    }

    public Study buildStudy(Study study) throws C3PRCodedException {
        if (study.getIdentifiers() == null || study.getIdentifiers().size() == 0) {
            throw exceptionHelper
                            .getException(getCode("C3PR.EXCEPTION.REGISTRATION.MISSING.STUDY_IDENTIFIER.CODE"));
        }
        List<Study> studies = null;
        OrganizationAssignedIdentifier identifier = null;
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : study
                        .getOrganizationAssignedIdentifiers()) {
            if (organizationAssignedIdentifier.getType().equals(OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER)) {
                identifier = organizationAssignedIdentifier;
                studies = studyRepository
                                .searchByCoOrdinatingCenterId(organizationAssignedIdentifier);
                break;
            }
        }
        if (identifier == null) {
            throw exceptionHelper
                            .getException(getCode("C3PR.EXCEPTION.REGISTRATION.MISSING.STUDY_COORDINATING_IDENTIFIER.CODE"));
        }
        if (studies == null) {
            throw exceptionHelper
                            .getException(
                                            getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.STUDY_WITH_IDENTIFIER.CODE"),
                                            new String[] {
                                                    identifier.getValue(),
                                                    OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER.getCode() });
        }
        if (studies.size() == 0) {
            throw exceptionHelper
                            .getException(
                                            getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.STUDY_WITH_IDENTIFIER.CODE"),
                                            new String[] {
                                                    identifier.getValue(),
                                                    OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER.getCode() });
        }
        if (studies.size() > 1) {
            throw exceptionHelper
                            .getException(
                                            getCode("C3PR.EXCEPTION.REGISTRATION.MULTIPLE.STUDY_SAME_CO_IDENTIFIER.CODE"),
                                            new String[] {
                                                    identifier.getValue(),
                                                    OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER.getCode() });
        }
        if (studies.get(0).getCoordinatingCenterStudyStatus() != CoordinatingCenterStudyStatus.OPEN) {
            throw exceptionHelper.getException(
                            getCode("C3PR.EXCEPTION.REGISTRATION.STUDY_NOT_ACTIVE"), new String[] {
                                    identifier.getHealthcareSite().getPrimaryIdentifier(),
                                    identifier.getValue() });
        }
        return studies.get(0);
    }

    public StudySite buildStudySite(StudySite studySite, Study study) throws C3PRCodedException {
        for (StudySite temp : study.getStudySites()) {
            if (temp.getHealthcareSite().getPrimaryIdentifier().equals(
                            studySite.getHealthcareSite().getPrimaryIdentifier())) {
                if (temp.getSiteStudyStatus() != SiteStudyStatus.ACTIVE) {
                    throw exceptionHelper
                                    .getException(
                                                    getCode("C3PR.EXCEPTION.REGISTRATION.STUDYSITE_NOT_ACTIVE"),
                                                    new String[] { temp.getHealthcareSite()
                                                                    .getPrimaryIdentifier() });
                }
                return temp;
            }
        }
        throw exceptionHelper
                        .getException(
                                        getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.STUDYSITE_WITH_NCICODE.CODE"),
                                        new String[] { studySite.getHealthcareSite()
                                                        .getPrimaryIdentifier() });
    }

    private Epoch buildEpoch(List<Epoch> epochs, ScheduledEpoch scheduledEpoch)
                    throws C3PRCodedException {
        for (Epoch epochCurr : epochs) {
            if (epochCurr.getName().equalsIgnoreCase(scheduledEpoch.getEpoch().getName())) {
                return epochCurr;
            }
        }
        throw exceptionHelper.getException(
                        getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.EPOCH_NAME.CODE"),
                        new String[] { scheduledEpoch.getEpoch().getName() });
    }

    private ScheduledEpoch buildScheduledEpoch(ScheduledEpoch source, Epoch epoch)
                    throws C3PRCodedException {
        ScheduledEpoch scheduledEpoch = null;
        ScheduledEpoch scheduledEpochSource = source;
        scheduledEpoch = new ScheduledEpoch();
        ScheduledEpoch scheduledTreatmentEpoch = scheduledEpoch;
        scheduledTreatmentEpoch.setEligibilityIndicator(true);
        if (scheduledEpochSource.getScheduledArm() != null
                        && scheduledEpochSource.getScheduledArm().getArm() != null
                        && scheduledEpochSource.getScheduledArm().getArm()
                                        .getName() != null) {
            Arm arm = null;
            for (Arm a : (epoch).getArms()) {
                if (a.getName().equals(
                		scheduledEpochSource.getScheduledArm().getArm()
                                                .getName())) {
                    arm = a;
                }
            }
            if (arm == null) {
                throw exceptionHelper
                                .getException(
                                                getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.ARM_NAME.CODE"),
                                                new String[] {
                                                	scheduledEpochSource
                                                                        .getScheduledArm()
                                                                        .getArm().getName(),
                                                                        scheduledEpochSource
                                                                        .getEpoch()
                                                                        .getName() });
            }
            scheduledTreatmentEpoch.getScheduledArms().get(0).setArm(arm);

        }
        scheduledEpoch.setEpoch(epoch);
        scheduledEpoch.setScEpochWorkflowStatus(source.getScEpochWorkflowStatus());
        scheduledEpoch.setStratumGroupNumber(source.getStratumGroupNumber());
        return scheduledEpoch;
    }

    private void fillStudySubjectDetails(StudySubject studySubject, StudySubject source){
        studySubject.getStudySubjectStudyVersion()
		.getStudySubjectConsentVersions().get(0).setInformedConsentSignedDate(source.getStudySubjectStudyVersion()
        		.getStudySubjectConsentVersions().get(0).getInformedConsentSignedDate());
        studySubject.setStartDate(source.getStartDate());
        studySubject.setPaymentMethod(source.getPaymentMethod());
        studySubject.getIdentifiers().addAll(source.getIdentifiers());
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : studySubject
                .getOrganizationAssignedIdentifiers()) {
        	HealthcareSite healthcareSite= healthcareSiteDao.getByPrimaryIdentifier(organizationAssignedIdentifier.getHealthcareSite().getPrimaryIdentifier());
        	organizationAssignedIdentifier.setHealthcareSite(healthcareSite);
        }
        if(source.getTreatingPhysician()!=null){
        	for(StudyInvestigator studyInvestigator:studySubject.getStudySite().getStudyInvestigators()){
        		if(studyInvestigator.getHealthcareSiteInvestigator().getInvestigator().getAssignedIdentifier().equals(source.getTreatingPhysician().getHealthcareSiteInvestigator().getInvestigator().getAssignedIdentifier())){
        			studySubject.setTreatingPhysician(studyInvestigator);
        			break;
        		}
        	}
        }
        if(source.getDiseaseHistory()!=null){
        	if(source.getDiseaseHistory().getStudyDisease()!=null){
        		for(StudyDisease studyDisease: studySubject.getStudySite().getStudy().getStudyDiseases()){<commit_msg>cpr-1578<commit_after>package edu.duke.cabig.c3pr.domain.factory;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.MessageSource;

import edu.duke.cabig.c3pr.constants.ContactMechanismType;
import edu.duke.cabig.c3pr.constants.CoordinatingCenterStudyStatus;
import edu.duke.cabig.c3pr.constants.OrganizationIdentifierTypeEnum;
import edu.duke.cabig.c3pr.constants.SiteStudyStatus;
import edu.duke.cabig.c3pr.dao.HealthcareSiteDao;
import edu.duke.cabig.c3pr.dao.ICD9DiseaseSiteDao;
import edu.duke.cabig.c3pr.dao.ParticipantDao;
import edu.duke.cabig.c3pr.dao.StudySubjectDao;
import edu.duke.cabig.c3pr.domain.Arm;
import edu.duke.cabig.c3pr.domain.Consent;
import edu.duke.cabig.c3pr.domain.ContactMechanism;
import edu.duke.cabig.c3pr.domain.Epoch;
import edu.duke.cabig.c3pr.domain.HealthcareSite;
import edu.duke.cabig.c3pr.domain.ICD9DiseaseSite;
import edu.duke.cabig.c3pr.domain.LocalContactMechanism;
import edu.duke.cabig.c3pr.domain.OrganizationAssignedIdentifier;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.duke.cabig.c3pr.domain.ScheduledEpoch;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.domain.StudyDisease;
import edu.duke.cabig.c3pr.domain.StudyInvestigator;
import edu.duke.cabig.c3pr.domain.StudySite;
import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.domain.StudySubjectConsentVersion;
import edu.duke.cabig.c3pr.domain.repository.StudyRepository;
import edu.duke.cabig.c3pr.exception.C3PRCodedException;
import edu.duke.cabig.c3pr.exception.C3PRExceptionHelper;
import edu.duke.cabig.c3pr.service.ParticipantService;

public class StudySubjectFactory {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(StudySubjectFactory.class);

    private C3PRExceptionHelper exceptionHelper;

    private MessageSource c3prErrorMessages;

    private final String prtIdentifierTypeValueStr = "MRN";

    private ParticipantService participantService;

    private StudyRepository studyRepository;

    private StudySubjectDao studySubjectDao;

    private ParticipantDao participantDao;
    
    private ICD9DiseaseSiteDao icd9DiseaseSiteDao;
    
    private HealthcareSiteDao healthcareSiteDao;

    public void setHealthcareSiteDao(HealthcareSiteDao healthcareSiteDao) {
		this.healthcareSiteDao = healthcareSiteDao;
	}

	public ICD9DiseaseSiteDao getIcd9DiseaseSiteDao() {
		return icd9DiseaseSiteDao;
	}

	public void setIcd9DiseaseSiteDao(ICD9DiseaseSiteDao icd9DiseaseSiteDao) {
		this.icd9DiseaseSiteDao = icd9DiseaseSiteDao;
	}

	public void setParticipantDao(ParticipantDao participantDao) {
        this.participantDao = participantDao;
    }

    public void setStudySubjectDao(StudySubjectDao studySubjectDao) {
        this.studySubjectDao = studySubjectDao;
    }

    public StudySubject buildStudySubject(StudySubject deserializedStudySubject)
                    throws C3PRCodedException {
        StudySubject built = new StudySubject();
        StudySite studySite = buildStudySite(deserializedStudySubject.getStudySite(),
                        buildStudy(deserializedStudySubject.getStudySite().getStudy()));
        Participant participant = buildParticipant(deserializedStudySubject.getParticipant());
        if (participant.getId() != null) {
            List<StudySubject> registrations = studySubjectDao
                            .searchBySubjectAndStudyIdentifiers(participant.getPrimaryIdentifier(),
                            		studySite.getStudy().getCoordinatingCenterAssignedIdentifier());
            if (registrations.size() > 0) {
                throw this.exceptionHelper
                                .getException(getCode("C3PR.EXCEPTION.REGISTRATION.STUDYSUBJECTS_ALREADY_EXISTS.CODE"));
            }
        }
        else {
            if (participant.validateParticipant()){ }//participantDao.save(participant);
            else {
                throw this.exceptionHelper
                                .getException(getCode("C3PR.EXCEPTION.REGISTRATION.SUBJECTS_INVALID_DETAILS.CODE"));
            }
        }
        buildAndAddStudySubjectConsentVersions(built, studySite);
        built.setStudySite(studySite);
    	studySite.getStudySiteStudyVersion().addStudySubjectStudyVersion(built.getStudySubjectStudyVersion());
        built.setParticipant(participant);
        Epoch epoch = buildEpoch(studySite.getStudy().getEpochs(), deserializedStudySubject
                        .getScheduledEpoch());
        ScheduledEpoch scheduledEpoch = buildScheduledEpoch(deserializedStudySubject
                        .getScheduledEpoch(), epoch);
        built.getScheduledEpochs().add(0, scheduledEpoch);
        fillStudySubjectDetails(built, deserializedStudySubject);
        return built;
    }
    
    public void buildAndAddStudySubjectConsentVersions(StudySubject built,StudySite studySite){
    	for (Consent consent :studySite.getStudySiteStudyVersion().getStudyVersion().getConsents()){
    		StudySubjectConsentVersion studySubjectConsentVersion = new StudySubjectConsentVersion();
    		studySubjectConsentVersion.setConsent(consent);
    		built.getStudySubjectStudyVersion().addStudySubjectConsentVersion(studySubjectConsentVersion);
    	}
    }

    public StudySubject buildReferencedStudySubject(StudySubject deserializedStudySubject)
                    throws C3PRCodedException {
        StudySubject built = new StudySubject();
        StudySite studySite = buildStudySite(deserializedStudySubject.getStudySite(),
                        buildStudy(deserializedStudySubject.getStudySite().getStudy()));
        Participant participant = buildParticipant(deserializedStudySubject.getParticipant());
        built.setStudySite(studySite);
        built.setParticipant(participant);
        Epoch epoch = buildEpoch(studySite.getStudy().getEpochs(), deserializedStudySubject
                        .getScheduledEpoch());
        ScheduledEpoch scheduledEpoch = buildScheduledEpoch(deserializedStudySubject
                        .getScheduledEpoch(), epoch);
        built.getScheduledEpochs().add(0, scheduledEpoch);
        fillStudySubjectDetails(built, deserializedStudySubject);
        return built;
    }

    public Participant buildParticipant(Participant participant) throws C3PRCodedException {
        if (participant.getIdentifiers() == null || participant.getIdentifiers().size() == 0) {
            throw exceptionHelper
                            .getException(getCode("C3PR.EXCEPTION.REGISTRATION.MISSING.SUBJECT_IDENTIFIER.CODE"));
        }
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : participant
                        .getOrganizationAssignedIdentifiers()) {
            if (organizationAssignedIdentifier.getType().getName().equals(this.prtIdentifierTypeValueStr)) {
                List<Participant> paList = participantService
                                .searchByMRN(organizationAssignedIdentifier);
                if (paList.size() > 1) {
                    throw exceptionHelper
                                    .getException(
                                                    getCode("C3PR.EXCEPTION.REGISTRATION.MULTIPLE.SUBJECTS_SAME_MRN.CODE"),
                                                    new String[] { organizationAssignedIdentifier
                                                                    .getValue() });
                }
                else if (paList.size() == 1) {
                    if (logger.isDebugEnabled()) {
                        logger
                                        .debug("buildParticipant(Participant) - Participant with the same MRN found in the database");
                    }
                    Participant temp = paList.get(0);
                    if (temp.getFirstName().equals(participant.getFirstName())
                                    && temp.getLastName().equals(participant.getLastName())
                                    && temp.getBirthDate().getTime() == participant.getBirthDate()
                                                    .getTime()) {
                        return temp;
                    }
                    else {
                        throw exceptionHelper
                                        .getException(
                                                        getCode("C3PR.EXCEPTION.REGISTRATION.INVALID.ANOTHER_SUBJECT_SAME_MRN.CODE"),
                                                        new String[] { organizationAssignedIdentifier
                                                                        .getValue() });
                    }
                }
            }
        }
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : participant
                .getOrganizationAssignedIdentifiers()) {
        	HealthcareSite healthcareSite= healthcareSiteDao.getByPrimaryIdentifier(organizationAssignedIdentifier.getHealthcareSite().getPrimaryIdentifier());
        	organizationAssignedIdentifier.setHealthcareSite(healthcareSite);
        }
//        addContactsToParticipant(participant);
        return participant;
    }

    public Study buildStudy(Study study) throws C3PRCodedException {
        if (study.getIdentifiers() == null || study.getIdentifiers().size() == 0) {
            throw exceptionHelper
                            .getException(getCode("C3PR.EXCEPTION.REGISTRATION.MISSING.STUDY_IDENTIFIER.CODE"));
        }
        List<Study> studies = null;
        OrganizationAssignedIdentifier identifier = null;
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : study
                        .getOrganizationAssignedIdentifiers()) {
            if (organizationAssignedIdentifier.getType().equals(OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER)) {
                identifier = organizationAssignedIdentifier;
                studies = studyRepository
                                .searchByCoOrdinatingCenterId(organizationAssignedIdentifier);
                break;
            }
        }
        if (identifier == null) {
            throw exceptionHelper
                            .getException(getCode("C3PR.EXCEPTION.REGISTRATION.MISSING.STUDY_COORDINATING_IDENTIFIER.CODE"));
        }
        if (studies == null) {
            throw exceptionHelper
                            .getException(
                                            getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.STUDY_WITH_IDENTIFIER.CODE"),
                                            new String[] {
                                                    identifier.getValue(),
                                                    OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER.getCode() });
        }
        if (studies.size() == 0) {
            throw exceptionHelper
                            .getException(
                                            getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.STUDY_WITH_IDENTIFIER.CODE"),
                                            new String[] {
                                                    identifier.getValue(),
                                                    OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER.getCode() });
        }
        if (studies.size() > 1) {
            throw exceptionHelper
                            .getException(
                                            getCode("C3PR.EXCEPTION.REGISTRATION.MULTIPLE.STUDY_SAME_CO_IDENTIFIER.CODE"),
                                            new String[] {
                                                    identifier.getValue(),
                                                    OrganizationIdentifierTypeEnum.COORDINATING_CENTER_IDENTIFIER.getCode() });
        }
        if (studies.get(0).getCoordinatingCenterStudyStatus() != CoordinatingCenterStudyStatus.OPEN) {
            throw exceptionHelper.getException(
                            getCode("C3PR.EXCEPTION.REGISTRATION.STUDY_NOT_ACTIVE"), new String[] {
                                    identifier.getHealthcareSite().getPrimaryIdentifier(),
                                    identifier.getValue() });
        }
        return studies.get(0);
    }

    public StudySite buildStudySite(StudySite studySite, Study study) throws C3PRCodedException {
        for (StudySite temp : study.getStudySites()) {
            if (temp.getHealthcareSite().getPrimaryIdentifier().equals(
                            studySite.getHealthcareSite().getPrimaryIdentifier())) {
                if (temp.getSiteStudyStatus() != SiteStudyStatus.ACTIVE) {
                    throw exceptionHelper
                                    .getException(
                                                    getCode("C3PR.EXCEPTION.REGISTRATION.STUDYSITE_NOT_ACTIVE"),
                                                    new String[] { temp.getHealthcareSite()
                                                                    .getPrimaryIdentifier() });
                }
                return temp;
            }
        }
        throw exceptionHelper
                        .getException(
                                        getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.STUDYSITE_WITH_NCICODE.CODE"),
                                        new String[] { studySite.getHealthcareSite()
                                                        .getPrimaryIdentifier() });
    }

    private Epoch buildEpoch(List<Epoch> epochs, ScheduledEpoch scheduledEpoch)
                    throws C3PRCodedException {
        for (Epoch epochCurr : epochs) {
            if (epochCurr.getName().equalsIgnoreCase(scheduledEpoch.getEpoch().getName())) {
                return epochCurr;
            }
        }
        throw exceptionHelper.getException(
                        getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.EPOCH_NAME.CODE"),
                        new String[] { scheduledEpoch.getEpoch().getName() });
    }

    private ScheduledEpoch buildScheduledEpoch(ScheduledEpoch source, Epoch epoch)
                    throws C3PRCodedException {
        ScheduledEpoch scheduledEpoch = null;
        ScheduledEpoch scheduledEpochSource = source;
        scheduledEpoch = new ScheduledEpoch();
        ScheduledEpoch scheduledTreatmentEpoch = scheduledEpoch;
        scheduledTreatmentEpoch.setEligibilityIndicator(true);
        if (scheduledEpochSource.getScheduledArm() != null
                        && scheduledEpochSource.getScheduledArm().getArm() != null
                        && scheduledEpochSource.getScheduledArm().getArm()
                                        .getName() != null) {
            Arm arm = null;
            for (Arm a : (epoch).getArms()) {
                if (a.getName().equals(
                		scheduledEpochSource.getScheduledArm().getArm()
                                                .getName())) {
                    arm = a;
                }
            }
            if (arm == null) {
                throw exceptionHelper
                                .getException(
                                                getCode("C3PR.EXCEPTION.REGISTRATION.NOTFOUND.ARM_NAME.CODE"),
                                                new String[] {
                                                	scheduledEpochSource
                                                                        .getScheduledArm()
                                                                        .getArm().getName(),
                                                                        scheduledEpochSource
                                                                        .getEpoch()
                                                                        .getName() });
            }
            scheduledTreatmentEpoch.getScheduledArms().get(0).setArm(arm);

        }
        scheduledEpoch.setEpoch(epoch);
        scheduledEpoch.setScEpochWorkflowStatus(source.getScEpochWorkflowStatus());
        scheduledEpoch.setStratumGroupNumber(source.getStratumGroupNumber());
        return scheduledEpoch;
    }

    private void fillStudySubjectDetails(StudySubject studySubject, StudySubject source){
    	
    	for (int i=0;i<source.getStudySubjectConsentVersions().size();i++){
    		studySubject.getStudySubjectStudyVersion().getStudySubjectConsentVersions().get(i).
    		setInformedConsentSignedDate(source.getStudySubjectConsentVersions().get(i).getInformedConsentSignedDate());
    	}
        studySubject.setStartDate(source.getStartDate());
        studySubject.setPaymentMethod(source.getPaymentMethod());
        studySubject.getIdentifiers().addAll(source.getIdentifiers());
        for (OrganizationAssignedIdentifier organizationAssignedIdentifier : studySubject
                .getOrganizationAssignedIdentifiers()) {
        	HealthcareSite healthcareSite= healthcareSiteDao.getByPrimaryIdentifier(organizationAssignedIdentifier.getHealthcareSite().getPrimaryIdentifier());
        	organizationAssignedIdentifier.setHealthcareSite(healthcareSite);
        }
        if(source.getTreatingPhysician()!=null){
        	for(StudyInvestigator studyInvestigator:studySubject.getStudySite().getStudyInvestigators()){
        		if(studyInvestigator.getHealthcareSiteInvestigator().getInvestigator().getAssignedIdentifier().equals(source.getTreatingPhysician().getHealthcareSiteInvestigator().getInvestigator().getAssignedIdentifier())){
        			studySubject.setTreatingPhysician(studyInvestigator);
        			break;
        		}
        	}
        }
        if(source.getDiseaseHistory()!=null){
        	if(source.getDiseaseHistory().getStudyDisease()!=null){
        		for(StudyDisease studyDisease: studySubject.getStudySite().getStudy().getStudyDiseases()){
{'fraction_non_alphanumeric': 0.08045236962181228, 'fraction_numerical': 0.004730845673655628, 'mean_word_length': 1.9298777987369802, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 164, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987650', 'n_tokens_mistral': 9186, 'n_tokens_neox': 8666, 'n_words': 1136}