Activiti7通过代码动态生成工作流实现详解

  List roles=new ArrayList<>();

  roles.add("1508574");

  roles.add("13765234");

  roles.add("18834222");

  listMap.put("province",roles);

  for (String auditRole : processParamsDto.getAuditRoles()) {

  if (auditRole.equals(processParamsDto.getAuditRoles().get(0))) {

  //开始连线

  process.addFlowElement(createUserTask("task".concat("_").concat(auditRole), "审批".concat(auditRole), auditRole));

  process.addFlowElement(createSequenceFlow("start", "task".concat("_").concat(auditRole), null));

  //正常的 第一个网关 process.addFlowElement(createExclusiveGateway("exclusiveGateWay".concat("_").concat(auditRole), "exclusiveGateWay".concat("_").concat(auditRole)));

  //第一个排他网关连线 任务->排他网关

  process.addFlowElement(createSequenceFlow("task".concat("_").concat(auditRole), "exclusiveGateWay".concat("_").concat(auditRole), null));

  //重新申请分支 创建重新申请任务

  process.addFlowElement(createUserTask("task".concat("_").concat("重新申请"), "指定人".concat("审批"), "${startBy}"));

  //申请网关->申请任务

  process.addFlowElement(createSequenceFlow("exclusiveGateWay".concat("_").concat(auditRole), "task".concat("_").concat("重新申请"), "${flag==false}"));

  //申请网关

  process.addFlowElement(createExclusiveGateway("exclusiveGateWay".concat("_").concat("重新申请"), "exclusiveGateWay".concat("_").concat("重新申请")));

  //申请任务->申请网关

  process.addFlowElement(createSequenceFlow("task".concat("_").concat("重新申请"),"exclusiveGateWay".concat("_").concat("重新申请"), null));

  } else if (!auditRole.equals(processParamsDto.getAuditRoles().get(processParamsDto.getAuditRoles().size() - 1))) {

  //创建排他网关 每个任务后都有一个排他网关

  //第二个任务至第size-1个任务

  process.addFlowElement(createUserTask("task".concat("_").concat(auditRole), "审批".concat(auditRole), auditRole));

  //第一个网关->第二个任务 任务之前的节点 网关->第二个任务....第N个任务 true

  process.addFlowElement(createSequenceFlow("exclusiveGateWay".concat("_").concat(getPreAuditRole(auditRole,processParamsDto.getAuditRoles())), "task".concat("_").concat(auditRole), "${flag==true}"));

  //网关->申请任务 false

  process.addFlowElement(createExclusiveGateway("exclusiveGateWay".concat("_").concat(auditRole), "exclusiveGateWay(当前)".concat("_").concat(auditRole)));

  process.addFlowElement(createSequenceFlow("task".concat("_").concat(auditRole), "exclusiveGateWay".concat("_").concat(auditRole), null));

  //排他网关—>重新申请任务

  process.addFlowElement(createSequenceFlow("exclusiveGateWay".concat("_").concat(auditRole), "task".concat("_").concat("重新申请"),"#{flag==false}"));

  } else if (auditRole.equals(processParamsDto.getAuditRoles().get(processParamsDto.getAuditRoles().size() - 1))) {

  String lastNode=processParamsDto.getAuditRoles().get(processParamsDto.getAuditRoles().size() - 1);

  System.out.println("当前节点"+lastNode);

  process.addFlowElement(createUserTask("task".concat(lastNode), "审批".concat(auditRole), auditRole));

  process.addFlowElement(createSequenceFlow("exclusiveGateWay".concat("_").concat(getPreAuditRole(auditRole,processParamsDto.getAuditRoles())),"task".concat(lastNode),"#{flag==true}"));

  process.addFlowElement(createEndEvent());

  process.addFlowElement(createSequenceFlow("task".concat(processParamsDto.getAuditRoles().get(processParamsDto.getAuditRoles().size() - 1)), "end", "${flag==true}")); process.addFlowElement(createSequenceFlow("exclusiveGateWay".concat("_").concat("重新申请"), "end", "${flag==false}"));

  process.addFlowElement(createSequenceFlow("exclusiveGateWay".concat("_").concat("重新申请"), "task".concat("_").concat(processParamsDto.getAuditRoles().get(0)), "${flag==true}"));

  }

  }

  new BpmnAutoLayout(model).execute();

  //定义并设置流程变量

  Map variables = new HashMap<>();

  variables.put("flag", 1 == 2);

  //deploy

  Deployment deployment = repositoryService.createDeployment().addBpmnModel("process/dynamic-model.bpmn", model).name("Dynamic process deployment").key("test_bpmn").deploy();

  processEngine.getRuntimeService().startProcessInstanceByKey(processDefinition.getKey(), variables);

  InputStream inputStreamXml = processEngine.getRepositoryService().getResourceAsStream(deployment.getId(), "process/dynamic-model.bpmn");

  //保存到本地,方便查看生成后的文件

  FileUtils.copyInputStreamToFile(inputStreamXml, new File("D:bpmn_dataprocess.bpmn.xml"));