Activiti源码跟踪之模型Model操作
模型model设计到的表ACT_RE_MODEL、ACT_GE_BYTEARRAY
ACT_RE_MODEL表结构:
java
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的有两个。
java
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;
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方法:
java
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事件:
java
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方法:
java
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
java
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
AddEditorSourceForModelCmd:
java
public Object execute(CommandContext commandContext) {
commandContext
.getModelEntityManager()
.insertEditorSourceForModel(modelId, bytes);
return null;
}
}
insertEditorSourceForModel
java
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