Example usage for org.hibernate Criteria createAlias

List of usage examples for org.hibernate Criteria createAlias

Introduction

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

Prototype

public Criteria createAlias(String associationPath, String alias) throws HibernateException;

Source Link

Document

Join an association, assigning an alias to the joined association.

Usage

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

License:Open Source License

public long getPhenoDataCount(PhenoDataSetCollection phenoCollection,
        PhenoDataSetCategory phenoDataSetCategory) {
    Criteria criteria = getSession().createCriteria(PhenoDataSetFieldDisplay.class);
    criteria.createAlias("phenoDataSetGroup", "qnaire");
    if (phenoCollection.getQuestionnaire() != null) {
        criteria.add(Restrictions.eq("qnaire.id", phenoCollection.getQuestionnaire().getId()));
    }/*from   w ww. j av  a2  s .c  o m*/
    criteria.setProjection(Projections.rowCount());
    Long count = (Long) criteria.uniqueResult();
    return count.intValue();
}

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

License:Open Source License

public long getPhenoCollectionCount(PhenoDataCollectionVO collectionCriteria) {
    Criteria criteria = getSession().createCriteria(PhenoDataSetCollection.class);
    criteria.createAlias("questionnaire", "qnaire");
    criteria.add(Restrictions.eq("linkSubjectStudy",
            collectionCriteria.getPhenoDataSetCollection().getLinkSubjectStudy()));
    // Just a precaution (PhenoCollection to should always map to a CustomFieldGroup where the ArkFunction will correspond to Pheno) 
    //criteria.add(Restrictions.eq("qnaire.arkFunction", collectionCriteria.getArkFunction()));   
    criteria.setProjection(Projections.rowCount());
    Long count = (Long) criteria.uniqueResult();
    return count;
}

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

License:Open Source License

public List<PhenoDataSetCollection> searchPageablePhenoCollection(PhenoDataCollectionVO collectionCriteria,
        int first, int count) {

    List<PhenoDataSetCollection> resultList = new ArrayList<PhenoDataSetCollection>();
    StringBuffer sb = new StringBuffer();
    sb.append("SELECT qnaire, pc ");
    sb.append("  FROM " + PhenoDataSetGroup.class.getName() + " AS qnaire ");
    sb.append("  LEFT JOIN qnaire.phenoDataSetCollections as pc ");
    sb.append("  WITH pc.linkSubjectStudy.id = :subjectId ");
    sb.append(" WHERE qnaire.study.id = :studyId ");
    //sb.append("   AND qnaire.arkFunction.id = :functionId ");
    sb.append("   AND qnaire.published = true ");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("subjectId",
            collectionCriteria.getPhenoDataSetCollection().getLinkSubjectStudy().getId());
    query.setParameter("studyId", collectionCriteria.getPhenoDataSetGroup().getStudy().getId());
    //log.info("colcrit ark=" + collectionCriteria.getArkFunction());
    //long id = collectionCriteria.getArkFunction().getId();
    //log.info("id=" + id);
    //query.setParameter("functionId",id);
    query.setFirstResult(first);/*from  w  w w . ja va  2  s .  co  m*/
    query.setMaxResults(count);

    List<Object[]> listOfObjects = query.list();
    for (Object[] objects : listOfObjects) {
        //CustomFieldGroup questionnaire = new CustomFieldGroup();
        PhenoDataSetGroup questionnaire = new PhenoDataSetGroup();
        PhenoDataSetCollection pc = new PhenoDataSetCollection();
        if (objects.length > 0 && objects.length >= 1) {
            questionnaire = (PhenoDataSetGroup) objects[0];
            if (objects[1] != null) {
                pc = (PhenoDataSetCollection) objects[1];
            } else {
                pc.setQuestionnaire(questionnaire);
            }
            resultList.add(pc);
        }
    }
    Criteria criteria = getSession().createCriteria(PhenoDataSetCollection.class);
    criteria.createAlias("questionnaire", "qnaire");
    criteria.add(Restrictions.eq("linkSubjectStudy",
            collectionCriteria.getPhenoDataSetCollection().getLinkSubjectStudy()));
    // Just a precaution (PhenoCollection to should always map to a CustomFieldGroup where the ArkFunction will correspond to Pheno) 
    criteria.add(Restrictions.eq("qnaire.arkFunction", collectionCriteria.getArkFunction()));
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);
    resultList = criteria.list();
    return resultList;
}

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

License:Open Source License

/**
 * check the Custom field category for the data intergrity.
 *//* w w  w . j av a 2 s  . co  m*/
