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

📄 taskinstance.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.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmException;
import org.jbpm.calendar.BusinessCalendar;
import org.jbpm.calendar.Duration;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.context.exe.TokenVariableMap;
import org.jbpm.context.exe.VariableContainer;
import org.jbpm.context.exe.VariableInstance;
import org.jbpm.graph.def.Event;
import org.jbpm.graph.def.Node;
import org.jbpm.graph.def.Transition;
import org.jbpm.graph.exe.Comment;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.Token;
import org.jbpm.graph.node.TaskNode;
import org.jbpm.security.SecurityHelper;
import org.jbpm.taskmgmt.def.Swimlane;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.def.TaskController;
import org.jbpm.taskmgmt.log.TaskAssignLog;
import org.jbpm.taskmgmt.log.TaskEndLog;
import org.jbpm.util.EqualsUtil;

/**
 * is one task instance that can be assigned to an actor (read: put in 
 * someones task list) and that can trigger the coninuation of execution 
 * of the token upon completion.
 */
public class TaskInstance extends VariableContainer implements Assignable {

  private static final long serialVersionUID = 1L;

  long id = 0;
  protected String name = null;
  protected String description = null;
  protected String actorId = null;
  protected Date create = null;
  protected Date start = null;
  protected Date end = null;
  protected Date dueDate = null;
  protected int priority = Task.PRIORITY_NORMAL;
  protected boolean isCancelled = false;
  protected boolean isSuspended = false;
  protected boolean isOpen = true;
  protected boolean isSignalling = true;
  protected boolean isBlocking = false;
  protected Task task = null;
  protected Token token = null;
  protected SwimlaneInstance swimlaneInstance = null;
  protected TaskMgmtInstance taskMgmtInstance = null;
  protected Set pooledActors = null;
  protected List comments = null;

  protected String previousActorId = null; // not persisted.  just extra information for listeners of the assign-event  

  public TaskInstance() {
  }

  public TaskInstance(String taskName) {
    this.name = taskName;
  }

  public TaskInstance(String taskName, String actorId) {
    this.name = taskName;
    this.actorId = actorId;
  }

  public void setTask(Task task) {
    this.name = task.getName();
    this.description = task.getDescription();
    this.task = task;
    this.isBlocking = task.isBlocking();
    this.priority = task.getPriority();
    if (task.getTaskNode()!=null) {
      int signal = task.getTaskNode().getSignal();
      this.isSignalling = ( (signal==TaskNode.SIGNAL_FIRST ) 
                            || (signal==TaskNode.SIGNAL_LAST ) 
                            || (signal==TaskNode.SIGNAL_FIRST_WAIT ) 
                            || (signal==TaskNode.SIGNAL_LAST_WAIT ) 
                          );
    }
    if (task.getDueDate()!=null) {
      BusinessCalendar businessCalendar = new BusinessCalendar();
      this.dueDate = businessCalendar.add(new Date(), new Duration(task.getDueDate()));
    }
  }
  
  void submitVariables() {
    TaskController taskController = (task!=null ? task.getTaskController() : null);
    // if there is a task controller, 
    if (taskController!=null) {
      // the task controller is responsible for copying variables back into the process
      taskController.submitParameters(this);
      
    // if there is no task controller
    } else if ( (token!=null)
                && (token.getProcessInstance()!=null)
              ) {
      // the default behaviour is that all task-local variables are flushed to the process 
      if (variableInstances!=null) {
        ContextInstance contextInstance = token.getProcessInstance().getContextInstance();
        Iterator iter = variableInstances.values().iterator();
        while(iter.hasNext()) {
          VariableInstance variableInstance = (VariableInstance) iter.next();
          log.debug("flushing variable '"+variableInstance.getName()+"' from task '"+name+"' to process variables");
          TokenVariableMap tokenVariableMap = contextInstance.getOrCreateTokenVariableMap(token);
          variableInstance.setTokenVariableMap(tokenVariableMap);
          tokenVariableMap.addVariableInstance(variableInstance);
        }
      }
    }
  }
  void initializeVariables() {
    TaskController taskController = (task!=null ? task.getTaskController() : null);
    if (taskController!=null) {
      taskController.initializeVariables(this);
    }
  }

  public void create() {
    create(null);
  }

  public void create(ExecutionContext executionContext) {
    if (create!=null) {
      throw new IllegalStateException("task instance '"+id+"' was already created");
    }
    create = new Date();
    
    // if this task instance is associated with a task...
    if ( (task!=null)
         && (executionContext!=null)
       ) {
      // the TASK_CREATE event is fired
      executionContext.setTaskInstance(this);
      executionContext.setTask(task);
      task.fireEvent(Event.EVENTTYPE_TASK_CREATE, executionContext);
    }
  }

