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()));
}
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);
}
Aggregations