taskinstance.java

来自「Fire-Workflow-Engine-All-In-One-20090208」· Java 代码 · 共 599 行 · 第 1/2 页

JAVA
599
字号
/**
 * Copyright 2007-2008 非也
 * All rights reserved. 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation。
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses. *
 */
package org.fireflow.engine.impl;

// Generated Feb 23, 2008 12:04:21 AM by Hibernate Tools 3.2.0.b9
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.fireflow.engine.EngineException;
import org.fireflow.engine.IProcessInstance;
import org.fireflow.engine.IRuntimeContextAware;
import org.fireflow.engine.ITaskInstance;
import org.fireflow.engine.RuntimeContext;
import org.fireflow.engine.ou.IAssignable;
import org.fireflow.engine.persistence.IPersistenceService;
import org.fireflow.engine.IWorkItem;
import org.fireflow.engine.IWorkflowSession;
import org.fireflow.engine.IWorkflowSessionAware;
import org.fireflow.engine.IWorkflowSessionCallback;
import org.fireflow.engine.definition.WorkflowDefinition;
import org.fireflow.engine.event.ITaskInstanceEventListener;
import org.fireflow.engine.event.TaskInstanceEvent;
import org.fireflow.engine.taskinstance.IApplicationHandler;
import org.fireflow.kenel.IActivityInstance;
import org.fireflow.kenel.INetInstance;
import org.fireflow.kenel.IToken;
import org.fireflow.kenel.KenelException;
import org.fireflow.model.DataField;
import org.fireflow.model.EventListener;
import org.fireflow.model.Task;
import org.fireflow.model.WorkflowProcess;
import org.fireflow.model.net.Activity;
import org.fireflow.model.reference.SubWorkflowProcess;

/**
 * TaskInstance generated by hbm2java
 */
public class TaskInstance implements ITaskInstance, IAssignable, IRuntimeContextAware, IWorkflowSessionAware, java.io.Serializable {

    private String id = null;
    private String taskId = null;
    private String activityId = null;
    private String name = null;
    private String displayName = null;
    private Integer state = null;
    private Date createdTime = null;
    private Date startedTime = null;
    private Date expiredTime = null;
    private Date endTime = null;
    private String completionStrategy = null;
    private String processInstanceId = null;
    private String processId = null;
    private Integer version = null;
//	private Set workItems = new HashSet(0);
    private String taskType = null;
    protected transient RuntimeContext rtCtx = null;
    protected transient IWorkflowSession workflowSession = null;
    private transient IProcessInstance processInsatance = null;

    public void setRuntimeContext(RuntimeContext ctx) {
        this.rtCtx = ctx;
    }

