Search in sources :

Example 16 with EvaluatedCohort

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

the class InpatientLocationCohortDefinitionEvaluatorTest method testThatTransferOutsAreNotIncluded.

@Test
public void testThatTransferOutsAreNotIncluded() throws Exception {
    InpatientLocationCohortDefinition definition = new InpatientLocationCohortDefinition();
    definition.addParameter(new Parameter("ward", "Ward", Location.class));
    definition.addParameter(new Parameter("effectiveDate", "Date", Date.class));
    Location womensInternalMedicine = locationService.getLocation("Sal Fanm");
    EvaluationContext ec = new EvaluationContext();
    ec.addParameterValue("ward", womensInternalMedicine);
    Date endOfDay = DateUtil.parseDate("2013-10-03 23:59:59", "yyyy-MM-dd HH:mm:ss");
    ec.addParameterValue("effectiveDate", endOfDay);
    EvaluatedCohort result = cohortDefinitionService.evaluate(definition, ec);
    assertThat(result, isCohortWithExactlyIds(patient1.getId()));
}
Also used : InpatientLocationCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition) Parameter(org.openmrs.module.reporting.evaluation.parameter.Parameter) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) Date(java.util.Date) Location(org.openmrs.Location) BaseInpatientReportTest(org.openmrs.module.pihcore.reporting.BaseInpatientReportTest) Test(org.junit.Test)

Example 17 with EvaluatedCohort

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

the class InpatientLocationCohortDefinitionEvaluatorTest method testEvaluate.

@Test
public void testEvaluate() throws Exception {
    InpatientLocationCohortDefinition definition = new InpatientLocationCohortDefinition();
    definition.addParameter(new Parameter("ward", "Ward", Location.class));
    definition.addParameter(new Parameter("effectiveDate", "Date", Date.class));
    Location womensInternalMedicine = locationService.getLocation("Sal Fanm");
    EvaluationContext ec = new EvaluationContext();
    ec.addParameterValue("ward", womensInternalMedicine);
    ec.addParameterValue("effectiveDate", DateUtil.parseDate("2013-10-03", "yyyy-MM-dd"));
    EvaluatedCohort result = cohortDefinitionService.evaluate(definition, ec);
    assertThat(result, isCohortWithExactlyIds(patient1.getId(), patient5.getId()));
}
Also used : InpatientLocationCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition) Parameter(org.openmrs.module.reporting.evaluation.parameter.Parameter) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) Date(java.util.Date) Location(org.openmrs.Location) BaseInpatientReportTest(org.openmrs.module.pihcore.reporting.BaseInpatientReportTest) Test(org.junit.Test)

Example 18 with EvaluatedCohort

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

the class ActiveVisitsWithEncountersCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    ActiveVisitsWithEncountersCohortDefinition cd = (ActiveVisitsWithEncountersCohortDefinition) cohortDefinition;
    HqlQueryBuilder encQuery = new HqlQueryBuilder();
    encQuery.select("e.patient.id, e.encounterId");
    encQuery.from(Encounter.class, "e");
    encQuery.whereEqual("e.voided", false);
    encQuery.whereEqual("e.visit.voided", false);
    encQuery.wherePatientIn("e.patient.id", context);
    if (cd.isActive() != null) {
        encQuery.whereNull("e.visit.stopDatetime");
    }
    if (cd.getWhichEncounter() == TimeQualifier.LAST) {
        // Ascending since we take last value found
        encQuery.orderAsc("e.encounterDatetime").orderAsc("e.dateCreated");
    } else if (cd.getWhichEncounter() == TimeQualifier.FIRST) {
        // Descending since we take last value found
        encQuery.orderDesc("e.encounterDatetime").orderDesc("e.dateCreated");
    }
    Set<Integer> encountersToInclude = new HashSet<Integer>();
    if (cd.getWhichEncounter() == TimeQualifier.LAST || cd.getWhichEncounter() == TimeQualifier.FIRST) {
        Map<Integer, Integer> encountersByPatient = evaluationService.evaluateToMap(encQuery, Integer.class, Integer.class, context);
        encountersToInclude.addAll(encountersByPatient.values());
    } else {
        List<Object[]> rawResults = evaluationService.evaluateToList(encQuery, context);
        for (Object[] resultRow : rawResults) {
            encountersToInclude.add((Integer) resultRow[1]);
        }
    }
    HqlQueryBuilder q = new HqlQueryBuilder();
    q.select("e.patient.id");
    q.from(Encounter.class, "e");
    q.whereIn("e.location", cd.getLocationList());
    q.whereIdIn("e.encounterId", encountersToInclude);
    Cohort c = new Cohort(evaluationService.evaluateToList(q, Integer.class, context));
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : ActiveVisitsWithEncountersCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.ActiveVisitsWithEncountersCohortDefinition) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) HqlQueryBuilder(org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder) HashSet(java.util.HashSet)

