use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.
the class BatchJob method applyBeanPropertiesWithScriptHelper.
private Map<String, Object> applyBeanPropertiesWithScriptHelper(Object bean, Map<String, Object> properties) {
Context scriptContext = new Context(Mapper.toMap(bean), bean.getClass());
ScriptHelper scriptHelper = new GroovyScriptHelper(scriptContext);
return applyBeanProperties(bean, properties, value -> scriptHelper.eval(value.toString()));
}
use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.
the class WkfCommonServiceImpl method evalExpression.
@Override
public Object evalExpression(Map<String, Object> varMap, String expr) {
if (Strings.isNullOrEmpty(expr)) {
return null;
}
if (expr.startsWith("${") && expr.endsWith("}")) {
expr = expr.replaceFirst("\\$\\{", "");
expr = expr.substring(0, expr.length() - 1);
}
GroovyScriptHelper helper;
if (varMap instanceof Context) {
helper = new GroovyScriptHelper((Context) varMap);
} else {
SimpleBindings simpleBindings = new SimpleBindings();
simpleBindings.putAll(varMap);
helper = new GroovyScriptHelper(simpleBindings);
}
helper.getBindings().put("$ctx", WkfContextHelper.class);
Object result = null;
try {
result = helper.eval(expr);
} catch (Exception e) {
}
log.debug("Eval expr: {}, result: {}", expr, result);
return result;
}
use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.
the class TestStaticCompileGroovy method test.
@Test
public void test() {
String script = "import com.axelor.auth.db.User\n" + "import groovy.transform.CompileStatic\n" + "@CompileStatic\n" + "void execute(){\n" + "User user = new User()\n" + "user.code = 'abc'\n" + "def x = user\n" + "println(x.code)\n" + "}\n" + "execute()";
Context ctx = new Context(User.class);
GroovyScriptHelper helper = new GroovyScriptHelper(ctx);
helper.eval(script);
}
use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.
the class AxelorScriptEngine method eval.
@Override
public Object eval(String script, ScriptContext ctx) {
Bindings bindings = ctx.getBindings(ctx.getScopes().get(0));
bindings.put("$json", Beans.get(MetaJsonRecordRepository.class));
bindings.put("$ctx", WkfContextHelper.class);
bindings.put("$beans", Beans.class);
bindings.put("__user__", new FullContext(AuthUtils.getUser()));
return new GroovyScriptHelper(bindings).eval(script);
}
use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.
the class BamlServiceImpl method execute.
@Override
@Transactional
public Model execute(BamlModel bamlModel, Model entity) {
Bindings bindings = new SimpleBindings();
if (entity != null) {
String varName = Beans.get(WkfCommonService.class).getVarName(entity);
bindings.put(varName, entity);
}
bindings.put("$ctx", WkfContextHelper.class);
GroovyScriptHelper helper = new GroovyScriptHelper(bindings);
Object object = helper.eval(bamlModel.getResultScript());
if (object != null && object instanceof Model) {
return JpaRepository.of(EntityHelper.getEntityClass((Model) object)).save((Model) object);
}
return null;
}
Aggregations