/// <summary>创建一个工作流网实例</summary> /// <param name="workflowDef"></param> /// <returns></returns> public INetInstance createNetInstance(WorkflowDefinition workflowDef) { if (workflowDef == null) { return(null); } WorkflowProcess workflowProcess = null; workflowProcess = workflowDef.getWorkflowProcess();//解析fpdl //Map nodeInstanceMap = new HashMap(); if (workflowProcess == null) { throw new KernelException(null, null, "The WorkflowProcess property of WorkflowDefinition[processId=" + workflowDef.ProcessId + "] is null. "); } String validateMsg = workflowProcess.validate();//校验工作流定义是否有效 if (validateMsg != null) { throw new KernelException(null, null, validateMsg); } NetInstance netInstance = new NetInstance(workflowProcess, KernelExtensions); //netInstance.setWorkflowProcess(workflowProcess); netInstance.Version = workflowDef.Version;//设置版本号 //map的key的组成规则:流程定义ID_V_版本号 netInstanceMap.Add(workflowDef.ProcessId + "_V_" + workflowDef.Version, netInstance); //netInstance.setRtCxt(new RuntimeContext()); return(netInstance); }
public void run(IWorkflowSession currentSession, RuntimeContext runtimeContext, IProcessInstance processInstance, ITaskInstance taskInstance)// throws EngineException, KernelException { if (taskInstance.TaskType != TaskTypeEnum.SUBFLOW) { throw new EngineException(processInstance, taskInstance.Activity, "DefaultSubflowTaskInstanceRunner:TaskInstance的任务类型错误,只能为SUBFLOW类型"); } Task task = taskInstance.Task; SubWorkflowProcess Subflow = ((SubflowTask)task).SubWorkflowProcess; WorkflowDefinition subWorkflowDef = runtimeContext.DefinitionService.GetTheLatestVersionOfWorkflowDefinition(Subflow.WorkflowProcessId); if (subWorkflowDef == null) { WorkflowProcess parentWorkflowProcess = taskInstance.WorkflowProcess; throw new EngineException(taskInstance.ProcessInstanceId, parentWorkflowProcess, taskInstance.TaskId, "系统中没有Id为" + Subflow.WorkflowProcessId + "的流程定义"); } WorkflowProcess subWorkflowProcess = subWorkflowDef.getWorkflowProcess(); if (subWorkflowProcess == null) { WorkflowProcess parentWorkflowProcess = taskInstance.WorkflowProcess; throw new EngineException(taskInstance.ProcessInstanceId, parentWorkflowProcess, taskInstance.TaskId, "系统中没有Id为" + Subflow.WorkflowProcessId + "的流程定义"); } IPersistenceService persistenceService = runtimeContext.PersistenceService; //更改任务的状态和开始时间 ((TaskInstance)taskInstance).State = TaskInstanceStateEnum.RUNNING; ((TaskInstance)taskInstance).StartedTime = runtimeContext.CalendarService.getSysDate(); persistenceService.SaveOrUpdateTaskInstance(taskInstance); IProcessInstance subProcessInstance = currentSession.createProcessInstance(subWorkflowProcess.Name, taskInstance); //初始化流程变量,从父实例获得初始值 Dictionary <String, Object> processVars = ((TaskInstance)taskInstance).AliveProcessInstance.ProcessInstanceVariables; List <DataField> datafields = subWorkflowProcess.DataFields; for (int i = 0; datafields != null && i < datafields.Count; i++) { DataField df = (DataField)datafields[i]; if (df.DataType == DataTypeEnum.STRING) { if (processVars[df.Name] != null && (processVars[df.Name] is String)) { subProcessInstance.setProcessInstanceVariable(df.Name, processVars[df.Name]); } else if (df.InitialValue != null) { subProcessInstance.setProcessInstanceVariable(df.Name, df.InitialValue); } else { subProcessInstance.setProcessInstanceVariable(df.Name, ""); } } else if (df.DataType == DataTypeEnum.INTEGER) { if (processVars[df.Name] != null && (processVars[df.Name] is Int32)) { subProcessInstance.setProcessInstanceVariable(df.Name, processVars[df.Name]); } else if (df.InitialValue != null) { try { Int32 intValue = Int32.Parse(df.InitialValue); subProcessInstance.setProcessInstanceVariable(df.Name, intValue); } catch { } } else { subProcessInstance.setProcessInstanceVariable(df.Name, (Int32)0); } } else if (df.DataType == DataTypeEnum.FLOAT) { if (processVars[df.Name] != null && (processVars[df.Name] is float)) { subProcessInstance.setProcessInstanceVariable(df.Name, processVars[df.Name]); } else if (df.InitialValue != null) { float floatValue = float.Parse(df.InitialValue); subProcessInstance.setProcessInstanceVariable(df.Name, floatValue); } else { subProcessInstance.setProcessInstanceVariable(df.Name, (float)0); } } else if (df.DataType == DataTypeEnum.BOOLEAN) { if (processVars[df.Name] != null && (processVars[df.Name] is Boolean)) { subProcessInstance.setProcessInstanceVariable(df.Name, processVars[df.Name]); } else if (df.InitialValue != null) { Boolean booleanValue = Boolean.Parse(df.InitialValue); subProcessInstance.setProcessInstanceVariable(df.Name, booleanValue); } else { subProcessInstance.setProcessInstanceVariable(df.Name, false); } } else if (df.DataType == DataTypeEnum.DATETIME) { //TODO 需要完善一下 ( 父子流程数据传递——时间类型的数据还未做传递-不知道为什么?) //wmj2003 20090925 补充上了 if (processVars[df.Name] != null && (processVars[df.Name] is DateTime)) { subProcessInstance.setProcessInstanceVariable(df.Name, processVars[df.Name]); } else if (df.InitialValue != null) { try { DateTime dateTmp = DateTime.Parse(df.InitialValue); subProcessInstance.setProcessInstanceVariable(df.Name, dateTmp); } catch { subProcessInstance.setProcessInstanceVariable(df.Name, null); } } } //TODO 应将下面这句删除!这里还需要吗?应该直接subProcessInstance.run()就可以了。 runtimeContext.PersistenceService.SaveOrUpdateProcessInstance(subProcessInstance); subProcessInstance.run(); } }
public object doInWorkflowSession(RuntimeContext ctx) { WorkflowDefinition workflowDef = ctx.DefinitionService.GetTheLatestVersionOfWorkflowDefinition(wfprocessId); WorkflowProcess wfProcess = null; wfProcess = workflowDef.getWorkflowProcess(); if (wfProcess == null) { throw new Exception("Workflow process NOT found,id=[" + wfprocessId + "]"); } ProcessInstance processInstance = new ProcessInstance(); processInstance.CreatorId = creatorId; processInstance.ProcessId = wfProcess.Id; processInstance.Version = workflowDef.Version; processInstance.DisplayName = wfProcess.DisplayName; processInstance.Name = wfProcess.Name; processInstance.State = ProcessInstanceEnum.INITIALIZED; processInstance.CreatedTime = ctx.CalendarService.getSysDate(); processInstance.ParentProcessInstanceId = parentProcessInstanceId; processInstance.ParentTaskInstanceId = parentTaskInstanceId; ctx.PersistenceService.SaveOrUpdateProcessInstance( processInstance); // 初始化流程变量 List <DataField> datafields = wfProcess.DataFields; for (int i = 0; datafields != null && i < datafields.Count; i++) { DataField df = (DataField)datafields[i]; if (df.DataType == DataTypeEnum.STRING) { if (df.InitialValue != null) { processInstance.setProcessInstanceVariable(df.Name, df.InitialValue); } else { processInstance.setProcessInstanceVariable(df.Name, ""); } } else if (df.DataType == DataTypeEnum.INTEGER) { if (df.InitialValue != null) { try { Int32 intValue = Int32.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, intValue); } catch (Exception) { } } else { processInstance.setProcessInstanceVariable(df.Name, (Int32)0); } } else if (df.DataType == DataTypeEnum.LONG) { if (df.InitialValue != null) { try { long longValue = long.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, longValue); } catch (Exception) { } } else { processInstance.setProcessInstanceVariable(df.Name, (long)0); } } else if (df.DataType == DataTypeEnum.FLOAT) { if (df.InitialValue != null) { float floatValue = float.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, floatValue); } else { processInstance.setProcessInstanceVariable(df.Name, (float)0); } } else if (df.DataType == DataTypeEnum.DOUBLE) { if (df.InitialValue != null) { Double doubleValue = Double.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df .Name, doubleValue); } else { processInstance.setProcessInstanceVariable(df.Name, (Double)0); } } else if (df.DataType == DataTypeEnum.BOOLEAN) { if (df.InitialValue != null) { Boolean booleanValue = Boolean.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, booleanValue); } else { processInstance.setProcessInstanceVariable(df.Name, false); } } else if (df.DataType == DataTypeEnum.DATETIME) { // TODO 需要完善一下 if (df.InitialValue != null && df.DataPattern != null) { try { //SimpleDateFormat dFormat = new SimpleDateFormat(df.DataPattern); DateTime dateTmp = DateTime.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, dateTmp); } catch (Exception) { processInstance.setProcessInstanceVariable(df.Name, null); //e.printStackTrace(); } } else { processInstance.setProcessInstanceVariable(df.Name, null); } } } ctx.PersistenceService.SaveOrUpdateProcessInstance( processInstance); return(processInstance); }
/// <summary>创建一个新的流程实例 (create a new process instance )</summary> /// <param name="workflowProcessId">流程定义ID</param> /// <param name="creatorId">创建人ID</param> /// <param name="parentProcessInstanceId">父流程实例ID</param> /// <param name="parentTaskInstanceId">父任务实例ID</param> protected IProcessInstance _createProcessInstance(String workflowProcessId, String creatorId, String parentProcessInstanceId, String parentTaskInstanceId) { String wfprocessId = workflowProcessId; WorkflowDefinition workflowDef = RuntimeContext.DefinitionService.GetTheLatestVersionOfWorkflowDefinition(wfprocessId); WorkflowProcess wfProcess = workflowDef.getWorkflowProcess(); if (wfProcess == null) { throw new Exception("Workflow process NOT found,id=[" + wfprocessId + "]"); } IProcessInstance processInstance = (IProcessInstance)this.execute( new WorkflowSessionIProcessInstanceCreateProcessInstance(creatorId, wfProcess, workflowDef, parentProcessInstanceId, parentTaskInstanceId)); // 初始化流程变量 processInstance.ProcessInstanceVariables = new Dictionary <String, Object>(); List <DataField> datafields = wfProcess.DataFields; for (int i = 0; datafields != null && i < datafields.Count; i++) { DataField df = datafields[i]; if (df.DataType == DataTypeEnum.STRING) { if (df.InitialValue != null) { processInstance.setProcessInstanceVariable(df.Name, df.InitialValue); } else { processInstance.setProcessInstanceVariable(df.Name, ""); } } else if (df.DataType == DataTypeEnum.INTEGER) { if (df.InitialValue != null) { try { int intValue = int.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, intValue); } catch { } } else { processInstance.setProcessInstanceVariable(df.Name, 0); } } else if (df.DataType == DataTypeEnum.LONG) { if (df.InitialValue != null) { try { long longValue = long.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, longValue); } catch { } } else { processInstance.setProcessInstanceVariable(df.Name, (long)0); } } else if (df.DataType == DataTypeEnum.FLOAT) { if (df.InitialValue != null) { float floatValue = float.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, floatValue); } else { processInstance.setProcessInstanceVariable(df.Name, (float)0); } } else if (df.DataType == DataTypeEnum.DOUBLE) { if (df.InitialValue != null) { Double doubleValue = Double.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, doubleValue); } else { processInstance.setProcessInstanceVariable(df.Name, (Double)0); } } else if (df.DataType == DataTypeEnum.BOOLEAN) { if (df.InitialValue != null) { Boolean booleanValue = Boolean.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, booleanValue); } else { processInstance.setProcessInstanceVariable(df.Name, false); } } else if (df.DataType == DataTypeEnum.DATETIME) { // TODO 需要完善一下 if (df.InitialValue != null && df.DataPattern != null) { try { DateTime dateTmp = DateTime.Parse(df.InitialValue); processInstance.setProcessInstanceVariable(df.Name, dateTmp); } catch { processInstance.setProcessInstanceVariable(df.Name, null); } } else { processInstance.setProcessInstanceVariable(df.Name, null); } } } return(processInstance); }