Преглед изворни кода

新增自定义流程属性-增加动态网关

wangliang пре 3 година
родитељ
комит
7b64f0414e

+ 18 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/activiti/custom/service/AbstractMultiWorkFlowService.java

@@ -19,6 +19,7 @@ import org.springframework.beans.BeanUtils;
 import javax.annotation.Resource;
 import java.text.MessageFormat;
 import java.util.StringJoiner;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * @Description: 并行/串行业务service
@@ -124,4 +125,21 @@ public abstract class AbstractMultiWorkFlowService implements DefaultInstanceCon
         flowTaskLink.add(nodeDefault);
         return customFlowDefaultDto;
     }
+
+    /**
+     * 更新atomicInteger值
+     *
+     * @param atomicInteger
+     * @return
+     */
+    @Override
+    public int getAndIncrement(AtomicInteger atomicInteger) {
+        int value = 1;
+        if (atomicInteger.get() >= Integer.MAX_VALUE) {
+            atomicInteger.compareAndSet(atomicInteger.get(), value);
+        } else {
+            value = atomicInteger.getAndIncrement();
+        }
+        return value;
+    }
 }

+ 6 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/activiti/custom/service/DefaultInstanceConvertToMultiInstance.java

@@ -3,6 +3,8 @@ package com.qmth.distributed.print.business.activiti.custom.service;
 import org.activiti.bpmn.model.MultiInstanceLoopCharacteristics;
 import org.activiti.bpmn.model.UserTask;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 /**
  * @Description: 创建并行/串行业务
  * @Param:
@@ -26,6 +28,10 @@ public interface DefaultInstanceConvertToMultiInstance extends MultiWorkFlow {
     public static final String FLOW_BPMN_PNG_NAME = "-multiple-process-diagram.png";
     public static final String FLOW_BPMN_DEPLOYMENT_NAME = "-multiple process deployment";
     public static final String BPMN_NAME = "bpmn";
+    public static final String GATEWAY_NAME = "gateway";
+    public static final String SEQUENCE_NAME = "sequence";
+    public static AtomicInteger GATEWAY_ID = new AtomicInteger(1);
+    public static AtomicInteger SEQUENCE_ID = new AtomicInteger(1);
 
     /**
      * 创建多实例行为解释器

+ 10 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/activiti/custom/service/MultiWorkFlow.java

@@ -4,6 +4,8 @@ import com.qmth.distributed.print.business.bean.flow.CustomFlowDto;
 import com.qmth.distributed.print.business.bean.flow.link.FlowTaskLink;
 import org.activiti.bpmn.model.UserTask;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 /**
  * @Description: 并行/串行业务
  * @Param:
@@ -30,4 +32,12 @@ public interface MultiWorkFlow {
      * @return
      */
     CustomFlowDto createDefaultUserTask(CustomFlowDto customFlowDto, FlowTaskLink flowTaskLink, String id);
+
+    /**
+     * 更新atomicInteger值
+     *
+     * @param atomicInteger
+     * @return
+     */
+    int getAndIncrement(AtomicInteger atomicInteger);
 }

+ 11 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/bean/flow/CustomFlowDto.java

