//这里使用了回调函数,这里手工处理事务的一种方法 currentProcessInstance = (IProcessInstance) transactionTemplate.execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus arg0) { try { //IWorkflowSession是流程操作的入口,需要从runtimeContext获得。 IWorkflowSession workflowSession = runtimeContext.getWorkflowSession(); //创建流程实例 //"LeaveApplicationProcess"是流程定义id // CurrentUserAssignmentHandler.APPLICANT:表示流程创建者,下面会有更详细的说明 IProcessInstance processInstance = workflowSession.createProcessInstance( "LeaveApplicationProcess",CurrentUserAssignmentHandler.APPLICANT); //运行流程实例 processInstance.run(); return processInstance; } catch (EngineException ex) { Logger.getLogger(LeaveApplicationTester.class.getName()).log(Level.SEVERE, null, ex); } catch (KernelException ex) { Logger.getLogger(LeaveApplicationTester.class.getName()).log(Level.SEVERE, null, ex); } return null; } });
WorkflowSession.java
public IProcessInstance createProcessInstance(String workflowProcessName, ITaskInstance parentTaskInstance) throws EngineException, KernelException { return _createProcessInstance(workflowProcessName, parentTaskInstance .getId(), parentTaskInstance.getProcessInstanceId(), parentTaskInstance.getId()); } /** * 创建一个新的流程实例 (create a new process instance ) * @param workflowProcessId 流程定义ID * @param creatorId 创建人ID * @param parentProcessInstanceId 父流程实例ID * @param parentTaskInstanceId 父任务实例ID * @return * @throws EngineException * @throws KernelException */ protected IProcessInstance _createProcessInstance(String workflowProcessId, final String creatorId, final String parentProcessInstanceId, final String parentTaskInstanceId) throws EngineException, KernelException { //设置流程ID final String wfprocessId = workflowProcessId; //得到流程定义文件 final WorkflowDefinition workflowDef = runtimeContext.getDefinitionService().getTheLatestVersionOfWorkflowDefinition(wfprocessId); //得到工作流模型 final WorkflowProcess wfProcess = workflowDef.getWorkflowProcess(); //如果工作流模型为null,则抛出异常 if (wfProcess == null) { throw new RuntimeException( "Workflow process NOT found,id=[" + wfprocessId + "]"); } //生成流程实列 IProcessInstance processInstance = (IProcessInstance) this.execute(new IWorkflowSessionCallback() { public Object doInWorkflowSession(RuntimeContext ctx) throws EngineException, KernelException { //新建一个流程实例 ProcessInstance processInstance = new ProcessInstance(); //设置创建者 processInstance.setCreatorId(creatorId); //设置工作流模型ID processInstance.setProcessId(wfProcess.getId()); //设置流程定义版本 processInstance.setVersion(workflowDef.getVersion()); //设置工作流模型显示的名称 processInstance.setDisplayName(wfProcess.getDisplayName()); //设置工作流名称 processInstance.setName(wfProcess.getName()); //设置流程的状态为初始状态 processInstance.setState(IProcessInstance.INITIALIZED); //设置流程实例的创建时间 processInstance.setCreatedTime(ctx.getCalendarService() .getSysDate()); //如果是当前是子流程,则设置父流程实例id,否则设置值为null processInstance .setParentProcessInstanceId(parentProcessInstanceId); //如果当前是子流程,则设置创建子流程的任务ID processInstance.setParentTaskInstanceId(parentTaskInstanceId); //将流程实例保存到数据库 ctx.getPersistenceService().saveOrUpdateProcessInstance( processInstance); return processInstance; } }); // 初始化流程变量 processInstance.setProcessInstanceVariables(new HashMap<String, Object>()); List<DataField> datafields = wfProcess.getDataFields(); for (int i = 0; datafields != null && i < datafields.size(); i++) { DataField df = datafields.get(i); if (df.getDataType().equals(DataField.STRING)) { if (df.getInitialValue() != null) { processInstance.setProcessInstanceVariable(df .getName(), df.getInitialValue()); } else { processInstance.setProcessInstanceVariable(df .getName(), ""); } } else if (df.getDataType().equals(DataField.INTEGER)) { 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.LONG)) { if (df.getInitialValue() != null) { try { Long longValue = new Long(df.getInitialValue()); processInstance.setProcessInstanceVariable(df .getName(), longValue); } catch (Exception e) { } } else { processInstance.setProcessInstanceVariable(df .getName(), new Long(0)); } } else if (df.getDataType().equals(DataField.FLOAT)) { 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.DOUBLE)) { if (df.getInitialValue() != null) { Double doubleValue = new Double(df .getInitialValue()); processInstance.setProcessInstanceVariable(df .getName(), doubleValue); } else { processInstance.setProcessInstanceVariable(df .getName(), new Double(0)); } } else if (df.getDataType().equals(DataField.BOOLEAN)) { 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 需要完善一下 if (df.getInitialValue() != null && df.getDataPattern() != null) { try { SimpleDateFormat dFormat = new SimpleDateFormat( df.getDataPattern()); Date dateTmp = dFormat.parse(df .getInitialValue()); processInstance.setProcessInstanceVariable(df .getName(), dateTmp); } catch (Exception e) { processInstance.setProcessInstanceVariable(df .getName(), null); e.printStackTrace(); } } else { processInstance.setProcessInstanceVariable(df .getName(), null); } } } //返回任务实例 return processInstance; }
ProcessInstance.java
/* * 流程实例开始运行 * @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 "); } //取得工作流网实例,这个接下来看:开源工作流Fireflow源码分析之启动流程实例二
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); //运行工作流网实例,从startnode开始,这个接下来看:开源工作流Fireflow源码分析之运行流程实例一 netInstance.run(this); }