Search in sources :

Example 1 with InpatientLocationCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition 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 = Metadata.lookup(MirebalaisLocations.WOMENS_INTERNAL_MEDICINE);
    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 2 with InpatientLocationCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition 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 = Metadata.lookup(MirebalaisLocations.WOMENS_INTERNAL_MEDICINE);
    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 3 with InpatientLocationCohortDefinition

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

the class PihCohortDefinitionLibrary method getInpatientAtLocationOnDate.

@DocumentedDefinition(value = "inpatientAtLocationOnDate")
public CohortDefinition getInpatientAtLocationOnDate() {
    InpatientLocationCohortDefinition cd = new InpatientLocationCohortDefinition();
    cd.addParameter(parameter(Date.class, "effectiveDate"));
    cd.addParameter(parameter(Location.class, "ward"));
    return new MappedParametersCohortDefinition(cd, "ward", "location", "effectiveDate", "date");
}
Also used : InpatientLocationCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition) Date(java.util.Date) Location(org.openmrs.Location) MappedParametersCohortDefinition(org.openmrs.module.reporting.cohort.definition.MappedParametersCohortDefinition) DocumentedDefinition(org.openmrs.module.reporting.definition.library.DocumentedDefinition)

Example 4 with InpatientLocationCohortDefinition

use of org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition 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)

Aggregations

Date (java.util.Date)4 Location (org.openmrs.Location)4 InpatientLocationCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition)4 EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)3 Test (org.junit.Test)2 BaseInpatientReportTest (org.openmrs.module.pihcore.reporting.BaseInpatientReportTest)2 EvaluationContext (org.openmrs.module.reporting.evaluation.EvaluationContext)2 Parameter (org.openmrs.module.reporting.evaluation.parameter.Parameter)2 List (java.util.List)1 SQLQuery (org.hibernate.SQLQuery)1 Cohort (org.openmrs.Cohort)1 EncounterType (org.openmrs.EncounterType)1 MappedParametersCohortDefinition (org.openmrs.module.reporting.cohort.definition.MappedParametersCohortDefinition)1 DocumentedDefinition (org.openmrs.module.reporting.definition.library.DocumentedDefinition)1