taskinstance.java.svn-base

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

SVN-BASE
535
字号
        IToken token = tokens.get(0);
        if (token.isAlive() == false) {
            throw new EngineException("与activityId=" + this.getActivityId() + "对应的token.alive=false,因此无法完成complete操作");
        }
        IProcessInstance processInstance = this.getProcessInstance();
        INetInstance netInstance = rtCtx.getKenelManager().getNetInstance(processInstance.getProcessId(), processInstance.getVersion());
        Object obj = netInstance.getWFElementInstance(this.getActivityId());
        if (obj == null) {
            throw new EngineException("系统没有找到与activityId=" + this.getActivityId() + "对应activityInstance,无法执行complete操作。");
        }

//		persistenceService.find
        

        List<ITaskInstance> taskInstanceList = persistenceService.findTaskInstancesForProcessInstance(this.getProcessInstance().getId(), this.getActivityId());

        boolean activityInstanceCanBeCompleted = true;
        WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(processInstance.getProcessId(), processInstance.getVersion());
        WorkflowProcess workflowProcess = null;

        workflowProcess = workflowDef.getWorkflowProcess();


        Activity activity = (Activity) workflowProcess.findWFElementById(this.getActivityId());
//        System.out.println("++++++++++The completeStrategy is " + activity.getCompletionStrategy());
        if (activity.getCompletionStrategy().equals(Activity.ALL)) {
            for (int i = 0; taskInstanceList != null && i < taskInstanceList.size(); i++) {
                ITaskInstance taskInst = taskInstanceList.get(i);
                if (taskInst.getState().intValue() == ITaskInstance.INITIALIZED || taskInst.getState().intValue() == ITaskInstance.STARTED) {
                    activityInstanceCanBeCompleted = false;
                }
            }
        }

        if (!activityInstanceCanBeCompleted) {
            return;
        }


        ((IActivityInstance) obj).complete(token,targetActivityInstance);

    }

    public void start() throws EngineException, KenelException {
        //触发事件
        TaskInstanceEvent e = new TaskInstanceEvent();
        e.setEventType(TaskInstanceEvent.BEFORE_TASK_INSTANCE_START);
        this.fireTaskInstanceEvent(e);

        if (Task.FORM.equals(this.getTaskType())) {
            startManulTask();
        } else if (Task.TOOL.equals(this.getTaskType())) {
            startToolTask();
        } else if (Task.SUBFLOW.equals(this.getTaskType())) {
            startSubflowTask();
        } else {
            throw new EngineException("无法识别的TaskType值,taskType=" + this.getTaskType());
        }
    }

    protected void startManulTask() throws EngineException {
        if (this.getState().intValue() == ITaskInstance.INITIALIZED) {
            IPersistenceService persistenceService = rtCtx.getPersistenceService();

            this.setState(ITaskInstance.STARTED);
            this.setStartedTime(rtCtx.getCalendarService().getSysDate());
            persistenceService.saveOrUpdateTaskInstance(this);
            if (Task.ANY.equals(this.getAssignmentStrategy())) {
                persistenceService.deleteWorkItemsInInitializedState(this.getId());
                /*
                List<IWorkItem> workItemsList = persistenceService.findWorkItemsForTaskInstance(this.getId());
                for (int i = 0; workItemsList != null && i < workItemsList.size(); i++) {
                    WorkItem tmpWorkItem = (WorkItem) workItemsList.get(i);
                    if (tmpWorkItem.getState().intValue() == IWorkItem.INITIALIZED) {

                        tmpWorkItem.setState(IWorkItem.CANCELED);
                        tmpWorkItem.setEndTime(ctx.getCalendarService().getSysDate());
                        persistenceService.saveOrUpdateWorkItem(tmpWorkItem);
                    }
                }
                 */
            }
        }
    }

    protected void startToolTask() throws EngineException, KenelException {
        Task task = this.getTask();
        if (task == null) {
            throw new EngineException("The Task is null,can NOT start the taskinstance,");
        }
        if (task.getApplication() == null || task.getApplication().getHandler() == null) {
            throw new EngineException("The task.getApplication() is null or task.getApplication().getHandler() is null,can NOT start the taskinstance,");
        }
    
        Object obj = rtCtx.getBeanByClassName(task.getApplication().getHandler());

        try {
            ((IApplicationHandler) obj).execute(this);
        } catch (Exception e) {
            //TODO, 对tool类型的task抛出的错误应该怎么处理?
        }
        this.complete();

    }

    public Task getTask()  throws EngineException{
     
        IProcessInstance processInstance = this.getProcessInstance();
        WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(processInstance.getProcessId(), processInstance.getVersion());
        if (workflowDef == null) {
            return null;
        }
        try {

            return (Task) workflowDef.getWorkflowProcess().findWFElementById(this.getTaskId());
        } catch (EngineException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Activity getActivity() throws EngineException{

        IProcessInstance processInstance = this.getProcessInstance();
        WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(processInstance.getProcessId(), processInstance.getVersion());
        if (workflowDef == null) {
            return null;
        }
        try {
            return (Activity) workflowDef.getWorkflowProcess().findWFElementById(this.getActivityId());
        } catch (EngineException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public WorkflowProcess getWorkflowProcess()  throws EngineException{
      
        IProcessInstance processInstance = this.getProcessInstance();
        WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(processInstance.getProcessId(), processInstance.getVersion());
        if (workflowDef == null) {
            return null;
        }
        try {

            return workflowDef.getWorkflowProcess();
        } catch (EngineException ex) {
            Logger.getLogger(TaskInstance.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    protected void startSubflowTask() throws EngineException, KenelException {
        Task task = this.getTask();
        SubWorkflowProcess subWorkflowProcess = task.getSubWorkflowProcess();
      
        WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getTheLatestVersionOfWorkflowDefinition(subWorkflowProcess.getWorkflowProcessId());
        WorkflowProcess wfProcess = workflowDef.getWorkflowProcess();

        if (wfProcess == null) {
            throw new EngineException("系统中没有Id为" + subWorkflowProcess.getWorkflowProcessId() + "的流程定义");
        }

        ProcessInstance processInstance = new ProcessInstance();
        processInstance.setProcessId(wfProcess.getId());
        processInstance.setVersion(workflowDef.getVersion());
        processInstance.setDisplayName(wfProcess.getDisplayName());
        processInstance.setName(wfProcess.getName());
        processInstance.setState(IProcessInstance.INITIALIZED);

        //初始化流程变量,从父实例获得初始值
        Map processVars = this.getProcessInstance().getProcessInstanceVariables();
        List datafields = wfProcess.getDataFields();
        for (int i = 0; datafields != null && i < datafields.size(); i++) {
            DataField df = (DataField) datafields.get(i);
            if (df.getDataType().equals(DataField.STRING)) {
                if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof String)) {
                    processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
                } else if (df.getInitialValue() != null) {
                    processInstance.setProcessInstanceVariable(df.getName(), df.getInitialValue());
                } else {
                    processInstance.setProcessInstanceVariable(df.getName(), "");
                }
            } else if (df.getDataType().equals(DataField.INTEGER)) {
                if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof Integer)) {
                    processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
                } else if (df.getInitialValue() != null) {
                    try {
                        Integer intValue = new Integer(df.getInitialValue());
                        processInstance.setProcessInstanceVariable(df.getName(), intValue);
                    } catch (Exception e) {
                    }
                } else {
                    processInstance.setProcessInstanceVariable(df.getName(), new Integer(0));
                }
            } else if (df.getDataType().equals(DataField.FLOAT)) {
                if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof Float)) {
                    processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
                } else if (df.getInitialValue() != null) {
                    Float floatValue = new Float(df.getInitialValue());
                    processInstance.setProcessInstanceVariable(df.getName(), floatValue);
                } else {
                    processInstance.setProcessInstanceVariable(df.getName(), new Float(0));
                }
            } else if (df.getDataType().equals(DataField.BOOLEAN)) {
                if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof Boolean)) {
                    processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
                } else if (df.getInitialValue() != null) {
                    Boolean booleanValue = new Boolean(df.getInitialValue());
                    processInstance.setProcessInstanceVariable(df.getName(), booleanValue);
                } else {
                    processInstance.setProcessInstanceVariable(df.getName(), Boolean.FALSE);
                }
            } else if (df.getDataType().equals(DataField.DATETIME)) {
                //TODO 需要完善一下
            }
        }

        processInstance.setParentProcessInstanceId(this.getProcessInstance().getId());
        processInstance.setParentTaskInstanceId(this.getId());

        rtCtx.getPersistenceService().saveOrUpdateProcessInstance(processInstance);

        processInstance.run();
    }

    /**
     * 触发task instance相关的事件
     * @param e
     * @throws org.fireflow.engine.EngineException
     */
    protected void fireTaskInstanceEvent(TaskInstanceEvent e) throws EngineException {
        Task task = this.getTask();
        if (task == null) {
            return;
        }

        List listeners = task.getEventListeners();
        for (int i = 0; i < listeners.size(); i++) {
            EventListener listener = (EventListener) listeners.get(i);
            Object obj = rtCtx.getBeanByClassName(listener.getClassName());
            if (obj != null) {
                ((ITaskInstanceEventListener) obj).onTaskInstanceFired(e);
            }
        }
    }

    public void abort() throws EngineException, KenelException {
    }

    public void asignToActor(String id, boolean needSign) throws EngineException,KenelException {
        IWorkItem wi = createWorkItem(id);
        if (!needSign){
            wi.sign();
        }
    }
    
    public IWorkflowSession getCurrentWorkflowSession() {
        return this.workflowSession;
    }

    public void setCurrentWorkflowSession(IWorkflowSession session) {
        this.workflowSession = session;
    }    
}

⌨️ 快捷键说明

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