<commit_before>package edu.duke.cabig.c3pr.dao;

import edu.duke.cabig.c3pr.domain.Arm;
import edu.duke.cabig.c3pr.domain.HealthcareSite;
import edu.duke.cabig.c3pr.domain.Identifier;
import edu.duke.cabig.c3pr.domain.OrganizationAssignedIdentifier;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.domain.SystemAssignedIdentifier;
import edu.duke.cabig.c3pr.exception.C3PRBaseException;
import edu.duke.cabig.c3pr.exception.C3PRCodedException;
import edu.emory.mathcs.backport.java.util.Collections;
import edu.nwu.bioinformatics.commons.CollectionUtils;
import gov.nih.nci.cabig.ctms.dao.MutableDomainObjectDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.*;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Hibernate implementation of StudyDao
 * @author Priyatam
 */
@Transactional(readOnly = true)
public class StudyDao extends GridIdentifiableDao<Study>
        implements MutableDomainObjectDao<Study> {

    private static final List<String> SUBSTRING_MATCH_PROPERTIES
            = Arrays.asList("shortTitleText");
    private static final List<String> EXACT_MATCH_PROPERTIES
            = Collections.emptyList();
    private static Log log = LogFactory.getLog(StudyDao.class);
    private HealthcareSiteDao healthcareSiteDao;

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

	@Override
    public Class<Study> domainClass() {
        return Study.class;
    }

    public Study getStudyDesignById(int id) {
        return (Study) getHibernateTemplate().get(domainClass(), id);
    }

    @SuppressWarnings("unchecked")
    public Study getByIdentifier(SystemAssignedIdentifier identifier) {
        Criteria criteria = getSession().createCriteria(domainClass());
        criteria = criteria.createCriteria("identifiers");

        if(identifier.getType() != null) {
            criteria.add(Restrictions.eq("type", identifier.getType()));
        }

        if(identifier.getSystemName() != null) {
            criteria.add(Restrictions.eq("systemName", identifier.getSystemName()));
        }

        if(identifier.getValue() != null) {
            criteria.add(Restrictions.eq("value", identifier.getValue()));
        }
        return (Study) CollectionUtils.firstElement(criteria.list());
    }
    
    @SuppressWarnings("unchecked")
    public List<Study> searchByOrgIdentifier(OrganizationAssignedIdentifier id) {
    	return (List<Study>) getHibernateTemplate().find("select S from Study S, Identifier I where I.healthcareSite.id=?" +
    			" and I.value=? and I.type=? and I=any elements(S.identifiers)",new Object[]{id.getHealthcareSite().getId(),id.getValue(),id.getType()});
    }
    @Transactional (readOnly = false)
    public Study merge(Study study) {
        return (Study)getHibernateTemplate().merge(study);
    }

    @Transactional (readOnly = false)
    public void save(Study study) {
        getHibernateTemplate().saveOrUpdate(study);
    }



    public List<Study> getBySubnames(String[] subnames) {
        return findBySubname(subnames,
                SUBSTRING_MATCH_PROPERTIES, EXACT_MATCH_PROPERTIES);
    }


    /*
      * Searches based on an example object. Typical usage from your service class: -
      * If you want to search based on diseaseCode, monitorCode,
      * <li><code>Study study = new Study();</li></code>
      * <li>code>study.setDiseaseCode("diseaseCode");</li></code>
      * <li>code>study.setDMonitorCode("monitorCode");</li></code>
      * <li>code>studyDao.searchByExample(study)</li></code>
      * @return list of matching study objects based on your sample study object
      */
  
    @Transactional(readOnly = true)
    public List<Study> searchByExample(Study study, boolean isWildCard) {
        List<Study> result = new ArrayList<Study>();

        Example example = Example.create(study).excludeZeroes().ignoreCase();
        try {
            Criteria studyCriteria = getSession().createCriteria(Study.class);
            studyCriteria.addOrder(Order.asc("shortTitleText"));
            studyCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            
            if (isWildCard)
            {
                example.excludeProperty("doNotUse").enableLike(MatchMode.ANYWHERE);
                studyCriteria.add(example);
                if (study.getIdentifiers().size() > 0) {
                    studyCriteria.createCriteria("identifiers")
                            .add(Restrictions.ilike("value", "%" + study.getIdentifiers().get(0)
                                    .getValue() + "%"));
                }
                result =  studyCriteria.list();
            }
            result =  studyCriteria.add(example).list();
        } catch (DataAccessResourceFailureException e) {
            log.error(e.getMessage());
        } catch (IllegalStateException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HibernateException e) {
            log.error(e.getMessage());
        }
        return result;
    }
    
    public List<OrganizationAssignedIdentifier> getCoordinatingCenterIdentifiersWithValue(String coordinatingCetnerIdentifierValue,  HealthcareSite site) throws DataAccessException {
		List<OrganizationAssignedIdentifier> orgAssignedIdentifiers = (List<OrganizationAssignedIdentifier>) getHibernateTemplate().
                find("from Identifier I where I.type='Coordinating Center Identifier' and I.healthcareSite = ?",site);
		List<OrganizationAssignedIdentifier> ccIdentifiers = new ArrayList<OrganizationAssignedIdentifier>();
		for(OrganizationAssignedIdentifier studyIdent:orgAssignedIdentifiers){
			if (studyIdent.getValue().equalsIgnoreCase(coordinatingCetnerIdentifierValue)){
				ccIdentifiers.add(studyIdent);
			}
		}
		return ccIdentifiers;
	}

    public List<OrganizationAssignedIdentifier> getFundingSponsorIdentifiersWithValue(String fundingSponsorIdentifierValue,  HealthcareSite site) throws DataAccessException {
		List<OrganizationAssignedIdentifier> orgAssignedIdentifiers = (List<OrganizationAssignedIdentifier>) getHibernateTemplate().
                find("from Identifier I where I.type='Protocol Authority Identifier' and I.healthcareSite = ?",site);
		List<OrganizationAssignedIdentifier> fsIdentifiers = new ArrayList<OrganizationAssignedIdentifier>();
		for(OrganizationAssignedIdentifier subIdent:orgAssignedIdentifiers){
			if (subIdent.getValue().equalsIgnoreCase(fundingSponsorIdentifierValue)){
				fsIdentifiers.add(subIdent);
			}
		}
		return fsIdentifiers;
	}


    /**
     * Default Search without a Wildchar
     * @param study
     * @return Search Results
     */
    public List<Study> searchByExample(Study study) {
        return searchByExample(study, false);
    }

    /**
     * Returns all study objects
     * @return list of study objects
     */
    public List<Study> getAll(){<commit_msg>mdae searches transactional to avoid the read-only exception in edit study flow(pending status).<commit_after>package edu.duke.cabig.c3pr.dao;

import edu.duke.cabig.c3pr.domain.Arm;
import edu.duke.cabig.c3pr.domain.HealthcareSite;
import edu.duke.cabig.c3pr.domain.Identifier;
import edu.duke.cabig.c3pr.domain.OrganizationAssignedIdentifier;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.domain.StudySubject;
import edu.duke.cabig.c3pr.domain.SystemAssignedIdentifier;
import edu.duke.cabig.c3pr.exception.C3PRBaseException;
import edu.duke.cabig.c3pr.exception.C3PRCodedException;
import edu.emory.mathcs.backport.java.util.Collections;
import edu.nwu.bioinformatics.commons.CollectionUtils;
import gov.nih.nci.cabig.ctms.dao.MutableDomainObjectDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.*;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Hibernate implementation of StudyDao
 * @author Priyatam
 */
@Transactional(readOnly = true)
public class StudyDao extends GridIdentifiableDao<Study>
        implements MutableDomainObjectDao<Study> {

    private static final List<String> SUBSTRING_MATCH_PROPERTIES
            = Arrays.asList("shortTitleText");
    private static final List<String> EXACT_MATCH_PROPERTIES
            = Collections.emptyList();
    private static Log log = LogFactory.getLog(StudyDao.class);
    private HealthcareSiteDao healthcareSiteDao;

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

	@Override
    public Class<Study> domainClass() {
        return Study.class;
    }

    public Study getStudyDesignById(int id) {
        return (Study) getHibernateTemplate().get(domainClass(), id);
    }

    @SuppressWarnings("unchecked")
    public Study getByIdentifier(SystemAssignedIdentifier identifier) {
        Criteria criteria = getSession().createCriteria(domainClass());
        criteria = criteria.createCriteria("identifiers");

        if(identifier.getType() != null) {
            criteria.add(Restrictions.eq("type", identifier.getType()));
        }

        if(identifier.getSystemName() != null) {
            criteria.add(Restrictions.eq("systemName", identifier.getSystemName()));
        }

        if(identifier.getValue() != null) {
            criteria.add(Restrictions.eq("value", identifier.getValue()));
        }
        return (Study) CollectionUtils.firstElement(criteria.list());
    }
    
    @SuppressWarnings("unchecked")
    public List<Study> searchByOrgIdentifier(OrganizationAssignedIdentifier id) {
    	return (List<Study>) getHibernateTemplate().find("select S from Study S, Identifier I where I.healthcareSite.id=?" +
    			" and I.value=? and I.type=? and I=any elements(S.identifiers)",new Object[]{id.getHealthcareSite().getId(),id.getValue(),id.getType()});
    }
    @Transactional (readOnly = false)
    public Study merge(Study study) {
        return (Study)getHibernateTemplate().merge(study);
    }

    @Transactional (readOnly = false)
    public void save(Study study) {
        getHibernateTemplate().saveOrUpdate(study);
    }



    public List<Study> getBySubnames(String[] subnames) {
        return findBySubname(subnames,
                SUBSTRING_MATCH_PROPERTIES, EXACT_MATCH_PROPERTIES);
    }


    /*
      * Searches based on an example object. Typical usage from your service class: -
      * If you want to search based on diseaseCode, monitorCode,
      * <li><code>Study study = new Study();</li></code>
      * <li>code>study.setDiseaseCode("diseaseCode");</li></code>
      * <li>code>study.setDMonitorCode("monitorCode");</li></code>
      * <li>code>studyDao.searchByExample(study)</li></code>
      * @return list of matching study objects based on your sample study object
      */
  
    @Transactional(readOnly = true)
    public List<Study> searchByExample(Study study, boolean isWildCard) {
        List<Study> result = new ArrayList<Study>();

        Example example = Example.create(study).excludeZeroes().ignoreCase();
        try {
            Criteria studyCriteria = getSession().createCriteria(Study.class);
            studyCriteria.addOrder(Order.asc("shortTitleText"));
            studyCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            
            if (isWildCard)
            {
                example.excludeProperty("doNotUse").enableLike(MatchMode.ANYWHERE);
                studyCriteria.add(example);
                if (study.getIdentifiers().size() > 0) {
                    studyCriteria.createCriteria("identifiers")
                            .add(Restrictions.ilike("value", "%" + study.getIdentifiers().get(0)
                                    .getValue() + "%"));
                }
                result =  studyCriteria.list();
            }
            result =  studyCriteria.add(example).list();
        } catch (DataAccessResourceFailureException e) {
            log.error(e.getMessage());
        } catch (IllegalStateException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HibernateException e) {
            log.error(e.getMessage());
        }
        return result;
    }
    
    @Transactional(readOnly = false)
    public List<OrganizationAssignedIdentifier> getCoordinatingCenterIdentifiersWithValue(String coordinatingCetnerIdentifierValue,  HealthcareSite site) throws DataAccessException {
		List<OrganizationAssignedIdentifier> orgAssignedIdentifiers = (List<OrganizationAssignedIdentifier>) getHibernateTemplate().
                find("from Identifier I where I.type='Coordinating Center Identifier' and I.healthcareSite = ?",site);
		List<OrganizationAssignedIdentifier> ccIdentifiers = new ArrayList<OrganizationAssignedIdentifier>();
		for(OrganizationAssignedIdentifier studyIdent:orgAssignedIdentifiers){
			if (studyIdent.getValue().equalsIgnoreCase(coordinatingCetnerIdentifierValue)){
				ccIdentifiers.add(studyIdent);
			}
		}
		return ccIdentifiers;
	}

    @Transactional(readOnly = false)
    public List<OrganizationAssignedIdentifier> getFundingSponsorIdentifiersWithValue(String fundingSponsorIdentifierValue,  HealthcareSite site) throws DataAccessException {
		List<OrganizationAssignedIdentifier> orgAssignedIdentifiers = (List<OrganizationAssignedIdentifier>) getHibernateTemplate().
                find("from Identifier I where I.type='Protocol Authority Identifier' and I.healthcareSite = ?",site);
		List<OrganizationAssignedIdentifier> fsIdentifiers = new ArrayList<OrganizationAssignedIdentifier>();
		for(OrganizationAssignedIdentifier subIdent:orgAssignedIdentifiers){
			if (subIdent.getValue().equalsIgnoreCase(fundingSponsorIdentifierValue)){
				fsIdentifiers.add(subIdent);
			}
		}
		return fsIdentifiers;
	}


    /**
     * Default Search without a Wildchar
     * @param study
     * @return Search Results
     */
    public List<Study> searchByExample(Study study) {
        return searchByExample(study, false);
    }

    /**
     * Returns all study objects
     * @return list of study objects
     */
    public List<Study> getAll(){
{'fraction_non_alphanumeric': 0.10611211091115111, 'fraction_numerical': 0.001999600079984003, 'mean_word_length': 3.6552901023890785, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '259163', 'n_tokens_mistral': 4380, 'n_tokens_neox': 4184, 'n_words': 765}