Search in sources :

Example 6 with DataSet

use of org.openmrs.module.reporting.dataset.DataSet in project openmrs-module-pihcore by PIH.

the class ActiveVisitsAjaxController method getActiveVisits.

@RequestMapping(value = "/module/pihcore/reports/ajax/activeVisitsList.form", method = RequestMethod.GET, headers = "Accept=application/json")
public void getActiveVisits(@RequestParam(value = "patientIds", required = false) Integer[] patientIds, HttpServletResponse response) throws EvaluationException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    AllDefinitionLibraries definitionLibraries = Context.getRegisteredComponents(AllDefinitionLibraries.class).get(0);
    EvaluationContext context = new EvaluationContext();
    Cohort c = null;
    if (patientIds != null && patientIds.length > 0) {
        if (patientIds[0] == null) {
            // there are no patients with last encounter at this specific location
            return;
        }
        c = new Cohort("active", "Patients with active visits", patientIds);
    }
    DataSet result = null;
    PatientDataSetDefinition dsd = new PatientDataSetDefinition();
    VisitCohortDefinition visitCohortDefinition = new VisitCohortDefinition();
    visitCohortDefinition.setActive(true);
    if (c != null) {
        context.setBaseCohort(c);
    }
    dsd.addRowFilter(visitCohortDefinition, null);
    dsd.addColumn("patientId", definitionLibraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.patientId"), "");
    dsd.addColumn("familyName", definitionLibraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredName.familyName"), "");
    dsd.addColumn("givenName", definitionLibraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredName.givenName"), "");
    // TODO: change this to not have to rely on an if/then
    if (config.getCountry().equals(ConfigDescriptor.Country.HAITI)) {
        dsd.addColumn("identifier", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.preferredZlEmrId.identifier"), "");
    } else {
        dsd.addColumn("identifier", definitionLibraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredIdentifier.identifier"), "");
    }
    dsd.addColumn("firstCheckinLocation", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.checkin.location"), "");
    dsd.addColumn("checkinDateTime", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.checkin.encounterDatetime"), "");
    dsd.addColumn("lastEncounterType", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.lastEncounter.type"), "");
    dsd.addColumn("lastEncounterLocation", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.lastEncounter.location"), "");
    dsd.addColumn("lastEncounterDateTime", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.lastEncounter.encounterDatetime"), "");
    dsd.addColumn("visitUuid", definitionLibraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.lastEncounter.visit.uuid"), "");
    result = dsdService.evaluate(dsd, context);
    String json = toJson(DataSetUtil.simplify(result));
    PrintWriter out = response.getWriter();
    out.print(json);
}
Also used : Cohort(org.openmrs.Cohort) DataSet(org.openmrs.module.reporting.dataset.DataSet) PatientDataDefinition(org.openmrs.module.reporting.data.patient.definition.PatientDataDefinition) PatientDataSetDefinition(org.openmrs.module.reporting.dataset.definition.PatientDataSetDefinition) AllDefinitionLibraries(org.openmrs.module.reporting.definition.library.AllDefinitionLibraries) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) VisitCohortDefinition(org.openmrs.module.reporting.cohort.definition.VisitCohortDefinition) PrintWriter(java.io.PrintWriter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with DataSet

use of org.openmrs.module.reporting.dataset.DataSet in project openmrs-module-pihcore by PIH.

the class CohortFragmentController method getCohort.

public SimpleObject getCohort(@RequestParam("memberIds") List<Integer> memberIds, @SpringBean AllDefinitionLibraries libraries, @SpringBean DataSetDefinitionService dsdService) throws EvaluationException {
    PatientDataSetDefinition dsd = new PatientDataSetDefinition();
    dsd.addColumn("patientId", libraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.patientId"), "");
    dsd.addColumn("familyName", libraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredName.familyName"), "");
    dsd.addColumn("givenName", libraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredName.givenName"), "");
    dsd.addColumn("zlEmrId", libraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.preferredZlEmrId.identifier"), "");
    dsd.addColumn("dossierNumber", libraries.getDefinition(PatientDataDefinition.class, "mirebalais.patientDataCalculation.mostRecentDossierNumber.identifier"), "");
    Cohort cohort = new Cohort(memberIds);
    EvaluationContext context = new EvaluationContext();
    context.setBaseCohort(cohort);
    DataSet result = dsdService.evaluate(dsd, context);
    return SimpleObject.create("members", simplify(result));
}
Also used : Cohort(org.openmrs.Cohort) PatientDataDefinition(org.openmrs.module.reporting.data.patient.definition.PatientDataDefinition) DataSet(org.openmrs.module.reporting.dataset.DataSet) PatientDataSetDefinition(org.openmrs.module.reporting.dataset.definition.PatientDataSetDefinition) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext)

Example 8 with DataSet

use of org.openmrs.module.reporting.dataset.DataSet in project openmrs-module-pihcore by PIH.

the class StatusDataEvaluator method evaluate.

public StatusData evaluate(Patient patient, StatusDataDefinition definition) {
    StatusData data = new StatusData(definition.getId());
    data.setLabel(messageSourceService.getMessage(definition.getLabelCode()));
    SqlFileDataSetDefinition dsd = new SqlFileDataSetDefinition();
    dsd.addParameter(new Parameter("patientId", "Patient ID", Integer.class));
    String sql = definition.getStatusDataQuery();
    if (sql.toLowerCase().endsWith(".sql")) {
        File statusDataDir = StatusDataLoader.getStatusDataDirectory();
        File statusDefFile = new File(statusDataDir, definition.getPath());
        File sqlFile = new File(statusDefFile.getParentFile(), definition.getStatusDataQuery());
        dsd.setSqlFile(sqlFile.getAbsolutePath());
    } else {
        dsd.setSql(sql);
    }
    EvaluationContext context = new EvaluationContext();
    context.addParameterValue("patientId", patient.getPatientId());
    List<Map<String, Object>> queryData = new ArrayList<>();
    try {
        DataSet dataSet = dataSetDefinitionService.evaluate(dsd, context);
        for (Iterator<DataSetRow> i = dataSet.iterator(); i.hasNext(); ) {
            queryData.add(i.next().getColumnValuesByKey());
        }
    } catch (EvaluationException e) {
        throw new RuntimeException(e);
    }
    data.getQueryData().put("data", queryData);
    if (queryData.size() == 1) {
        Map<String, Object> vals = queryData.get(0);
        for (String key : vals.keySet()) {
            if (data.getQueryData().containsKey(key)) {
                throw new IllegalArgumentException("Key <" + key + "> is already defined, please adjust your query column names");
            }
            data.getQueryData().put(key, vals.get(key));
        }
    }
    VelocityContext velocityContext = StatusDataFunctions.getVelocityContext(data.getQueryData());
    boolean enabled = true;
    if (StringUtils.isNotEmpty(definition.getConditionExpression())) {
        String conditionValue = StatusDataFunctions.evaluateExpression(velocityContext, definition.getConditionExpression());
        if (conditionValue != null && (conditionValue.equalsIgnoreCase("false") || conditionValue.equalsIgnoreCase("0"))) {
            enabled = false;
        }
    }
    data.setEnabled(enabled);
    String displayValue = StatusDataFunctions.evaluateExpression(velocityContext, definition.getValueExpression());
    data.setDisplayValue(displayValue);
    String displayFormat = "";
    if (StringUtils.isNotBlank(definition.getFormatExpression())) {
        displayFormat = StatusDataFunctions.evaluateExpression(velocityContext, definition.getFormatExpression());
    }
    data.setDisplayFormat(displayFormat);
    return data;
}
Also used : DataSet(org.openmrs.module.reporting.dataset.DataSet) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) EvaluationException(org.openmrs.module.reporting.evaluation.EvaluationException) Parameter(org.openmrs.module.reporting.evaluation.parameter.Parameter) SqlFileDataSetDefinition(org.openmrs.module.reporting.dataset.definition.SqlFileDataSetDefinition) EvaluationContext(org.openmrs.module.reporting.evaluation.EvaluationContext) File(java.io.File) Map(java.util.Map) DataSetRow(org.openmrs.module.reporting.dataset.DataSetRow)

Aggregations

DataSet (org.openmrs.module.reporting.dataset.DataSet)8 EvaluationContext (org.openmrs.module.reporting.evaluation.EvaluationContext)8 PatientDataDefinition (org.openmrs.module.reporting.data.patient.definition.PatientDataDefinition)4 DataSetRow (org.openmrs.module.reporting.dataset.DataSetRow)4 Test (org.junit.Test)3 Cohort (org.openmrs.Cohort)3 BaseReportTest (org.openmrs.module.pihcore.reporting.BaseReportTest)3 DataSetDescriptor (org.openmrs.module.reporting.config.DataSetDescriptor)3 DataSetDefinition (org.openmrs.module.reporting.dataset.definition.DataSetDefinition)3 PatientDataSetDefinition (org.openmrs.module.reporting.dataset.definition.PatientDataSetDefinition)3 Map (java.util.Map)2 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 VelocityContext (org.apache.velocity.VelocityContext)1 AppDescriptor (org.openmrs.module.appframework.domain.AppDescriptor)1 Extension (org.openmrs.module.appframework.domain.Extension)1 AwaitingAdmissionVisitQuery (org.openmrs.module.emrapi.adt.reporting.query.AwaitingAdmissionVisitQuery)1 InpatientLocationCohortDefinition (org.openmrs.module.pihcore.reporting.cohort.definition.InpatientLocationCohortDefinition)1