/// <summary> /// 重置所有流程实例的状态,如果流程状态是Running将设置为Idled,通过存储过程 /// </summary> public void ResetWorkflowRunning() { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { workflowPersistenceModelDataContext.spWorkflowProcessResetRunningStatus(); } }
/// <summary> /// WorkflowProcessInstanceStatus表获取状态,为什么把状态放在独立的表里呢? /// </summary> /// <param name="processId"></param> /// <returns></returns> public ProcessStatus GetInstanceStatus(Guid processId) { ProcessStatus result; using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessInstanceStatus instance = workflowPersistenceModelDataContext.WorkflowProcessInstanceStatus.FirstOrDefault((WorkflowProcessInstanceStatus wpis) => wpis.Id == processId); if (instance == null) { result = ProcessStatus.NotFound; } else { ProcessStatus processStatus = ProcessStatus.All.SingleOrDefault((ProcessStatus ins) => ins.Id == instance.Status); if (processStatus == null) { result = ProcessStatus.Unknown; } else { result = processStatus; } } } return(result); }
public void SetWorkflowIniialized(ProcessInstance processInstance) { using (TransactionScope serializableSupressedScope = PredefinedTransactionScopes.SerializableSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessInstanceStatus workflowProcessInstanceStatus = workflowPersistenceModelDataContext.WorkflowProcessInstanceStatus.SingleOrDefault((WorkflowProcessInstanceStatus wpis) => wpis.Id == processInstance.ProcessId); if (workflowProcessInstanceStatus == null) { workflowProcessInstanceStatus = new WorkflowProcessInstanceStatus { Id = processInstance.ProcessId, Lock = Guid.NewGuid(), Status = ProcessStatus.Initialized.Id }; workflowPersistenceModelDataContext.WorkflowProcessInstanceStatus.InsertOnSubmit(workflowProcessInstanceStatus); } else { workflowProcessInstanceStatus.Status = ProcessStatus.Initialized.Id; } workflowPersistenceModelDataContext.SubmitChanges(); } serializableSupressedScope.Complete(); } }
private IEnumerable <ParameterDefinitionWithValue> GetPersistedProcessParameters(Guid processId, ProcessDefinition processDefinition) { List <ParameterDefinition> persistenceParameters = processDefinition.PersistenceParameters.ToList <ParameterDefinition>(); List <ParameterDefinitionWithValue> list = new List <ParameterDefinitionWithValue>(persistenceParameters.Count <ParameterDefinition>()); List <WorkflowProcessInstancePersistence> list2; using (PredefinedTransactionScopes.ReadUncommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { list2 = ( from wpip in workflowPersistenceModelDataContext.WorkflowProcessInstancePersistences where wpip.ProcessId == processId && ( from pp in persistenceParameters select pp.Name).Contains(wpip.ParameterName) select wpip).ToList <WorkflowProcessInstancePersistence>(); } } foreach (WorkflowProcessInstancePersistence persistedParameter in list2) { ParameterDefinition parameterDefinition = persistenceParameters.Single((ParameterDefinition p) => p.Name == persistedParameter.ParameterName); list.Add(ParameterDefinition.Create(parameterDefinition, this.DeserializeParameter(persistedParameter.Value, parameterDefinition.Type))); } return(list); }
public void SetWorkflowRunning(ProcessInstance processInstance) { using (TransactionScope serializableSupressedScope = PredefinedTransactionScopes.SerializableSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessInstanceStatus workflowProcessInstanceStatus = workflowPersistenceModelDataContext.WorkflowProcessInstanceStatus.SingleOrDefault((WorkflowProcessInstanceStatus wpis) => wpis.Id == processInstance.ProcessId); if (workflowProcessInstanceStatus == null) { throw new StatusNotDefinedException(); } workflowProcessInstanceStatus.Lock = Guid.NewGuid();//为什么要Lock字段 workflowProcessInstanceStatus = workflowPersistenceModelDataContext.WorkflowProcessInstanceStatus.SingleOrDefault((WorkflowProcessInstanceStatus wpis) => wpis.Id == processInstance.ProcessId); if (workflowProcessInstanceStatus == null) { throw new StatusNotDefinedException(); } if (workflowProcessInstanceStatus.Status == ProcessStatus.Running.Id) { throw new ImpossibleToSetStatusException(); } workflowProcessInstanceStatus.Status = ProcessStatus.Running.Id; workflowPersistenceModelDataContext.SubmitChanges(); } serializableSupressedScope.Complete(); } }
public WorkflowPersistenceModelDataContext CreateContext() { var context = new WorkflowPersistenceModelDataContext(ConnectionString) { CommandTimeout = 600, DeferredLoadingEnabled = true }; return context; }
/// <summary> /// 检查WorkflowProcessInstance是否存在 /// </summary> /// <param name="processId">Document.id</param> /// <returns></returns> public bool IsProcessExists(Guid processId) { bool result; using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { result = (workflowPersistenceModelDataContext.WorkflowProcessInstances.Count((WorkflowProcessInstance wpi) => wpi.Id == processId) > 0); } return(result); }
public WorkflowPersistenceModelDataContext CreateContext() { var context = new WorkflowPersistenceModelDataContext(ConnectionString) { CommandTimeout = 600, DeferredLoadingEnabled = true }; return(context); }
/// <summary> /// 根据WorkflowProcessScheme的schemeId获取指定的WorkflowProcessScheme /// 并根据其Scheme的内容通过构建SchemeDefinition<XElement>的审批方案的XML关系定义 /// </summary> /// <param name="schemeId"></param> /// <returns></returns> public SchemeDefinition <XElement> GetProcessSchemeBySchemeId(Guid schemeId) { WorkflowProcessScheme workflowProcessScheme; using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { workflowProcessScheme = workflowPersistenceModelDataContext.WorkflowProcessSchemes.FirstOrDefault((WorkflowProcessScheme pss) => pss.Id == schemeId); } if (workflowProcessScheme == null || string.IsNullOrEmpty(workflowProcessScheme.Scheme)) { throw new SchemeNotFoundException(); } //XML如何与SchemeDefinition<XElement>关联起来的呢?SchemeDefinition只是简单的保存schemeId,Scheme等的数据,未进行审批关系分析 return(new SchemeDefinition <XElement>(schemeId, XElement.Parse(workflowProcessScheme.Scheme), workflowProcessScheme.IsObsolete)); }
public void SavePersistenceParameters(ProcessInstance processInstance) { var list = ( from ptp in processInstance.ProcessParameters where ptp.Purpose == ParameterPurpose.Persistence select new { Parameter = ptp, SerializedValue = this.SerializeParameter(ptp.Value) }).ToList(); List <ParameterDefinition> persistenceParameters = processInstance.ProcessScheme.PersistenceParameters.ToList <ParameterDefinition>(); using (TransactionScope readUncommittedSupressedScope = PredefinedTransactionScopes.ReadUncommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { List <WorkflowProcessInstancePersistence> source = ( from wpip in workflowPersistenceModelDataContext.WorkflowProcessInstancePersistences where wpip.ProcessId == processInstance.ProcessId && ( from pp in persistenceParameters select pp.Name).Contains(wpip.ParameterName) select wpip).ToList <WorkflowProcessInstancePersistence>(); foreach (var parameterDefinitionWithValue in list) { WorkflowProcessInstancePersistence workflowProcessInstancePersistence = source.SingleOrDefault((WorkflowProcessInstancePersistence pp) => pp.ParameterName == parameterDefinitionWithValue.Parameter.Name); if (workflowProcessInstancePersistence == null) { workflowProcessInstancePersistence = new WorkflowProcessInstancePersistence { Id = Guid.NewGuid(), ParameterName = parameterDefinitionWithValue.Parameter.Name, ProcessId = processInstance.ProcessId, Value = parameterDefinitionWithValue.SerializedValue }; workflowPersistenceModelDataContext.WorkflowProcessInstancePersistences.InsertOnSubmit(workflowProcessInstancePersistence); } else { workflowProcessInstancePersistence.Value = parameterDefinitionWithValue.SerializedValue; } } workflowPersistenceModelDataContext.SubmitChanges(); } readUncommittedSupressedScope.Complete(); } }
/// <summary> /// 设置WorkflowProcessInstanceStatus新的状态 /// </summary> /// <param name="processId"></param> /// <param name="status"></param> private void SetCustomStatus(Guid processId, ProcessStatus status) { using (TransactionScope serializableSupressedScope = PredefinedTransactionScopes.SerializableSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessInstanceStatus workflowProcessInstanceStatus = workflowPersistenceModelDataContext.WorkflowProcessInstanceStatus.SingleOrDefault((WorkflowProcessInstanceStatus wpis) => wpis.Id == processId); if (workflowProcessInstanceStatus == null) { throw new StatusNotDefinedException(); } workflowProcessInstanceStatus.Status = status.Id; workflowPersistenceModelDataContext.SubmitChanges(); } serializableSupressedScope.Complete(); } }
private WorkflowProcessInstance GetProcessInstance(Guid processId) { WorkflowProcessInstance result; using (PredefinedTransactionScopes.ReadCommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessInstance workflowProcessInstance = workflowPersistenceModelDataContext.WorkflowProcessInstances.SingleOrDefault((WorkflowProcessInstance wpi) => wpi.Id == processId); if (workflowProcessInstance == null) { throw new ProcessNotFoundException(); } result = workflowProcessInstance; } } return(result); }
/// <summary> /// 将WorkflowScheme表中的Scheme的XML转换为XElement /// 将指定流程转换为XElement /// </summary> /// <param name="processName">流程名,如:SimpleWF</param> /// <param name="schemeId">没有用</param> /// <param name="parameters">没用用,但必须要有值</param> /// <returns></returns> public XElement Generate(string processName, Guid schemeId, IDictionary <string, IEnumerable <object> > parameters) { if (parameters.Count > 0) { throw new InvalidOperationException("Parameters not supported"); } // 返回如:SimpleWF string code = (!this.TemplateTypeMapping.ContainsKey(processName.ToLower())) ? processName : this.TemplateTypeMapping[processName.ToLower()]; WorkflowScheme workflowScheme = null; using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { workflowScheme = workflowPersistenceModelDataContext.WorkflowSchemes.FirstOrDefault((WorkflowScheme ws) => ws.Code == code); } if (workflowScheme == null) { throw new InvalidOperationException(string.Format("Scheme with Code={0} not found", code)); } return(XElement.Parse(workflowScheme.Scheme)); }
public void BindProcessToNewScheme(ProcessInstance processInstance, bool resetIsDeterminingParametersChanged) { using (TransactionScope readCommittedSupressedScope = PredefinedTransactionScopes.ReadCommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessInstance workflowProcessInstance = workflowPersistenceModelDataContext.WorkflowProcessInstances.SingleOrDefault((WorkflowProcessInstance wpi) => wpi.Id == processInstance.ProcessId); if (workflowProcessInstance == null) { throw new ProcessNotFoundException(); } workflowProcessInstance.SchemeId = new Guid?(processInstance.SchemeId); if (resetIsDeterminingParametersChanged) { workflowProcessInstance.IsDeterminingParametersChanged = false; } workflowPersistenceModelDataContext.SubmitChanges(); } readCommittedSupressedScope.Complete(); } }
/// <summary> /// WorkflowProcessInstances /// 根据公文流转实例processId获取其基于的审批流程方案WorkflowProcessScheme, /// 并根据其Scheme的XML内容转换为XElement结构 /// </summary> /// <param name="processId">如:具体《建设项目选址意见书》ID</param> /// <returns></returns> public SchemeDefinition <XElement> GetProcessSchemeByProcessId(Guid processId) { WorkflowProcessInstance workflowProcessInstance; using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { workflowProcessInstance = workflowPersistenceModelDataContext.WorkflowProcessInstances.FirstOrDefault((WorkflowProcessInstance pis) => pis.Id == processId); } if (workflowProcessInstance == null) { throw new ProcessNotFoundException(); } if (!workflowProcessInstance.SchemeId.HasValue) //模版类(WorkflowProcessScheme)不能为空 { throw new SchemeNotFoundException(); } SchemeDefinition <XElement> processSchemeBySchemeId = this.GetProcessSchemeBySchemeId(workflowProcessInstance.SchemeId.Value); processSchemeBySchemeId.IsDeterminingParametersChanged = workflowProcessInstance.IsDeterminingParametersChanged; return(processSchemeBySchemeId); }
public RuntimeTimer LoadTimer(Guid runtimeId) { string timer; using (TransactionScope readUncommittedSupressedScope = PredefinedTransactionScopes.ReadUncommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowRuntime workflowRuntime = workflowPersistenceModelDataContext.WorkflowRuntimes.FirstOrDefault((WorkflowRuntime wr) => wr.RuntimeId == runtimeId); if (workflowRuntime == null) { workflowRuntime = new WorkflowRuntime { RuntimeId = runtimeId, Timer = string.Empty }; workflowPersistenceModelDataContext.WorkflowRuntimes.InsertOnSubmit(workflowRuntime); } timer = workflowRuntime.Timer; workflowPersistenceModelDataContext.SubmitChanges(); } readUncommittedSupressedScope.Complete(); } if (string.IsNullOrEmpty(timer)) { return(new RuntimeTimer()); } DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(Dictionary <TimerKey, DateTime>)); RuntimeTimer result; using (StringReader stringReader = new StringReader(timer)) { using (XmlReader xmlReader = XmlReader.Create(stringReader)) { result = new RuntimeTimer(dataContractSerializer.ReadObject(xmlReader) as IDictionary <TimerKey, DateTime>); } } return(result); }
/// <summary> /// 序列化TransitionDefinition的内容到WorkflowProcessTransitionHistory /// </summary> /// <param name="processInstance"></param> /// <param name="transition"></param> public void UpdatePersistenceState(ProcessInstance processInstance, TransitionDefinition transition) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowProcessTransitionHistory entity = new WorkflowProcessTransitionHistory { ActorIdentityId = Guid.NewGuid(), ExecutorIdentityId = Guid.NewGuid(), Id = Guid.NewGuid(), IsFinalised = false, ProcessId = processInstance.ProcessId, FromActivityName = transition.From.Name, FromStateName = transition.From.State, ToActivityName = transition.To.Name, ToStateName = transition.To.State, TransitionClassifier = transition.Classifier.ToString(), TransitionTime = DateTime.Now }; workflowPersistenceModelDataContext.WorkflowProcessTransitionHistories.InsertOnSubmit(entity); workflowPersistenceModelDataContext.SubmitChanges(); } }
/// <summary> /// 保存新的WorkflowProcessScheme,可以动态的修改参数后保存 /// </summary> /// <param name="processName"></param> /// <param name="schemeId"></param> /// <param name="scheme"></param> /// <param name="parameters"></param> public void SaveScheme(string processName, Guid schemeId, XElement scheme, IDictionary <string, IEnumerable <object> > parameters) { string text = this.SerializeParameters(parameters); string definingParametersHash = HashHelper.GenerateStringHash(text); using (TransactionScope serializableSupressedScope = PredefinedTransactionScopes.SerializableSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { List <WorkflowProcessScheme> list = ( from wps in workflowPersistenceModelDataContext.WorkflowProcessSchemes where wps.DefiningParametersHash == definingParametersHash && wps.ProcessName == processName && !wps.IsObsolete select wps).ToList <WorkflowProcessScheme>(); if (list.Count <WorkflowProcessScheme>() > 0) { foreach (WorkflowProcessScheme current in list) { if (current.DefiningParameters == text) { throw new SchemeAlredyExistsException(); } } } WorkflowProcessScheme entity = new WorkflowProcessScheme { Id = schemeId, DefiningParameters = text, DefiningParametersHash = definingParametersHash, Scheme = scheme.ToString(), ProcessName = processName }; workflowPersistenceModelDataContext.WorkflowProcessSchemes.InsertOnSubmit(entity); workflowPersistenceModelDataContext.SubmitChanges(); } serializableSupressedScope.Complete(); } }
public void SaveTimer(Guid runtimeId, RuntimeTimer timer) { DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(Dictionary <TimerKey, DateTime>)); string timer2; using (StringWriter stringWriter = new StringWriter()) { using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter)) { dataContractSerializer.WriteObject(xmlWriter, timer.Timers); xmlWriter.Flush(); timer2 = stringWriter.ToString(); } } using (TransactionScope readUncommittedSupressedScope = PredefinedTransactionScopes.ReadUncommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowRuntime workflowRuntime = workflowPersistenceModelDataContext.WorkflowRuntimes.FirstOrDefault((WorkflowRuntime wr) => wr.RuntimeId == runtimeId); if (workflowRuntime == null) { workflowRuntime = new WorkflowRuntime { RuntimeId = runtimeId, Timer = timer2 }; workflowPersistenceModelDataContext.WorkflowRuntimes.InsertOnSubmit(workflowRuntime); } else { workflowRuntime.Timer = timer2; } workflowPersistenceModelDataContext.SubmitChanges(); } readUncommittedSupressedScope.Complete(); } }
/// <summary> /// 根据公文流转处理流程名称processName,以及参数获取适配的SchemeDefinition /// 注意这里没有processId /// </summary> /// <param name="processName"></param> /// <param name="parameters">参数,转换为hash在WorkflowProcessScheme中的DefiningParametersHash进行比较</param> /// <param name="ignoreObsolete"></param> /// <returns></returns> public SchemeDefinition <XElement> GetProcessSchemeWithParameters(string processName, IDictionary <string, IEnumerable <object> > parameters, bool ignoreObsolete) { string definingParameters = this.SerializeParameters(parameters); string hash = HashHelper.GenerateStringHash(definingParameters); IEnumerable <WorkflowProcessScheme> source; using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { source = ( from pss in workflowPersistenceModelDataContext.WorkflowProcessSchemes where pss.ProcessName == processName && pss.DefiningParametersHash == hash && (!ignoreObsolete || !pss.IsObsolete) select pss).ToList <WorkflowProcessScheme>(); } if (!source.Any()) // 表示数据表WorkflowProcessScheme中没有匹配的WorkflowProcessScheme记录,source.Count<WorkflowProcessScheme>() < 1 卢远宗修改 { throw new SchemeNotFoundException(); } if (source.Count <WorkflowProcessScheme>() == 1) { var workflowProcessScheme = source.First <WorkflowProcessScheme>(); return(new SchemeDefinition <XElement>(workflowProcessScheme.Id, XElement.Parse(workflowProcessScheme.Scheme), workflowProcessScheme.IsObsolete)); } //如果有多个匹配项的WorkflowProcessScheme using (IEnumerator <WorkflowProcessScheme> enumerator = ( from processScheme in source where processScheme.DefiningParameters == definingParameters select processScheme).GetEnumerator()) { if (enumerator.MoveNext()) { WorkflowProcessScheme current = enumerator.Current; return(new SchemeDefinition <XElement>(current.Id, XElement.Parse(current.Scheme), current.IsObsolete)); } } throw new SchemeNotFoundException(); }
public void InitializeProcess(ProcessInstance processInstance) { using (TransactionScope readCommittedSupressedScope = PredefinedTransactionScopes.ReadCommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { var workflowProcessInstance = workflowPersistenceModelDataContext.WorkflowProcessInstances.SingleOrDefault((WorkflowProcessInstance wpi) => wpi.Id == processInstance.ProcessId); if (workflowProcessInstance != null) { throw new ProcessAlredyExistsException(); } var entity = new WorkflowProcessInstance { Id = processInstance.ProcessId, SchemeId = new Guid?(processInstance.SchemeId), ActivityName = processInstance.ProcessScheme.InitialActivity.Name, StateName = processInstance.ProcessScheme.InitialActivity.State }; workflowPersistenceModelDataContext.WorkflowProcessInstances.InsertOnSubmit(entity); workflowPersistenceModelDataContext.SubmitChanges(); } readCommittedSupressedScope.Complete(); } }