Search in sources :

Example 1 with DiagnosisCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition in project openmrs-module-pihcore by PIH.

the class DiagnosisCohortDefinitionEvaluatorTest method testEvaluateByAnyCoded.

@Test
public void testEvaluateByAnyCoded() throws Exception {
    createDiagnosisObs();
    DiagnosisCohortDefinition cd = new DiagnosisCohortDefinition();
    cd.setIncludeAllCodedDiagnoses(true);
    EvaluatedCohort cohort = evaluator.evaluate(cd, new EvaluationContext());
    assertThat(cohort, hasExactlyIds(2, 6));
}
Also used : EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) DiagnosisCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 2 with DiagnosisCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition in project openmrs-module-pihcore by PIH.

the class DiagnosisCohortDefinitionEvaluatorTest method testEvaluateSimple.

@Test
public void testEvaluateSimple() throws Exception {
    DiagnosisCohortDefinition cd = new DiagnosisCohortDefinition();
    EvaluatedCohort cohort = evaluator.evaluate(cd, new EvaluationContext());
    assertThat(cohort.size(), is(0));
    assertThat((DiagnosisCohortDefinition) cohort.getDefinition(), is(cd));
    createDiagnosisObs();
    cd = new DiagnosisCohortDefinition();
    cohort = evaluator.evaluate(cd, new EvaluationContext());
    assertThat(cohort, hasExactlyIds(2, 6, 7));
    cd.setOnOrAfter(DateUtil.parseDate("2013-01-02", "yyyy-MM-dd"));
    cd.setOnOrBefore(DateUtil.parseDate("2013-01-02", "yyyy-MM-dd"));
    cohort = evaluator.evaluate(cd, new EvaluationContext());
    assertThat(cohort, hasExactlyIds(2));
}
Also used : EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) DiagnosisCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 3 with DiagnosisCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition in project openmrs-module-pihcore by PIH.

the class DiagnosisCohortDefinitionEvaluatorTest method testEvaluateByCertainty.

@Test
public void testEvaluateByCertainty() throws Exception {
    createDiagnosisObs();
    DiagnosisCohortDefinition cd = new DiagnosisCohortDefinition();
    cd.setCertainty(Diagnosis.Certainty.CONFIRMED);
    cd.setCodedDiagnoses(Arrays.asList(conceptService.getConcept(9)));
    EvaluatedCohort cohort = evaluator.evaluate(cd, new EvaluationContext());
    assertThat(cohort, hasExactlyIds(6));
}
Also used : EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) DiagnosisCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 4 with DiagnosisCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition in project openmrs-module-pihcore by PIH.

the class DiagnosisCohortDefinitionEvaluatorTest method testEvaluateByOrder.

@Test
public void testEvaluateByOrder() throws Exception {
    createDiagnosisObs();
    DiagnosisCohortDefinition cd = new DiagnosisCohortDefinition();
    cd.setDiagnosisOrder(Diagnosis.Order.SECONDARY);
    EvaluatedCohort cohort = evaluator.evaluate(cd, new EvaluationContext());
    assertThat(cohort, hasExactlyIds(2));
}
Also used : EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) DiagnosisCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 5 with DiagnosisCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition in project openmrs-module-pihcore by PIH.

