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