    public RuntimeContext getRuntimeContext() {
        return this.rtCtx;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    public TaskInstance() {
    }

    public TaskInstance(ProcessInstance workflowProcessInsatnce) {
        this.processInsatance = workflowProcessInsatnce;
    }

//	public TaskInstance(String taskId, String activityId, String name,
//			String displayName, Integer state, Date createdTime, Date startedTime,
//			Date expiredTime, Date endTime, Boolean asignToEveryone,
//			ProcessInstance workflowProcessInsatnceId, Set workItems) {
//		this.taskId = taskId;
//		this.activityId = activityId;
//		this.name = name;
//		this.label = displayName;
//		this.state = state;
//		this.createdTime = createdTime;
//		this.startedTime = startedTime;
//		this.expiredTime = expiredTime;
//		this.endTime = endTime;
//		this.asignToEveryone = asignToEveryone;
//		this.processInsatnce = workflowProcessInsatnceId;
//		this.workItems = workItems;
//	}
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTaskId() {
        return this.taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getActivityId() {
        return this.activityId;
    }

    public void setActivityId(String activityId) {
        this.activityId = activityId;
    }

    public String getName() {
        return this.name;
    }

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

    public String getDisplayName() {
        return this.displayName;
    }

    public void setDisplayName(String label) {
        this.displayName = label;
    }

    public Integer getState() {
        return this.state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Date getCreatedTime() {
        return this.createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Date getStartedTime() {
        return this.startedTime;
    }

    public void setStartedTime(Date startedTime) {
        this.startedTime = startedTime;
    }

    public Date getExpiredTime() {
        return this.expiredTime;
    }

    public void setExpiredTime(Date expiredTime) {
        this.expiredTime = expiredTime;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getAssignmentStrategy() {
        return completionStrategy;
    }

    public void setAssignmentStrategy(String completionStrategy) {
        this.completionStrategy = completionStrategy;
    }

    public IProcessInstance getProcessInstance() {
        if (this.processInsatance != null) {
            ((IWorkflowSessionAware)this.processInsatance).setCurrentWorkflowSession(this.workflowSession);
            return this.processInsatance;
        } else {
            try {
                final String procInstId = this.processInstanceId;
                this.processInsatance = (IProcessInstance) this.workflowSession.execute(new IWorkflowSessionCallback() {

                    public Object doInWorkflowSession(RuntimeContext ctx) throws EngineException, KenelException {
                        return ctx.getPersistenceService().findProcessInstanceById(procInstId);
                    }
                });
            } catch (EngineException ex) {
                Logger.getLogger(TaskInstance.class.getName()).log(Level.SEVERE, null, ex);
            } catch (KenelException ex) {
                Logger.getLogger(TaskInstance.class.getName()).log(Level.SEVERE, null, ex);
            }
            return this.processInsatance;
        }

    }

    /*
    public void setProcessInstance(
    IProcessInstance processInsatnce) {
    this.processInsatnce = processInsatnce;
    if (this.processInsatnce!=null ){
    this.processInstanceId = this.processInsatnce.getId();
    }else {
    this.processInstanceId = null;
    }
    }
     */
//	public Set getWorkItems() {
//		return this.workItems;
//	}
//
//	public void setWorkItems(Set workItems) {
//		this.workItems = workItems;
//	}
    public void asignToActor(String id) throws EngineException, KenelException {
        this.asignToActor(id, true);
    }

    public void asignToActors(List<String> ids) throws EngineException, KenelException {
        //task应该有一个标志(asignToEveryone),表明asign的规则
        for (int i = 0; ids != null && i < ids.size(); i++) {
            createWorkItem(ids.get(i));
        }
    }

    protected IWorkItem createWorkItem(String actorId) throws EngineException {
        IPersistenceService persistenceService = rtCtx.getPersistenceService();

        WorkItem wi = new WorkItem();
        wi.setTaskInstance(this);
        wi.setActorId(actorId);
        wi.setState(IWorkItem.INITIALIZED);
        wi.setCreatedTime(rtCtx.getCalendarService().getSysDate());
        wi.setRuntimeContext(rtCtx);
        wi.setCurrentWorkflowSession(workflowSession);
        persistenceService.saveOrUpdateWorkItem(wi);
        return wi;
//		this.getWorkItems().add(wi);
//		wi.setAsignToEveryone(asignToEveryone);
    //保存到数据库
    }

    public void complete() throws EngineException, KenelException {
        complete(null);
    }

    public void complete(IActivityInstance targetActivityInstance) throws EngineException, KenelException {
        
        IPersistenceService persistenceService = rtCtx.getPersistenceService();

        //第一步,首先结束当前taskInstance
        List<IWorkItem> workItemsList = persistenceService.findWorkItemsForTaskInstance(this.getId());
        boolean taskInstanceCanBeCompleted = true;
        for (int i = 0; workItemsList != null && i < workItemsList.size(); i++) {
            IWorkItem wi = workItemsList.get(i);
            if (wi.getState().intValue() == IWorkItem.INITIALIZED || wi.getState().intValue() == IWorkItem.STARTED) {
                taskInstanceCanBeCompleted = false;
                break;
            }
        }
        if (!taskInstanceCanBeCompleted) {
            return;
        }

        this.setState(ITaskInstance.COMPLETED);
        this.setEndTime(rtCtx.getCalendarService().getSysDate());
        persistenceService.saveOrUpdateTaskInstance(this);
        //触发相应的事件
        TaskInstanceEvent e = new TaskInstanceEvent();
        e.setEventType(TaskInstanceEvent.AFTER_TASK_INSTANCE_COMPLETE);

⌨️ 快捷键说明

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