update - 处理多实例自动跳过功能,未完成

This commit is contained in:
wangli 2023-07-28 20:33:56 +08:00
parent 590e54e0bd
commit 72cf5dd8d2
5 changed files with 84 additions and 12 deletions

View File

@ -333,7 +333,8 @@ public class BpmTransformUtil {
createTaskListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
// FIXME 如果监听器想设置多个,这里还需要额外处理
createTaskListener.setImplementation("${engineTaskEventListener}");
userTask.setTaskListeners(Arrays.asList(createTaskListener));
List<FlowableListener> taskListeners = new ArrayList<>();
taskListeners.add(createTaskListener);
if ("root".equalsIgnoreCase(id)) {
} else {
ArrayList<FlowableListener> listeners = new ArrayList<>();
@ -371,12 +372,22 @@ public class BpmTransformUtil {
// 设置审批人为空时,允许自动通过
if (!ObjectUtils.isEmpty(flowNode.getProperty()) && flowNode.getProperty().getAllowSkip()) {
userTask.setSkipExpression("${" + BPM_ALLOW_SKIP_USER_TASK + "}");
FlowableListener autoSkipRecordTaskListener = new FlowableListener();
autoSkipRecordTaskListener.setEvent(TaskListener.EVENTNAME_COMPLETE);
autoSkipRecordTaskListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
// FIXME 如果监听器想设置多个,这里还需要额外处理
autoSkipRecordTaskListener.setImplementation("${engineAutoSkipTaskEventListener}");
taskListeners.add(autoSkipRecordTaskListener);
}
if (!ObjectUtils.isEmpty(flowNode.getProperty()) && StringUtils.isNotBlank(flowNode.getProperty().getFormKey())) {
userTask.setFormKey(flowNode.getProperty().getFormKey());
}
}
userTask.setTaskListeners(taskListeners);
}
return id;
}
}

View File

@ -7,10 +7,13 @@ import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.FlowableMultiInstanceActivityEvent;
import org.flowable.engine.impl.bpmn.helper.SkipExpressionUtil;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@ -34,10 +37,10 @@ public class EngineActivityEventListener extends AbstractFlowableEventListener {
private ObjectProvider<BpmActivityEventListener> listener;
@Resource
@Lazy
private RuntimeService runtimeService;
private RepositoryService repositoryService;
@Resource
@Lazy
private RepositoryService repositoryService;
private RuntimeService runtimeService;
@Override
public Collection<? extends FlowableEventType> getTypes() {
@ -56,17 +59,17 @@ public class EngineActivityEventListener extends AbstractFlowableEventListener {
.singleResult();
FlowElement flowElement =
repositoryService.getBpmnModel(activityEvent.getProcessDefinitionId()).getFlowElement(activityEvent.getActivityId());
if (Objects.nonNull(execution)) {
execution.setVariableLocal("loopCounter", -1);
if (flowElement instanceof UserTask) {
String assignee = ((UserTask) flowElement).getAssignee();
if (assignee.contains("${")) {
String replace = assignee.replace("${", "").replace("}", "");
execution.setTransientVariableLocal(replace, "");
}
if (Objects.nonNull(execution) && flowElement instanceof UserTask) {
UserTask userTask = (UserTask) flowElement;
CommandContext commandContext = CommandContextUtil.getCommandContext();
boolean skipUserTask = SkipExpressionUtil.isSkipExpressionEnabled(userTask.getSkipExpression(),
userTask.getId(), execution, commandContext)
&& SkipExpressionUtil.shouldSkipFlowElement(userTask.getSkipExpression(), userTask.getId(),
execution, commandContext);
if (skipUserTask) {
execution.setVariableLocal("loopCounter", -1);
}
}
} else {
listener.ifAvailable(i -> i.onEvent(event));
}

View File

@ -0,0 +1,53 @@
package cn.axzo.workflow.core.service.engine;
import org.flowable.engine.HistoryService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.impl.persistence.entity.HistoricTaskInstanceEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Objects;
/**
* TODO
*
* @author wangli
* @since 2023/7/28 15:28
*/
@Component
public class EngineAutoSkipTaskEventListener implements TaskListener {
@Resource
private HistoryService historyService;
@Resource
private SpringProcessEngineConfiguration engineConfiguration;
@Override
public void notify(DelegateTask delegateTask) {
if (delegateTask.getEventName().equals(TaskListener.EVENTNAME_COMPLETE)) {
HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
HistoricTaskInstanceEntity historicTaskInstance =
(HistoricTaskInstanceEntity) historicTaskInstanceQuery.taskId(delegateTask.getId()).singleResult();
if (Objects.nonNull(historicTaskInstance) && historicTaskInstance.getDeleteReason() == null) {
// String deleteReason = historicTaskInstance.getDeleteReason();
// if (deleteReason == null) {
// historicTaskInstance.
// historicTaskInstance.setDeleteReason("Auto Skipped");
// delegateTask.getProcessEngineServices().getHistoryService()
// .saveHistoricTaskInstance(historicTaskInstance);
// }
TaskEntity taskEntity = new TaskEntityImpl();
engineConfiguration.getInternalHistoryTaskManager().recordTaskCreated(taskEntity);
// historicTaskInstance.setDeleteReason("自动跳过");
// historicTaskService.updateHistoricTask(historicTaskInstance, false);
}
}
}
}

View File

@ -73,6 +73,10 @@ public class EngineExecutionStartListener implements ExecutionListener {
// 设置当前 UserTask 使用的 skip 表达式
execution.setTransientVariable(BPM_ALLOW_SKIP_USER_TASK, true);
}
if (userTask.hasMultiInstanceLoopCharacteristics()) {
//如果是配置了多实例,如果没有获取到审批人时, 就自动跳过任务了, 如果通过该参数, 强制让引擎创建一个任务
execution.setVariableLocal("loopCounter", -1);
}
}
execution.setTransientVariable(variable, assigneeIdList);
});

View File

@ -16,6 +16,7 @@
delegateExpression="${engineExecutionStartListener}"></flowable:executionListener>
<flowable:taskListener event="all"
delegateExpression="${engineTaskEventListener}"></flowable:taskListener>
<modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">
<![CDATA[false]]></modeler:initiator-can-complete>
</extensionElements>