Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

In this page you can find the example usage for org.hibernate Criteria createCriteria.

Prototype

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

From source file:edu.duke.cabig.c3pr.dao.ConsentDao.java

License:BSD License

@SuppressWarnings("unchecked")
public List<Consent> searchByExampleAndStudy(Consent consent, Study study) {
    Example example = Example.create(consent).excludeZeroes().ignoreCase();
    example.enableLike(MatchMode.ANYWHERE);
    Criteria criteria = getSession().createCriteria(Consent.class);
    if (!StringUtils.isBlank(consent.getVersionId())) {
        criteria.add(Restrictions.eq("versionId", consent.getVersionId()));
    }//w  w  w  .j a  va  2  s  .c om
    criteria.add(example);
    criteria.createCriteria("studyVersion.study").add(Restrictions.eq("id", study.getId()));
    return criteria.list();
}

From source file:edu.duke.cabig.c3pr.dao.HealthcareSiteDao.java

License:BSD License

/**Runs a like search against the identifier value for all the strings in the array that is passed in.
 * Used by auto-completer. Can be changed to use hql.
 * /*ww w  .  j  a va 2s  . c  o  m*/
 * @param subnames
 * @return List<HealthcareSite> which have an identifier matching to one of the strings in subnames array
 */
private List<HealthcareSite> getBySubIdentifier(String[] subnames) {
    List<HealthcareSite> healthcareSitesByIdentifier = new ArrayList<HealthcareSite>();
    for (String subname : subnames) {
        if (!StringUtils.isEmpty(subname)) {
            Criteria orgCriteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
                    .createCriteria(HealthcareSite.class);
            Criteria identifiersAssignedToOrganizationCriteria = orgCriteria
                    .createCriteria("identifiersAssignedToOrganization");
            identifiersAssignedToOrganizationCriteria.add(Expression.ilike("value", "%" + subname + "%"));

            if (orgCriteria.list().size() > 0) {
                healthcareSitesByIdentifier.addAll(orgCriteria.list());
            }
        }
    }
    return healthcareSitesByIdentifier;
}

From source file:edu.duke.cabig.c3pr.dao.HealthcareSiteDao.java

License:BSD License

/**
 * Gets by primary IDentifier code from local. 
 * /*from  ww  w  .  j  a  v a  2 s . co  m*/
 * @param primaryIdentifierCode the nci institute code
 * @return the HealthcareSite (Null if not match is found)
 * @throws C3PRBaseException    
 * @throws C3PRBaseRuntimeException     */
public HealthcareSite getByPrimaryIdentifierFromLocal(String primaryIdentifierCode) {
    if (StringUtils.isEmpty(primaryIdentifierCode)) {
        return null;
    }

    Criteria orgCriteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
            .createCriteria(HealthcareSite.class);
    Criteria identifiersAssignedToOrganizationCriteria = orgCriteria
            .createCriteria("identifiersAssignedToOrganization");

    identifiersAssignedToOrganizationCriteria.add(Expression.eq("value", primaryIdentifierCode));
    identifiersAssignedToOrganizationCriteria.add(Expression.eq("primaryIndicator", Boolean.TRUE));

    if (orgCriteria.list().size() > 0) {
        return (HealthcareSite) orgCriteria.list().get(0);
    } else {
        return null;
    }
}

From source file:edu.duke.cabig.c3pr.dao.HealthcareSiteDao.java

License:BSD License

@SuppressWarnings("unchecked")
public HealthcareSite getByTypeAndCodeFromLocal(String typeName, String code, Boolean isPrimary) {
    if (StringUtils.isEmpty(code)) {
        return null;
    }/* w w w  .j  a va2  s  .  co m*/

    Criteria orgCriteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
            .createCriteria(HealthcareSite.class);
    Criteria identifiersAssignedToOrganizationCriteria = orgCriteria
            .createCriteria("identifiersAssignedToOrganization");
    Conjunction conjunction = Restrictions.conjunction();
    conjunction.add(Expression.eq("typeInternal", typeName));
    conjunction.add(Expression.eq("value", code));
    if (isPrimary != null) {
        conjunction.add(Expression.eq("primaryIndicator", isPrimary));
    }
    identifiersAssignedToOrganizationCriteria.add(conjunction);
    return CollectionUtils.firstElement((List<HealthcareSite>) orgCriteria.list());
}

From source file:edu.duke.cabig.c3pr.dao.HealthcareSiteDao.java

License:BSD License

/**
  * Search by example.//from  w  ww . j av a 2 s  .c  o  m
  * 
  * @param hcs the hcs
  * @param isWildCard the is wild card
  * @return the list< healthcare site>
  */

