最近想做一個投票節(jié)點,具體需求是這樣的
- 流程同意到一定比例時,往下走
- 如果流程不同意時,即返回指定的任何節(jié)點.
注意:流程引擎是我用flowable進(jìn)行改造的,有些是需要重寫節(jié)點的行為,現(xiàn)在只說關(guān)鍵的地方,供大家參考.
- 流程定義-投票的行為
生成的流程定義如下:
<userTask id="N5" name="審批" flowable:async="true" flowable:assignee="1,2,3">
<multiInstanceLoopCharacteristics isSequential="false" flowable:collection="{starmark_emptycollection}" flowable:elementVariable="starmark_atuser">
<completionCondition>${voteMultiInstanceService.completeTask(execution,'22','N6')}</completionCondition>
</multiInstanceLoopCharacteristics>
</userTask>
其中voteMultiInstanceService是一個決策判斷的bean類
- 流程判斷是否達(dá)到同意或不同意的標(biāo)準(zhǔn)
@Service("voteMultiInstanceService")
public class VoteMultiInstanceService implements Serializable {
@Autowired
private RuntimeService runtimeService;
public boolean completeTask(DelegateExecution execution, String voteRate, String voteRefuseNode) {
//所有人員審批
int nrOfInstances = (Integer) execution.getVariable("nrOfInstances"); //總的會簽任務(wù)數(shù)量
int nrOfCompletedInstances = (Integer) execution.getVariable("nrOfCompletedInstances"); //總的會簽任務(wù)數(shù)量---已執(zhí)行
int vote_pass_count = execution.getVariable("starmark_vote_pass_count") == null ? 0 : (Integer) execution.getVariable("starmark_vote_pass_count"); //總的會簽任務(wù)數(shù)量---已執(zhí)行的
int voteRateInteger = Integer.parseInt(voteRate);
if (((float) vote_pass_count / nrOfInstances * 100.0) >= voteRateInteger) {
return true;
}
//不同意
if ( ((float)(nrOfCompletedInstances - vote_pass_count) / nrOfInstances) * 100.0 > (100 - voteRateInteger)) {
execution.setVariableLocal("starmark_voteRefuseNode",voteRefuseNode);
return true;
}
return false;
}
}
- 流程不同意時,返回指定的節(jié)點
按上所示,流程不同意時,流程設(shè)置了一個變量starmark_voteRefuseNode,如果該變量有值,就需要流轉(zhuǎn)到指定的節(jié)點
這個時候,需要重寫ParallelMultiInstanceBehavior的cleanupMiRoot,具體重寫如下:
protected void cleanupMiRoot(DelegateExecution execution) {
// Delete multi instance root and all child executions.
// Create a fresh execution to continue
ExecutionEntity multiInstanceRootExecution = (ExecutionEntity) getMultiInstanceRootExecution(execution);
String voteRefuseNode=execution.getVariableLocal("starmark_voteRefuseNode")==null?"":execution.getVariableLocal("starmark_voteRefuseNode")+"";
FlowElement flowElement = multiInstanceRootExecution.getCurrentFlowElement();
ExecutionEntity parentExecution = multiInstanceRootExecution.getParent();
ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
Collection<String> executionIdsNotToSendCancelledEventsFor = execution.isMultiInstanceRoot() ? null : Collections.singletonList(execution.getId());
executionEntityManager.deleteChildExecutions(multiInstanceRootExecution, null, executionIdsNotToSendCancelledEventsFor, DELETE_REASON_END, true, flowElement);
executionEntityManager.deleteRelatedDataForExecution(multiInstanceRootExecution, DELETE_REASON_END);
executionEntityManager.delete(multiInstanceRootExecution);
ExecutionEntity newExecution = executionEntityManager.createChildExecution(parentExecution);
//投票節(jié)點
if(StringUtils.isEmpty(voteRefuseNode)) {
//正常流轉(zhuǎn)
newExecution.setCurrentFlowElement(flowElement);
super.leave(newExecution);
} else {
//不同意時,返回指定節(jié)點
org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(newExecution.getProcessDefinitionId());
FlowElement targetFlowElement = process.getFlowElement(voteRefuseNode);
newExecution.setCurrentFlowElement(targetFlowElement);
FlowableEngineAgenda agenda = CommandContextUtil.getAgenda();
agenda.planContinueProcessInCompensation(newExecution);
}
}
至此,投票功能已完成