• Activiti源码跟踪之模型Model操作


    Activiti源码跟踪之模型Model操作

    模型model设计到的表ACT_RE_MODEL、ACT_GE_BYTEARRAY

    ACT_RE_MODEL表结构:

    CREATE TABLE `ACT_RE_MODEL` (
      `ID_` varchar(64) COLLATE utf8_bin NOT NULL,
      `REV_` int(11) DEFAULT NULL,
      `NAME_` varchar(255) COLLATE utf8_bin DEFAULT NULL,
      `KEY_` varchar(255) COLLATE utf8_bin DEFAULT NULL,
      `CATEGORY_` varchar(255) COLLATE utf8_bin DEFAULT NULL,
      `CREATE_TIME_` timestamp NULL DEFAULT NULL,
      `LAST_UPDATE_TIME_` timestamp NULL DEFAULT NULL,
      `VERSION_` int(11) DEFAULT NULL,
      `META_INFO_` varchar(4000) COLLATE utf8_bin DEFAULT NULL,
      `DEPLOYMENT_ID_` varchar(64) COLLATE utf8_bin DEFAULT NULL,
      `EDITOR_SOURCE_VALUE_ID_` varchar(64) COLLATE utf8_bin DEFAULT NULL,
      `EDITOR_SOURCE_EXTRA_VALUE_ID_` varchar(64) COLLATE utf8_bin DEFAULT NULL,
      `TENANT_ID_` varchar(255) COLLATE utf8_bin DEFAULT '',
      PRIMARY KEY (`ID_`),
      KEY `ACT_FK_MODEL_SOURCE` (`EDITOR_SOURCE_VALUE_ID_`),
      KEY `ACT_FK_MODEL_SOURCE_EXTRA` (`EDITOR_SOURCE_EXTRA_VALUE_ID_`),
      KEY `ACT_FK_MODEL_DEPLOYMENT` (`DEPLOYMENT_ID_`),
      CONSTRAINT `ACT_FK_MODEL_DEPLOYMENT` FOREIGN KEY (`DEPLOYMENT_ID_`) REFERENCES `ACT_RE_DEPLOYMENT` (`ID_`),
      CONSTRAINT `ACT_FK_MODEL_SOURCE` FOREIGN KEY (`EDITOR_SOURCE_VALUE_ID_`) REFERENCES `ACT_GE_BYTEARRAY` (`ID_`),
      CONSTRAINT `ACT_FK_MODEL_SOURCE_EXTRA` FOREIGN KEY (`EDITOR_SOURCE_EXTRA_VALUE_ID_`) REFERENCES `ACT_GE_BYTEARRAY` (`ID_`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    其中ACT_RE_MODEL有三个外键,对应ACT_GE_BYTEARRAY的有两个。

    CREATE TABLE `ACT_GE_BYTEARRAY`  (
      `ID_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
      `REV_` int NULL DEFAULT NULL,
      `NAME_` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
      `DEPLOYMENT_ID_` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
      `BYTES_` longblob NULL,
      `GENERATED_` tinyint NULL DEFAULT NULL,
      PRIMARY KEY (`ID_`) USING BTREE,
      INDEX `ACT_FK_BYTEARR_DEPL`(`DEPLOYMENT_ID_`) USING BTREE,
      CONSTRAINT `ACT_FK_BYTEARR_DEPL` FOREIGN KEY (`DEPLOYMENT_ID_`) REFERENCES `ACT_RE_DEPLOYMENT` (`ID_`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    EDITOR_SOURCE_VALUE_ID_对应ACT_GE_BYTEARRAY的ID_,表示该模型对应的模型文件(json格式数据)。repositoryService.addModelEditorSource方法实现。

    EDITOR_SOURCE_EXTRA_VALUE_ID_对应ACT_GE_BYTEARRAY的ID_,表示该模型生成的图片文件。repositoryService.addModelEditorSourceExtra方法实现。

    1、保存模型:

    Model modelData = repositoryService.newModel();
    repositoryService.saveModel(modelData);

    执行对应的SaveModelCmd。会插入或更新ACT_RE_MODEL的数据。

    SaveModelCmd对应的execute方法:

    public Void execute(CommandContext commandContext) {
        if(model == null) {
          throw new ActivitiIllegalArgumentException("model is null");
        }
        if (model.getId() == null) {
          commandContext.getModelEntityManager().insertModel(model);
        } else {
          commandContext.getModelEntityManager().updateModel(model);
        }
        return null;
      }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    执行对应的insert或update方法:
    insert方法分发ENTITY_CREATED和ENTITY_INITIALIZED事件:

      public void insertModel(Model model) {
        ((ModelEntity) model).setCreateTime(Context.getProcessEngineConfiguration().getClock().getCurrentTime());
        ((ModelEntity) model).setLastUpdateTime(Context.getProcessEngineConfiguration().getClock().getCurrentTime());
        getDbSqlSession().insert((PersistentObject) model);
        
        if(Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
        			ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, model));
        	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
        			ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_INITIALIZED, model));
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    update方法分发ENTITY_UPDATED方法:

    public void updateModel(ModelEntity updatedModel) {
        CommandContext commandContext = Context.getCommandContext();
        updatedModel.setLastUpdateTime(Context.getProcessEngineConfiguration().getClock().getCurrentTime());
        DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
        dbSqlSession.update(updatedModel);
        
        if(Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
        			ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_UPDATED, updatedModel));
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、保存其他信息(模型文件或图片)

    保存模型文件(json格式)。涉及到的表:ACT_RE_MODEL、ACT_GE_BYTEARRAY

    repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
    
    • 1

    AddEditorSourceForModelCmd:

    public Object execute(CommandContext commandContext) {
        commandContext
          .getModelEntityManager()
          .insertEditorSourceForModel(modelId, bytes);
        
        return null;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    insertEditorSourceForModel

    public void insertEditorSourceForModel(String modelId, byte[] modelSource) {
        ModelEntity model = findModelById(modelId);
        if (model != null) {
          ByteArrayRef ref = new ByteArrayRef(model.getEditorSourceValueId());
          ref.setValue("source", modelSource);
          
          if (model.getEditorSourceValueId() == null) {
            model.setEditorSourceValueId(ref.getId());
            updateModel(model);
          }
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其中ref.setValue(“source”, modelSource)方法对

    model.getEditorSourceValueId() == null 生成DbSqlSession的inser语句,最后执行updateModel方法
    model.getEditorSourceValueId() != null ,ByteArrayRef.ensureInitialized()方法调用DbSqlSession.selectById(),ByteArrayEntity存放在canche。ByteArrayRef.setBytes(bytes); 把新的byte值设进 ByteArrayEntity
    DbSqlSession.flush()的getUpdatedObjects()方法,比较persistentObject对象,生成update

  • 相关阅读:
    【JVM学习笔记】内存回收与内存回收算法 就哪些地方需要回收、什么时候回收、如何回收三个问题进行分析和说明
    Python3数据分析与挖掘建模(1)python数据分析的流程与概述
    【eProsima Fast DDS(1)】安装eProsima Fast DDS
    ProxySQL + Mysql MGR实现数据库高可用
    非对称加密——网络安全
    网格化微型空气质量站是什么?系统参数?工作特点?
    HTML CSS大学生期末网页大作业 DW个人网页设计 人物介绍 历史人物岳飞介绍
    Android学习笔记 74. 调试程序
    【雷达信号处理基础】第1讲 -- 雷达系统概述
    1045 Favorite Color Stripe
  • 原文地址:https://blog.csdn.net/weixin_45817985/article/details/132851359