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

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;

import edu.duke.cabig.c3pr.domain.ContactMechanism;
import edu.duke.cabig.c3pr.domain.HealthcareSite;
import edu.duke.cabig.c3pr.domain.Identifier;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.emory.mathcs.backport.java.util.Collections;
import edu.nwu.bioinformatics.commons.CollectionUtils;
import gov.nih.nci.cabig.ctms.dao.MutableDomainObjectDao;

/**
 * @author Priyatam, kulasekaran
 */
public class ParticipantDao extends GridIdentifiableDao<Participant> implements MutableDomainObjectDao<Participant>{

	private List<String> SUBSTRING_MATCH_PROPERTIES = Arrays.asList("lastName");

	private List<String> EXACT_MATCH_PROPERTIES = Collections.emptyList();

	private static Log log = LogFactory.getLog(ParticipantDao.class);

	public Class<Participant> domainClass() {
		return Participant.class;
	}

	/**
	 * /* Searches based on an example object. Typical usage from your service
	 * class: - If you want to search based on diseaseCode, monitorCode,
	 * <li><code>Participant participant = new Participant();</li></code>
	 * <li>code>participant.setLastName("last_namee");</li>
	 * </code>
	 * <li>code>participantDao.searchByExample(study)</li>
	 * </code>
	 * 
	 * @return list of matching participant objects based on your sample
	 *         participant object
	 * @param participant
	 * @return
	 */
	public List<Participant> searchByExample(Participant participant,
			boolean isWildCard) {
		Example example = Example.create(participant).excludeZeroes()
				.ignoreCase();
		Criteria participantCriteria = getSession().createCriteria(
				Participant.class);
		if (isWildCard) {
			example.excludeProperty("doNotUse").enableLike(MatchMode.ANYWHERE);
			participantCriteria.add(example);
			if (participant.getIdentifiers().size() > 0) {
				participantCriteria.createCriteria("identifiers").add(
						Restrictions.like("value", participant.getIdentifiers()
								.get(0).getValue()
								+ "%"));
			}
			return participantCriteria.list();

		}
		return participantCriteria.add(example).list();

	}

	/**
	 * Default Search without a Wildchar
	 * 
	 * @see edu.duke.cabig.c3pr.dao.searchByExample(Participant participant,
	 *      boolean isWildCard)
	 * @param participant
	 * @return Search results
	 */
	public List<Participant> searchByExample(Participant participant) {
		return searchByExample(participant, true);
	}

	/**
	 * 
	 * @return
	 * @throws DataAccessException
	 */
	public List<Participant> getAll() throws DataAccessException {
		return getHibernateTemplate().find("from Participant");
	}
	
	/**
	 * 
	 * @return
	 * @throws DataAccessException
	 */
	public List<Identifier> getSubjectIdentifiersWithMRN(String MRN) throws DataAccessException {
		return (List<Identifier>) getHibernateTemplate().
                find("from Identifier I where I.prt_id is not null and I.type='MRN' and I.value = ?", MRN);
	}

