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;

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

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;
  }

执行对应的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));
    }
  }

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));
    }
  }

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


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

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

AddEditorSourceForModelCmd:

public Object execute(CommandContext commandContext) {
    commandContext
      .getModelEntityManager()
      .insertEditorSourceForModel(modelId, bytes);
    
    return null;
  }
}

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);
      }
    }
  }


其中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


3、导入BPMN模型

可以导入json的格式,需要转化成BpmnModel


如果是导入的文件是json格式的,转化成BpmnModel格式:


Input in = file.getInputStream();
JsonNode modelNode = new ObjectMapper().readTree(in);
BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);

验证的方法代码如下所示:

//验证bpmnModel 是否是正确的bpmn xml文件

ProcessValidatorFactory processValidatorFactory=new ProcessValidatorFactory();

ProcessValidator defaultProcessValidator =processValidatorFactory.createDefaultProcessValidator();

//验证失败信息的封装ValidationError

List validate = defaultProcessValidator.validate(bpmnModel);


需要说明的是:ValidationError封装的是验证信息,如果size0说明,bpmnmodel正确,大于0,说明自定义的bpmnmodel是错误的,不可以使用的。

验证还是很有必要使用的,因为流程部署的时候,我们最好验证一次,没有问题在部署。


参考:

activiti bpmnModel使用http://blog.csdn.net/qq_30739519/article/details/51271580

粗览Activiti Modeler操作和源代码http://blog.sina.com.cn/s/blog_72ef7bea0102vjb8.html 

activiti 5.12中新增的动态bpmn模型部署 http://blog.csdn.net/jackyrongvip/article/details/9256531


你可能感兴趣的:(Activiti工作流,Activiti,模型)