Search in sources :

Example 1 with SqlFileDataSetDefinition

use of org.openmrs.module.reporting.dataset.definition.SqlFileDataSetDefinition 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

File (java.io.File)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 VelocityContext (org.apache.velocity.VelocityContext)1 DataSet (org.openmrs.module.reporting.dataset.DataSet)1 DataSetRow (org.openmrs.module.reporting.dataset.DataSetRow)1 SqlFileDataSetDefinition (org.openmrs.module.reporting.dataset.definition.SqlFileDataSetDefinition)1 EvaluationContext (org.openmrs.module.reporting.evaluation.EvaluationContext)1 EvaluationException (org.openmrs.module.reporting.evaluation.EvaluationException)1 Parameter (org.openmrs.module.reporting.evaluation.parameter.Parameter)1