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