  public void assign(ExecutionContext executionContext) {
    TaskMgmtInstance taskMgmtInstance = executionContext.getTaskMgmtInstance();
    
    Swimlane swimlane = task.getSwimlane();
    // if this task is in a swimlane
    if (swimlane!=null) {
      
      // if this is a task assignment for a start-state
      if (isStartTaskInstance()) {
        // initialize the swimlane
        swimlaneInstance = new SwimlaneInstance(swimlane);
        taskMgmtInstance.addSwimlaneInstance(swimlaneInstance);
        // with the current authenticated actor
        swimlaneInstance.setActorId(SecurityHelper.getAuthenticatedActorId());
        
      } else {
        
        // lazy initialize the swimlane...
        // get the swimlane instance (if there is any) 
        swimlaneInstance = taskMgmtInstance.getInitializedSwimlaneInstance(executionContext, swimlane);
        
        // copy the swimlaneInstance assignment into the taskInstance assignment
        copySwimlaneInstanceAssignment(swimlaneInstance);
      }

    } else { // this task is not in a swimlane
      taskMgmtInstance.performAssignment(task.getAssignmentDelegation(), 
                                         task.getActorIdExpression(),
                                         task.getPooledActorsExpression(),
                                         this, 
                                         executionContext);
    }
    
    updatePooledActorsReferences(swimlaneInstance);
  }


  public boolean isStartTaskInstance() {
    boolean isStartTaskInstance = false;
    if ( (taskMgmtInstance!=null)
         && (taskMgmtInstance.getTaskMgmtDefinition()!=null)
       ) {
      isStartTaskInstance = ( (task!=null)
                              && (task.equals(taskMgmtInstance.getTaskMgmtDefinition().getStartTask()))
                            );
    }
    return isStartTaskInstance;
  }

  void updatePooledActorsReferences(SwimlaneInstance swimlaneInstance) {
    if (pooledActors!=null) {
      Iterator iter = pooledActors.iterator();
      while (iter.hasNext()) {
        PooledActor pooledActor = (PooledActor) iter.next();
        pooledActor.setSwimlaneInstance(swimlaneInstance);
        pooledActor.addTaskInstance(this);
      }
    }
  }

  /**
   * copies the assignment (that includes both the swimlaneActorId and the set of pooledActors) of 
   * the given swimlane into this taskInstance. 
   */
  public void copySwimlaneInstanceAssignment(SwimlaneInstance swimlaneInstance) {
    setSwimlaneInstance(swimlaneInstance);
    setActorId(swimlaneInstance.getActorId());
    setPooledActors(swimlaneInstance.getPooledActors());
  }

  /**
   * gets the pool of actors for this task instance.  If this task has a simlaneInstance 
   * and no pooled actors, the pooled actors of the swimlane instance are returned.
   */
  public Set getPooledActors() {
    if ( (swimlaneInstance!=null)
         && ( (pooledActors==null)
              || (pooledActors.isEmpty())
            )
       ){
      return swimlaneInstance.pooledActors; 
    }
    return pooledActors;
  }

  /**
   * (re)assign this task to the given actor.  If this task is related 
   * to a swimlane instance, that swimlane instance will be updated as well.
   */
  public void setActorId(String actorId) {
    setActorId(actorId, true);
  }

  /**
   * (re)assign this task to the given actor.
   * @param actorId is reference to the person that is assigned to this task.
   * @param overwriteSwimlane specifies if the related swimlane 
   * should be overwritten with the given swimlaneActorId.
   */
  public void setActorId(String actorId, boolean overwriteSwimlane){
    // do the actual assignment
    this.previousActorId = this.actorId;
    this.actorId = actorId;
    if ( (swimlaneInstance!=null)
         && (overwriteSwimlane) ) {
      log.debug("assigning task '"+name+"' to '"+actorId+"'");
      swimlaneInstance.setActorId(actorId);
    }
    
    // fire the event
    if ( (task!=null)
         && (token!=null) 
       ) {
      ExecutionContext executionContext = new ExecutionContext(token);
      executionContext.setTask(task);
      executionContext.setTaskInstance(this);
      task.fireEvent(Event.EVENTTYPE_TASK_ASSIGN, executionContext);
    }
    
    // add the log
    if (token!=null) {
      // log this assignment
      token.addLog(new TaskAssignLog(this, previousActorId, actorId));
    }
  }

  public void setPooledActors(String[] actorIds) {
    this.pooledActors = PooledActor.createPool(actorIds, null, this);
  }

  /**
   * can optionally be used to indicate that the actor is starting to 
   * work on this task instance. 
   */
  public void start(){
    if (start!=null) {
      throw new IllegalStateException("task instance '"+id+"' is already started");
    }
    
    start = new Date();
    if ( (task!=null)
         && (token!=null)
       ) {
      ExecutionContext executionContext = new ExecutionContext(token);
      executionContext.setTask(task);
      executionContext.setTaskInstance(this);
      task.fireEvent(Event.EVENTTYPE_TASK_START, executionContext);
    }
  }

⌨️ 快捷键说明

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