Search in sources :

Example 1 with RepeatedRecordsException

use of com.rebuild.core.service.general.RepeatedRecordsException 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 2 with RepeatedRecordsException

use of com.rebuild.core.service.general.RepeatedRecordsException in project rebuild by getrebuild.

the class ModelExtrasController method transform.

// 记录转换
@RequestMapping("transform")
public RespBody transform(HttpServletRequest request) {
    ID transid = getIdParameterNotNull(request, "transid");
    ID sourceRecord = getIdParameterNotNull(request, "source");
    ID mainid = getIdParameter(request, "mainid");
    ConfigBean config = TransformManager.instance.getTransformConfig(transid, null);
    Entity targetEntity = MetadataHelper.getEntity(config.getString("target"));
    RecordTransfomer transfomer = new RecordTransfomer(targetEntity, (JSONObject) config.getJSON("config"));
    if (!transfomer.checkFilter(sourceRecord)) {
        return RespBody.error(Language.L("当前记录不符合转换条件"), 400);
    }
    try {
        ID newId = transfomer.transform(sourceRecord, mainid);
        return RespBody.ok(newId);
    } catch (Exception ex) {
        log.warn(">>>>> {}", ex.getLocalizedMessage());
        String detail = ex.getLocalizedMessage();
        if (ex instanceof RepeatedRecordsException) {
            detail = Language.L("存在重复记录");
        }
        return RespBody.errorl("记录转换失败 (%s)", detail);
    }
}
Also used : EasyEntity(com.rebuild.core.metadata.easymeta.EasyEntity) Entity(cn.devezhao.persist4j.Entity) ID(cn.devezhao.persist4j.engine.ID) ConfigBean(com.rebuild.core.configuration.ConfigBean) RecordTransfomer(com.rebuild.core.service.general.transform.RecordTransfomer) RepeatedRecordsException(com.rebuild.core.service.general.RepeatedRecordsException) RepeatedRecordsException(com.rebuild.core.service.general.RepeatedRecordsException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ID (cn.devezhao.persist4j.engine.ID)2 RepeatedRecordsException (com.rebuild.core.service.general.RepeatedRecordsException)2 Entity (cn.devezhao.persist4j.Entity)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 RebuildException (com.rebuild.core.RebuildException)1 ConfigBean (com.rebuild.core.configuration.ConfigBean)1 EasyEntity (com.rebuild.core.metadata.easymeta.EasyEntity)1 RecordTransfomer (com.rebuild.core.service.general.transform.RecordTransfomer)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1