	/**
	 * An overloaded method to return Participant Object along with the
	 * collection of associated identifiers
	 * 
	 * @return Participant Object based on the id
	 * @throws DataAccessException
	 */
	public Participant getById(int id, boolean withIdentifiers) {

		Participant participant = (Participant) getHibernateTemplate().get(
				domainClass(), id);
		if (withIdentifiers) {
			List<Identifier> identifiers = participant.getIdentifiers();
			int size = identifiers.size();
		}
		List<ContactMechanism> contactMechanisms = participant.getContactMechanisms();
		contactMechanisms.size();

		return participant;<commit_msg>added method to retrieve participants at a given Organization and given MRN<commit_after>package edu.duke.cabig.c3pr.dao;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;

import edu.duke.cabig.c3pr.domain.ContactMechanism;
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.emory.mathcs.backport.java.util.Collections;
import edu.nwu.bioinformatics.commons.CollectionUtils;
import gov.nih.nci.cabig.ctms.dao.MutableDomainObjectDao;

/**
 * @author Priyatam, kulasekaran
 */
public class ParticipantDao extends GridIdentifiableDao<Participant> implements MutableDomainObjectDao<Participant>{

	private List<String> SUBSTRING_MATCH_PROPERTIES = Arrays.asList("lastName");

	private List<String> EXACT_MATCH_PROPERTIES = Collections.emptyList();

	private static Log log = LogFactory.getLog(ParticipantDao.class);

	public Class<Participant> domainClass() {
		return Participant.class;
	}

	/**
	 * /* Searches based on an example object. Typical usage from your service
	 * class: - If you want to search based on diseaseCode, monitorCode,
	 * <li><code>Participant participant = new Participant();</li></code>
	 * <li>code>participant.setLastName("last_namee");</li>
	 * </code>
	 * <li>code>participantDao.searchByExample(study)</li>
	 * </code>
	 * 
	 * @return list of matching participant objects based on your sample
	 *         participant object
	 * @param participant
	 * @return
	 */
	public List<Participant> searchByExample(Participant participant,
			boolean isWildCard) {
		Example example = Example.create(participant).excludeZeroes()
				.ignoreCase();
		Criteria participantCriteria = getSession().createCriteria(
				Participant.class);
		if (isWildCard) {
			example.excludeProperty("doNotUse").enableLike(MatchMode.ANYWHERE);
			participantCriteria.add(example);
			if (participant.getIdentifiers().size() > 0) {
				participantCriteria.createCriteria("identifiers").add(
						Restrictions.like("value", participant.getIdentifiers()
								.get(0).getValue()
								+ "%"));
			}
			return participantCriteria.list();

		}
		return participantCriteria.add(example).list();

	}

	/**
	 * Default Search without a Wildchar
	 * 
	 * @see edu.duke.cabig.c3pr.dao.searchByExample(Participant participant,
	 *      boolean isWildCard)
	 * @param participant
	 * @return Search results
	 */
	public List<Participant> searchByExample(Participant participant) {
		return searchByExample(participant, true);
	}

	/**
	 * 
	 * @return
	 * @throws DataAccessException
	 */
	public List<Participant> getAll() throws DataAccessException {
		return getHibernateTemplate().find("from Participant");
	}
	
	/**
	 * 
	 * @return
	 * @throws DataAccessException
	 */
	public List<OrganizationAssignedIdentifier> getSubjectIdentifiersWithMRN(String MRN,  HealthcareSite site) throws DataAccessException {
		List<OrganizationAssignedIdentifier> orgAssignedIdentifiers = (List<OrganizationAssignedIdentifier>) getHibernateTemplate().
                find("from Identifier I where I.type='MRN' and I.healthcareSite = ?",site);
		List<OrganizationAssignedIdentifier> subIdentifiers = new ArrayList<OrganizationAssignedIdentifier>();
		for(OrganizationAssignedIdentifier subIdent:orgAssignedIdentifiers ){
			if (subIdent.getValue().equalsIgnoreCase(MRN))
				subIdentifiers.add(subIdent);
		}
		return subIdentifiers;
	}

	/**
	 * An overloaded method to return Participant Object along with the
	 * collection of associated identifiers
	 * 
	 * @return Participant Object based on the id
	 * @throws DataAccessException
	 */
	public Participant getById(int id, boolean withIdentifiers) {

		Participant participant = (Participant) getHibernateTemplate().get(
				domainClass(), id);
		if (withIdentifiers) {
			List<Identifier> identifiers = participant.getIdentifiers();
			int size = identifiers.size();
		}
		List<ContactMechanism> contactMechanisms = participant.getContactMechanisms();
		contactMechanisms.size();

		return participant;
{'fraction_non_alphanumeric': 0.11027568922305764, 'fraction_numerical': 0.0020288817281298483, 'mean_word_length': 4.905567300916138, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 56, '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': '141647', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2445, 'n_words': 511}