/* (non-Javadoc) * @see org.fireflow.engine.IProcessInstance#run() */ public void run() throws EngineException, KernelException { if (this.getState().intValue() != IProcessInstance.INITIALIZED) { throw new EngineException( this.getId(), this.getWorkflowProcess(), this.getProcessId(), "The state of the process instance is " + this.getState() + ",can not run it "); } INetInstance netInstance = rtCtx.getKernelManager().getNetInstance(this.getProcessId(), this.getVersion()); if (netInstance == null) { throw new EngineException( this.getId(), this.getWorkflowProcess(), this.getProcessId(), "The net instance for the workflow process [Id=" + this.getProcessId() + "] is Not found"); } // 触发事件 ProcessInstanceEvent event = new ProcessInstanceEvent(); event.setEventType(ProcessInstanceEvent.BEFORE_PROCESS_INSTANCE_RUN); event.setSource(this); this.fireProcessInstanceEvent(event); this.setState(IProcessInstance.RUNNING); this.setStartedTime(rtCtx.getCalendarService().getSysDate()); rtCtx.getPersistenceService().saveOrUpdateProcessInstance(this); netInstance.run(this); // 运行工作流网实例,从startnode开始 }
/* (non-Javadoc) * @see org.fireflow.engine.IProcessInstance#abort() */ public void abort() throws EngineException { if (this.state.intValue() == IProcessInstance.COMPLETED || this.state.intValue() == IProcessInstance.CANCELED) { throw new EngineException( this, this.getWorkflowProcess(), "The process instance can not be aborted,the state of this process instance is " + this.getState()); } IPersistenceService persistenceService = rtCtx.getPersistenceService(); persistenceService.abortProcessInstance(this); }
/* (non-Javadoc) * @see org.fireflow.engine.IProcessInstance#getWorkflowProcess() */ public WorkflowProcess getWorkflowProcess() throws EngineException { WorkflowDefinition workflowDef = rtCtx .getDefinitionService() .getWorkflowDefinitionByProcessIdAndVersionNumber( this.getProcessId(), this.getVersion()); WorkflowProcess workflowProcess = null; workflowProcess = workflowDef.getWorkflowProcess(); return workflowProcess; }
/** * 正常结束工作流 1、首先检查有无活动的token,如果有则直接返回,如果没有则结束当前流程 2、执行结束流程的操作,将state的值设置为结束状态 * 3、然后检查parentTaskInstanceId是否为null,如果不为null则,调用父taskinstance的complete操作。 */ public void complete() throws EngineException, KernelException { List<IToken> tokens = rtCtx.getPersistenceService().findTokensForProcessInstance(this.getId(), null); boolean canBeCompleted = true; for (int i = 0; tokens != null && i < tokens.size(); i++) { IToken token = tokens.get(i); if (token.isAlive()) { canBeCompleted = false; break; } } if (!canBeCompleted) { return; } this.setState(IProcessInstance.COMPLETED); // 记录结束时间 this.setEndTime(rtCtx.getCalendarService().getSysDate()); rtCtx.getPersistenceService().saveOrUpdateProcessInstance(this); // 删除所有的token for (int i = 0; tokens != null && i < tokens.size(); i++) { IToken token = tokens.get(i); rtCtx.getPersistenceService().deleteToken(token); } // 触发事件 ProcessInstanceEvent event = new ProcessInstanceEvent(); event.setEventType(ProcessInstanceEvent.AFTER_PROCESS_INSTANCE_COMPLETE); event.setSource(this); this.fireProcessInstanceEvent(event); if (this.getParentTaskInstanceId() != null && !this.getParentTaskInstanceId().trim().equals("")) { ITaskInstance taskInstance = rtCtx.getPersistenceService().findAliveTaskInstanceById(this.getParentTaskInstanceId()); ((IRuntimeContextAware) taskInstance).setRuntimeContext(rtCtx); ((IWorkflowSessionAware) taskInstance).setCurrentWorkflowSession(workflowSession); ((TaskInstance) taskInstance).complete(null); } }
/** * 触发process instance相关的事件 * * @param e * @throws org.fireflow.engine.EngineException */ protected void fireProcessInstanceEvent(ProcessInstanceEvent e) throws EngineException { WorkflowProcess workflowProcess = this.getWorkflowProcess(); if (workflowProcess == null) { return; } List<EventListener> listeners = workflowProcess.getEventListeners(); for (int i = 0; i < listeners.size(); i++) { EventListener listener = listeners.get(i); Object obj = rtCtx.getBeanByName(listener.getClassName()); if (obj != null) { ((IProcessInstanceEventListener) obj).onProcessInstanceEventFired(e); } } }
/** * 生成joinPoint * * @param synchInst * @param token * @return * @throws EngineException */ public IJoinPoint createJoinPoint(ISynchronizerInstance synchInst, IToken token) throws EngineException { int enterTransInstanceCount = synchInst.getEnteringTransitionInstances().size(); if (enterTransInstanceCount == 0) { // 检查流程定义是否合法,同步器节点必须有输入边 throw new EngineException( this.getId(), this.getWorkflowProcess(), synchInst.getSynchronizer().getId(), "The process definition [" + this.getName() + "] is invalid,the synchronizer[" + synchInst.getSynchronizer() + "] has no entering transition"); } IPersistenceService persistenceService = rtCtx.getPersistenceService(); // 保存到数据库 persistenceService.saveOrUpdateToken(token); IJoinPoint resultJoinPoint = null; resultJoinPoint = new JoinPoint(); resultJoinPoint.setProcessInstance(this); resultJoinPoint.setSynchronizerId(synchInst.getSynchronizer().getId()); if (enterTransInstanceCount == 1) { // 生成一个不存储到数据库中的JoinPoint resultJoinPoint.addValue(token.getValue()); if (token.isAlive()) { resultJoinPoint.setAlive(true); resultJoinPoint.setFromActivityId(token.getFromActivityId()); } resultJoinPoint.setStepNumber(token.getStepNumber() + 1); return resultJoinPoint; } else { int stepNumber = 0; List<IToken> tokensList_0 = persistenceService.findTokensForProcessInstance( this.getId(), synchInst.getSynchronizer().getId()); Map<String, IToken> tokensMap = new HashMap<String, IToken>(); for (int i = 0; i < tokensList_0.size(); i++) { IToken tmpToken = tokensList_0.get(i); String tmpFromActivityId = tmpToken.getFromActivityId(); if (!tokensMap.containsKey(tmpFromActivityId)) { tokensMap.put(tmpFromActivityId, tmpToken); } else { // TODO ====下面的代码有意义吗?===start===wmj2003 IToken tmpToken2 = tokensMap.get(tmpFromActivityId); if (tmpToken2.getStepNumber() > tmpToken.getStepNumber()) { tokensMap.put(tmpFromActivityId, tmpToken2); } // TODO ====下面的代码有意义吗?===end===wmj2003 } } List<IToken> tokensList = new ArrayList<IToken>(tokensMap.values()); for (int i = 0; i < tokensList.size(); i++) { IToken _token = tokensList.get(i); resultJoinPoint.addValue(_token.getValue()); if (_token.isAlive()) { // 如果token的状态是alive resultJoinPoint.setAlive(true); String oldFromActivityId = resultJoinPoint.getFromActivityId(); if (oldFromActivityId == null || oldFromActivityId.trim().equals("")) { resultJoinPoint.setFromActivityId(_token.getFromActivityId()); } else { resultJoinPoint.setFromActivityId( oldFromActivityId + IToken.FROM_ACTIVITY_ID_SEPARATOR + _token.getFromActivityId()); } } if (token.getStepNumber() > stepNumber) { stepNumber = token.getStepNumber(); } } resultJoinPoint.setStepNumber(stepNumber + 1); return resultJoinPoint; } }