Example usage for org.hibernate.criterion DetachedCriteria add

List of usage examples for org.hibernate.criterion DetachedCriteria add

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria add.

Prototype

public DetachedCriteria add(Criterion criterion) 

Source Link

Document

Add a restriction

Usage

From source file:au.org.theark.lims.model.dao.BioCollectionDao.java

License:Open Source License

public Boolean hasBiospecimens(BioCollection bioCollection) {
    // Use WHERE EXIST to optimise query even further
    StatelessSession session = getStatelessSession();
    Criteria criteria = session.createCriteria(BioCollection.class, "bc");
    DetachedCriteria sizeCriteria = DetachedCriteria.forClass(Biospecimen.class, "b");
    criteria.add(Restrictions.eq("bc.id", bioCollection.getId()));
    sizeCriteria.add(Property.forName("bc.id").eqProperty("b.bioCollection.id"));
    criteria.add(Subqueries.exists(sizeCriteria.setProjection(Projections.property("b.id"))));
    criteria.setProjection(Projections.rowCount());
    Boolean result = ((Long) criteria.uniqueResult()) > 0L;
    session.close();// w ww  .j  av  a  2  s.c om

    return result;
}

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

/**
 * The method checks if the given questionnaire's fields have data linked to it.
 * //from  www . j av  a2  s . c  o m
 * @param customFieldGroup
 */