Example 19 with EvaluatedCohort

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

the class InpatientLocationCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    InpatientLocationCohortDefinition cd = (InpatientLocationCohortDefinition) cohortDefinition;
    Date onDate = cd.getEffectiveDate();
    if (onDate == null) {
        onDate = new Date();
    }
    Location ward = cd.getWard();
    Location visitLocation = null;
    if (ward != null) {
        visitLocation = adtService.getLocationThatSupportsVisits(ward);
    }
    EncounterType admissionEncounterType = emrApiProperties.getAdmissionEncounterType();
    EncounterType dischargeEncounterType = emrApiProperties.getExitFromInpatientEncounterType();
    EncounterType transferEncounterType = emrApiProperties.getTransferWithinHospitalEncounterType();
    StringBuilder sb = new StringBuilder("select distinct v.patient_id " + "from visit v " + "inner join encounter admission " + "  on v.visit_id = admission.visit_id " + "  and admission.voided = false " + "  and admission.encounter_type = :admissionEncounterType " + "  and admission.encounter_datetime <= :onDate " + "inner join encounter mostRecentAdt " + "  on v.visit_id = mostRecentAdt.visit_id " + "  and mostRecentAdt.encounter_id = ( " + "    select encounter_id " + "    from encounter " + "    where visit_id = v.visit_id " + "    and voided = false " + "    and encounter_type in (:adtEncounterTypes) " + "    and encounter_datetime <= :onDate " + "    order by encounter_datetime desc, date_created desc limit 1" + "  ) ");
    sb.append("where v.voided = false");
    if (visitLocation != null) {
        sb.append("  and v.location_id = :visitLocation ");
    }
    sb.append("  and v.date_started <= :onDate ");
    sb.append("  and (v.date_stopped is null or v.date_stopped > :onDate) ");
    if (ward != null) {
        sb.append("  and mostRecentAdt.location_id = :ward ");
    }
    sb.append("  and mostRecentAdt.encounter_type in (:admitOrTransferEncounterTypes)");
    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
    query.setInteger("admissionEncounterType", admissionEncounterType.getId());
    query.setTimestamp("onDate", onDate);
    if (visitLocation != null) {
        query.setInteger("visitLocation", visitLocation.getId());
    }
    if (ward != null) {
        query.setInteger("ward", ward.getId());
    }
    query.setParameterList("adtEncounterTypes", new Integer[] { admissionEncounterType.getId(), dischargeEncounterType.getId(), transferEncounterType.getId() });
    query.setParameterList("admitOrTransferEncounterTypes", new Integer[] { admissionEncounterType.getId(), transferEncounterType.getId() });
    // This does not actually work: org.hibernate.hql.ast.QuerySyntaxException: with-clause referenced two different from-clause elements
    // Query hql = sessionFactory.getCurrentSession().createQuery("select distinct(v.patient.id) " +
    // "from Visit v " +
    // "join v.encounters as mostRecentAdt " +
    // "    with mostRecentAdt.voided = false " +
    // "    and mostRecentAdt.encounterType in (:adtEncounterTypes) " +
    // "    and mostRecentAdt.encounterDatetime = ( " +
    // "        select max(encounterDatetime)" +
    // "        from Encounter " +
    // "        where visit = v " +
    // "        and voided = false " +
    // "        and encounterType in (:adtEncounterTypes) " +
    // "        and encounterDatetime <= :onDate " +
    // "    ) " +
    // "where v.voided = false " +
    // "and v.location = :visitLocation " +
    // "and v.startDatetime <= :onDate " +
    // "and (v.stopDatetime is null or v.stopDatetime > :onDate) " +
    // "and exists ( " +
    // "    from Encounter admission " +
    // "    where admission.visit = v " +
    // "    and admission.voided = false " +
    // "    and admission.encounterType = :admissionEncounterType " +
    // "    and admission.encounterDatetime <= :onDate " +
    // ") " +
    // "and mostRecentAdt.location = :ward " +
    // "and mostRecentAdt.encounterType in (:admitOrTransferEncounterTypes) ");
    // 
    // hql.setParameter("onDate", onDate);
    // hql.setParameter("visitLocation", visitLocation);
    // hql.setParameter("ward", ward);
    // hql.setParameter("admissionEncounterType", admissionEncounterType);
    // hql.setParameterList("adtEncounterTypes", adtEncounterTypes);
    // hql.setParameterList("admitOrTransferEncounterTypes", admitOrTransferEncounterTypes);
    Cohort c = new Cohort();
    for (Integer i : (List<Integer>) query.list()) {
        c.addMember(i);
    }
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : InpatientLocationCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) List(java.util.List) EncounterType(org.openmrs.EncounterType) SQLQuery(org.hibernate.SQLQuery) Date(java.util.Date) Location(org.openmrs.Location)

