use of org.apache.commons.jexl2.JexlContext in project dbeaver by serge-rider.
the class DBVUtils method evaluateDataExpression.
public static Object evaluateDataExpression(DBDAttributeBinding[] allAttributes, Object[] row, JexlExpression expression, String attributeName) {
Map<String, Object> nsList = getExpressionNamespaces();
JexlContext context = new JexlContext() {
@Override
public Object get(String s) {
Object ns = nsList.get(s);
if (ns != null) {
return ns;
}
if (s.equals(attributeName)) {
return null;
}
for (DBDAttributeBinding attr : allAttributes) {
if (s.equals(attr.getLabel())) {
return DBUtils.getAttributeValue(attr, allAttributes, row);
}
}
return null;
}
@Override
public void set(String s, Object o) {
}
@Override
public boolean has(String s) {
return get(s) != null;
}
};
try {
return expression.evaluate(context);
} catch (Exception e) {
return GeneralUtils.getExpressionParseMessage(e);
}
}
use of org.apache.commons.jexl2.JexlContext in project dbeaver by serge-rider.
the class DashboardUpdater method fetchDashboardMapData.
private void fetchDashboardMapData(DBRProgressMonitor monitor, DashboardContainer dashboard) {
MapQueryInfo mqi = getMapQueryData(dashboard);
if (mqi == null) {
return;
}
Map<String, Object> mapValue = mqi.mapValue;
if (mapValue != null) {
String[] mapKeys = dashboard.getMapKeys();
String[] mapLabels = dashboard.getMapLabels();
if (!ArrayUtils.isEmpty(mapKeys)) {
if (ArrayUtils.isEmpty(mapLabels)) {
mapLabels = mapKeys;
}
DashboardDataset dataset = new DashboardDataset(mapLabels);
Object[] mapValues = new Object[mapKeys.length];
for (int i = 0; i < mapKeys.length; i++) {
Object value = mapValue.get(mapKeys[i]);
Number numValue;
if (value instanceof Number) {
numValue = (Number) value;
} else {
numValue = CommonUtils.toDouble(value);
}
mapValues[i] = numValue;
}
Date timestamp = mqi.timestamp;
if (timestamp == null) {
timestamp = new Date();
}
dataset.addRow(new DashboardDatasetRow(timestamp, mapValues));
dashboard.updateDashboardData(dataset);
} else if (dashboard.getMapFormula() != null) {
Map<String, Object> ciMap = new HashMap<>(mapValue.size());
for (Map.Entry<String, Object> me : mapValue.entrySet()) {
ciMap.put(me.getKey().toLowerCase(Locale.ENGLISH), me.getValue());
}
JexlContext context = new JexlContext() {
@Override
public Object get(String name) {
if (name.equals("map")) {
return ciMap;
} else if (name.equals("dashboard")) {
return dashboard;
}
return null;
}
@Override
public void set(String name, Object value) {
log.warn("Set is not implemented in DBX model");
}
@Override
public boolean has(String name) {
return name.equals("object") || name.equals("dashboard");
}
};
Object result = dashboard.getMapFormula().evaluate(context);
if (result instanceof Number) {
String columnName = dashboard.getDashboardTitle();
if (!ArrayUtils.isEmpty(mapLabels)) {
columnName = mapLabels[0];
}
DashboardDataset dataset = new DashboardDataset(new String[] { columnName });
dataset.addRow(new DashboardDatasetRow(new Date(), new Object[] { result }));
dashboard.updateDashboardData(dataset);
} else {
log.debug("Wrong expression result: " + result);
}
}
}
}
use of org.apache.commons.jexl2.JexlContext in project dbeaver by dbeaver.
the class DBVUtils method evaluateDataExpression.
public static Object evaluateDataExpression(DBDAttributeBinding[] allAttributes, Object[] row, JexlExpression expression, String attributeName) {
Map<String, Object> nsList = getExpressionNamespaces();
JexlContext context = new JexlContext() {
@Override
public Object get(String s) {
Object ns = nsList.get(s);
if (ns != null) {
return ns;
}
if (s.equals(attributeName)) {
return null;
}
for (DBDAttributeBinding attr : allAttributes) {
if (s.equals(attr.getLabel())) {
return DBUtils.getAttributeValue(attr, allAttributes, row);
}
}
return null;
}
@Override
public void set(String s, Object o) {
}
@Override
public boolean has(String s) {
return get(s) != null;
}
};
try {
return expression.evaluate(context);
} catch (Exception e) {
return GeneralUtils.getExpressionParseMessage(e);
}
}
use of org.apache.commons.jexl2.JexlContext in project shifu by ShifuML.
the class JexlTest method testJavaNull.
@Test
public void testJavaNull() {
JexlEngine jexl = new JexlEngine();
String jexlExp = "is_bad_new != null";
String jexlExpEqual = "is_bad_new == null";
Expression e = jexl.createExpression(jexlExp);
Expression exp = jexl.createExpression(jexlExpEqual);
JexlContext jc = new MapContext();
jc.set("is_bad_new", null);
Assert.assertEquals(Boolean.FALSE, e.evaluate(jc));
Assert.assertEquals(Boolean.TRUE, exp.evaluate(jc));
jc.set("is_bad_new", new Object());
Assert.assertEquals(Boolean.TRUE, e.evaluate(jc));
Assert.assertEquals(Boolean.FALSE, exp.evaluate(jc));
}
use of org.apache.commons.jexl2.JexlContext in project shifu by ShifuML.
the class JexlTest method testMathMethod.
@Test
public void testMathMethod() {
JexlEngine jexl = new JexlEngine();
String jexlExp = "NumberUtils.max(a, b, c)";
Expression e = jexl.createExpression(jexlExp);
JexlContext jc = new MapContext();
jc.set("NumberUtils", new NumberUtils());
jc.set("a", 7);
jc.set("b", 5);
jc.set("c", 9);
Assert.assertEquals(9, e.evaluate(jc));
}
Aggregations