/** * 正常结束工作流 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); } }
/** * 生成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; } }