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

📄 taskbean.java

📁 jBpm是一个灵活可扩展的工作流管理系统。作为jBpm运行时server输入的业务流程使用简单强大的语言表达并打包在流程档案中
💻 JAVA
字号:
/*
 * 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.webapp.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmContext;
import org.jbpm.context.def.VariableAccess;
import org.jbpm.db.GraphSession;
import org.jbpm.db.TaskMgmtSession;
import org.jbpm.graph.def.Transition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.logging.exe.LoggingInstance;
import org.jbpm.taskmgmt.def.TaskController;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.taskmgmt.log.TaskAssignLog;

public class TaskBean {

  UserBean userBean = null;
  List taskFormParameters;
  List availableTransitions;
  List availableTransitionItems;
  TaskInstance taskInstance;
  long taskInstanceId;

  JbpmContext jbpmContext;
  GraphSession graphSession;
  TaskMgmtSession taskMgmtSession;

  // For monitoring purposes
  String name;
  String actorId;
  Date end;

  public TaskBean() {
    this.jbpmContext = JbpmContext.getCurrentJbpmContext();
    this.graphSession = jbpmContext.getGraphSession();
    this.taskMgmtSession = jbpmContext.getTaskMgmtSession();

    // get the parameters from the session
    this.taskFormParameters = (List) JsfHelper.getSessionAttribute("taskFormParameters");
  }

  /**
   * 
   * Constructor uses by {@link org.jbpm.webapp.bean.ProcessInstanceBean} to
   * monitor tasks list
   * 
   * @param taskInstanceId
   *          The id of the task instance
   * @param name
   *          The task name
   * @param actorId
   *          The task actor Id
   * @param end
   *          The task end date
   * 
   * @author David Loiseau
   * 
   */
  public TaskBean(long taskInstanceId, String name, String actorId, Date end) {
    this.taskInstanceId = taskInstanceId;
    this.name = name;
    this.actorId = actorId;
    this.end = end;
  }

  public void initialize(TaskInstance taskInstance) {
    this.taskInstance = taskInstance;
    this.taskInstanceId = taskInstance.getId();

    // set the parameters
    this.taskFormParameters = new ArrayList();
    TaskController taskController = taskInstance.getTask().getTaskController();
    if (taskController!=null) {
      List variableAccesses = taskController.getVariableAccesses();
      Iterator iter = variableAccesses.iterator();
      while (iter.hasNext()) {
        VariableAccess variableAccess = (VariableAccess) iter.next();
        String mappedName = variableAccess.getMappedName();
        Object value = taskInstance.getVariable(mappedName);
        TaskFormParameter tfp = new TaskFormParameter(variableAccess, value);
        taskFormParameters.add(tfp);
      }
    }

    // store the parameters in the session
    JsfHelper.setSessionAttribute("taskFormParameters", taskFormParameters);

    // get the available transitions
    availableTransitions = null;

    availableTransitions = taskInstance.getAvailableTransitions();
    if ((availableTransitions != null) && (availableTransitions.size() <= 1)) {
      availableTransitions = null;
      availableTransitionItems = null;
    } else {
      availableTransitionItems = new ArrayList();
      Iterator iter = availableTransitions.iterator();
      while (iter.hasNext()) {
        Transition transition = (Transition) iter.next();
        SelectItem transitionItem = new SelectItem();
        transitionItem.setValue(transition.getName());
        transitionItem.setLabel(transition.getName());
        transitionItem.setDisabled(false);
        availableTransitionItems.add(transitionItem);
      }
    }

    log.debug("initialized availableTransitions " + availableTransitions);
  }

  public String save() {
    log.debug("saving the task parameters " + taskFormParameters);

    // submit the parameters in the jbpm task controller
    TaskInstance taskInstance = taskMgmtSession.loadTaskInstance(taskInstanceId);

    // collect the parameter values from the values that were updated in the
    // parameters by jsf.
    Iterator iter = taskFormParameters.iterator();
    while (iter.hasNext()) {
      TaskFormParameter taskFormParameter = (TaskFormParameter) iter.next();

      if ((taskFormParameter.isWritable()) && (taskFormParameter.getValue() != null)) {
        log.debug("submitting [" + taskFormParameter.getLabel() + "]=" + taskFormParameter.getValue());
        taskInstance.setVariable(taskFormParameter.getLabel(), taskFormParameter.getValue());
      } else {
        log.debug("ignoring unwritable [" + taskFormParameter.getLabel() + "]");
      }
    }

    // save the process instance and hence the updated task instance variables
    jbpmContext.save(taskInstance);

    // remove the parameters from the session
    JsfHelper.removeSessionAttribute("taskFormParameters");

    return "home";
  }

  public String saveAndClose() {
    // save
    save();

    // close the task instance
    String transitionButton = JsfHelper.getParameter("taskform:transitionButton");
    log.debug("Submitted button:" + transitionButton);
    TaskInstance taskInstance = taskMgmtSession.loadTaskInstance(taskInstanceId);
    if ("Save and Close Task".equals(transitionButton)) {
      taskInstance.end();
    } else {
      taskInstance.end(transitionButton);
    }

    ProcessInstance processInstance = taskInstance.getTaskMgmtInstance().getProcessInstance();
    if (processInstance.hasEnded()) {
      JsfHelper.addMessage("The process has finished.");
    }

    LoggingInstance loggingInstance = processInstance.getLoggingInstance();
    List assignmentLogs = loggingInstance.getLogs(TaskAssignLog.class);

    log.debug("assignmentlogs: " + assignmentLogs);

    if (assignmentLogs.size() == 1) {
      TaskAssignLog taskAssignLog = (TaskAssignLog) assignmentLogs.get(0);
      JsfHelper.addMessage("A new task has been assigned to '" + taskAssignLog.getTaskNewActorId() + "'");

    } else if (assignmentLogs.size() > 1) {
      String msg = "New tasks have been assigned to: ";
      Iterator iter = assignmentLogs.iterator();
      while (iter.hasNext()) {
        TaskAssignLog taskAssignLog = (TaskAssignLog) iter.next();
        msg += taskAssignLog.getActorId();
        if (iter.hasNext())
          msg += ", ";
      }
      msg += ".";
      JsfHelper.addMessage(msg);
    }

    jbpmContext.save(taskInstance);

    return "home";
  }

  public long getTaskInstanceId() {
    return taskInstanceId;
  }
  public void setTaskInstanceId(long taskInstanceId) {
    this.taskInstanceId = taskInstanceId;
  }
  public UserBean getUserBean() {
    return userBean;
  }
  public void setUserBean(UserBean userBean) {
    this.userBean = userBean;
  }
  public List getTaskFormParameters() {
    return taskFormParameters;
  }
  public List getAvailableTransitions() {
    return availableTransitions;
  }
  public void setAvailableTransitions(List availableTransitions) {
    this.availableTransitions = availableTransitions;
  }
  public List getAvailableTransitionItems() {
    return availableTransitionItems;
  }
  public TaskInstance getTaskInstance() {
    return taskInstance;
  }

  private static final Log log = LogFactory.getLog(TaskBean.class);

  public String getActorId() {
    return actorId;
  }

  public void setActorId(String actorId) {
    this.actorId = actorId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Date getEnd() {
    return end;
  }

  public void setEnd(Date end) {
    this.end = end;
  }

  public boolean isEnded() {
    if (end == null)
      return true;
    return false;
  }
}

⌨️ 快捷键说明

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