public void isDataAvailableForQuestionnaire(CustomFieldGroup customFieldGroup) {

    Criteria criteria = getSession().createCriteria(CustomField.class, "cf");
    criteria.createAlias("customFieldDisplay", "cfd", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldDisplay
    criteria.createAlias("cfd.customFieldGroup", "cfg", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldGroup
    criteria.add(Restrictions.eq("cf.study", customFieldGroup.getStudy()));

    ArkFunction function = iArkCommonService
            .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_DATA_DICTIONARY);
    criteria.add(Restrictions.eq("cf.arkFunction", function));
    criteria.add(Restrictions.eq("cfg.id", customFieldGroup.getId()));

    DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "pd");
    // Join CustomFieldDisplay and PhenoData on ID FK
    fieldDataCriteria.add(Property.forName("cfd.id").eqProperty("pd." + "customFieldDisplay.id"));
    criteria.add(
            Subqueries.exists(fieldDataCriteria.setProjection(Projections.property("pd.customFieldDisplay"))));

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("cfg.name"), "questionnaire");
    projectionList.add(Projections.property("cf.name"), "fieldName");
    projectionList.add(Projections.property("cf.description"), "description");

}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public List<ConsentDetailsDataRow> getStudyCompConsentList(ConsentDetailsReportVO cdrVO) {
    // NB: There should only ever be one Consent record for a particular Subject for a particular StudyComp

    List<ConsentDetailsDataRow> results = new ArrayList<ConsentDetailsDataRow>();
    Criteria criteria = getSession().createCriteria(LinkSubjectStudy.class, "lss");
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("lss." + "subjectUID"), "subjectUID");

    criteria.add(//from   w  w w  .j av  a2s  .  c  om
            Restrictions.eq("lss." + Constants.LINKSUBJECTSTUDY_STUDY, cdrVO.getLinkSubjectStudy().getStudy()));
    if (cdrVO.getLinkSubjectStudy().getSubjectUID() != null) {
        criteria.add(Restrictions.ilike("lss." + Constants.LINKSUBJECTSTUDY_SUBJECTUID,
                cdrVO.getLinkSubjectStudy().getSubjectUID(), MatchMode.ANYWHERE));
    }
    if (cdrVO.getLinkSubjectStudy().getSubjectStatus() != null) {
        criteria.add(Restrictions.eq("lss." + Constants.LINKSUBJECTSTUDY_SUBJECTSTATUS,
                cdrVO.getLinkSubjectStudy().getSubjectStatus()));
    }

    if (cdrVO.getConsentDate() != null) {
        // NB: constraint on consentDate or consentStatus automatically removes "Not Consented" state
        // So LinkSubjectStudy inner join to Consent ok for populated consentDate
        criteria.createAlias("lss." + Constants.LINKSUBJECTSTUDY_CONSENT, "c");
        criteria.createAlias("c." + Constants.CONSENT_CONSENTSTATUS, "cs");
        // constrain on studyComp
        criteria.add(Restrictions.eq("c." + Constants.CONSENT_STUDYCOMP, cdrVO.getStudyComp()));
        // constrain on consentDate
        criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTDATE, cdrVO.getConsentDate()));
        // ConsentStatus is optional for this query...
        if (cdrVO.getConsentStatus() != null) {
            criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTSTATUS, cdrVO.getConsentStatus()));
        }
        projectionList.add(Projections.property("cs.name"), "consentStatus");
        projectionList.add(Projections.property("c." + Constants.CONSENT_CONSENTDATE), "consentDate");

    } else if (cdrVO.getConsentStatus() != null) {
        if (cdrVO.getConsentStatus().getName().equals(Constants.NOT_CONSENTED)) {
            // Need to handle "Not Consented" status differently (since it doesn't have a Consent record)
            // Helpful website: http://www.cereslogic.com/pages/2008/09/22/hibernate-criteria-subqueries-exists/

            // Build subquery to find all Consent records for a Study Comp
            DetachedCriteria consentCriteria = DetachedCriteria.forClass(Consent.class, "c");
            // Constrain on StudyComponent
            consentCriteria.add(Restrictions.eq("c." + Constants.CONSENT_STUDYCOMP, cdrVO.getStudyComp()));
            // Just in case "Not Consented" is erroneously entered into a row in the Consent table
            // consentCriteria.add(Restrictions.ne("c." + Constants.CONSENT_CONSENTSTATUS, cdrVO.getConsentStatus()));
            // Join LinkSubjectStudy and Consent on ID FK
            consentCriteria.add(Property.forName("c.linkSubjectStudy.id").eqProperty("lss." + "id"));
            criteria.add(Subqueries.notExists(consentCriteria.setProjection(Projections.property("c.id"))));

            // If Consent records follows design for "Not Consented", then:
            // - consentStatus and consentDate are not populated
        } else {
            // NB: constraint on consentDate or consentStatus automatically removes "Not Consented" state
            // So LinkSubjectStudy inner join to Consent ok for all recordable consentStatus
            criteria.createAlias("lss." + Constants.LINKSUBJECTSTUDY_CONSENT, "c");
            criteria.createAlias("c." + Constants.CONSENT_CONSENTSTATUS, "cs");
            // Constrain on StudyComponent
            criteria.add(Restrictions.eq("c." + Constants.CONSENT_STUDYCOMP, cdrVO.getStudyComp()));
            // ConsentStatus is NOT optional for this query!
            criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTSTATUS, cdrVO.getConsentStatus()));
            if (cdrVO.getConsentDate() != null) {
                criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTDATE, cdrVO.getConsentDate()));
            }
            projectionList.add(Projections.property("cs.name"), "consentStatus");
            projectionList.add(Projections.property("c." + Constants.CONSENT_CONSENTDATE), "consentDate");
        }
    } else {
        // Should not attempt to run this query with no consentDate nor consentStatus criteria provided
        log.error(
                "reportDao.getStudyCompConsentList(..) is missing consentDate or consentStatus parameters in the VO");
        return null;
    }

    criteria.addOrder(Order.asc("lss." + "subjectUID"));
    criteria.setProjection(projectionList);
    criteria.setResultTransformer(Transformers.aliasToBean(ConsentDetailsDataRow.class));
    // This gives a list of subjects matching the specific studyComp and consentStatus
    results = criteria.list();

    return results;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public List<FieldDetailsDataRow> getPhenoFieldDetailsList(FieldDetailsReportVO fdrVO) {
    List<FieldDetailsDataRow> results = new ArrayList<FieldDetailsDataRow>();
    Criteria criteria = getSession().createCriteria(PhenoDataSetCollection.class, "fpc");
    criteria.createAlias("phenoCollection", "pc"); // Inner join to Field
    criteria.createAlias("field", "f"); // Inner join to Field
    criteria.createAlias("f.fieldType", "ft"); // Inner join to FieldType
    criteria.add(Restrictions.eq("study", fdrVO.getStudy()));
    if (fdrVO.getPhenoCollection() != null) {
        criteria.add(Restrictions.eq("phenoCollection", fdrVO.getPhenoCollection()));
    }/*  w  ww  .j  ava  2 s  .c o  m*/
    if (fdrVO.getFieldDataAvailable()) {
        DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "fd");
        // Join FieldPhenoCollection and FieldData on ID FK
        fieldDataCriteria.add(Property.forName("f.id").eqProperty("fd." + "field.id"));
        fieldDataCriteria.add(Property.forName("pc.id").eqProperty("fd." + "collection.id"));
        criteria.add(Subqueries.exists(fieldDataCriteria.setProjection(Projections.property("fd.id"))));
    }
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("pc.name"), "collection");
    projectionList.add(Projections.property("f.name"), "fieldName");
    projectionList.add(Projections.property("f.description"), "description");
    projectionList.add(Projections.property("f.minValue"), "minValue");
    projectionList.add(Projections.property("f.maxValue"), "maxValue");
    projectionList.add(Projections.property("f.encodedValues"), "encodedValues");
    projectionList.add(Projections.property("f.missingValue"), "missingValue");
    projectionList.add(Projections.property("f.units"), "units");
    projectionList.add(Projections.property("ft.name"), "type");
    criteria.setProjection(projectionList); // only return fields required for report
    criteria.setResultTransformer(Transformers.aliasToBean(FieldDetailsDataRow.class));
    criteria.addOrder(Order.asc("pc.id"));
    criteria.addOrder(Order.asc("f.name"));
    results = criteria.list();

    return results;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public List<CustomFieldDetailsDataRow> getPhenoCustomFieldDetailsList(CustomFieldDetailsReportVO fdrVO) {
    List<CustomFieldDetailsDataRow> results = new ArrayList<CustomFieldDetailsDataRow>();
    if (fdrVO.getCustomFieldDisplay() != null) {
        /*//from  w w  w. j  a  v a  2  s.  c  om
         * Following query returns customFields whether or not they are 
         * associated with a customFieldGroups (via customFieldDisplay)
         */
        Criteria criteria = getSession().createCriteria(CustomField.class, "cf");
        criteria.createAlias("customFieldDisplay", "cfd", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldDisplay
        criteria.createAlias("cfd.customFieldGroup", "cfg", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldGroup
        criteria.createAlias("fieldType", "ft", JoinType.LEFT_OUTER_JOIN); // Left join to FieldType
        criteria.createAlias("unitType", "ut", JoinType.LEFT_OUTER_JOIN); // Left join to UnitType
        criteria.add(Restrictions.eq("cf.study", fdrVO.getStudy()));
        ArkFunction function = iArkCommonService
                .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION);
        criteria.add(Restrictions.eq("cf.arkFunction", function));

        if (fdrVO.getCustomFieldDisplay().getCustomFieldGroup() != null) {
            criteria.add(
                    Restrictions.eq("cfg.id", fdrVO.getCustomFieldDisplay().getCustomFieldGroup().getId()));
        }
        if (fdrVO.getFieldDataAvailable()) {
            DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "pd");
            // Join CustomFieldDisplay and PhenoData on ID FK
            fieldDataCriteria.add(Property.forName("cfd.id").eqProperty("pd." + "customFieldDisplay.id"));
            criteria.add(Subqueries
                    .exists(fieldDataCriteria.setProjection(Projections.property("pd.customFieldDisplay"))));
        }

        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("cfg.name"), "questionnaire");
        projectionList.add(Projections.property("cf.name"), "fieldName");
        projectionList.add(Projections.property("cf.description"), "description");
        projectionList.add(Projections.property("cf.minValue"), "minValue");
        projectionList.add(Projections.property("cf.maxValue"), "maxValue");
        projectionList.add(Projections.property("cf.encodedValues"), "encodedValues");
        projectionList.add(Projections.property("cf.missingValue"), "missingValue");
        projectionList.add(Projections.property("ut.name"), "units");
        projectionList.add(Projections.property("ft.name"), "type");

        criteria.setProjection(projectionList); // only return fields required for report
        criteria.setResultTransformer(Transformers.aliasToBean(CustomFieldDetailsDataRow.class));
        criteria.addOrder(Order.asc("cfg.id"));
        criteria.addOrder(Order.asc("cfd.sequence"));
        results = criteria.list();
    }

    return results;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public List<PhenoDataSetFieldDetailsDataRow> getPhenoDataSetFieldDetailsList(
        PhenoDataSetFieldDetailsReportVO reportVO) {
    List<PhenoDataSetFieldDetailsDataRow> results = new ArrayList<PhenoDataSetFieldDetailsDataRow>();
    if (reportVO.getPhenoDataSetFieldDisplay() != null) {
        /*/*w  w w .j av a  2 s.  com*/
         * Following query returns customFields whether or not they are
         * associated with a customFieldGroups (via customFieldDisplay)
         */
        Criteria criteria = getSession().createCriteria(PhenoDataSetField.class, "pf");
        criteria.createAlias("phenoDatasetFieldDisplay", "pdfd", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldDisplay
        criteria.createAlias("pdfd.phenoDatasetFieldGroup", "pdfg", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldGroup
        criteria.createAlias("fieldType", "ft", JoinType.LEFT_OUTER_JOIN); // Left join to FieldType
        criteria.createAlias("unitType", "ut", JoinType.LEFT_OUTER_JOIN); // Left join to UnitType
        criteria.add(Restrictions.eq("pf.study", reportVO.getStudy()));
        ArkFunction function = iArkCommonService
                .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION);
        criteria.add(Restrictions.eq("pf.arkFunction", function));

        if (reportVO.getPhenoDataSetFieldDisplay().getPhenoDataSetGroup() != null) {
            criteria.add(Restrictions.eq("pdfg.id",
                    reportVO.getPhenoDataSetFieldDisplay().getPhenoDataSetGroup().getId()));
        }
        if (reportVO.getFieldDataAvailable()) {
            DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "pd");
            // Join CustomFieldDisplay and PhenoData on ID FK
            fieldDataCriteria
                    .add(Property.forName("pdfd.id").eqProperty("pd." + "phenoDatasetFieldDisplay.id"));
            criteria.add(Subqueries.exists(
                    fieldDataCriteria.setProjection(Projections.property("pd.phenoDatasetFieldDisplay"))));
        }

        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("pdfg.name"), "questionnaire");
        projectionList.add(Projections.property("pf.name"), "fieldName");
        projectionList.add(Projections.property("pf.description"), "description");
        projectionList.add(Projections.property("pf.minValue"), "minValue");
        projectionList.add(Projections.property("pf.maxValue"), "maxValue");
        projectionList.add(Projections.property("pf.encodedValues"), "encodedValues");
        projectionList.add(Projections.property("pf.missingValue"), "missingValue");
        projectionList.add(Projections.property("ut.name"), "units");
        projectionList.add(Projections.property("ft.name"), "type");

        criteria.setProjection(projectionList); // only return fields required for report
        criteria.setResultTransformer(Transformers.aliasToBean(PhenoDataSetFieldDetailsDataRow.class));
        criteria.addOrder(Order.asc("pdfg.id"));
        criteria.addOrder(Order.asc("pdfd.sequence"));
        results = criteria.list();
    }

    return results;
}

