Search in sources :

Example 1 with AdmissionSoonAfterExitCohortDefinition

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

the class AdmissionSoonAfterExitCohortDefinitionEvaluatorTest method testEvaluate.

@Test
public void testEvaluate() throws Exception {
    Date startDate = DateUtil.parseDate("2013-10-03 00:00:00", "yyyy-MM-dd HH:mm:ss");
    Date endDate = DateUtil.parseDate("2013-10-03 23:59:59", "yyyy-MM-dd HH:mm:ss");
    AdmissionSoonAfterExitCohortDefinition definition = new AdmissionSoonAfterExitCohortDefinition();
    definition.setOnOrAfter(startDate);
    definition.setOnOrBefore(endDate);
    EvaluatedCohort result = cohortDefinitionService.evaluate(definition, new EvaluationContext());
    assertThat(result, isCohortWithExactlyIds(patient3.getId()));
}
Also used : AdmissionSoonAfterExitCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.AdmissionSoonAfterExitCohortDefinition) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) Date(java.util.Date) BaseInpatientReportTest(org.openmrs.module.pihcore.reporting.BaseInpatientReportTest) Test(org.junit.Test)

Example 2 with AdmissionSoonAfterExitCohortDefinition

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

the class AdmissionSoonAfterExitCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    AdmissionSoonAfterExitCohortDefinition cd = (AdmissionSoonAfterExitCohortDefinition) cohortDefinition;
    String sql = "select distinct admit.patient_id \n" + "from encounter admit \n" + "inner join encounter discharge \n" + " on discharge.patient_id = admit.patient_id \n" + " and discharge.voided = false \n" + " and discharge.encounter_type = :dischargeEncounterType \n" + // was mysql-specific " and discharge.encounter_datetime >= DATE_SUB(admit.encounter_datetime, INTERVAL :windowInDays DAY) \n" +
    " and discharge.encounter_datetime >= TIMESTAMPADD(DAY, -:windowInDays, admit.encounter_datetime) \n" + " and discharge.encounter_datetime < admit.encounter_datetime \n" + "where admit.voided = false \n" + " and admit.encounter_type = :admitEncounterType \n" + " and admit.encounter_datetime between :onOrAfter and :onOrBefore ";
    if (cd.getAdmissionLocation() != null) {
        sql += " and admit.location_id = :admissionLocation ";
    }
    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql);
    query.setInteger("admitEncounterType", emrApiProperties.getAdmissionEncounterType().getId());
    query.setInteger("dischargeEncounterType", emrApiProperties.getExitFromInpatientEncounterType().getId());
    query.setTimestamp("onOrAfter", cd.getOnOrAfter());
    query.setTimestamp("onOrBefore", cd.getOnOrBefore());
    query.setInteger("windowInDays", cd.getWindowInDays());
    if (cd.getAdmissionLocation() != null) {
        query.setInteger("admissionLocation", cd.getAdmissionLocation().getId());
    }
    Cohort c = new Cohort();
    for (Integer i : (List<Integer>) query.list()) {
        c.addMember(i);
    }
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : AdmissionSoonAfterExitCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.AdmissionSoonAfterExitCohortDefinition) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) List(java.util.List) SQLQuery(org.hibernate.SQLQuery)

Aggregations

AdmissionSoonAfterExitCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.AdmissionSoonAfterExitCohortDefinition)2 EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)2 Date (java.util.Date)1 List (java.util.List)1 SQLQuery (org.hibernate.SQLQuery)1 Test (org.junit.Test)1 Cohort (org.openmrs.Cohort)1 BaseInpatientReportTest (org.openmrs.module.pihcore.reporting.BaseInpatientReportTest)1 EvaluationContext (org.openmrs.module.reporting.evaluation.EvaluationContext)1