Search in sources :

Example 1 with BatchableFunction

use of org.structr.core.function.BatchableFunction in project structr by structr.

the class FunctionExpression method evaluate.

@Override
public Object evaluate(final ActionContext ctx, final GraphObject entity) throws FrameworkException, UnlicensedException {
    final ArrayList<Object> results = new ArrayList<>();
    for (Expression expr : expressions) {
        final Object result = expr.evaluate(ctx, entity);
        results.add(result);
    }
    if (results.isEmpty() && expressions.size() > 0) {
        return function.usage(ctx.isJavaScriptContext());
    }
    if (function instanceof BatchableFunction) {
        // enable batching if batchable function is found
        ((BatchableFunction) function).setBatchSize(getBatchSize());
        ((BatchableFunction) function).setBatched(isBatched());
        // batchable functions must create their own transaction when in batched mode
        return function.apply(ctx, entity, results.toArray());
    } else if (isBatched()) {
        // when in batched mode,
        try (final Tx tx = StructrApp.getInstance(ctx.getSecurityContext()).tx()) {
            final Object result = function.apply(ctx, entity, results.toArray());
            tx.success();
            return result;
        }
    } else {
        // default execution path: enclosing transaction exists, no batching
        return function.apply(ctx, entity, results.toArray());
    }
}
Also used : BatchableFunction(org.structr.core.function.BatchableFunction) Tx(org.structr.core.graph.Tx) ArrayList(java.util.ArrayList) GraphObject(org.structr.core.GraphObject)

Aggregations

ArrayList (java.util.ArrayList)1 GraphObject (org.structr.core.GraphObject)1 BatchableFunction (org.structr.core.function.BatchableFunction)1 Tx (org.structr.core.graph.Tx)1