Search in sources :

Example 1 with HqlQueryBuilder

use of org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder in project openmrs-module-pihcore by PIH.

the class ActiveVisitsWithEncountersCohortDefinitionEvaluator method evaluate.

@Override
public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) throws EvaluationException {
    ActiveVisitsWithEncountersCohortDefinition cd = (ActiveVisitsWithEncountersCohortDefinition) cohortDefinition;
    HqlQueryBuilder encQuery = new HqlQueryBuilder();
    encQuery.select("e.patient.id, e.encounterId");
    encQuery.from(Encounter.class, "e");
    encQuery.whereEqual("e.voided", false);
    encQuery.whereEqual("e.visit.voided", false);
    encQuery.wherePatientIn("e.patient.id", context);
    if (cd.isActive() != null) {
        encQuery.whereNull("e.visit.stopDatetime");
    }
    if (cd.getWhichEncounter() == TimeQualifier.LAST) {
        // Ascending since we take last value found
        encQuery.orderAsc("e.encounterDatetime").orderAsc("e.dateCreated");
    } else if (cd.getWhichEncounter() == TimeQualifier.FIRST) {
        // Descending since we take last value found
        encQuery.orderDesc("e.encounterDatetime").orderDesc("e.dateCreated");
    }
    Set<Integer> encountersToInclude = new HashSet<Integer>();
    if (cd.getWhichEncounter() == TimeQualifier.LAST || cd.getWhichEncounter() == TimeQualifier.FIRST) {
        Map<Integer, Integer> encountersByPatient = evaluationService.evaluateToMap(encQuery, Integer.class, Integer.class, context);
        encountersToInclude.addAll(encountersByPatient.values());
    } else {
        List<Object[]> rawResults = evaluationService.evaluateToList(encQuery, context);
        for (Object[] resultRow : rawResults) {
            encountersToInclude.add((Integer) resultRow[1]);
        }
    }
    HqlQueryBuilder q = new HqlQueryBuilder();
    q.select("e.patient.id");
    q.from(Encounter.class, "e");
    q.whereIn("e.location", cd.getLocationList());
    q.whereIdIn("e.encounterId", encountersToInclude);
    Cohort c = new Cohort(evaluationService.evaluateToList(q, Integer.class, context));
    return new EvaluatedCohort(c, cohortDefinition, context);
}
Also used : ActiveVisitsWithEncountersCohortDefinition(org.openmrs.module.pihcore.reporting.cohort.definition.ActiveVisitsWithEncountersCohortDefinition) Cohort(org.openmrs.Cohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) EvaluatedCohort(org.openmrs.module.reporting.cohort.EvaluatedCohort) HqlQueryBuilder(org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder) HashSet(java.util.HashSet)

Example 2 with HqlQueryBuilder

use of org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder in project openmrs-module-pihcore by PIH.

the class MultipleObsForEncounterDataEvaluator method evaluate.

@Override
public EvaluatedEncounterData evaluate(EncounterDataDefinition definition, EvaluationContext context) throws EvaluationException {
    MultipleObsForEncounterDataDefinition def = (MultipleObsForEncounterDataDefinition) definition;
    EvaluatedEncounterData data = new EvaluatedEncounterData();
    HqlQueryBuilder q = new HqlQueryBuilder();
    q.select("obs.encounter.encounterId, obs");
    q.from(Obs.class, "obs");
    q.whereIn("obs.concept", def.getQuestions());
    q.whereIn("obs.valueCoded", def.getAnswers());
    q.whereEncounterIn("obs.encounter.encounterId", context);
    List<Object[]> result = Context.getService(EvaluationService.class).evaluateToList(q, context);
    for (Object[] row : result) {
        Integer encounterId = (Integer) row[0];
        Obs obs = (Obs) row[1];
        List l = (List) data.getData().get(encounterId);
        if (l == null) {
            l = new ArrayList();
            data.getData().put(encounterId, l);
        }
        l.add(obs);
    }
    return data;
}
Also used : Obs(org.openmrs.Obs) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationService(org.openmrs.module.reporting.evaluation.service.EvaluationService) EvaluatedEncounterData(org.openmrs.module.reporting.data.encounter.EvaluatedEncounterData) HqlQueryBuilder(org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder) MultipleObsForEncounterDataDefinition(org.openmrs.module.pihcore.reporting.encounter.definition.MultipleObsForEncounterDataDefinition)

Aggregations

HqlQueryBuilder (org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Cohort (org.openmrs.Cohort)1 Obs (org.openmrs.Obs)1 ActiveVisitsWithEncountersCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.ActiveVisitsWithEncountersCohortDefinition)1 MultipleObsForEncounterDataDefinition (org.openmrs.module.pihcore.reporting.encounter.definition.MultipleObsForEncounterDataDefinition)1 EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)1 EvaluatedEncounterData (org.openmrs.module.reporting.data.encounter.EvaluatedEncounterData)1 EvaluationService (org.openmrs.module.reporting.evaluation.service.EvaluationService)1