<commit_before>/**
 * 
 */
package gov.nih.nci.cabig.caaers.api.impl;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.annotation.Transactional;

import gov.nih.nci.cabig.caaers.api.StudyService;
import gov.nih.nci.cabig.caaers.dao.CaaersDao;
import gov.nih.nci.cabig.caaers.dao.ParticipantDao;
import gov.nih.nci.cabig.caaers.dao.SiteDao;
import gov.nih.nci.cabig.caaers.dao.StudyDao;
import gov.nih.nci.cabig.caaers.dao.StudyParticipantAssignmentDao;
import gov.nih.nci.cabig.caaers.domain.DomainObject;
import gov.nih.nci.cabig.caaers.domain.Participant;
import gov.nih.nci.cabig.caaers.domain.Site;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudyParticipantAssignment;
import gov.nih.nci.cabig.caaers.domain.StudySite;

/**
 * @author <a href="mailto:<EMAIL>><NAME></a>
 *
 */
@Transactional
public class DefaultStudyService implements StudyService {
    
    private ParticipantDao participantDao;
    private StudyParticipantAssignmentDao studyParticipantAssignmentDao;
    private SiteDao siteDao;
    private StudyDao studyDao;
    private SessionFactory sessionFactory;
    


    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /* (non-Javadoc)
     * @see gov.nih.nci.cabig.caaers.api.StudyService#assignParticipant(gov.nih.nci.cabig.caaers.domain.Study, gov.nih.nci.cabig.caaers.domain.Participant, gov.nih.nci.cabig.caaers.domain.Site)
     */
    public StudyParticipantAssignment assignParticipant(Study study, Participant participant,
                    Site site) {

        
        
        StudyParticipantAssignment newAssignment = new StudyParticipantAssignment();
        ParameterLoader loader = new ParameterLoader(study, site);

        Participant loadedParticipant = load(participant, getParticipantDao(), false);
        if (loadedParticipant == null) {
            getParticipantDao().save(participant);
            loadedParticipant = participant;
            
        } else {
            StudyParticipantAssignment existingAssignment = 
                getStudyParticipantAssignmentDao().getAssignment(loadedParticipant, loader.getStudy());
            if (existingAssignment != null) {
                throw new IllegalArgumentException(
                    "Participant already assigned to this study.");
            }
        }
        
        StudySite studySite = loader.validateSiteInStudy();
        newAssignment.setParticipant(loadedParticipant);
        newAssignment.setStudySite(studySite);
        newAssignment.setDateOfEnrollment(new Date());
        loadedParticipant.addAssignment(newAssignment);
        getParticipantDao().save(loadedParticipant);
        
        return newAssignment;
    }
    
//    public StudyParticipantAssignment getStudyParticipantAssignment(Participant participant, Study study){
//        return getStudyParticipantAssignmentDao().getAssignment(participant, study);
//    }
    

   
    
    private <T extends DomainObject> T load(T param, CaaersDao<T> dao, boolean required) {
        T loaded = null;
        if(param.getId() != null){
            loaded = dao.getById(param.getId());
        }
        if(required && loaded == null){
            throw new IllegalArgumentException(param.getClass().getSimpleName() + " doesn't exist.");
        }
        return loaded;
    }
    
    private class ParameterLoader{
        private Study study;
        private Site site;
        
        public ParameterLoader(Study study, Site site){
            loadStudy(study);
            loadSite(site);
        }

