public BizFlow GetFlow(Expression <Func <BizFlow, bool> > predicate, bool loadAllInfo = true) { BizFlow flow = repoBizFlow.Query(predicate).FirstOrDefault(); if (flow == null) { throw new ArgumentNullException("Flow not defined!"); } if (loadAllInfo) { flow.States = repoFlowState.Query(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.Operations = repoFlowOp.Query(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.Incomes = repoFlowIncome.Query(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.Outcomes = repoFlowOutcome.Query(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.NextBizFlows = repoNextFlow.Query(o => o.FlowId == flow.FlowId).ToArray(); var stateOps = repoStateOp.Query(o => o.State.FlowId == flow.FlowId).ToArray(); var stateIncomes = repoStateIncome.Query(o => o.State.FlowId == flow.FlowId).ToArray(); var stateOutcomes = repoStateOutcome.Query(o => o.State.FlowId == flow.FlowId).ToArray(); flow.States.ForEach(it => { it.Operations = stateOps.Where(op => op.StateId == it.StateId).ToArray(); it.Incomes = stateIncomes.Where(op => op.StateId == it.StateId).ToArray(); it.Outcomes = stateOutcomes.Where(op => op.StateId == it.StateId).ToArray(); it.Operations.ForEach(o => { o.Operation = flow.Operations.FirstOrDefault(op => op.OperationId == o.OperationId); }); it.Incomes.ForEach(o => { o.Income = flow.Incomes.FirstOrDefault(d => d.IncomeId == o.IncomeId); }); it.Outcomes.ForEach(o => { o.Outcome = flow.Outcomes.FirstOrDefault(d => d.OutcomeId == o.OutcomeId); }); }); flow.NextBizFlows.ForEach(it => { it.FromBizFlow = flow; it.ToBizFlow = repoBizFlow.Query(o => o.FlowId == it.NextFlowId).FirstOrDefault(); }); } return(flow); }
public static BizFlow BuildBizFlow(this BizFlowInfo bfi) { if (bfi == null || bfi.BizFlow == null) { return(null); } BizFlow flow = bfi.BizFlow; flow.States = bfi.FlowStates.Where(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.Operations = bfi.FlowOperations.Where(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.Incomes = bfi.FlowIncomes.Where(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.Outcomes = bfi.FlowOutcomes.Where(o => o.FlowId == flow.FlowId).OrderBy(o => o.Code).ToArray(); flow.NextBizFlows = bfi.NextBizFlows.ToArray(); flow.States.ForEach(it => { it.Operations = bfi.StateOperations.Where(op => op.StateId == it.StateId).ToArray(); it.Incomes = bfi.StateIncomes.Where(op => op.StateId == it.StateId).ToArray(); it.Outcomes = bfi.StateOutcomes.Where(op => op.StateId == it.StateId).ToArray(); it.Operations.ForEach(o => { o.Operation = flow.Operations.FirstOrDefault(op => op.OperationId == o.OperationId); }); it.Incomes.ForEach(o => { o.Income = flow.Incomes.FirstOrDefault(d => d.IncomeId == o.IncomeId); }); it.Outcomes.ForEach(o => { o.Outcome = flow.Outcomes.FirstOrDefault(d => d.OutcomeId == o.OutcomeId); }); }); return(flow); }
public bool VerifyFlow(string flowId, out string message) { bool verified = true; message = string.Empty; BizFlow flow = GetFlow(o => o.FlowId == flowId, true); if (flow.States.Count <= 0) { verified = false; message += string.Format("There is no states in the flow!{0}", Environment.NewLine); } var starts = flow.States.Where(o => o.StateType == StateType.Begin); if (starts == null || starts.Count() <= 0) { verified = false; message += string.Format("There is no start states in the flow! Need one start state. {0}", Environment.NewLine); } if (starts.Count() > 1) { verified = false; message += string.Format("There is more than one start states in the flow! Need one start state only. {0}", Environment.NewLine); } if (flow.States.Any(o => o.StateType == StateType.End && o.Result == null)) { verified = false; message += string.Format("There are some End states without Result.{0}", Environment.NewLine); } if (flow.States.Any(o => o.StateType == StateType.End) == false) { verified = false; message += string.Format("There is no finish states in the flow! At least one finish state needed.{0}", Environment.NewLine); } return(verified); }
public bool SaveFlow(BizFlow flow) { try { string flowId = flow.FlowId; IEnumerable <FlowState> states = flow.States; if (states == null) { states = Enumerable.Empty <FlowState>(); } IEnumerable <FlowOperation> operations = flow.Operations; if (operations == null) { operations = Enumerable.Empty <FlowOperation>(); } IEnumerable <FlowIncome> incomes = flow.Incomes; if (incomes == null) { incomes = Enumerable.Empty <FlowIncome>(); } IEnumerable <FlowOutcome> outcomes = flow.Outcomes; if (outcomes == null) { outcomes = Enumerable.Empty <FlowOutcome>(); } IEnumerable <NextBizFlow> nfs = flow.NextBizFlows; if (nfs == null) { nfs = Enumerable.Empty <NextBizFlow>(); } trans.BeginTransaction(); // save states states.ForEach(o => { if (string.IsNullOrEmpty(o.StateId)) { o.StateId = idGenerator.NewId(); } }); var ostates = repoFlowState.Query(o => o.FlowId == flowId).ToArray(); repoFlowState.SaveAll(trans, ostates, states, (o, c) => o.StateId == c.StateId, false); // save operations operations.ForEach(o => { if (string.IsNullOrEmpty(o.OperationId)) { o.OperationId = idGenerator.NewId(); } }); var oops = repoFlowOp.Query(o => o.FlowId == flowId).ToArray(); repoFlowOp.SaveAll(trans, oops, operations, (o, c) => o.OperationId == c.OperationId, true); // save incomes incomes.ForEach(o => { if (string.IsNullOrEmpty(o.IncomeId)) { o.IncomeId = idGenerator.NewId(); } }); var oincomes = repoFlowIncome.Query(o => o.FlowId == flowId).ToArray(); repoFlowIncome.SaveAll(trans, oincomes, incomes, (o, c) => o.IncomeId == c.IncomeId, true); // save outcomes outcomes.ForEach(o => { if (string.IsNullOrEmpty(o.OutcomeId)) { o.OutcomeId = idGenerator.NewId(); } }); var ooutcomes = repoFlowOutcome.Query(o => o.FlowId == flowId).ToArray(); repoFlowOutcome.SaveAll(trans, ooutcomes, outcomes, (o, c) => o.OutcomeId == c.OutcomeId, true); // save next flows var onfs = repoNextFlow.Query(o => o.FlowId == flowId).ToArray(); repoNextFlow.SaveAll(trans, onfs, nfs, (o, c) => o.FlowId == c.FlowId && o.NextFlowId == c.NextFlowId, (repo, o) => { repo.Insert(o); }, null, (repo, o) => { repo.Delete(o); }); // save state incomes outcomes operations List <FlowStateIncome> newIncomes = new List <FlowStateIncome>(); List <FlowStateOutcome> newOutcomes = new List <FlowStateOutcome>(); List <FlowStateOperation> newOps = new List <FlowStateOperation>(); states.ForEach(o => { if (o.Incomes != null && o.Incomes.Count > 0) { newIncomes.AddRange(o.Incomes); } if (o.Outcomes != null && o.Outcomes.Count > 0) { newOutcomes.AddRange(o.Outcomes); } if (o.Operations != null && o.Operations.Count > 0) { newOps.AddRange(o.Operations); } }); newIncomes.ForEach(o => { if (string.IsNullOrEmpty(o.IncomeId)) { o.IncomeId = idGenerator.NewId(); } }); newOutcomes.ForEach(o => { if (string.IsNullOrEmpty(o.OutcomeId)) { o.OutcomeId = idGenerator.NewId(); } }); newOps.ForEach(o => { if (string.IsNullOrEmpty(o.OperationId)) { o.OperationId = idGenerator.NewId(); } }); var sincomes = repoStateIncome.Query(o => o.State.FlowId == flowId).ToArray(); repoStateIncome.SaveAll(trans, sincomes, newIncomes, (o, c) => o.IncomeId == c.IncomeId && o.StateId == c.StateId, true); var soutcomes = repoStateOutcome.Query(o => o.State.FlowId == flowId).ToArray(); repoStateOutcome.SaveAll(trans, soutcomes, newOutcomes, (o, c) => o.OutcomeId == c.OutcomeId && o.StateId == c.StateId, true); var ops = repoStateOp.Query(o => o.State.FlowId == flowId).ToArray(); repoStateOp.SaveAll(trans, ops, newOps, (o, c) => o.OperationId == c.OperationId && o.StateId == c.StateId, (repo, o) => { repo.Insert(o); }, null, (repo, o) => { repo.Delete(o); }); var deleteStates = ostates.Where(o => !states.Any(it => it.StateId == o.StateId)); deleteStates.ForEach(o => repoFlowState.Delete(o)); trans.Commit(); return(true); } catch (Exception ex) { trans.Rollback(); throw ex; } }
public ServerResponse SaveFlow([FromBody] BizFlow obj) { return(obj.Save(flow => flowManager.SaveFlow(flow))); }