use of org.openmrs.module.pihcore.reporting.cohort.definition.LastDispositionBeforeExitCohortDefinition in project openmrs-module-pihcore by PIH.
the class PihCohortDefinitionLibrary method lastDispositionBeforeExitHelper.
private CohortDefinition lastDispositionBeforeExitHelper(Concept disposition) {
LastDispositionBeforeExitCohortDefinition cd = new LastDispositionBeforeExitCohortDefinition();
cd.addParameter(new Parameter("exitOnOrAfter", "mirebalaisreports.parameter.exitOnOrAfter", Date.class));
cd.addParameter(new Parameter("exitOnOrBefore", "mirebalaisreports.parameter.exitOnOrBefore", Date.class));
cd.addParameter(new Parameter("exitFromWard", "mirebalaisreports.parameter.exitFromWard", Location.class));
cd.setDispositionsToConsider(getDispositionsToConsiderAsExit());
cd.addDisposition(disposition);
return new MappedParametersCohortDefinition(cd, "exitOnOrAfter", "startDate", "exitOnOrBefore", "endDate", "exitFromWard", "location");
}
use of org.openmrs.module.pihcore.reporting.cohort.definition.LastDispositionBeforeExitCohortDefinition in project openmrs-module-pihcore by PIH.
the class LastDispositionBeforeExitCohortDefinitionEvaluatorTest method testEvaluate.
@Test
public void testEvaluate() throws Exception {
Location mensInternalMedicine = Metadata.lookup(MirebalaisLocations.MENS_INTERNAL_MEDICINE);
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");
Concept discharged = conceptService.getConceptByMapping("DISCHARGED", "PIH");
LastDispositionBeforeExitCohortDefinition definition = new LastDispositionBeforeExitCohortDefinition();
definition.setExitFromWard(mensInternalMedicine);
definition.setExitOnOrAfter(startDate);
definition.setExitOnOrBefore(endDate);
definition.addDisposition(discharged);
EvaluatedCohort result = cohortDefinitionService.evaluate(definition, new EvaluationContext());
assertThat(result, isCohortWithExactlyIds(patient4.getId()));
}
use of org.openmrs.module.pihcore.reporting.cohort.definition.LastDispositionBeforeExitCohortDefinition 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);
}
Aggregations