/**
 * 流程转向操作
 *
 * @param taskId
 *            当前任务ID
 * @param activityId
 *            目标节点任务ID
 * @param variables
 *            流程变量
 * @throws Exception
 */
@Override
public void turnTransition(String taskId, String activityId,
                            Map<String, Object> variables) throws Exception {
    // 当前节点
    ActivityImpl currActivity = findActivitiImpl(taskId, null);
    // 清空当前流向
    List<PvmTransition> oriPvmTransitionList = clearTransition(currActivity);
    if (StringUtils.isEmpty(activityId)) {
        List<PvmTransition> incomingTransitions = currActivity.getIncomingTransitions();
        PvmTransition pvmTransition = incomingTransitions.get(0);
        activityId = pvmTransition.getSource().getId();
    }
    // 创建新流向
    TransitionImpl newTransition = currActivity.createOutgoingTransition();
    // 目标节点
    ActivityImpl pointActivity = findActivitiImpl(taskId, activityId);
    // 设置新流向的目标节点
    newTransition.setDestination(pointActivity);


    // 执行转向任务
    missionService.complete(taskId, variables);
    // 删除目标节点新流入
    pointActivity.getIncomingTransitions().remove(newTransition);


    // 还原以前流向
    restoreTransition(currActivity, oriPvmTransitionList);
}

 

 

/**
     * 根据任务ID和节点ID获取活动节点 <br>
     *
     * @param taskId
     *            任务ID
     * @param activityId
     *            活动节点ID <br>
     *            如果为null或"",则默认查询当前活动节点 <br>
     *            如果为"end",则查询结束节点 <br>
     *
     * @return
     * @throws Exception
     */
    private ActivityImpl findActivitiImpl(String taskId, String activityId)
            throws Exception {
        // 取得流程定义
        ProcessDefinitionEntity processDefinition = findProcessDefinitionEntityByTaskId(taskId);

        // 获取当前活动节点ID
        if (StringUtils.isEmpty(activityId)) {
            activityId = findTaskById(taskId).getTaskDefinitionKey();
        }
        // 根据流程定义,获取该流程实例的结束节点
        if (activityId.toUpperCase().equals("END")) {
            for (ActivityImpl activityImpl : processDefinition.getActivities()) {
                List<PvmTransition> pvmTransitionList = activityImpl
                        .getOutgoingTransitions();
                if (pvmTransitionList.isEmpty()) {
                    return activityImpl;
                }
            }
        }

// 根据节点ID,获取对应的活动节点
        ActivityImpl activityImpl = ((ProcessDefinitionImpl) processDefinition)
                .findActivity(activityId);


        return activityImpl;
    }

 

/**
 * 清空指定活动节点流向
 *
 * @param activityImpl
 *            活动节点
 * @return 节点流向集合
 */
private List<PvmTransition> clearTransition(ActivityImpl activityImpl) {
    // 存储当前节点所有流向临时变量
    List<PvmTransition> oriPvmTransitionList = new ArrayList<PvmTransition>();
    // 获取当前节点所有流向,存储到临时变量,然后清空
    List<PvmTransition> pvmTransitionList = activityImpl
            .getOutgoingTransitions();
    for (PvmTransition pvmTransition : pvmTransitionList) {
        oriPvmTransitionList.add(pvmTransition);
    }
    pvmTransitionList.clear();


    return oriPvmTransitionList;
}

 

 

/**
 * 还原指定活动节点流向
 *
 * @param activityImpl
 *            活动节点
 * @param oriPvmTransitionList
 *            原有节点流向集合
 */
private void restoreTransition(ActivityImpl activityImpl,
                               List<PvmTransition> oriPvmTransitionList) {
    // 清空现有流向
    List<PvmTransition> pvmTransitionList = activityImpl
            .getOutgoingTransitions();
    pvmTransitionList.clear();
    // 还原以前流向
    for (PvmTransition pvmTransition : oriPvmTransitionList) {
        pvmTransitionList.add(pvmTransition);
    }
}