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 bool SaveFlows(BizFlow[] newItems, BizFlow[] modifiedItems, BizFlow[] deletedItems) { return repoBizFlow.SaveAll(trans, newItems, modifiedItems, deletedItems); }
private BizFlowInfo GetBizFlow(BizFlow flow) { if (flow != null) { BizFlowInfo bfi = new BizFlowInfo(); bfi.BizFlow = flow; bfi.FlowStates = repoFlowState.Query(o => o.FlowId == flow.FlowId).ToArray(); bfi.FlowOperations = repoFlowOp.Query(o => o.FlowId == flow.FlowId).ToArray(); bfi.FlowIncomes = repoFlowIncome.Query(o => o.FlowId == flow.FlowId).ToArray(); bfi.FlowOutcomes = repoFlowOutcome.Query(o => o.FlowId == flow.FlowId).ToArray(); bfi.StateOperations = repoStateOp.Query(o => o.State.FlowId == flow.FlowId).ToArray(); bfi.StateIncomes = repoStateIncome.Query(o => o.State.FlowId == flow.FlowId).ToArray(); bfi.StateOutcomes = repoStateOutcome.Query(o => o.State.FlowId == flow.FlowId).ToArray(); bfi.NextBizFlows = repoNextFlow.Query(o => o.FlowId == flow.FlowId).ToArray(); bfi.NextBizFlows.ForEach(n => { n.FromBizFlow = bfi.BizFlow; n.ToBizFlow = repoBizFlow.Query(o => o.FlowId == n.NextFlowId).FirstOrDefault(); }); return bfi; } return null; }