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;
}
Aggregations