Search in sources :

Example 41 with JexlContext

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);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBException(org.jkiss.dbeaver.DBException)

Example 42 with JexlContext

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);
            }
        }
    }
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) DashboardDataset(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset) JexlContext(org.apache.commons.jexl3.JexlContext)

Example 43 with JexlContext

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);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBException(org.jkiss.dbeaver.DBException)

Example 44 with JexlContext

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));
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) Test(org.testng.annotations.Test)

Example 45 with JexlContext

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));
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) NumberUtils(org.apache.commons.lang.math.NumberUtils) Test(org.testng.annotations.Test)

Aggregations

JexlContext (org.apache.commons.jexl2.JexlContext)31 Expression (org.apache.commons.jexl2.Expression)25 MapContext (org.apache.commons.jexl2.MapContext)23 JexlContext (org.apache.commons.jexl3.JexlContext)21 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 MapContext (org.apache.commons.jexl3.MapContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 JexlException (org.apache.commons.jexl2.JexlException)3 ObjectContext (org.apache.commons.jexl2.ObjectContext)3 IOException (java.io.IOException)2 ReadonlyContext (org.apache.commons.jexl2.ReadonlyContext)2 JexlScript (org.apache.commons.jexl3.JexlScript)2 NumberUtils (org.apache.commons.lang.math.NumberUtils)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2 IntWritable (org.apache.hadoop.io.IntWritable)2 Text (org.apache.hadoop.io.Text)2 VersionMismatchException (org.apache.hadoop.io.VersionMismatchException)2