@@ -45,6 +45,17 @@ public class CustomFlowDto implements Serializable {
     @ApiModelProperty(value = "流程属性id")
     String flowTaskId;
 
+    public CustomFlowDto() {
+
+    }
+
+    public CustomFlowDto(String id, CustomFlowTypeEnum type, String content, String flowTaskId) {
+        this.id = id;
+        this.type = type;
+        this.content = content;
+        this.flowTaskId = flowTaskId;
+    }
+
     public String getFlowTaskId() {
         return flowTaskId;
     }

+ 3 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/enums/CustomFlowDynamicBuildEnum.java

@@ -15,7 +15,9 @@ public enum CustomFlowDynamicBuildEnum {
 
     USER_TASK("usertask", "任务节点"),
 
-    GATE_WAY("exclusivegateway", "网关"),
+    PARALLEL_GATE_WAY("parallelgateway", "并行网关"),
+
+    EXCLUSIVE_GATE_WAY("exclusivegateway", "排他网关"),
 
     SEQUENCE("flow", "箭头"),
 

+ 5 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/enums/CustomFlowTypeEnum.java

@@ -15,7 +15,11 @@ public enum CustomFlowTypeEnum {
 
     PROCESS("过程节点"),
 
-    END("结束节点");
+    END("结束节点"),
+
+    EXCLUSIVE_GATE_WAY("排他网关"),
+
+    PARALLEL_GATE_WAY("并行网关");
 
     private String title;
 

+ 56 - 7
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ActivitiServiceImpl.java

@@ -1483,6 +1483,8 @@ public class ActivitiServiceImpl implements ActivitiService {
         Map<CustomFlowTypeEnum, CustomFlowDto> customFlowTypeEnumCustomFlowDtoMap = new HashMap<>();
         FlowTaskLink flowTaskLink = new FlowTaskLink();
         Map<String, Object> flowProcessVarMap = new HashMap<>();
+        String exclusiveGatewayId = null;
+        Map<String, Map<String, List<String>>> gatewayMap = new HashMap<>();
         for (int i = 0; i < customFlowLists.size(); i++) {
             CustomFlowDto customFlowDto = customFlowLists.get(i);
             FlowTaskNode node = new FlowTaskNode(customFlowDto);
@@ -1535,6 +1537,15 @@ public class ActivitiServiceImpl implements ActivitiService {
                         //驳回属性
                         switch (customFlowPropertyDto.getRejectType()) {
                             case PREV://上一节点
+                                exclusiveGatewayId = DefaultInstanceConvertToMultiInstance.GATEWAY_NAME + multiWorkFlow.getAndIncrement(DefaultInstanceConvertToMultiInstance.GATEWAY_ID);
+                                CustomFlowDto customFlowExclusiveGatewayDto = new CustomFlowDto(exclusiveGatewayId, CustomFlowTypeEnum.EXCLUSIVE_GATE_WAY, CustomFlowTypeEnum.EXCLUSIVE_GATE_WAY.getTitle(), CustomFlowDynamicBuildEnum.EXCLUSIVE_GATE_WAY.getId() + exclusiveGatewayId);
+                                FlowTaskNode exclusiveGatewayNode = new FlowTaskNode(customFlowExclusiveGatewayDto);
+                                flowTaskLink.add(exclusiveGatewayNode);
+                                Map<String, List<String>> sequenceFlowMap = new HashMap<>();
+                                sequenceFlowMap.put(CustomFlowElementEnum.REJECT.getId(), Arrays.asList(node.getBefore().getTask().getFlowTaskId()));
+                                sequenceFlowMap.put(CustomFlowTypeEnum.EXCLUSIVE_GATE_WAY.name(), Arrays.asList(customFlowExclusiveGatewayDto.getFlowTaskId()));
+                                gatewayMap.put(customFlowDto.getFlowTaskId(), sequenceFlowMap);
+                                process.addFlowElement(createExclusiveGateway(customFlowExclusiveGatewayDto.getFlowTaskId(), CustomFlowTypeEnum.EXCLUSIVE_GATE_WAY.getTitle()));
                                 break;
                             case START://发起人节点
                                 break;
@@ -1558,7 +1569,6 @@ public class ActivitiServiceImpl implements ActivitiService {
                         }
                     }
                     process.addFlowElement(createUserTask(CustomFlowDynamicBuildEnum.USER_TASK.getId() + i, Objects.nonNull(customFlowDto.getContent()) && !Objects.equals(customFlowDto.getContent(), "") ? customFlowDto.getContent() : DefaultInstanceConvertToMultiInstance.USER_TASK_APPROVE_NAME, approveUserIds, customFlowPropertyDto.getMultipleUserApproveType()));
-                    //提取过程节点属性
                     break;
                 case END://结束节点
                     if (customFlowTypeEnumCustomFlowDtoMap.containsKey(CustomFlowTypeEnum.END)) {
@@ -1574,11 +1584,30 @@ public class ActivitiServiceImpl implements ActivitiService {
             }
         }
 
-        for (int i = 1; i < flowTaskLink.size() - 1; i++) {
+        String normalSequenceId = null;
+        for (int i = 1; i < flowTaskLink.size(); i++) {
             FlowTaskNode flowTaskNode = flowTaskLink.get(i);
-            process.addFlowElement(createSequenceFlow(flowTaskNode.getBefore().getTask().getFlowTaskId(), flowTaskNode.getTask().getFlowTaskId(), "", ""));
+            normalSequenceId = DefaultInstanceConvertToMultiInstance.SEQUENCE_NAME + multiWorkFlow.getAndIncrement(DefaultInstanceConvertToMultiInstance.SEQUENCE_ID);
+            log.info("start:{},end:{}", flowTaskNode.getBefore().getTask().getFlowTaskId(), flowTaskNode.getTask().getFlowTaskId());
+            process.addFlowElement(createSequenceFlow(flowTaskNode.getBefore().getTask().getFlowTaskId(), flowTaskNode.getTask().getFlowTaskId(), normalSequenceId, normalSequenceId, null));
+            if (Objects.nonNull(gatewayMap.get(flowTaskNode.getTask().getFlowTaskId()))) {
+                Map<String, List<String>> map = gatewayMap.get(flowTaskNode.getTask().getFlowTaskId());
+                List<String> gatewayList = map.get(CustomFlowTypeEnum.EXCLUSIVE_GATE_WAY.name());
+                map.remove(CustomFlowTypeEnum.EXCLUSIVE_GATE_WAY.name());
+                if (Objects.nonNull(map) && map.size() > 0) {
+                    map.forEach((k, v) -> {
+                        String gatewaySequenceId = null;
+                        if (Objects.equals(k, CustomFlowElementEnum.REJECT.getId())) {
+                            for (String s : v) {
+                                gatewaySequenceId = DefaultInstanceConvertToMultiInstance.SEQUENCE_NAME + multiWorkFlow.getAndIncrement(DefaultInstanceConvertToMultiInstance.SEQUENCE_ID);
+                                log.info("start:{},end:{}", gatewayList.get(0), s);
+                                process.addFlowElement(createSequenceFlow(gatewayList.get(0), s, gatewaySequenceId, gatewaySequenceId, null));
+                            }
+                        }
+                    });
+                }
+            }
         }
-        process.addFlowElement(createSequenceFlow(flowTaskLink.getLast().getBefore().getTask().getFlowTaskId(), flowTaskLink.getLast().getTask().getFlowTaskId(), "", ""));
 
         //生成图像信息
         new BpmnAutoLayout(model).execute();
@@ -1676,26 +1705,46 @@ public class ActivitiServiceImpl implements ActivitiService {
     }
 
     /**
-     * @param id 网关id
+     * 排他网关
+     *
+     * @param id
+     * @param name
      * @return
      */
-    protected ExclusiveGateway createExclusiveGateway(String id) {
+    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 parallelGateway = new ParallelGateway();
+        parallelGateway.setId(id);
+        parallelGateway.setName(name);
+        return parallelGateway;
+    }
+
     /**
      * @param from                连线来源节点
      * @param to                  连线目标节点
+     * @param id
      * @param name                连线名称(可不填)
      * @param conditionExpression 网关每一种线路走向的条件表达式
      * @return
      */
-    protected SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression) {
+    protected SequenceFlow createSequenceFlow(String from, String to, String id, String name, String conditionExpression) {
         SequenceFlow flow = new SequenceFlow();
         flow.setSourceRef(from);
         flow.setTargetRef(to);
+        flow.setId(id);
         flow.setName(name);
         if (Objects.nonNull(conditionExpression)) {
             flow.setConditionExpression(conditionExpression);