public List<HealthcareSite> searchByExample(HealthcareSite hcs, boolean isWildCard, int maxResults) {

    List<HealthcareSite> remoteHealthcareSites = new ArrayList<HealthcareSite>();
    remoteHealthcareSites.addAll(getExternalOrganizationsByExampleFromResolver(hcs));
    updateDatabaseWithRemoteHealthcareSites(remoteHealthcareSites);

    List<HealthcareSite> result = new ArrayList<HealthcareSite>();
    Example example = Example.create(hcs).excludeZeroes().ignoreCase();
    try {
        Criteria orgCriteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
                .createCriteria(HealthcareSite.class);
        Criteria identifiersAssignedToOrganizationCriteria = orgCriteria
                .createCriteria("identifiersAssignedToOrganization");

        if (StringUtils.isNotBlank(hcs.getPrimaryIdentifier())) {
            identifiersAssignedToOrganizationCriteria
                    .add(Expression.ilike("value", "%" + hcs.getPrimaryIdentifier() + "%"));
            identifiersAssignedToOrganizationCriteria
                    .add(Expression.eq("typeInternal", OrganizationIdentifierTypeEnum.CTEP.getName()));
        }
        if (StringUtils.isNotBlank(hcs.getName())) {
            orgCriteria.add(Expression.ilike("name", "%" + hcs.getName() + "%"));
        }

        if (isWildCard) {
            example.enableLike(MatchMode.ANYWHERE);
        }
        example.excludeProperty("studyEndPointProperty");
        example.excludeProperty("registrationEndPointProperty");

        orgCriteria.addOrder(Order.asc("name"));
        orgCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        if (maxResults > 0)
            orgCriteria.setMaxResults(maxResults);
        result = orgCriteria.add(example).list();
    } catch (Exception e) {
        log.error(e.getMessage());
    }
    return result;
}

From source file:edu.duke.cabig.c3pr.dao.InvestigatorDao.java

License:BSD License