From source file:biblioteca.datos.hibernate.dao.imp.UsuarioDAOImp.java

@Override
@Transactional//from ww w  .j  av a 2s .com
public Usuario validarUsuario(String nombreUsuario, String password) {
    System.out.println("aca entramos al validar usuario de usuariodaoimpl");
    System.out.println("nombreUsuario " + nombreUsuario + " password " + password);
    DetachedCriteria dc = DetachedCriteria.forClass(Usuario.class);
    dc.add(Restrictions.eq("nombreUsu", nombreUsuario));
    dc.add(Restrictions.eq("claveUsu", password));
    System.out.println("usuario " + dc.getAlias());
    System.out.println("aca agregamos restrictions nombreusuario y password deteched criteria");
    ArrayList<Usuario> resu = new ArrayList<Usuario>();
    resu = (ArrayList) getHibernateTemplate().findByCriteria(dc);
    System.out.println("usuariodaoimpl devuelve el array de tipo usuario resu");
    //aqui deberrias poner condigo para ver si resu es null
    //en este caso no se encontraron usuarios con los parametros de otro modo retornas el primer elemento
    if (resu.isEmpty()) {
        System.out.println("Aca devolvemos usuario null");
        return null;
    } else {
        System.out.println("Aca devolvemos usuario get(0)");
        return resu.get(0);
    }
}

