1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
@Slf4j public class BackOneStepCmd implements Command<Void>, Serializable { private static final long serialVersionUID = 1L;
private Task task; private String backReason;
public BackOneStepCmd(Task task) { this.task = Objects.requireNonNull(task, "Task can not be null"); }
public BackOneStepCmd(Task task, String deleteReason) { this(task); this.backReason = deleteReason; }
@Override public Void execute(CommandContext commandContext) { String processDefinitionId = task.getProcessDefinitionId(); BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionId);
String activityId = task.getTaskDefinitionKey(); UserTask userTask = (UserTask) bpmnModel.getFlowElement(activityId);
if (ActivitiUtil.checkCurrentFlowNodeIsParallelIncomingBaseProcessDefinition(userTask)) { throw new RuntimeException("Current node is parallel gateway, can not execute back cmd"); }
UserTask lastUserTask = ActivitiUtil.getLastUserTask((TaskEntity) task, userTask); log.info("back one step to {}", lastUserTask.getId());
SequenceFlow backStepSequenceFlow = new SequenceFlow(); backStepSequenceFlow.setId(FlowNodeIDFactory.randomSequenceFlowId()); backStepSequenceFlow.setSourceFlowElement(userTask); backStepSequenceFlow.setTargetFlowElement(lastUserTask);
TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager(); TaskEntity taskEntity = taskEntityManager.findById(task.getId());
if (taskEntity.getDelegationState() != null && taskEntity.getDelegationState().equals(DelegationState.PENDING)) { throw new ActivitiException("A delegated task cannot be completed, but should be resolved instead."); }
taskEntityManager.deleteTask(taskEntity, backReason, false, false); ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager(); ExecutionEntity executionEntity = executionEntityManager.findById(taskEntity.getExecutionId());
MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = userTask.getLoopCharacteristics(); if (multiInstanceLoopCharacteristics != null) { if (multiInstanceLoopCharacteristics.isSequential()) { leaveSequentialMultiInstance(executionEntity, backStepSequenceFlow); } else { leaveParallelMultiInstance(executionEntity, backStepSequenceFlow); } } else { leaveUserTask(executionEntity, backStepSequenceFlow); } return null; }
protected void leaveUserTask(ExecutionEntity executionEntity, SequenceFlow backStepSequenceFlow) { executionEntity.setCurrentFlowElement(backStepSequenceFlow); Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true); }
protected void leaveParallelMultiInstance(ExecutionEntity childExecution, SequenceFlow backStepSequenceFlow) { Context.getCommandContext().getHistoryManager().recordActivityEnd(childExecution, null); ExecutionEntity multiInstanceRootExecution = getMultiInstanceRootExecution(childExecution);
childExecution.removeVariableLocal(EActiviti.LOOP_COUNTER); multiInstanceRootExecution.removeVariableLocal(EActiviti.NUMBER_OF_COMPLETED_INSTANCES); multiInstanceRootExecution.removeVariableLocal(EActiviti.NUMBER_OF_ACTIVE_INSTANCES); multiInstanceRootExecution.removeVariableLocal(EActiviti.NUMBER_OF_INSTANCES); this.deleteChildExecutions((ExecutionEntity) multiInstanceRootExecution, false, Context.getCommandContext());
multiInstanceRootExecution.setScope(false); multiInstanceRootExecution.setMultiInstanceRoot(false); multiInstanceRootExecution.setActive(true); this.leaveUserTask(multiInstanceRootExecution, backStepSequenceFlow); }
protected void leaveSequentialMultiInstance(ExecutionEntity childExecution, SequenceFlow backStepSequenceFlow) { Context.getCommandContext().getHistoryManager().recordActivityEnd(childExecution, null); ExecutionEntity multiInstanceRootExecution = getMultiInstanceRootExecution(childExecution);
childExecution.removeVariableLocal(EActiviti.LOOP_COUNTER); multiInstanceRootExecution.removeVariableLocal(EActiviti.NUMBER_OF_COMPLETED_INSTANCES); multiInstanceRootExecution.removeVariableLocal(EActiviti.NUMBER_OF_ACTIVE_INSTANCES); multiInstanceRootExecution.removeVariableLocal(EActiviti.NUMBER_OF_INSTANCES); Context.getCommandContext().getExecutionEntityManager() .deleteChildExecutions((ExecutionEntity) multiInstanceRootExecution, "MI_END", false);
multiInstanceRootExecution.setScope(false); multiInstanceRootExecution.setMultiInstanceRoot(false); multiInstanceRootExecution.setActive(true); this.leaveUserTask(multiInstanceRootExecution, backStepSequenceFlow); }
protected void deleteChildExecutions(ExecutionEntity parentExecution, boolean deleteExecution, CommandContext commandContext) { ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager(); Collection<ExecutionEntity> childExecutions = executionEntityManager .findChildExecutionsByParentExecutionId(parentExecution.getId()); if (CollectionUtil.isNotEmpty(childExecutions)) { for (ExecutionEntity childExecution : childExecutions) { deleteChildExecutions(childExecution, true, commandContext); } }
if (deleteExecution) { executionEntityManager.deleteExecutionAndRelatedData(parentExecution, null, false); } }
protected ExecutionEntity getMultiInstanceRootExecution(ExecutionEntity executionEntity) { ExecutionEntity multiInstanceRootExecution = null; ExecutionEntity currentExecution = executionEntity; while (currentExecution != null && multiInstanceRootExecution == null && currentExecution.getParent() != null) { if (currentExecution.isMultiInstanceRoot()) { multiInstanceRootExecution = currentExecution; } else { currentExecution = currentExecution.getParent(); } } return multiInstanceRootExecution; }
}
|