Пример #1
0
  /** 校验目标节点是否可以回退. */
  public boolean validateTargetActivity(String targetActivityId) {
    JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean(JdbcTemplate.class);
    String historyTaskId =
        jdbcTemplate.queryForObject(
            "select id_ from ACT_HI_TASKINST where act_id_=? order by END_TIME_ desc",
            String.class,
            targetActivityId);

    // 先找到历史任务
    HistoricTaskInstanceEntity historicTaskInstanceEntity =
        Context.getCommandContext()
            .getHistoricTaskInstanceEntityManager()
            .findHistoricTaskInstanceById(historyTaskId);

    // 再反向查找历史任务对应的历史节点
    HistoricActivityInstanceEntity historicActivityInstanceEntity =
        getHistoricActivityInstanceEntity(historyTaskId);

    logger.info("historic activity instance is : {}", historicActivityInstanceEntity.getId());

    Graph graph =
        new ActivitiHistoryGraphBuilder(historicTaskInstanceEntity.getProcessInstanceId()).build();

    Node node = graph.findById(historicActivityInstanceEntity.getId());

    if (!this.checkCouldRollback(node)) {
      logger.info("cannot rollback {}", taskId);

      return false;
    }

    return true;
  }
Пример #2
0
  public String findNearestUserTask() {
    TaskEntity taskEntity = Context.getCommandContext().getTaskEntityManager().findTaskById(taskId);

    if (taskEntity == null) {
      logger.debug("cannot find task : {}", taskId);

      return null;
    }

    Graph graph = new ActivitiHistoryGraphBuilder(taskEntity.getProcessInstanceId()).build();
    JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean(JdbcTemplate.class);
    String historicActivityInstanceId =
        jdbcTemplate.queryForObject(
            "select id_ from ACT_HI_ACTINST where task_id_=?", String.class, taskId);
    Node node = graph.findById(historicActivityInstanceId);

    String previousHistoricActivityInstanceId = this.findIncomingNode(graph, node);

    if (previousHistoricActivityInstanceId == null) {
      logger.debug("cannot find previous historic activity instance : {}", taskEntity);

      return null;
    }

    return jdbcTemplate.queryForObject(
        "select task_id_ from ACT_HI_ACTINST where id_=?",
        String.class,
        previousHistoricActivityInstanceId);
  }
  /**
   * 撤销流程.
   *
   * @return 0-撤销成功 1-流程结束 2-下一结点已经通过,不能撤销
   */
  public Integer execute(CommandContext commandContext) {
    // 获得历史任务
    HistoricTaskInstanceEntity historicTaskInstanceEntity =
        Context.getCommandContext()
            .getHistoricTaskInstanceEntityManager()
            .findHistoricTaskInstanceById(historyTaskId);

    // 获得历史节点
    HistoricActivityInstanceEntity historicActivityInstanceEntity =
        getHistoricActivityInstanceEntity(historyTaskId);

    Graph graph =
        new ActivitiHistoryGraphBuilder(historicTaskInstanceEntity.getProcessInstanceId()).build();

    Node node = graph.findById(historicActivityInstanceEntity.getId());

    if (!checkCouldWithdraw(node)) {
      logger.info("cannot withdraw {}", historyTaskId);

      return 2;
    }

    // 删除所有活动中的task
    this.deleteActiveTasks(historicTaskInstanceEntity.getProcessInstanceId());

    // 获得期望撤销的节点后面的所有节点历史
    List<String> historyNodeIds = new ArrayList<String>();
    this.collectNodes(node, historyNodeIds);
    this.deleteHistoryActivities(historyNodeIds);
    // 恢复期望撤销的任务和历史
    this.processHistoryTask(historicTaskInstanceEntity, historicActivityInstanceEntity);

    logger.info("activiti is withdraw {}", historicTaskInstanceEntity.getName());

    return 0;
  }
Пример #4
0
  public String findIncomingNode(Graph graph, Node node) {
    for (Edge edge : graph.getEdges()) {
      Node src = edge.getSrc();
      Node dest = edge.getDest();
      String srcType = src.getType();

      if (!dest.getId().equals(node.getId())) {
        continue;
      }

      if ("userTask".equals(srcType)) {
        boolean isSkip = isSkipActivity(src.getId());

        if (isSkip) {
          return this.findIncomingNode(graph, src);
        } else {
          return src.getId();
        }
      } else if (srcType.endsWith("Gateway")) {
        return this.findIncomingNode(graph, src);
      } else {
        logger.info(
            "cannot rollback, previous node is not userTask : "
                + src.getId()
                + " "
                + srcType
                + "("
                + src.getName()
                + ")");

        return null;
      }
    }

    logger.info(
        "cannot rollback, this : "
            + node.getId()
            + " "
            + node.getType()
            + "("
            + node.getName()
            + ")");

    return null;
  }
Пример #5
0
  /**
   * 退回流程.
   *
   * @return true success, false failure
   */
  public Boolean execute(CommandContext commandContext) {
    // 初始化当前的起点信息
    this.initSource();

    // 找到回退的目标节点
    String targetActivityId = this.findTargetActivityId();

    if (targetActivityId == null) {
      logger.info("cannot find targetActivity for : {}", this.taskId);

      return Boolean.FALSE;
    }

    /*
     * // 尝试查找最近的上游userTask String historyTaskId = this.findNearestUserTask();
     * logger.info("nearest history user task is : {}", historyTaskId);
     *
     * if (historyTaskId == null) { logger.info("cannot rollback {}", taskId);
     *
     * return "activity"; }
     */

    // 校验这个节点是否可以回退
    boolean isValid = this.validateTargetActivity(targetActivityId);

    if (!isValid) {
      logger.info("cannot rollback for : {} to {}", this.taskId, targetActivityId);

      return Boolean.FALSE;
    }

    JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean(JdbcTemplate.class);
    String historyTaskId =
        jdbcTemplate.queryForObject(
            "select id_ from ACT_HI_TASKINST where act_id_=? order by END_TIME_ desc",
            String.class,
            targetActivityId);
    HistoricTaskInstanceEntity historicTaskInstanceEntity =
        Context.getCommandContext()
            .getHistoricTaskInstanceEntityManager()
            .findHistoricTaskInstanceById(historyTaskId);
    HistoricActivityInstanceEntity historicActivityInstanceEntity =
        getHistoricActivityInstanceEntity(historyTaskId);

    // 开始回退
    if (this.isSameBranch(historicTaskInstanceEntity)) {
      // 如果退回的目标节点的executionId与当前task的executionId一样,说明是同一个分支
      // 只删除当前分支的task
      this.deleteActiveTask();
    } else {
      // 否则认为是从分支跳回主干
      // 删除所有活动中的task
      this.deleteActiveTasks(historicTaskInstanceEntity.getProcessInstanceId());

      // 获得期望退回的节点后面的所有节点历史
      List<String> historyNodeIds = new ArrayList<String>();
      Graph graph =
          new ActivitiHistoryGraphBuilder(historicTaskInstanceEntity.getProcessInstanceId())
              .build();

      Node node = graph.findById(historicActivityInstanceEntity.getId());
      this.collectNodes(node, historyNodeIds);
      this.deleteHistoryActivities(historyNodeIds);
    }

    // 恢复期望退回的任务和历史
    this.processHistoryTask(historicTaskInstanceEntity, historicActivityInstanceEntity);

    logger.info("activiti is rollback {}", historicTaskInstanceEntity.getName());

    return Boolean.TRUE;
  }