/** Find Task Instances */
  @SuppressWarnings("rawtypes")
  public static List<TaskInstance> findTaskInstances(long processInstanceId)
      throws WorkflowException {
    log.debug("findTaskInstances({})", processInstanceId);
    JbpmContext jbpmContext = JBPMUtils.getConfig().createJbpmContext();
    ArrayList<TaskInstance> al = new ArrayList<TaskInstance>();

    try {
      GraphSession graphSession = jbpmContext.getGraphSession();
      org.jbpm.graph.exe.ProcessInstance pi = graphSession.getProcessInstance(processInstanceId);
      TaskMgmtInstance taskMgmtInstance = pi.getTaskMgmtInstance();

      if (taskMgmtInstance.getTaskInstances() != null) {
        for (Iterator it = taskMgmtInstance.getTaskInstances().iterator(); it.hasNext(); ) {
          org.jbpm.taskmgmt.exe.TaskInstance ti = (org.jbpm.taskmgmt.exe.TaskInstance) it.next();
          al.add(WorkflowUtils.copy(ti));
        }
      }

      // Sort
      Collections.sort(al);
    } catch (JbpmException e) {
      throw new WorkflowException(e.getMessage(), e);
    } finally {
      jbpmContext.close();
    }

    log.debug("findTaskInstances: {}", al);
    return al;
  }
  /** Start Process Definition */
  public static ProcessInstance runProcessDefinition(
      String user, long processDefinitionId, String uuid, List<FormElement> variables)
      throws WorkflowException {
    log.debug(
        "runProcessDefinition({}, {}, {}, {})",
        new Object[] {user, processDefinitionId, uuid, variables});
    JbpmContext jbpmContext = JBPMUtils.getConfig().createJbpmContext();
    ProcessInstance vo = new ProcessInstance();

    if (Config.SYSTEM_READONLY) {
      throw new WorkflowException("System is in read-only mode");
    }

    try {
      jbpmContext.setActorId(user);
      GraphSession graphSession = jbpmContext.getGraphSession();
      Map<String, Object> hm = new HashMap<String, Object>();
      hm.put(Config.WORKFLOW_PROCESS_INSTANCE_VARIABLE_UUID, uuid);

      for (FormElement fe : variables) {
        hm.put(fe.getName(), fe);
      }

      org.jbpm.graph.def.ProcessDefinition pd =
          graphSession.getProcessDefinition(processDefinitionId);
      org.jbpm.graph.exe.ProcessInstance pi = pd.createProcessInstance(hm);

      if (pi != null) {
        org.jbpm.taskmgmt.exe.TaskMgmtInstance tmi = pi.getTaskMgmtInstance();

        // http://community.jboss.org/thread/115182
        if (tmi.getTaskMgmtDefinition().getStartTask() != null) {
          org.jbpm.taskmgmt.exe.TaskInstance ti = tmi.createStartTaskInstance();

          if (Config.WORKFLOW_START_TASK_AUTO_RUN) {
            ti.start();
            ti.end();
          }
        } else {
          pi.getRootToken().signal();
        }

        jbpmContext.save(pi);
        vo = WorkflowUtils.copy(pi);
      }
    } catch (JbpmException e) {
      throw new WorkflowException(e.getMessage(), e);
    } finally {
      jbpmContext.close();
    }

    log.debug("runProcessDefinition: {}", vo);
    return vo;
  }
  public Object resolveVariable(String name) throws ELException {
    ExecutionContext executionContext = ExecutionContext.currentExecutionContext();
    Object value = null;

    if ("taskInstance".equals(name)) {
      value = executionContext.getTaskInstance();

    } else if ("processInstance".equals(name)) {
      value = executionContext.getProcessInstance();

    } else if ("processDefinition".equals(name)) {
      value = executionContext.getProcessDefinition();

    } else if ("token".equals(name)) {
      value = executionContext.getToken();

    } else if ("taskMgmtInstance".equals(name)) {
      value = executionContext.getTaskMgmtInstance();

    } else if ("contextInstance".equals(name)) {
      value = executionContext.getContextInstance();

    } else if ((executionContext.getTaskInstance() != null)
        && (executionContext.getTaskInstance().hasVariableLocally(name))) {
      value = executionContext.getTaskInstance().getVariable(name);

    } else {
      ContextInstance contextInstance = executionContext.getContextInstance();
      TaskMgmtInstance taskMgmtInstance = executionContext.getTaskMgmtInstance();
      Token token = executionContext.getToken();

      if ((contextInstance != null) && (contextInstance.hasVariable(name))) {
        value = contextInstance.getVariable(name, token);

      } else if ((contextInstance != null) && (contextInstance.hasTransientVariable(name))) {
        value = contextInstance.getTransientVariable(name);

      } else if ((taskMgmtInstance != null)
          && (taskMgmtInstance.getSwimlaneInstances() != null)
          && (taskMgmtInstance.getSwimlaneInstances().containsKey(name))) {
        SwimlaneInstance swimlaneInstance = taskMgmtInstance.getSwimlaneInstance(name);
        value = (swimlaneInstance != null ? swimlaneInstance.getActorId() : null);

      } else if ((contextInstance != null) && (contextInstance.hasTransientVariable(name))) {
        value = contextInstance.getTransientVariable(name);

      } else if (JbpmConfiguration.Configs.hasObject(name)) {
        value = JbpmConfiguration.Configs.getObject(name);
      }
    }

    return value;
  }
    public void execute(ExecutionContext executionContext) throws Exception {
      TaskMgmtDefinition tmd =
          (TaskMgmtDefinition) executionContext.getDefinition(TaskMgmtDefinition.class);
      Task task = tmd.getTask("watch movie amadeus");

      // create as many task instances as the scenario prescribes :
      // 0 tasks for scenario 1
      // 1 task for scenario 2
      // 2 tasks for scenario 3
      // 3 tasks for scenario 4
      TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
      for (int i = 1; i < scenario; i++) {
        tmi.createTaskInstance(task, executionContext.getToken());
      }
    }
  public void execute(ExecutionContext executionContext) throws Exception {
    TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();

    // 取得当前流程实例所有的子Token上的任务实例

    Token rootToken = executionContext.getProcessInstance().getRootToken();
    Collection childTokeList = rootToken.getChildren().values();

    for (Iterator iterator = childTokeList.iterator(); iterator.hasNext(); ) {
      Token childToken = (Token) iterator.next();
      Collection c = tmi.getUnfinishedTasks(childToken);
      for (Iterator iterator2 = c.iterator(); iterator2.hasNext(); ) {
        TaskInstance ti = (TaskInstance) iterator2.next();
        ti.cancel();
      }
    }
  }
 public static void endOneTask(Token token) {
   TaskMgmtInstance tmi = token.getProcessInstance().getTaskMgmtInstance();
   TaskInstance taskInstance = (TaskInstance) tmi.getUnfinishedTasks(token).iterator().next();
   taskInstance.end();
 }