Search in sources :

Example 6 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class StringReplaceAllFunction method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
    String target = FunctionUtils.getStringValue(arg1, env);
    if (target == null)
        throw new ExpressionRuntimeException("Could not replace with null string");
    String regex = FunctionUtils.getStringValue(arg2, env);
    String replacement = FunctionUtils.getStringValue(arg3, env);
    return new AviatorString(target.replaceAll(regex, replacement));
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 7 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorJavaTypeUnitTest method testNot.

@Test
public void testNot() {
    AviatorJavaType intType = new AviatorJavaType("intType");
    AviatorJavaType boolType = new AviatorJavaType("boolType");
    AviatorJavaType stringType = new AviatorJavaType("stringType");
    AviatorJavaType charType = new AviatorJavaType("charType");
    AviatorJavaType dateType = new AviatorJavaType("dateType");
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("intType", 3);
    env.put("boolType", Boolean.FALSE);
    env.put("stringType", "hello");
    env.put("charType", 'c');
    env.put("dateType", new Date());
    assertEquals(Boolean.TRUE, boolType.not(env).getValue(env));
    try {
        intType.not(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        stringType.not(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        charType.not(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        dateType.not(env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) HashMap(java.util.HashMap) Date(java.util.Date) Test(org.junit.Test)

Example 8 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class AviatorJavaTypeUnitTest method compareWithJavaType.

@Test
public void compareWithJavaType() {
    AviatorJavaType intType = new AviatorJavaType("intType");
    AviatorJavaType boolType = new AviatorJavaType("boolType");
    AviatorJavaType stringType = new AviatorJavaType("stringType");
    AviatorJavaType charType = new AviatorJavaType("charType");
    AviatorJavaType dateType = new AviatorJavaType("dateType");
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("intType", 3);
    env.put("boolType", Boolean.FALSE);
    env.put("stringType", "hello");
    env.put("charType", 'c');
    env.put("dateType", new Date());
    assertEquals(0, intType.innerCompare(intType, env));
    assertEquals(1, intType.innerCompare(new AviatorJavaType("unknow"), env));
    try {
        intType.innerCompare(boolType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        intType.innerCompare(stringType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        intType.innerCompare(charType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        intType.innerCompare(dateType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        boolType.innerCompare(intType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    assertEquals(0, boolType.innerCompare(boolType, env));
    try {
        boolType.innerCompare(stringType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        boolType.innerCompare(charType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        boolType.innerCompare(dateType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        stringType.innerCompare(intType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        stringType.innerCompare(boolType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    assertEquals(0, stringType.innerCompare(stringType, env));
    try {
        stringType.innerCompare(dateType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    assertTrue(stringType.innerCompare(charType, env) > 0);
    try {
        charType.innerCompare(intType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        charType.innerCompare(boolType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    assertTrue(charType.innerCompare(stringType, env) < 0);
    assertEquals(0, charType.innerCompare(charType, env));
    try {
        charType.innerCompare(dateType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        dateType.innerCompare(intType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        dateType.innerCompare(boolType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        dateType.innerCompare(stringType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    try {
        dateType.innerCompare(charType, env);
        Assert.fail();
    } catch (ExpressionRuntimeException e) {
    }
    assertEquals(0, dateType.innerCompare(dateType, env));
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) HashMap(java.util.HashMap) Date(java.util.Date) Test(org.junit.Test)

Example 9 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project rebuild by getrebuild.

the class RobotTriggerObserver method execAction.

/**
 * 执行触发内容
 *
 * @param context
 * @param when
 */
protected void execAction(OperatingContext context, TriggerWhen when) {
    final ID primaryId = context.getAnyRecord().getPrimary();
    final String sourceName = primaryId + ":" + when.name().charAt(0);
    TriggerAction[] beExecuted = when == TriggerWhen.DELETE ? DELETE_ACTION_HOLDS.get(primaryId) : RobotTriggerManager.instance.getActions(getEffectedId(context), when);
    if (beExecuted == null || beExecuted.length == 0) {
        return;
    }
    final boolean originTriggerSource = getTriggerSource() == null;
    // 设置原始触发源
    if (originTriggerSource) {
        TRIGGER_SOURCE.set(context);
    } else {
        // 自己触发自己,避免无限执行
        boolean x = primaryId.equals(getTriggerSource().getAnyRecord().getPrimary());
        boolean xor = x || sourceName.equals(TRIGGER_SOURCE_LAST.get());
        if (x || xor) {
            if (Application.devMode())
                log.warn("Self trigger, ignore : {}", sourceName);
            return;
        }
    }
    TRIGGER_SOURCE_LAST.set(sourceName);
    try {
        for (TriggerAction action : beExecuted) {
            log.info("Trigger [ {} ] executing on record ({}) : {}", action.getType(), when.name(), primaryId);
            try {
                action.execute(context);
                CommonsLog.createLog(TYPE_TRIGGER, context.getOperator(), action.getActionContext().getConfigId());
            } catch (Throwable ex) {
                // DataValidate 直接抛出
                if (ex instanceof DataValidateException)
                    throw ex;
                log.error("Trigger execution failed : {} << {}", action, context, ex);
                CommonsLog.createLog(TYPE_TRIGGER, context.getOperator(), action.getActionContext().getConfigId(), ex);
                // FIXME 触发器执行失败是否抛出
                if (ex instanceof MissingMetaExcetion || ex instanceof ExpressionRuntimeException || ex instanceof RepeatedRecordsException) {
                    throw new TriggerException(Language.L("触发器执行失败 : %s", ex.getLocalizedMessage()));
                } else if (ex instanceof TriggerException) {
                    throw (TriggerException) ex;
                } else {
                    throw new RebuildException(ex);
                }
            } finally {
                if (originTriggerSource) {
                    action.clean();
                }
            }
        }
    } finally {
        if (originTriggerSource) {
            TRIGGER_SOURCE.remove();
            TRIGGER_SOURCE_LAST.remove();
        }
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) RebuildException(com.rebuild.core.RebuildException) MissingMetaExcetion(cn.devezhao.persist4j.metadata.MissingMetaExcetion) ID(cn.devezhao.persist4j.engine.ID) RepeatedRecordsException(com.rebuild.core.service.general.RepeatedRecordsException)

Example 10 with ExpressionRuntimeException

use of com.googlecode.aviator.exception.ExpressionRuntimeException in project aviatorscript by killme2008.

the class String2DateFunction method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
    String source = FunctionUtils.getStringValue(arg1, env);
    String format = FunctionUtils.getStringValue(arg2, env);
    SimpleDateFormat dateFormat = DateFormatCache.getOrCreateDateFormat(format);
    try {
        return AviatorRuntimeJavaType.valueOf(dateFormat.parse(source));
    } catch (ParseException e) {
        throw new ExpressionRuntimeException("Cast string to date failed", e);
    }
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)19 Date (java.util.Date)7 Test (org.junit.Test)7 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 HashMap (java.util.HashMap)6 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)4 SimpleDateFormat (java.text.SimpleDateFormat)4 ID (cn.devezhao.persist4j.engine.ID)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 NoSuchPropertyException (com.googlecode.aviator.exception.NoSuchPropertyException)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)1 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)1 Env (com.googlecode.aviator.utils.Env)1 RebuildException (com.rebuild.core.RebuildException)1 RepeatedRecordsException (com.rebuild.core.service.general.RepeatedRecordsException)1