@Override
public boolean isPhenoDataSetCategoryAlreadyUsed(PhenoDataSetCategory phenoDataSetCategory) {
    /**
     * if a phenoDatasetCategory been used by the system it should be at least one or more of this table.
     * PickedPhenoDataSetCategory
     * LinkPhenoDataSetCategoryField
     * PhenoDataSetFieldDisplay
     *  
     */
    Boolean status1 = false, status2 = false, status3 = false;

    StatelessSession stateLessSessionOne = getStatelessSession();
    Criteria criteria = stateLessSessionOne.createCriteria(PickedPhenoDataSetCategory.class);
    ArkFunction arkFunction = iArkCommonService
            .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION);
    criteria.add(Restrictions.eq("arkFunction", arkFunction));
    criteria.add(Restrictions.eq("study", phenoDataSetCategory.getStudy()));
    criteria.add(Restrictions.eq("phenoDataSetCategory", phenoDataSetCategory));
    List<PickedPhenoDataSetCategory> phenoDataSetCategories = (List<PickedPhenoDataSetCategory>) criteria
            .list();
    if (phenoDataSetCategories.size() > 0) {
        status1 = true;
    } else {
        status1 = false;
    }
    StatelessSession stateLessSessionTwo = getStatelessSession();
    Criteria criteriaTwo = stateLessSessionTwo.createCriteria(LinkPhenoDataSetCategoryField.class);
    criteriaTwo.add(Restrictions.eq("arkFunction", arkFunction));
    criteriaTwo.add(Restrictions.eq("study", phenoDataSetCategory.getStudy()));
    criteriaTwo.add(Restrictions.eq("phenoDataSetCategory", phenoDataSetCategory));
    List<LinkPhenoDataSetCategoryField> linkPhenoDataSetCategoryFields = (List<LinkPhenoDataSetCategoryField>) criteriaTwo
            .list();
    if (linkPhenoDataSetCategoryFields.size() > 0) {
        status2 = true;
    } else {
        status2 = false;
    }
    StatelessSession stateLessSessionThree = getStatelessSession();
    Criteria criteriaThree = stateLessSessionThree.createCriteria(PhenoDataSetFieldDisplay.class);
    criteriaThree.createAlias("phenoDataSetGroup", "phenoDSG");
    criteriaThree.add(Restrictions.eq("phenoDSG.arkFunction", arkFunction));
    criteriaThree.add(Restrictions.eq("phenoDSG.study", phenoDataSetCategory.getStudy()));
    criteriaThree.add(Restrictions.eq("phenoDataSetCategory", phenoDataSetCategory));
    List<PhenoDataSetFieldDisplay> phenoDataSetFieldDisplays = (List<PhenoDataSetFieldDisplay>) criteriaThree
            .list();
    if (phenoDataSetFieldDisplays.size() > 0) {
        status3 = true;
    } else {
        status3 = false;
    }
    return status1 || status2 || status3;
}

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

License:Open Source License

/**
 * Search method to the  fileds.//from  w w  w  .  ja va2  s  .co  m
 * @param phenoDataSet
 * @return
 */
protected Criteria buildGeneralPhenoFieldCritera(PhenoDataSetField phenoDataSetField) {
    Criteria criteria = getSession().createCriteria(PhenoDataSetField.class);

    criteria.add(Restrictions.eq("study", phenoDataSetField.getStudy()));
    criteria.add(Restrictions.eq("arkFunction", phenoDataSetField.getArkFunction()));

    if (phenoDataSetField.getFieldType() != null) {
        criteria.add(Restrictions.eq("fieldType", phenoDataSetField.getFieldType()));
    }
    if (phenoDataSetField.getId() != null) {
        criteria.add(Restrictions.eq("id", phenoDataSetField.getId()));
    }
    if (phenoDataSetField.getName() != null) {
        criteria.add(Restrictions.ilike("name", phenoDataSetField.getName(), MatchMode.ANYWHERE));
    }
    if (phenoDataSetField.getDescription() != null) {
        criteria.add(Restrictions.ilike("description", phenoDataSetField.getDescription(), MatchMode.ANYWHERE));
    }
    if (phenoDataSetField.getUnitType() != null && phenoDataSetField.getUnitType().getName() != null
            && phenoDataSetField.getUnitTypeInText() != null) {
        criteria.createAlias("unitType", "ut");
        criteria.add(
                Restrictions.ilike("ut.name", phenoDataSetField.getUnitType().getName(), MatchMode.ANYWHERE));
    }
    if (phenoDataSetField.getUnitTypeInText() != null) {
        criteria.add(Restrictions.ilike("unitTypeInText", phenoDataSetField.getUnitTypeInText(),
                MatchMode.ANYWHERE));
    }
    if (phenoDataSetField.getMinValue() != null) {
        criteria.add(Restrictions.ilike("minValue", phenoDataSetField.getMinValue(), MatchMode.ANYWHERE));
    }
    if (phenoDataSetField.getMaxValue() != null) {
        criteria.add(Restrictions.ilike("maxValue", phenoDataSetField.getMaxValue(), MatchMode.ANYWHERE));
    }
    return criteria;
}

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