public List<Investigator> searchByExample(Investigator inv, boolean isWildCard) {
    RemoteInvestigator remoteInvestigator = convertToRemoteInvestigatorForCoppaQuery(inv);
    getRemoteInvestigatorsAndUpdateDatabase(remoteInvestigator);

    List<Investigator> result = new ArrayList<Investigator>();

    Example example = Example.create(inv).excludeZeroes().ignoreCase();
    try {//from  w  w w  .j  ava 2 s  .  co  m
        Criteria orgCriteria = getSession().createCriteria(Investigator.class);

        if (inv.getHealthcareSiteInvestigators() != null
                && inv.getHealthcareSiteInvestigators().get(0).getHealthcareSite() != null) {
            Criteria healthcareSiteInvestigatorCriteria = orgCriteria
                    .createCriteria("healthcareSiteInvestigatorsInternal");
            Criteria healthcareSiteCriteria = healthcareSiteInvestigatorCriteria
                    .createCriteria("healthcareSite");
            healthcareSiteCriteria.add(Expression.eq("id",
                    inv.getHealthcareSiteInvestigators().get(0).getHealthcareSite().getId()));
        }
        orgCriteria.addOrder(Order.asc("assignedIdentifier"));
        orgCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

        if (isWildCard) {
            example.enableLike(MatchMode.ANYWHERE);
            orgCriteria.add(example);
            result = orgCriteria.list();
        } else {
            result = orgCriteria.add(example).list();
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }
    return result;
}

From source file:edu.duke.cabig.c3pr.dao.ParticipantDao.java

License:BSD License

/**
 * 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>/*  w  w  w . j a  v a 2s  .  c o  m*/
 * <li>code>participantDao.searchByExample(study)</li>
 * </code>
 * 
 * @param participant the participant
 * @param isWildCard the is wild card
 * @param useAddress if set to true and {@link Address} is present, use it for the search.
 * @param useContactInfo if set to true and {@link ContactMechanism} is present, use it for the search.
 * @return list of matching participant objects based on your sample participant object
 */
public List<Participant> searchByExample(Participant participant, boolean isWildCard, boolean useAddress,
        boolean useContactInfo, boolean forceEager) {
    Example example = Example.create(participant).excludeZeroes().ignoreCase();
    Criteria participantCriteria = getSession().createCriteria(Participant.class)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    example.excludeProperty("doNotUse").enableLike(MatchMode.ANYWHERE);
    if (forceEager) {
        // TODO: The following lines may cause Participant.identifiers to contain duplicate entries!
        // In order to avoid that, Participant.identifiers will need to be changed from List to Set.
        // Related to http://www.jroller.com/eyallupu/entry/hibernate_exception_simultaneously_fetch_multiple.
        participantCriteria.setFetchMode("identifiers", FetchMode.JOIN);
        participantCriteria.setFetchMode("raceCodeAssociations", FetchMode.JOIN);
        participantCriteria.setFetchMode("contactMechanisms", FetchMode.JOIN);
    }
    if (isWildCard) {
        participantCriteria.add(example);

        if (participant.getIdentifiers().size() > 0) {
            Criterion identifierValueCriterion = Restrictions.ilike("value",
                    "%" + participant.getIdentifiers().get(0).getValue() + "%");
            Criterion identifierTypeCriterion = Restrictions.ilike("typeInternal",
                    "%" + participant.getIdentifiers().get(0).getTypeInternal() + "%");
            if (participant.getIdentifiers().get(0) instanceof SystemAssignedIdentifier) {
                Criterion identifierSourceCriterion = Restrictions.ilike("systemName",
                        "%" + ((SystemAssignedIdentifier) participant.getIdentifiers().get(0)).getSystemName()
                                + "%");
                participantCriteria.createCriteria("identifiers").add(identifierValueCriterion)
                        .add(identifierSourceCriterion).add(identifierTypeCriterion);
            } else {
                Criteria identifiersCriteria = participantCriteria.createCriteria("identifiers");
                identifiersCriteria.add(identifierValueCriterion).add(identifierTypeCriterion);

                if (((OrganizationAssignedIdentifier) participant.getIdentifiers().get(0)).getHealthcareSite()
                        .getIdentifiersAssignedToOrganization().size() > 0) {
                    Criteria organizationCriteria = identifiersCriteria.createCriteria("healthcareSite");
                    organizationCriteria.createCriteria("identifiersAssignedToOrganization")
                            .add(Restrictions.ilike("value",
                                    "%" + ((OrganizationAssignedIdentifier) participant.getIdentifiers().get(0))
                                            .getHealthcareSite().getIdentifiersAssignedToOrganization().get(0)
                                            .getValue() + "%"));

                }
            }

            for (int i = 1; i < participant.getIdentifiers().size(); i++) {
                identifierValueCriterion = Restrictions.or(identifierValueCriterion, Restrictions.ilike("value",
                        "%" + participant.getIdentifiers().get(i).getValue() + "%"));
            }
        }
        final Address address = participant.getAddressInternal();
        if (useAddress && address != null) {
            final Criteria addrCrit = participantCriteria.createCriteria("addresses");
            if (StringUtils.isNotBlank(address.getStreetAddress()))
                addrCrit.add(Restrictions.ilike("streetAddress", "%" + address.getStreetAddress() + "%"));
            if (StringUtils.isNotBlank(address.getCity()))
                addrCrit.add(Restrictions.ilike("city", "%" + address.getCity() + "%"));
            if (StringUtils.isNotBlank(address.getStateCode()))
                addrCrit.add(Restrictions.ilike("stateCode", "%" + address.getStateCode() + "%"));
            if (StringUtils.isNotBlank(address.getCountryCode()))
                addrCrit.add(Restrictions.ilike("countryCode", "%" + address.getCountryCode() + "%"));
            if (StringUtils.isNotBlank(address.getPostalCode()))
                addrCrit.add(Restrictions.ilike("postalCode", "%" + address.getPostalCode() + "%"));
        }
        if (useContactInfo) {
            List<Criterion> criterions = new ArrayList<Criterion>();
            if (StringUtils.isNotBlank(participant.getEmail())) {
                criterions.add(
                        Example.create(new ContactMechanism(ContactMechanismType.EMAIL, participant.getEmail()))
                                .enableLike().ignoreCase());
            }
            if (StringUtils.isNotBlank(participant.getPhone())) {
                criterions.add(
                        Example.create(new ContactMechanism(ContactMechanismType.PHONE, participant.getPhone()))
                                .enableLike().ignoreCase());
            }
            if (StringUtils.isNotBlank(participant.getFax())) {
                criterions.add(
                        Example.create(new ContactMechanism(ContactMechanismType.Fax, participant.getFax()))
                                .enableLike().ignoreCase());
            }
            if (!criterions.isEmpty()) {
                Disjunction disjunction = Restrictions.disjunction();
                for (Criterion criterion : criterions) {
                    disjunction.add(criterion);
                }
                final Criteria contactCrit = participantCriteria.createCriteria("contactMechanisms");
                contactCrit.add(disjunction);
            }
        }

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

From source file:edu.duke.cabig.c3pr.dao.ParticipantDao.java

License:BSD License

/**
  * Search by org identifier./*  ww w  . j a  v  a2  s .  c  o  m*/
  * 
  * @param identifier the id
  * 
  * @return the list< participant>
  */
@SuppressWarnings("unchecked")
public List<Participant> searchByOrgIdentifier(OrganizationAssignedIdentifier identifier) {

    List<Participant> participantList = new ArrayList<Participant>();
    Criteria participantCriteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
            .createCriteria(Participant.class);
    Criteria organizationAssignedIdentifiersCriteria = participantCriteria.createCriteria("identifiers");
    Criteria healthcareSiteCriteria = organizationAssignedIdentifiersCriteria.createCriteria("healthcareSite");

    if (identifier.getType() != null && identifier.getHealthcareSite() != null
            && identifier.getValue() != null) {
        if (identifier.getHealthcareSite().getId() != null) {
            healthcareSiteCriteria.add(Expression.eq("id", identifier.getHealthcareSite().getId()));
            organizationAssignedIdentifiersCriteria
                    .add(Expression.eq("typeInternal", identifier.getTypeInternal()));
            organizationAssignedIdentifiersCriteria.add(Expression.eq("value", identifier.getValue()));
        } else {
            Criteria identifiersAssignedToOrganizationCriteria = healthcareSiteCriteria
                    .createCriteria("identifiersAssignedToOrganization");

            identifiersAssignedToOrganizationCriteria
                    .add(Expression.eq("value", identifier.getHealthcareSite().getPrimaryIdentifier()));
            identifiersAssignedToOrganizationCriteria.add(Expression.eq("primaryIndicator", Boolean.TRUE));
            organizationAssignedIdentifiersCriteria
                    .add(Expression.eq("typeInternal", identifier.getType().getName()));
            organizationAssignedIdentifiersCriteria.add(Expression.eq("value", identifier.getValue()));
        }
        return participantCriteria.list();
    }
    return participantList;
}

From source file:edu.duke.cabig.c3pr.dao.ParticipantDao.java

License:BSD License

private List<HealthcareSite> getBySubIdentifier(String[] subnames) {
    List<HealthcareSite> healthcareSitesByIdentifier = new ArrayList<HealthcareSite>();
    for (String subname : subnames) {
        if (!StringUtils.isEmpty(subname)) {
            Criteria orgCriteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
                    .createCriteria(HealthcareSite.class);
            Criteria identifiersAssignedToOrganizationCriteria = orgCriteria
                    .createCriteria("identifiersAssignedToOrganization");
            identifiersAssignedToOrganizationCriteria.add(Expression.ilike("value", "%" + subname + "%"));

            if (orgCriteria.list().size() > 0) {
                healthcareSitesByIdentifier.addAll(orgCriteria.list());
            }//from ww  w .  ja  v a 2s  .com
        }
    }
    return healthcareSitesByIdentifier;
}

From source file:edu.duke.cabig.c3pr.dao.PersonUserDao.java

License:BSD License

/**
 * Search by example./*w  w w  . j a v  a  2  s .  com*/
 *
 * @param personUser the staff
 * @param isWildCard the is wild card
 * @return the list
 */
public List<PersonUser> searchByExample(PersonUser personUser, boolean isWildCard, String emailAddress) {

    // get the remote staff and update the database first
    RemotePersonUser remotePersonUser = convertToRemotePersonUser(personUser);
    getRemoteResearchStaffFromResolverByExample(remotePersonUser);

    List<PersonUser> result = new ArrayList<PersonUser>();

    Example example = Example.create(personUser).excludeZeroes().ignoreCase();
    example.excludeProperty("salt");
    example.excludeProperty("passwordLastSet");
    try {
        Criteria criteria = getSession().createCriteria(PersonUser.class);
        criteria.addOrder(Order.asc("assignedIdentifier"));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

        if (isWildCard) {
            example.enableLike(MatchMode.ANYWHERE);
            criteria.add(example);
            if (personUser.getHealthcareSites().size() > 0) {
                criteria.createCriteria("healthcareSites").add(Restrictions.ilike("name",
                        "%" + personUser.getHealthcareSites().get(0).getName() + "%"));
                // As per discussion in search by example staff will have only one healthcare site
            }
            if (!StringUtils.isBlank(emailAddress)) {
                Criteria emailCriteria = criteria.createCriteria("contactMechanisms");
                emailCriteria.add(Restrictions.ilike("value", "%" + emailAddress + "%"));
                //emailCriteria.add(Restrictions.ilike("type", ContactMechanismType.EMAIL)); 
            }
            result = criteria.list();
        } else {
            result = criteria.add(example).list();
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }
    return result;
}