|
@@ -0,0 +1,229 @@
|
|
|
+package com.persagy.fm.workflow.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
+import org.flowable.bpmn.model.Process;
|
|
|
+import org.flowable.bpmn.model.*;
|
|
|
+import org.flowable.engine.RepositoryService;
|
|
|
+import org.flowable.engine.repository.Deployment;
|
|
|
+import org.flowable.validation.ProcessValidator;
|
|
|
+import org.flowable.validation.ProcessValidatorFactory;
|
|
|
+import org.flowable.validation.ValidationError;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 工作流模型调研
|
|
|
+ * @author Charlie Yu
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("workflowmodel")
|
|
|
+public class WorkflowModelController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RepositoryService repositoryService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动流程实例
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "create")
|
|
|
+ public String createModel() {
|
|
|
+ Deployment deploy=null;
|
|
|
+ try {
|
|
|
+ BpmnModel bpmnModel=new BpmnModel();
|
|
|
+ //设置流程信息
|
|
|
+ //此信息都可以通过前期自定义数据,使用时再查询
|
|
|
+ Process process = new Process();
|
|
|
+ process.setId("test_model_3");
|
|
|
+ process.setName("测试流程图三");
|
|
|
+ //添加流程节点信息---start
|
|
|
+ //创建数组存储所有流程节点信息
|
|
|
+ List<FlowElement> elementList=new ArrayList<>();
|
|
|
+ //创建开始节点
|
|
|
+ elementList.add(createStartFlowElement("startEvent_id_1", "开始_1"));
|
|
|
+ elementList.add(createEndFlowElement("endEvent_id_1", "结束_1"));
|
|
|
+
|
|
|
+ //查询普通任务节点信息
|
|
|
+ elementList.addAll(findUserTaskElements());
|
|
|
+
|
|
|
+ //把节点放入process
|
|
|
+ elementList.stream().forEach(item -> process.addFlowElement(item));
|
|
|
+
|
|
|
+ //查询各个节点的关系信息,并添加进流程
|
|
|
+ List<SequenceFlow> sequenceFlowList = createSequenceFlow();
|
|
|
+ sequenceFlowList.stream().forEach(item -> process.addFlowElement(item));
|
|
|
+
|
|
|
+ bpmnModel.addProcess(process);
|
|
|
+
|
|
|
+ //校验bpmModel
|
|
|
+ ProcessValidator processValidator=new ProcessValidatorFactory().createDefaultProcessValidator();
|
|
|
+ List<ValidationError> validationErrorList=processValidator.validate(bpmnModel);
|
|
|
+ if (validationErrorList.size()>0){
|
|
|
+ throw new RuntimeException("流程有误,请检查后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileName="model_test_bpmn20.xml";
|
|
|
+
|
|
|
+ //生成自动布局
|
|
|
+/// new BpmnAutoLayout(bpmnModel).execute();
|
|
|
+ deploy =repositoryService.createDeployment().addBpmnModel(fileName, bpmnModel)
|
|
|
+// .tenantId("intelligentAsset")
|
|
|
+ .deploy();
|
|
|
+ }catch (Exception e){
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ }
|
|
|
+ return "ok";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建开始节点信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public FlowElement createStartFlowElement(String id,String name){
|
|
|
+ StartEvent startEvent=new StartEvent();
|
|
|
+ startEvent.setId(id);
|
|
|
+ startEvent.setName(name);
|
|
|
+ return startEvent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建结束节点信息
|
|
|
+ * @param id
|
|
|
+ * @param name
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public FlowElement createEndFlowElement(String id,String name){
|
|
|
+ EndEvent endEvent=new EndEvent();
|
|
|
+ endEvent.setId(id);
|
|
|
+ endEvent.setName(name);
|
|
|
+ return endEvent;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<FlowElement> findUserTaskElements(){
|
|
|
+ List<FlowElement> taskList = new ArrayList<>();
|
|
|
+ taskList.add(createCommonUserTask("userTask_0", "任务0", "zhangsan"));
|
|
|
+ taskList.add(createCommonUserTask("userTask_1", "任务1", "zhangsan"));
|
|
|
+ taskList.add(createMultiUserTask("userTask_2", "任务2"));
|
|
|
+ return taskList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建普通任务节点信息
|
|
|
+ * @param id
|
|
|
+ * @param name
|
|
|
+ * @param assignee
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public FlowElement createCommonUserTask(String id,String name,String assignee){
|
|
|
+ UserTask userTask=new UserTask();
|
|
|
+ userTask.setId(id);
|
|
|
+ userTask.setName(name);
|
|
|
+ userTask.setAssignee(assignee);
|
|
|
+ return userTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建会签节点信息
|
|
|
+ * @param id
|
|
|
+ * @param name
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public FlowElement createMultiUserTask(String id,String name){
|
|
|
+ UserTask userTask=new UserTask();
|
|
|
+ userTask.setId(id);
|
|
|
+ userTask.setName(name);
|
|
|
+ //分配用户
|
|
|
+ userTask.setAssignee("${assignee}");
|
|
|
+ MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics=new MultiInstanceLoopCharacteristics();
|
|
|
+/// multiInstanceLoopCharacteristics.setCollectionString("${collectionList}");
|
|
|
+ //完成条件,默认所有人都完成
|
|
|
+ multiInstanceLoopCharacteristics.setCompletionCondition("${completionCondition}");
|
|
|
+ //元素变量多实例,一般和设置的assignee变量是对应的
|
|
|
+ multiInstanceLoopCharacteristics.setElementVariable("assignee");
|
|
|
+ //集合多实例,用于接收集合数据的表达式
|
|
|
+ multiInstanceLoopCharacteristics.setInputDataItem("${itemList}");
|
|
|
+ userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);
|
|
|
+ return userTask;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询各节点关联流转信息,即流转线
|
|
|
+ */
|
|
|
+ public List<SequenceFlow> createSequenceFlow() {
|
|
|
+ List<SequenceFlow> flowList = new ArrayList<>();
|
|
|
+ flowList.add(createSequenceFlow("sid_0","startEvent_id_1","userTask_0"));
|
|
|
+ flowList.add(createSequenceFlow("sid_1","userTask_0","userTask_1"));
|
|
|
+ flowList.add(createSequenceFlow("sid_2","userTask_1","userTask_2"));
|
|
|
+ flowList.add(createSequenceFlow("sid_3","userTask_2","endEvent_id_1"));
|
|
|
+ return flowList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public SequenceFlow createSequenceFlow(String id, String sourceId, String targetId){
|
|
|
+ return createSequenceFlow(id, null, sourceId, targetId, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绘制节点流转顺序
|
|
|
+ * @param id
|
|
|
+ * @param name
|
|
|
+ * @param targetId
|
|
|
+ * @param sourceId
|
|
|
+ * @param conditionExpression
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public SequenceFlow createSequenceFlow(String id,String name,String sourceId,String targetId,String conditionExpression){
|
|
|
+ SequenceFlow sequenceFlow=new SequenceFlow();
|
|
|
+ sequenceFlow.setId(id);
|
|
|
+ if (ObjectUtils.isNotEmpty(name)){
|
|
|
+ sequenceFlow.setName(name);
|
|
|
+ } else {
|
|
|
+ sequenceFlow.setName("流转");
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(targetId)){
|
|
|
+ sequenceFlow.setTargetRef(targetId);
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(sourceId)){
|
|
|
+ sequenceFlow.setSourceRef(sourceId);
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(conditionExpression)){
|
|
|
+ sequenceFlow.setConditionExpression(conditionExpression);
|
|
|
+ } else {
|
|
|
+ sequenceFlow.setConditionExpression("${a==\"f\"}");
|
|
|
+ }
|
|
|
+ return sequenceFlow;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 排他网关
|
|
|
+ * @param id
|
|
|
+ * @param name
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected ExclusiveGateway createExclusiveGateway(String id, String name) {
|
|
|
+ ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
|
|
|
+ exclusiveGateway.setId(id);
|
|
|
+ exclusiveGateway.setName(name);
|
|
|
+ return exclusiveGateway;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 并行网关
|
|
|
+ * @param id
|
|
|
+ * @param name
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected ParallelGateway createParallelGateway(String id, String name) {
|
|
|
+ ParallelGateway gateway = new ParallelGateway();
|
|
|
+ gateway.setId(id);
|
|
|
+ gateway.setName(name);
|
|
|
+ return gateway;
|
|
|
+ }
|
|
|
+}
|