From source file:br.com.digilabs.service.UserServiceImpl.java

License:Apache License

public List<User> listUser(User user) {
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
    detachedCriteria.add(Restrictions.eq("user", user));
    detachedCriteria.addOrder(Order.asc("id"));
    return simpleDao.findByCriteria(detachedCriteria);
}

From source file:br.msf.commons.persistence.dao.AbstractEntityDaoBean.java

License:Open Source License

@Override
public Collection<T> findByProperty(final String propertyName, final Object propertyValue,
        final Order... orders) {
    ArgumentUtils.rejectIfNull(propertyName);
    final DetachedCriteria dc = createCriteria().setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    dc.add((propertyValue == null) ? Restrictions.isNull(propertyName)
            : Restrictions.eq(propertyName, propertyValue));
    return findByCriteria(dc, orders);
}

From source file:br.msf.commons.persistence.dao.AbstractEntityDaoBean.java

License:Open Source License

@Override
public Collection<T> findByProperties(final Map<String, Object> properties, final Order... orders) {
    ArgumentUtils.rejectIfEmptyOrNull(properties);
    final DetachedCriteria dc = createCriteria().setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final Set<Map.Entry<String, Object>> props = properties.entrySet();
    for (Map.Entry<String, Object> entry : props) {
        final String propName = entry.getKey();
        final Object propValue = entry.getValue();
        dc.add((propValue == null) ? Restrictions.isNull(propName) : Restrictions.eq(propName, propValue));
    }//from  www. ja  va  2 s  . c  o m
    return findByCriteria(dc, orders);
}