activiti构造属于自己的流程定义

简介: 说起actviti,很多人都会说它支持bpmn标准,它的流转都是基于bpmn文件来运行!但我们在设计流程时,流程定义真的只能是bpmn定义吗?  其实不然,activti可以支持任意流程定义,只要你发布流程时,将你的流程定义转成bpmn文件即可! ...

说起actviti,很多人都会说它支持bpmn标准,它的流转都是基于bpmn文件来运行!
但我们在设计流程时,流程定义真的只能是bpmn定义吗?  
其实不然,activti可以支持任意流程定义,只要你发布流程时,将你的流程定义转成bpmn文件即可!   
分析如下:

  1. 表act_re_model是activiti用于存储流程模板的表,其中字段EDITOR_SOURCE_VALUE_ID_,EDITOR_SOURCE_EXTRA_VALUE_ID_是用于提供给用户存储自己的私有定义.
    EDITOR_SOURCE_VALUE_ID一般存自己的定义,EDITOR_SOURCE_EXTRA_VALUE_ID_存流程定义的图片,如activti-explorer就是这么存储,真正使用可以只用其中一个
  2. 表act_ge_bytearray是activti用于存储流程定义,其中name值为source就是对应act_re_model表中EDITOR_SOURCE_VALUE_ID定义存储,值为source-extra为自己的EDITOR_SOURCE_EXTRA_VALUE_ID_定义存储
    现在代码如下
  3. 保存自己的私有流程定义
public class CdpProcessDefintionCreateCommand implements Command<String> {
    private static Logger logger = LoggerFactory.getLogger(CdpProcessDefintionCreateCommand.class);

    private String modelId;
    private String modelName;
    private String flowContent;
 
    @Override
    public String execute(CommandContext commandContext) {
        
        try {
     
        Model model = commandContext.getModelEntityManager().findModelById(modelId);
        String processName = modelName;
        
        if (model == null) {
            model = commandContext.getModelEntityManager().createNewModel();
            ((ModelEntity) model).setId(modelId);
            ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
            modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processName);
            modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
            model.setMetaInfo(modelObjectNode.toString());
            model.setName(processName);
            commandContext.getModelEntityManager().insertModel(model);
            commandContext.getDbSqlSession().flush();
        }   
        //保存流程定义
        commandContext.getModelEntityManager().insertEditorSourceForModel(modelId, flowContent.getBytes("utf-8"));
        //
        return modelId;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new ActivitiException(e.getMessage());
        }
    }
    public CdpProcessDefintionCreateCommand(String modelId, String modelName,String flowContent ) {
        super();
        this.modelId = modelId;
        this.modelName=modelName;
        this.flowContent = flowContent;
         
    }

2.获取自己的私有流程定义

 /**
     * 增加自定义流程定义
     * 
     * @param modelId
     * @return
     */
    public String getProcessDefiniton(String modelId) throws Exception {
        byte[] flowContent= ((RuntimeServiceImpl) runtimeService).getCommandExecutor().execute(new GetModelEditorSourceCmd(modelId));
        return new String(flowContent);
    }
  1. 保存并部署自己的流程定义
public class CdpProcessDefintionDeployCommand implements Command<String> {
    private static Logger logger = LoggerFactory.getLogger(CdpProcessDefintionDeployCommand.class);

    private String modelId;
    private String modelKey;
    private String flowContent;

    private RepositoryService repositoryService;

    @Override
    public String execute(CommandContext commandContext) {
        try {

            BpmnModel bpmnModel = new MxBpmnXMLConverter().convertToBpmnModel(flowContent);
            Model model = commandContext.getModelEntityManager().findModelById(modelId);
            //一定要设置ID,否则发布时这个东西会有问题
            bpmnModel.getMainProcess().setId(modelKey);
            bpmnModel.getProcesses().get(0).setId(modelKey);
            String processName = null;
            if (StringUtils.isNotEmpty(bpmnModel.getMainProcess().getName())) {
                processName = bpmnModel.getMainProcess().getName();
            } else {
                processName = model.getName();
            }
            if (model == null) {
                model = commandContext.getModelEntityManager().createNewModel();
                ((ModelEntity) model).setId(modelId);
                ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
                modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processName);
                modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
                model.setMetaInfo(modelObjectNode.toString());
                model.setName(processName);
                commandContext.getModelEntityManager().insertModel(model);
            } else {
                commandContext.getModelEntityManager().updateModel((ModelEntity) model);
            }
            commandContext.getDbSqlSession().flush();

            commandContext.getModelEntityManager().insertEditorSourceForModel(modelId, flowContent.getBytes("utf-8"));
            commandContext.getDbSqlSession().flush();
            //名称一定要是bpmn20.xml,否则发布不会成功
            String bpmnName = modelKey + ".bpmn20.xml";
            Deployment deployment = repositoryService.createDeployment().name(bpmnName)
                    .addBpmnModel(bpmnName, bpmnModel).deploy();
            //发布完成后,更新表act_re_model的DEPLOYMENT_ID_字段,activti好像不会自动更新该字段,不知道是不是bug
            model.setDeploymentId(deployment.getId());
            commandContext.getModelEntityManager().updateModel((ModelEntity) model);
            return deployment.getId();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("部署流程失败", e);
            throw new ActivitiException(e.getMessage());
        }
    }

    public CdpProcessDefintionDeployCommand(String modelId, String flowContent, String modelKey,
            final RepositoryService repositoryService) {
        super();
        this.modelId = modelId;
        this.flowContent = flowContent;
        this.repositoryService = repositoryService;
        this.modelKey = modelKey;
    }

}

上述代码中,new MxBpmnXMLConverter().convertToBpmnModel(flowContent);需要自己构造转换bpmn对象的方法.
至此,activti构造属于自己流程定义已成功!

相关文章
|
6月前
18activiti - 流程管理定义(删除key相同的所有不同版本的流程定义)
18activiti - 流程管理定义(删除key相同的所有不同版本的流程定义)
21 0
|
6月前
|
XML 缓存 数据格式
12activiti - 流程管理定义(设计流程定义文档)
12activiti - 流程管理定义(设计流程定义文档)
31 0
|
6月前
|
存储 数据库
13activiti - 流程管理定义(部署流程定义)
13activiti - 流程管理定义(部署流程定义)
35 0
|
6月前
15activiti - 流程管理定义(删除流程定义)
15activiti - 流程管理定义(删除流程定义)
54 0
|
6月前
29activiti - 流程变量(总结)
29activiti - 流程变量(总结)
29 0
|
6月前
14activiti - 流程管理定义(查看流程定义)
14activiti - 流程管理定义(查看流程定义)
32 0
|
6月前
25activiti - 流程变量(设置和获取流程变量)
25activiti - 流程变量(设置和获取流程变量)
20 0
|
6月前
28activiti - 流程变量(支持的类型)
28activiti - 流程变量(支持的类型)
16 0
|
6月前
26activiti - 流程变量(模拟流程变量的设置和获取的场景)
26activiti - 流程变量(模拟流程变量的设置和获取的场景)
17 0
|
6月前
|
数据库
23activiti - 流程变量(流程图)
23activiti - 流程变量(流程图)
20 0