License:Open Source License

public Map<String, Long> getSubjectStatusCounts(Study study) {
    Criteria criteria = getSession().createCriteria(LinkSubjectStudy.class);
    criteria.add(Restrictions.eq("study", study));
    ProjectionList projectionList = Projections.projectionList();
    criteria.createAlias("subjectStatus", "subjectStatusAlias");
    projectionList.add(Projections.groupProperty("subjectStatusAlias.name"));
    projectionList.add(Projections.rowCount());
    criteria.setProjection(projectionList);
    List results = criteria.list();
    Map<String, Long> statusMap = new HashMap<String, Long>();
    for (Object r : results) {
        Object[] obj = (Object[]) r;
        String statusName = (String) obj[0];
        statusMap.put(statusName, (Long) obj[1]);
    }//  w ww  .  j av  a 2s .c  o  m
    return statusMap;
}

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

License:Open Source License

public Map<String, Long> getStudyConsentCounts(Study study) {
    Map<String, Long> statusMap = new HashMap<String, Long>();

    Criteria criteria = getSession().createCriteria(LinkSubjectStudy.class);
    criteria.add(Restrictions.eq("study", study));
    ProjectionList projectionList = Projections.projectionList();
    criteria.createAlias("consentStatus", "consentStatusAlias");
    projectionList.add(Projections.groupProperty("consentStatusAlias.name"));
    projectionList.add(Projections.rowCount());
    criteria.setProjection(projectionList);
    List results = criteria.list();
    for (Object r : results) {
        Object[] obj = (Object[]) r;
        String statusName = (String) obj[0];
        statusMap.put(statusName, (Long) obj[1]);
    }/*from   w w w . j  a  va 2 s  .  com*/

    // Tack on count of when consentStatus = undefined (NULL)
    criteria = getSession().createCriteria(LinkSubjectStudy.class);
    criteria.add(Restrictions.eq("study", study));
    criteria.add(Restrictions.isNull("consentStatus"));
    projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    criteria.setProjection(projectionList);
    Long undefCount = (Long) criteria.uniqueResult();
    String statusName = Constants.NOT_CONSENTED;
    statusMap.put(statusName, undefCount);

    return statusMap;
}

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

License:Open Source License

public Map<String, Long> getStudyCompConsentCounts(Study study, StudyComp studyComp) {
    Map<String, Long> statusMap = new HashMap<String, Long>();

    Criteria criteria = getSession().createCriteria(Consent.class);
    criteria.add(Restrictions.eq("study", study));
    criteria.add(Restrictions.eq("studyComp", studyComp));
    ProjectionList projectionList = Projections.projectionList();
    criteria.createAlias("consentStatus", "consentStatusAlias");
    projectionList.add(Projections.groupProperty("consentStatusAlias.name"));
    projectionList.add(Projections.rowCount());
    criteria.setProjection(projectionList);
    List results = criteria.list();
    if ((results != null) && (results.size() > 0)) {
        for (Object r : results) {
            Object[] obj = (Object[]) r;
            String statusName = (String) obj[0];
            statusMap.put(statusName, (Long) obj[1]);
        }//from  w w  w  .  jav a 2s.c om
    } else {
        statusMap.put("(none found)", new Long(0));
    }
    return statusMap;
}

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(//  ww w  .ja v  a2 s.co m
            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 Phone getWorkPhone(LinkSubjectStudy subject) {
    Phone result = null;//from   w  w  w .j a  v  a 2s .c  o m
    Criteria criteria = getSession().createCriteria(Phone.class);
    criteria.add(Restrictions.eq("person", subject.getPerson()));
    criteria.createAlias("phoneType", "pt");
    criteria.add(Restrictions.eq("pt.name", "Work"));
    criteria.setMaxResults(1);

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("areaCode"), "areaCode");
    projectionList.add(Projections.property("phoneNumber"), "phoneNumber");
    criteria.setProjection(projectionList); // only return fields required for report
    criteria.setResultTransformer(Transformers.aliasToBean(Phone.class));

    if (criteria.uniqueResult() != null) {
        result = (Phone) criteria.uniqueResult();
    }
    return result;
}