        public StudySite validateSiteInStudy() {
            StudySite studySite = null;
            for (StudySite aStudySite : getStudy().getStudySites()) {<commit_msg>loading the parameter entity is changed to load based on the gridId instead of the id.<commit_after>/**
 * 
 */
package gov.nih.nci.cabig.caaers.api.impl;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.annotation.Transactional;

import gov.nih.nci.cabig.caaers.api.StudyService;
import gov.nih.nci.cabig.caaers.dao.CaaersDao;
import gov.nih.nci.cabig.caaers.dao.GridIdentifiableDao;
import gov.nih.nci.cabig.caaers.dao.ParticipantDao;
import gov.nih.nci.cabig.caaers.dao.SiteDao;
import gov.nih.nci.cabig.caaers.dao.StudyDao;
import gov.nih.nci.cabig.caaers.dao.StudyParticipantAssignmentDao;
import gov.nih.nci.cabig.caaers.domain.DomainObject;
import gov.nih.nci.cabig.caaers.domain.GridIdentifiable;
import gov.nih.nci.cabig.caaers.domain.Participant;
import gov.nih.nci.cabig.caaers.domain.Site;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.domain.StudyParticipantAssignment;
import gov.nih.nci.cabig.caaers.domain.StudySite;

/**
 * @author <a href="mailto:<EMAIL>><NAME></a>
 *
 */
@Transactional
public class DefaultStudyService implements StudyService {
    
    private ParticipantDao participantDao;
    private StudyParticipantAssignmentDao studyParticipantAssignmentDao;
    private SiteDao siteDao;
    private StudyDao studyDao;
    private SessionFactory sessionFactory;
    


    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /* (non-Javadoc)
     * @see gov.nih.nci.cabig.caaers.api.StudyService#assignParticipant(gov.nih.nci.cabig.caaers.domain.Study, gov.nih.nci.cabig.caaers.domain.Participant, gov.nih.nci.cabig.caaers.domain.Site)
     */
    public StudyParticipantAssignment assignParticipant(Study study, Participant participant,
                    Site site) {

        
        
        StudyParticipantAssignment newAssignment = new StudyParticipantAssignment();
        ParameterLoader loader = new ParameterLoader(study, site);

        Participant loadedParticipant = load(participant, getParticipantDao(), false);
        if (loadedParticipant == null) {
            getParticipantDao().save(participant);
            loadedParticipant = participant;
            
        } else {
            StudyParticipantAssignment existingAssignment = 
                getStudyParticipantAssignmentDao().getAssignment(loadedParticipant, loader.getStudy());
            if (existingAssignment != null) {
                throw new IllegalArgumentException(
                    "Participant already assigned to this study.");
            }
        }
        
        StudySite studySite = loader.validateSiteInStudy();
        newAssignment.setParticipant(loadedParticipant);
        newAssignment.setStudySite(studySite);
        newAssignment.setDateOfEnrollment(new Date());
        loadedParticipant.addAssignment(newAssignment);
        getParticipantDao().save(loadedParticipant);
        
        return newAssignment;
    }
    
//    public StudyParticipantAssignment getStudyParticipantAssignment(Participant participant, Study study){
//        return getStudyParticipantAssignmentDao().getAssignment(participant, study);
//    }
    

   
    
    private <T extends DomainObject & GridIdentifiable> T load(T param, GridIdentifiableDao<T> dao, boolean required) {
    	checkForGridId(param);
    	T loaded = null;
        if(param.getGridId() != null){
            loaded = dao.getByGridId(param.getGridId());
        }
        if(required && loaded == null){
            throw new IllegalArgumentException(param.getClass().getSimpleName() + " doesn't exist.");
        }
        return loaded;
    }

    private void checkForGridId(GridIdentifiable gridIdentifiable) {
        if (!gridIdentifiable.hasGridId()) {
            throw new IllegalArgumentException(
                "No gridId on " + gridIdentifiable.getClass().getSimpleName().toLowerCase() + " parameter");
        }
    }    
    
    private class ParameterLoader{
        private Study study;
        private Site site;
        
        public ParameterLoader(Study study, Site site){
            loadStudy(study);
            loadSite(site);
        }

        public StudySite validateSiteInStudy() {
            StudySite studySite = null;
            for (StudySite aStudySite : getStudy().getStudySites()) {
{'fraction_non_alphanumeric': 0.09514650978617491, 'fraction_numerical': 0.00045253988007693175, 'mean_word_length': 3.1173730787144853, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4744555', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2586, 'n_words': 435}