Search in sources :

Example 1 with GroovyScriptHelper

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()));
}
Also used : JobExecutionContext(org.quartz.JobExecutionContext) Context(com.axelor.rpc.Context) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper)

Example 2 with GroovyScriptHelper

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;
}
Also used : FullContext(com.axelor.apps.tool.context.FullContext) Context(com.axelor.rpc.Context) SimpleBindings(javax.script.SimpleBindings) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) AxelorException(com.axelor.exception.AxelorException)

Example 3 with GroovyScriptHelper

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);
}
Also used : Context(com.axelor.rpc.Context) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) Test(org.junit.Test)

Example 4 with GroovyScriptHelper

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);
}
Also used : FullContext(com.axelor.apps.tool.context.FullContext) MetaJsonRecordRepository(com.axelor.meta.db.repo.MetaJsonRecordRepository) Bindings(javax.script.Bindings) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper)

Example 5 with GroovyScriptHelper

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;
}
Also used : SimpleBindings(javax.script.SimpleBindings) Model(com.axelor.db.Model) BamlModel(com.axelor.apps.bpm.db.BamlModel) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) WkfCommonService(com.axelor.apps.bpm.service.WkfCommonService) Transactional(com.google.inject.persist.Transactional)

Aggregations

GroovyScriptHelper (com.axelor.script.GroovyScriptHelper)12 Context (com.axelor.rpc.Context)4 FullContext (com.axelor.apps.tool.context.FullContext)3 Model (com.axelor.db.Model)3 ScriptBindings (com.axelor.script.ScriptBindings)3 ScriptHelper (com.axelor.script.ScriptHelper)3 Bindings (javax.script.Bindings)3 SimpleBindings (javax.script.SimpleBindings)3 BamlModel (com.axelor.apps.bpm.db.BamlModel)2 User (com.axelor.auth.db.User)2 AxelorException (com.axelor.exception.AxelorException)2 Transactional (com.google.inject.persist.Transactional)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ProcessActionNode (com.axelor.apps.baml.xml.ProcessActionNode)1 ProcessActionRootNode (com.axelor.apps.baml.xml.ProcessActionRootNode)1 Company (com.axelor.apps.base.db.Company)1 FileTab (com.axelor.apps.base.db.FileTab)1 GlobalTrackingConfigurationLine (com.axelor.apps.base.db.GlobalTrackingConfigurationLine)1 GlobalTrackingLog (com.axelor.apps.base.db.GlobalTrackingLog)1