/// <summary> /// 统一创建流程引擎 /// </summary> /// <returns></returns> public FlowEngine BuildEngine() { FlowEngine flowEngine = new FlowEngine(); flowEngine.FinishedHandler -= new FlowEngine.FinishedEventHandler(FlowManager.FlowFinished); flowEngine.FinishedHandler += new FlowEngine.FinishedEventHandler(FlowManager.FlowFinished); return flowEngine; }
/// <summary> /// 拼接当前流程至已存在流程后 /// </summary> /// <param name="newFlowEngine">新流程</param> /// <param name="oldFlowEngine">old流程</param> public static void Concat(FlowEngine newFlowEngine, FlowEngine oldFlowEngine) { foreach (var step in newFlowEngine.FlowSteps) { oldFlowEngine.Add(step); } }
/// <summary> /// 普通节点设置IActor /// 条件节点设置ICondition /// </summary> /// <param name="flowEngine">流程对象</param> public void Assign(FlowEngine flowEngine, FlowAttachment flowAttachment) { foreach (var step in flowEngine.FlowSteps) { foreach (var node in step.Nodes) { //node.Participant.Actor = new DepartmentActor(); if (node is NodeCondition) { ((NodeCondition)node).Condition = new RevenuCondition(); } } } flowEngine.Reset(flowAttachment); }
/// <summary> /// 拼接当前流程至已存在流程后 /// </summary> /// <param name="id">编号</param> /// <param name="category">分类</param> /// <param name="flowEngine">新流程</param> public static void Concat(int id, FlowEngine newFlowEngine) { FlowManager flowMgr = FlowManager.Instance(); if (flowMgr.FlowExists(id)) { int index = 0; FlowEngine oldFlowEngine = flowMgr.Load(id); Step returnedStep = FindLastReturned(oldFlowEngine); if (returnedStep == null) return; SetDisableAtReturned(oldFlowEngine, returnedStep); foreach (var step in oldFlowEngine.FlowSteps) { newFlowEngine.Insert(step, index++); } } }
/// <summary> /// 序列化流程至文件 BinaryFormatter /// </summary> /// <param name="path">文件全路径名称</param> /// <param name="workFlow">流程对象</param> public static void Save(string path, FlowEngine workFlow) { try { if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } Stream sw = File.Create(path); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(sw, workFlow); sw.Close(); } catch (Exception ex) { throw new Exception("Exception save workflow file", ex); } }
/// <summary> /// 保存流程数据 /// </summary> /// <param name="flowEngine">流程</param> /// <param name="category">分类</param> /// <param name="owner">具体业务数据对应的PK</param> public void FlowSave(Eastday.WorkFlowEngine.FlowEngine flowEngine) { using (WorkFlowBLL workFlowBLL = new WorkFlowBLL()) { workFlowBLL.DbContext = workFlowBLL.CreateDbContext(true); WorkFlowEntity workFlowEntity = workFlowBLL.Usp_Flow_ByOwner(flowEngine.Attachment.Owner); if (workFlowEntity == null) { workFlowEntity = new WorkFlowEntity(); } workFlowEntity.I_Owner = flowEngine.Attachment.Owner; workFlowEntity.I_State = (int)flowEngine.FlowState; workFlowEntity.I_Flag = 1; //ParticipantEntity var aCurrent = flowEngine.GetCurrent(); var steps = from step in flowEngine.FlowSteps where !(step is StepEnd) select step; List <ParticipantEntity> participants = new List <ParticipantEntity>(); foreach (var step in steps) { foreach (var node in step.Nodes) { ParticipantEntity participantEntity = new ParticipantEntity(); participantEntity.I_Reference = node.Participant.Reference; participantEntity.I_WorkFlow = workFlowEntity.Id; participantEntity.C_Step = step.Identification; participantEntity.C_Node = node.Identification; participantEntity.I_Auditer = node.Conclusion.Auditer; participantEntity.D_Audit = node.Conclusion.AuditDate; participantEntity.I_Bind = node.Participant.Category; participantEntity.I_Current = step == aCurrent && node.AuditType == AuditType.UnAudit ? 1 : (int)node.Conclusion.AuditType; participants.Add(participantEntity); } } if (workFlowBLL.Usp_Flow_Insert(workFlowEntity, participants) > 0) { workFlowBLL.DbContext.CommitTransaction(); this.Save(flowEngine); } } }
/// <summary> /// 反序列化加载流程 BinaryFormatter /// </summary> /// <param name="path">文件全路径名称</param> /// <returns>流程对象</returns> public static FlowEngine Load(string path) { if (!System.IO.File.Exists(path)) { return(null); } try { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); FlowEngine workFlow = (FlowEngine)bf.Deserialize(fs); fs.Close(); return(workFlow); } catch (Exception ex) { throw new Exception("Exception load workflow file", ex); } }
private static Step FindLastReturned(FlowEngine flowEngine) { IEnumerable<Step> reverseSteps = flowEngine.FlowSteps.Reverse(); foreach (var step in reverseSteps) { foreach (var node in step.Nodes) { if (node.AuditType == AuditType.Returned) return step; } } return null; }
private static Step FindByReference(FlowEngine flowEngine, long reference) { foreach (var step in flowEngine.FlowSteps) { foreach (var node in step.Nodes) { if (node.AuditType == AuditType.UnAudit && node.Participant.Reference == reference) { return step; } } } return null; }
/// <summary> /// 保存模板 /// </summary> /// <param name="template">模板名称</param> /// <param name="flowEngine">流程</param> /// <remarks>命名: TEMP_0001 TEMP_002</remarks> public void TemplateSave(string template, FlowEngine flowEngine) { string file = this.GetTemplateFile(template); string directory = System.IO.Path.GetDirectoryName(file); if (!System.IO.Directory.Exists(directory)) { System.IO.Directory.CreateDirectory(directory); } FlowEngine.Save(file, flowEngine); }
private static void SetDisableAtReturned(FlowEngine flowEngine, Step returnedStep) { LinkedListNode<Step> stepNode = flowEngine.FlowSteps.Find(returnedStep); if (stepNode == null) return; stepNode = stepNode.Next; while (stepNode != null) { Step step = stepNode.Value; foreach (var node in step.Nodes) { if (node.AuditType != AuditType.Disable) { node.AuditType = AuditType.Disable; } } stepNode = stepNode.Next; } }
/// <summary> /// 流程终止 /// </summary> /// <param name="flowEngine"></param> public bool Abort(FlowEngine flowEngine, string auditDesc, int userId) { if (this.Audit(flowEngine, AuditType.Passed, auditDesc, userId)) { flowEngine.Abort(); return true; } return false; }
/// <summary> /// 流程退回 /// </summary> /// <param name="flowEngine"></param> public bool Returned(FlowEngine flowEngine, AuditType auditType, string auditDesc, int userId) { if (this.Audit(flowEngine, auditType, auditDesc, userId)) { flowEngine.Returned(); return true; } return false; }
/// <summary> /// 判断是否属于当前人审核 /// </summary> /// <param name="flowEngine"></param> /// <param name="userId"></param> /// <returns></returns> public bool CanAudit(FlowEngine flowEngine, int userId) { Step current = flowEngine.GetCurrent(); if (current == null || current.NodeValidCount == 0) return false; IList<Node> nodes = current.Nodes; using (AppBLL bll = new AppBLL()) { IList<FunctionEntity> userFuncs = bll.FillList<FunctionEntity>("Usp_Funcs_ByUser", new { UserId = userId }); foreach (var userFunc in userFuncs) { var auditNodes = from node in nodes where node.Participant.Reference == userFunc.Id || node.Participant.Department == userId select node; if (auditNodes.Any()) return true; } } return false; }
/// <summary> /// 流程审核 /// </summary> /// <param name="flowEngine">流程</param> /// <param name="auditType">审核类型</param> /// <param name="auditDesc">审核描述</param> /// <param name="userId">Sys_User.Id</param> /// <returns>true 审核成功 false 不能审核</returns> public bool Audit(FlowEngine flowEngine, AuditType auditType, string auditDesc, int userId) { using (AppBLL bll = new AppBLL()) { Participant participant = null; Conclusion conclusion = null; IList<FunctionEntity> userFuncs = bll.FillList<FunctionEntity>("Usp_Funcs_ByUser", new { UserId = userId }); foreach (var userFunc in userFuncs) { participant = new Participant() { Category = 1, Department = -1, Reference = (long)userFunc.Id }; conclusion = new Conclusion(auditType, (long)userId, DateTime.Now) { Description = auditDesc }; if (flowEngine.Audit(conclusion, participant)) { return true; } } participant = new Participant() { Category = 0, Department = userId, Reference = -1 }; conclusion = new Conclusion(auditType, userId, DateTime.Now) { Description = auditDesc }; if (flowEngine.Audit(conclusion, participant)) { return true; } } return false; }
/// <summary> /// 增加会签 /// </summary> /// <param name="flowEngine">流程</param> /// <param name="funcEntry"></param> /// <param name="users"></param> private static void AddCountersign(FlowEngine flowEngine, FunctionEntity funcEntry, IList<UserEntity> users) { Step firstStep = flowEngine.FlowSteps.First(); Step newStep = null; foreach (var user in users) { newStep = new StepGeneral(); Participant participant = new Participant() { Department = -1, Category = 1, Reference = (long)funcEntry.Id }; newStep.Add(new Node(funcEntry.C_Name, participant) { Description = funcEntry.C_Name }); flowEngine.AddAfter(firstStep, newStep); firstStep = newStep; } }
private void Save(FlowEngine flowEngine) { if (flowEngine.Attachment.Owner == -1) { throw new ArgumentException("Owner is null"); } if (flowEngine.FlowState != FlowState.Finished) { FlowKey flowKey = new FlowKey() { Owner = flowEngine.Attachment.Owner }; if (!this.dicFlow.ContainsKey(flowKey)) { this.dicFlow.Add(flowKey, flowEngine); } else if (flowEngine.GetCurrent() == null) { this.dicFlow.Remove(flowKey); } else if (this.dicFlow.ContainsKey(flowKey)) { this.dicFlow[flowKey] = flowEngine; } } string fileName = FlowManager.GetFlowFile(flowEngine.Attachment.Owner); FlowEngine.Save(fileName, flowEngine); }