the class DiagnosisCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    DiagnosisMetadata dmd = emrApiProperties.getDiagnosisMetadata();
    DiagnosisCohortDefinition cd = (DiagnosisCohortDefinition) cohortDefinition;
    if (cd.isIncludeAllCodedDiagnoses() && cd.getCodedDiagnoses() != null) {
        throw new IllegalArgumentException("Cannot specify both includeAllCodedDiagnoses, and specific coded diagnoses");
    }
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Obs.class, "obsgroup");
    crit.setProjection(Projections.distinct(Projections.property("person.id")));
    crit.add(Restrictions.eq("voided", false));
    crit.createCriteria("person").add(Restrictions.eq("voided", false));
    // we're looking for an obs group whose grouping concept is VISIT DIAGNOSES (or the equivalent)
    crit.add(Restrictions.eq("concept", dmd.getDiagnosisSetConcept()));
    if (cd.getOnOrAfter() != null) {
        crit.add(Restrictions.ge("obsDatetime", cd.getOnOrAfter()));
    }
    if (cd.getOnOrBefore() != null) {
        crit.add(Restrictions.le("obsDatetime", DateUtil.getEndOfDayIfTimeExcluded(cd.getOnOrBefore())));
    }
    if (cd.getDiagnosisOrder() != null) {
        DetachedCriteria orderClause = DetachedCriteria.forClass(Obs.class, "orderObs");
        orderClause.add(Restrictions.eq("voided", false));
        orderClause.add(Restrictions.eq("concept", dmd.getDiagnosisOrderConcept()));
        orderClause.add(Restrictions.eq("valueCoded", dmd.getConceptFor(cd.getDiagnosisOrder())));
        orderClause.add(Restrictions.eqProperty("obsGroup", "obsgroup.id"));
        orderClause.setProjection(Projections.property("id"));
        crit.add(Subqueries.exists(orderClause));
    }
    if (cd.getCertainty() != null) {
        DetachedCriteria certaintyClause = DetachedCriteria.forClass(Obs.class, "certaintyObs");
        certaintyClause.add(Restrictions.eq("voided", false));
        certaintyClause.add(Restrictions.eq("concept", dmd.getDiagnosisCertaintyConcept()));
        certaintyClause.add(Restrictions.eq("valueCoded", dmd.getConceptFor(cd.getCertainty())));
        certaintyClause.add(Restrictions.eqProperty("obsGroup", "obsgroup.id"));
        certaintyClause.setProjection(Projections.property("id"));
        crit.add(Subqueries.exists(certaintyClause));
    }
    if (cd.isIncludeAllCodedDiagnoses()) {
        // diagnoses, we can just ignore both clauses
        if (!cd.isIncludeAllNonCodedDiagnoses()) {
            DetachedCriteria anyCodedClause = DetachedCriteria.forClass(Obs.class, "codedDiagnosisObs");
            anyCodedClause.add(Restrictions.eq("voided", false));
            anyCodedClause.add(Restrictions.eq("concept", dmd.getCodedDiagnosisConcept()));
            anyCodedClause.add(Restrictions.eqProperty("obsGroup", "obsgroup.id"));
            anyCodedClause.setProjection(Projections.property("id"));
            crit.add(Subqueries.exists(anyCodedClause));
        }
    } else if (cd.getCodedDiagnoses() != null || cd.getExcludeCodedDiagnoses() != null) {
        if (cd.isIncludeAllNonCodedDiagnoses()) {
            throw new IllegalArgumentException("Not Yet Implemented: handling both all-non-coded and specific coded diagnoses together");
        }
        if (!cd.isIncludeAllNonCodedDiagnoses()) {
            DetachedCriteria specificCodedClause = DetachedCriteria.forClass(Obs.class, "codedDiagnosisObs");
            specificCodedClause.add(Restrictions.eq("voided", false));
            specificCodedClause.add(Restrictions.eq("concept", dmd.getCodedDiagnosisConcept()));
            if (cd.getCodedDiagnoses() != null) {
                specificCodedClause.add(Restrictions.in("valueCoded", cd.getCodedDiagnoses()));
            }
            if (cd.getExcludeCodedDiagnoses() != null) {
                specificCodedClause.add(Restrictions.not(Restrictions.in("valueCoded", cd.getExcludeCodedDiagnoses())));
            }
            specificCodedClause.add(Restrictions.eqProperty("obsGroup", "obsgroup.id"));
            specificCodedClause.setProjection(Projections.property("id"));
            crit.add(Subqueries.exists(specificCodedClause));
        }
    } else if (cd.isIncludeAllNonCodedDiagnoses()) {
        DetachedCriteria anyNonCodedClause = DetachedCriteria.forClass(Obs.class, "nonCodedDiagnosisObs");
        anyNonCodedClause.add(Restrictions.eq("voided", false));
        anyNonCodedClause.add(Restrictions.eq("concept", dmd.getNonCodedDiagnosisConcept()));
        anyNonCodedClause.add(Restrictions.eqProperty("obsGroup", "obsgroup.id"));
        anyNonCodedClause.setProjection(Projections.property("id"));
        crit.add(Subqueries.exists(anyNonCodedClause));
    }
    Cohort c = new Cohort();
    for (Integer i : (List<Integer>) crit.list()) {
        c.addMember(i);
    }
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : Obs(org.openmrs.Obs) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) DiagnosisMetadata(org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) List(java.util.List) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) DiagnosisCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition)

Aggregations

DiagnosisCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition)9 EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)8 Test (org.junit.Test)7 EvaluationContext (org.openmrs.module.reporting.evaluation.EvaluationContext)7 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)7 List (java.util.List)2 Date (java.util.Date)1 Criteria (org.hibernate.Criteria)1 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)1 Cohort (org.openmrs.Cohort)1 Concept (org.openmrs.Concept)1 Obs (org.openmrs.Obs)1 DiagnosisMetadata (org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata)1 DocumentedDefinition (org.openmrs.module.reporting.definition.library.DocumentedDefinition)1 Parameter (org.openmrs.module.reporting.evaluation.parameter.Parameter)1