flwoable如何实现后加签(动态添加节点)

4 篇文章 0 订阅
1 篇文章 0 订阅

flowable的功能基本都是基于命令模式实现的,flowalble提供了一个方法,可以调用我们自己编写的命令。

ManagementService managementService;这个service里有个executeCommand方法 入参为Command,今天就是基于这个方法来实现后加签的。有兴趣的同学还可以看下这个service的其它方法。

AbstractDynamicInjectionCmd 由flowable提供的基础命令,修改流程的基类。

下面是实现后加签的命令源码 自定义后加签cmd

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.*;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.cmd.AbstractDynamicInjectionCmd;
import org.flowable.engine.impl.dynamic.BaseDynamicSubProcessInjectUtil;
import org.flowable.engine.impl.persistence.entity.DeploymentEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.TaskService;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;

import java.util.Collections;
import java.util.List;

/**
 * 后加签
 */
@Slf4j
@AllArgsConstructor
public class AfterSignUserTaskCmd extends AbstractDynamicInjectionCmd implements Command<Void>  {
  //流程实例id
  protected String processInstanceId;

  //后加签的节点信息
  private DynamicUserTaskBuilder signUserTaskBuilder;

  //当前流程节点的id
  private String taskId;

  @Override
  public Void execute(CommandContext commandContext) {
    //AbstractDynamicInjectionCmd提供的修改方法入口
    createDerivedProcessDefinitionForProcessInstance(commandContext, processInstanceId);
    return null;
  }

  @Override
  protected void updateBpmnProcess(CommandContext commandContext, Process process,
                                   BpmnModel bpmnModel, ProcessDefinitionEntity originalProcessDefinitionEntity, DeploymentEntity newDeploymentEntity) {
    
    //找到当前节点的实体
    TaskService taskService = CommandContextUtil.getTaskService(commandContext);
    TaskEntity taskEntity = taskService.getTask(taskId);
    if(taskEntity==null){
      throw new FlowableObjectNotFoundException("task:"+taskId+" not found");
    }
    //查找当前节点对应的执行执行实体(感觉兴趣的可以搜下流程执行的实例信息 表为ACT_RU_EXECUTION)
    ExecutionEntity currentExecutionEntity = CommandContextUtil.getExecutionEntityManager(commandContext).findById(taskEntity.getExecutionId());
    if(currentExecutionEntity==null){
      throw new FlowableObjectNotFoundException("task:"+taskId+",execution:"+taskEntity.getExecutionId()+" not found");
    }
    //定义的节点id
    String activityId = currentExecutionEntity.getActivityId();
    FlowElement flowElement = process.getFlowElement(activityId,true);
    if(!(flowElement instanceof Task)){
      throw new FlowableException("task type error");
    }
    Activity activity = (Activity) flowElement;
    //由于是后加签所以需要获取当前流程节点流出的sequence
    SequenceFlow currentTaskOutSequenceFlow = activity.getOutgoingFlows().get(0);

    //定义新的节点
    UserTask addUserTask = new UserTask();
    
    //流程的id 可以在调用的时候传入 也可以根据flwoable提供的方法生成
    String id=signUserTaskBuilder.getId();
    if (id == null) {
      id=signUserTaskBuilder.nextTaskId(process.getFlowElementMap());
    }
    addUserTask.setId(id);
    addUserTask.setName(signUserTaskBuilder.getName());
    addUserTask.setAssignee(signUserTaskBuilder.getAssignee());


    process.addFlowElement(addUserTask);
    signUserTaskBuilder.setDynamicTaskId(id);

    //定义新的sequence 将节点的source改为新的节点  taget 改为当前节点的target
    String flowId = "sequenceFlow-"+CommandContextUtil.getProcessEngineConfiguration(commandContext).getIdGenerator().getNextId();
    SequenceFlow newSequenceFlow = this.createSequenceFlow(addUserTask.getId(),currentTaskOutSequenceFlow.getTargetFlowElement().getId());
    newSequenceFlow.setId(flowId);
    process.addFlowElement(newSequenceFlow);


    activity.setOutgoingFlows(Collections.singletonList(newSequenceFlow));
    //修改原有sequence 将原来的sequence的taget改为新的节点
    currentTaskOutSequenceFlow.setTargetFlowElement(addUserTask);
    currentTaskOutSequenceFlow.setTargetRef(addUserTask.getId());


    //clean清除原有的一些图的坐标信息
    bpmnModel.getLocationMap().clear();
    bpmnModel.getFlowLocationMap().clear();
    bpmnModel.getLabelLocationMap().clear();
    //当前流程的重新定义
    BaseDynamicSubProcessInjectUtil.processFlowElements(commandContext, process, bpmnModel, originalProcessDefinitionEntity, newDeploymentEntity);

  }

  //这个方法也可以单独写在一个工具类里
  public  SequenceFlow createSequenceFlow(String from, String to) {
    SequenceFlow flow = new SequenceFlow();
    flow.setSourceRef(from);
    flow.setTargetRef(to);
    return flow;
  }
 


命令的具体调用

DynamicUserTaskBuilder taskBuilder = new DynamicUserTaskBuilder();
taskBuilder.setName(processTransferDto.getTransferType().getDesc()+nodeTask.getName());
taskBuilder.setId(generateTaskId(isJoinNode));
taskBuilder.setAssignee(Long.toString(processTransferDto.getAssignee()));
managementService.executeCommand(new AfterSignUserTaskCmd(nodeTask.getProcessInstanceId(),taskBuilder,nodeTask.getId()));

写的比较粗糙。

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值