dataStoreMeta = dataStore.getMetaData();
calculatedFieldsMeta = dataStoreMeta.findFieldMeta("calculated", Boolean.TRUE);
for(int i = 0; i < calculatedFieldsMeta.size(); i++) {
IFieldMetaData fieldMeta = (IFieldMetaData)calculatedFieldsMeta.get(i);
DataSetVariable variable = (DataSetVariable)fieldMeta.getProperty("variable");
ScriptEngineManager scriptManager = new ScriptEngineManager();
ScriptEngine groovyScriptEngine = scriptManager.getEngineByName("groovy");
// handle bindings
// ... static bindings first
Iterator it = bindings.keySet().iterator();
while(it.hasNext()) {
String bindingName = (String)it.next();
Object bindingValue = bindings.get(bindingName);
groovyScriptEngine.put(bindingName, bindingValue);
}
// ... then runtime bindings
Map qFields = new HashMap();
Map dmFields = new HashMap();
Object[] columns = new Object[dataStoreMeta.getFieldCount()];
for(int j = 0; j < dataStoreMeta.getFieldCount(); j++) {
qFields.put(dataStoreMeta.getFieldMeta(j).getAlias(), record.getFieldAt(j).getValue());
dmFields.put(dataStoreMeta.getFieldMeta(j).getProperty("uniqueName"), record.getFieldAt(j).getValue());
columns[j] = record.getFieldAt(j).getValue();
}
groovyScriptEngine.put("qFields", qFields); // key = alias
groovyScriptEngine.put("dmFields", dmFields); // key = id
groovyScriptEngine.put("fields", qFields); // default key = alias
groovyScriptEngine.put("columns", columns); // key = col-index
// show time
Object calculatedValue = null;
try {
calculatedValue = groovyScriptEngine.eval(variable.getExpression());
} catch (ScriptException ex) {
calculatedValue = "NA";
ex.printStackTrace();
}
//logger.debug("Field [" + fieldMeta.getName()+ "] is equals to [" + calculatedValue + "]");
variable.setValue(calculatedValue);
record.getFieldAt(dataStoreMeta.getFieldIndex(fieldMeta.getName())).setValue(variable.getValue());
}
}