Example 20 with EvaluatedCohort

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

the class LastDispositionBeforeExitCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    LastDispositionBeforeExitCohortDefinition cd = (LastDispositionBeforeExitCohortDefinition) cohortDefinition;
    Location exitFromWard = cd.getExitFromWard();
    List<Concept> dispositions = cd.getDispositions();
    List<Concept> dispositionsToConsider = cd.getDispositionsToConsider();
    String sql = "select distinct v.patient_id " + "from visit v " + "inner join encounter exit_encounter " + " on exit_encounter.visit_id = v.visit_id " + " and exit_encounter.voided = false " + " and exit_encounter.encounter_type = :exitEncounterType " + " and exit_encounter.encounter_datetime between :exitOnOrAfter and :exitOnOrBefore ";
    if (exitFromWard != null) {
        sql += " and exit_encounter.location_id = :exitFromWard ";
    }
    sql += "inner join encounter obs_encounter " + " on obs_encounter.visit_id = v.visit_id " + " and obs_encounter.encounter_id = (" + "   select find_obs_encounter.encounter_id " + "   from encounter find_obs_encounter " + "   inner join obs has_obs " + "     on has_obs.encounter_id = find_obs_encounter.encounter_id " + "     and has_obs.voided = false " + "     and has_obs.concept_id = :dispositionConcept ";
    if (dispositionsToConsider != null) {
        sql += "     and has_obs.value_coded in (:dispositionsToConsider) ";
    }
    sql += "    where find_obs_encounter.visit_id = v.visit_id " + "     and find_obs_encounter.voided = false " + "    order by find_obs_encounter.encounter_datetime desc, find_obs_encounter.date_created desc limit 1 " + // "     and find_obs_encounter.location_id = :exitFromWard " +
    " )" + "inner join obs o " + " on o.voided = false " + " and o.concept_id = :dispositionConcept " + " and o.encounter_id = obs_encounter.encounter_id " + "where v.voided = false " + " and o.value_coded in (:dispositions) ";
    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
    query.setInteger("dispositionConcept", dispositionService.getDispositionDescriptor().getDispositionConcept().getId());
    query.setParameterList("dispositions", idList(dispositions));
    query.setInteger("exitEncounterType", emrApiProperties.getExitFromInpatientEncounterType().getId());
    query.setTimestamp("exitOnOrAfter", cd.getExitOnOrAfter());
    query.setTimestamp("exitOnOrBefore", cd.getExitOnOrBefore());
    if (exitFromWard != null) {
        query.setInteger("exitFromWard", exitFromWard.getId());
    }
    if (dispositionsToConsider != null) {
        query.setParameterList("dispositionsToConsider", idList(dispositionsToConsider));
    }
    Cohort c = new Cohort();
    for (Integer i : (List<Integer>) query.list()) {
        c.addMember(i);
    }
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : Concept(org.openmrs.Concept) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) ArrayList(java.util.ArrayList) List(java.util.List) LastDispositionBeforeExitCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.LastDispositionBeforeExitCohortDefinition) SQLQuery(org.hibernate.SQLQuery) Location(org.openmrs.Location)

Aggregations

EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)26 Test (org.junit.Test)18 EvaluationContext (org.openmrs.module.reporting.evaluation.EvaluationContext)18 PihCoreContextSensitiveTest (org.openmrs.module.pihcore.PihCoreContextSensitiveTest)12 Date (java.util.Date)8 Cohort (org.openmrs.Cohort)8 Location (org.openmrs.Location)8 DiagnosisCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.DiagnosisCohortDefinition)8 List (java.util.List)7 BaseInpatientReportTest (org.openmrs.module.pihcore.reporting.BaseInpatientReportTest)6 PersonAuditInfoCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.PersonAuditInfoCohortDefinition)6 SQLQuery (org.hibernate.SQLQuery)5 InpatientLocationCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition)3 InpatientTransferCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.InpatientTransferCohortDefinition)3 Criteria (org.hibernate.Criteria)2 Concept (org.openmrs.Concept)2 EncounterType (org.openmrs.EncounterType)2 AdmissionSoonAfterExitCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.AdmissionSoonAfterExitCohortDefinition)2 LastDispositionBeforeExitCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.LastDispositionBeforeExitCohortDefinition)2 Parameter (org.openmrs.module.reporting.evaluation.parameter.Parameter)2