AddTaskExecutionCmd.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.flow.flowable.cmd;
  2. import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
  3. import org.flowable.common.engine.api.FlowableException;
  4. import org.flowable.common.engine.impl.interceptor.Command;
  5. import org.flowable.common.engine.impl.interceptor.CommandContext;
  6. import org.flowable.engine.ManagementService;
  7. import org.flowable.engine.RuntimeService;
  8. import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
  9. import org.flowable.engine.impl.util.CommandContextUtil;
  10. import org.flowable.engine.runtime.Execution;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Objects;
  15. public class AddTaskExecutionCmd implements Command<Void> {
  16. protected String processInstanceId;
  17. protected String activityId;
  18. protected String assignee;
  19. public AddTaskExecutionCmd(String processInstanceId, String activityId, String assignee) {
  20. this.processInstanceId = processInstanceId;
  21. this.activityId = activityId;
  22. this.assignee = assignee;
  23. }
  24. @Override
  25. public Void execute(CommandContext commandContext) {
  26. ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
  27. ManagementService managementService = processEngineConfiguration.getManagementService();
  28. MultiInstanceLoopCharacteristics loopCharacteristics = managementService.executeCommand(new GetLoopCharacteristicsCmd(this.processInstanceId, this.activityId));
  29. if (Objects.nonNull(loopCharacteristics)) {
  30. RuntimeService runtimeService = processEngineConfiguration.getRuntimeService();
  31. String collectionName = loopCharacteristics.getInputDataItem();
  32. collectionName = collectionName.substring(2, collectionName.length() - 1);
  33. List<String> assigneeList = runtimeService.getVariable(
  34. this.processInstanceId,
  35. collectionName,
  36. List.class
  37. );
  38. if (Objects.nonNull(assigneeList) && assigneeList.contains(this.assignee)) {
  39. throw new FlowableException("加签用户已存在");
  40. }
  41. assigneeList.add(this.assignee);
  42. runtimeService.setVariable(this.processInstanceId, collectionName, assigneeList);
  43. Execution miExecution = managementService.executeCommand(new GetMultiInstanceRootExecutionCmd(this.processInstanceId, this.activityId));
  44. Integer nrOfInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfInstances", Integer.class);
  45. runtimeService.setVariableLocal(miExecution.getId(), "nrOfInstances", nrOfInstances + 1);
  46. if (!loopCharacteristics.isSequential()) {
  47. Integer nrOfActiveInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfActiveInstances", Integer.class);
  48. runtimeService.setVariableLocal(miExecution.getId(), "nrOfActiveInstances", nrOfActiveInstances + 1);
  49. Map<String, Object> executionVariables = new HashMap<>();
  50. executionVariables.put(loopCharacteristics.getElementVariable(), this.assignee);
  51. runtimeService.addMultiInstanceExecution(
  52. this.activityId,
  53. this.processInstanceId,
  54. executionVariables
  55. );
  56. }
  57. } else {
  58. throw new FlowableException("当前节点不支持加签");
  59. }
  60. return null;
  61. }
  62. }