⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taskmgmtinstance.java

📁 jBpm是一个灵活可扩展的工作流管理系统。作为jBpm运行时server输入的业务流程使用简单强大的语言表达并打包在流程档案中
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.taskmgmt.exe;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmException;
import org.jbpm.graph.def.DelegationException;
import org.jbpm.graph.def.GraphElement;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.Token;
import org.jbpm.instantiation.Delegation;
import org.jbpm.jpdl.el.impl.JbpmExpressionEvaluator;
import org.jbpm.module.exe.ModuleInstance;
import org.jbpm.security.SecurityHelper;
import org.jbpm.svc.Services;
import org.jbpm.taskmgmt.TaskInstanceFactory;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.def.Swimlane;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.def.TaskMgmtDefinition;
import org.jbpm.taskmgmt.log.TaskCreateLog;

/**
 * process instance extension for managing tasks on a process instance.
 */
public class TaskMgmtInstance extends ModuleInstance {

  private static final long serialVersionUID = 1L;

  TaskMgmtDefinition taskMgmtDefinition = null;
  Map swimlaneInstances = null;
  Set taskInstances = null;
  /** non persistent collection that stores all the task instances that have
   * variable updates*/
  Collection taskInstanceVariableUpdates = null; 
  
  public TaskMgmtInstance() {
  }
  
  public TaskMgmtInstance(TaskMgmtDefinition taskMgmtDefinition) {
    this.taskMgmtDefinition = taskMgmtDefinition;
  }

  // task instances ///////////////////////////////////////////////////////////

  public TaskInstance createTaskInstance() {
    return createTaskInstance(null, (ExecutionContext)null);
  }

  public TaskInstance createTaskInstance(Task task) {
    return createTaskInstance(task, (ExecutionContext)null);
  }

  public TaskInstance createTaskInstance(Token token) {
    return createTaskInstance(null, new ExecutionContext(token));
  }

  /**
   * creates a new task instance on the given token, for the given task.
   */
  public TaskInstance createTaskInstance(Task task, Token token) {
    ExecutionContext executionContext = new ExecutionContext(token);
    executionContext.setTask(task);
    return createTaskInstance(task, executionContext);
  }

  /**
   * creates a new task instance on the given task, in the given execution context.
   */
  public TaskInstance createTaskInstance(Task task, ExecutionContext executionContext) {
    // instantiate the new task instance
    TaskInstance taskInstance = instantiateNewTaskInstance(executionContext);

    // bind the task instance to the TaskMgmtInstance
    addTaskInstance(taskInstance);

    // initialize the task instance
    if (task!=null) taskInstance.setTask(task);

    // assign an id to the task instance
    Services.assignId(taskInstance);

    // copy the task properties
    String description = null;
    if (task!=null) {
      description = task.getDescription();
      taskInstance.setDescription(description);
      taskInstance.setBlocking(task.isBlocking());
      taskInstance.setSignalling(task.isSignalling());
    }

    if (executionContext!=null) {
      Token token = executionContext.getToken();
      taskInstance.setToken(token);
      
      taskInstance.initializeVariables();
      
      try {
        // update the executionContext
        executionContext.setTask(task);
        executionContext.setTaskInstance(taskInstance);
        executionContext.setEventSource(task);

        // evaluate the description
        if ( (description!=null) 
             && (description.indexOf("#{")!=-1)
           ) {
          Object result = JbpmExpressionEvaluator.evaluate(description, executionContext);
          if (result!=null) {
            taskInstance.setDescription(result.toString());
          }
        }

        // create the task instance
        taskInstance.create(executionContext);

        // if this task instance is created for a task, perform assignment
        if (task!=null) {
          taskInstance.assign(executionContext);
        }
        
      } finally {
        // clean the executionContext
        executionContext.setTask(null);
        executionContext.setTaskInstance(null);
        executionContext.setEventSource(null);
      }
      
      // log this creation 
      token.addLog(new TaskCreateLog(taskInstance, taskInstance.getActorId()));

    } else {
      taskInstance.create();
    }

    return taskInstance;
  }

  public SwimlaneInstance getInitializedSwimlaneInstance(ExecutionContext executionContext, Swimlane swimlane) {
    // initialize the swimlane
    if (swimlaneInstances==null) swimlaneInstances = new HashMap();
    SwimlaneInstance swimlaneInstance = (SwimlaneInstance) swimlaneInstances.get(swimlane.getName());
    if (swimlaneInstance==null) {
      swimlaneInstance = new SwimlaneInstance(swimlane);
      addSwimlaneInstance(swimlaneInstance);
      // assign the swimlaneInstance
      performAssignment(swimlane.getAssignmentDelegation(), 
                        swimlane.getActorIdExpression(),
                        swimlane.getPooledActorsExpression(),
                        swimlaneInstance, 
                        executionContext);
    }

    return swimlaneInstance;
  }
  
  public void performAssignment(Delegation assignmentDelegation, 
                                String actorIdExpression, 
                                String pooledActorsExpression, 
                                Assignable assignable, 
                                ExecutionContext executionContext) {
    try {
      if (assignmentDelegation!=null) {
        performAssignmentDelegation(assignmentDelegation, assignable, executionContext);
      } else {
        if (actorIdExpression!=null) {
          performAssignmentActorIdExpr(actorIdExpression, assignable, executionContext);
        }
        if (pooledActorsExpression!=null) {
          performAssignmentPooledActorsExpr(pooledActorsExpression, assignable, executionContext);
        }
      }
      
    } catch (Exception exception) {
      GraphElement graphElement = executionContext.getEventSource();
      if (graphElement!=null) {
        graphElement.raiseException(exception, executionContext);
      } else {
        throw new DelegationException(exception, executionContext);
      }
    }
  }

  void performAssignmentDelegation(Delegation assignmentDelegation, Assignable assignable, ExecutionContext executionContext) throws Exception {
    // instantiate the assignment handler
    AssignmentHandler assignmentHandler = (AssignmentHandler) assignmentDelegation.instantiate();
    // invoke the assignment handler
    assignmentHandler.assign(assignable, executionContext);
  }

  void performAssignmentActorIdExpr(String actorIdExpression, Assignable assignable, ExecutionContext executionContext) {
    Object result = null;
    String actorId = null;
    try {
      result = JbpmExpressionEvaluator.evaluate(actorIdExpression, executionContext);
      if (result==null) {
        throw new JbpmException("actor-id expression '"+actorIdExpression+"' returned null");
      }
      actorId = (String) result;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -