/// <summary> /// Executes an insert/delete operation on the priority & priority funds tables /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="simulationId">Simulation identifier</param> /// <param name="budgets">Simulation investment budgets</param> /// <param name="db">BridgeCareContext</param> public static void SavePriorityFundInvestmentData(int simulationId, List <string> budgets, BridgeCareContext db) { if (!db.Priorities.Any(p => p.SIMULATIONID == simulationId)) { db.Priorities.Add(new PriorityEntity() { SIMULATIONID = simulationId, PRIORITYLEVEL = 1, CRITERIA = "", PRIORITYFUNDS = budgets .Select(budget => new PriorityFundEntity() { BUDGET = budget, FUNDING = 100 }).ToList(), YEARS = DateTime.Now.Year }); } else { var priorities = db.Priorities.Include(p => p.PRIORITYFUNDS) .Where(p => p.SIMULATIONID == simulationId).ToList(); priorities.ForEach(priorityEntity => { if (priorityEntity.PRIORITYFUNDS.Any()) { priorityEntity.PRIORITYFUNDS.ToList().ForEach(priorityFundEntity => { if (!budgets.Contains(priorityFundEntity.BUDGET)) { PriorityFundEntity.DeleteEntry(priorityFundEntity, db); } else { budgets.Remove(priorityFundEntity.BUDGET); } }); } db.PriorityFunds.AddRange(budgets .Select(budget => new PriorityFundEntity() { PRIORITYID = priorityEntity.PRIORITYID, BUDGET = budget, FUNDING = 100 }) ); }); } db.SaveChanges(); }
/// <summary> /// Executes an upsert/delete operation on a simulation's priority library data /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="model">PriorityLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <returns>PriorityLibraryModel</returns> private PriorityLibraryModel SaveSimulationPriorityLibrary(PriorityLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); var simulation = db.Simulations.Include(s => s.PRIORITIES) .Include(s => s.PRIORITIES.Select(p => p.PRIORITYFUNDS)) .Single(s => s.SIMULATIONID == id); if (simulation.PRIORITIES.Any()) { simulation.PRIORITIES.ToList().ForEach(priorityEntity => { var priorityModel = model.Priorities .SingleOrDefault(m => m.Id == priorityEntity.PRIORITYID.ToString()); if (priorityModel == null) { PriorityEntity.DeleteEntry(priorityEntity, db); } else { priorityModel.matched = true; priorityModel.UpdatePriority(priorityEntity); if (priorityEntity.PRIORITYFUNDS.Any()) { priorityEntity.PRIORITYFUNDS.ToList().ForEach(priorityFundEntity => { var priorityFundModel = priorityModel.PriorityFunds .SingleOrDefault(m => m.Id == priorityFundEntity.PRIORITYFUNDID.ToString()); if (priorityFundModel == null) { PriorityFundEntity.DeleteEntry(priorityFundEntity, db); } else { priorityFundModel.matched = true; priorityFundModel.UpdatePriorityFund(priorityFundEntity); } }); } if (priorityModel.PriorityFunds.Any(m => !m.matched)) { db.PriorityFunds.AddRange(priorityModel.PriorityFunds .Where(priorityFundModel => !priorityFundModel.matched) .Select(priorityFundModel => new PriorityFundEntity(priorityEntity.PRIORITYID, priorityFundModel)) .ToList() ); } } }); } if (model.Priorities.Any(m => !m.matched)) { db.Priorities.AddRange(model.Priorities .Where(priorityModel => !priorityModel.matched) .Select(priorityModel => new PriorityEntity(id, priorityModel)) .ToList() ); } db.SaveChanges(); return(new PriorityLibraryModel(simulation)); }
/// <summary> /// Executes an upsert/delete operation on a simulation's investment library data /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="model">InvestmentLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <returns>InvestmentLibraryModel</returns> private InvestmentLibraryModel SaveSimulationInvestmentLibrary(InvestmentLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); var simulation = db.Simulations .Include(s => s.INVESTMENTS) .Include(s => s.YEARLYINVESTMENTS) .Include(s => s.CriteriaDrivenBudgets) .Include(s => s.PRIORITIES).Include(s => s.PRIORITIES.Select(p => p.PRIORITYFUNDS)) .Single(s => s.SIMULATIONID == id); if (simulation.CriteriaDrivenBudgets.Any()) { simulation.CriteriaDrivenBudgets.ToList().ForEach(criteriaDrivenBudget => { var criteriaDrivenBudgetModel = model.CriteriaDrivenBudgets.SingleOrDefault(m => m.Id == criteriaDrivenBudget.BUDGET_CRITERIA_ID.ToString()); if (criteriaDrivenBudgetModel == null) { CriteriaDrivenBudgetEntity.DeleteEntry(criteriaDrivenBudget, db); } else { criteriaDrivenBudgetModel.matched = true; criteriaDrivenBudgetModel.UpdateCriteriaDrivenBudget(criteriaDrivenBudget); } }); } if (simulation.INVESTMENTS != null) { model.UpdateInvestment(simulation.INVESTMENTS); } if (simulation.YEARLYINVESTMENTS.Any()) { simulation.YEARLYINVESTMENTS.ToList().ForEach(yearlyInvestment => { var yearlyInvestmentModel = model.BudgetYears.SingleOrDefault(m => m.Id == yearlyInvestment.YEARID.ToString()); if (yearlyInvestmentModel == null) { YearlyInvestmentEntity.DeleteEntry(yearlyInvestment, db); } else { yearlyInvestmentModel.matched = true; yearlyInvestmentModel.UpdateYearlyInvestment(yearlyInvestment); } }); } simulation.PRIORITIES.ToList().ForEach(priorityEntity => { var budgetsForNewFunds = new List <string>(); if (priorityEntity.PRIORITYFUNDS.Any()) { model.BudgetOrder.ForEach(budget => { budgetsForNewFunds.Add(budget); }); } if (budgetsForNewFunds.Any()) { priorityEntity.PRIORITYFUNDS .ToList() .ForEach(priorityFundEntity => { PriorityFundEntity.DeleteEntry(priorityFundEntity, db); }); db.PriorityFunds.AddRange(budgetsForNewFunds .Select(budget => new PriorityFundEntity(priorityEntity.PRIORITYID, budget)) ); } }); if (simulation.INVESTMENTS == null) { db.Investments.Add(new InvestmentsEntity(model)); } if (model.BudgetYears.Any(m => !m.matched)) { db.YearlyInvestments.AddRange(model.BudgetYears .Where(yearlyInvestmentModel => !yearlyInvestmentModel.matched) .Select(yearlyInvestmentModel => new YearlyInvestmentEntity(id, yearlyInvestmentModel)) .ToList() ); } if (model.CriteriaDrivenBudgets.Any(m => !m.matched)) { db.CriteriaDrivenBudgets.AddRange(model.CriteriaDrivenBudgets .Where(criteriaDrivenBudgetModel => !criteriaDrivenBudgetModel.matched) .Select(criteriaDrivenBudgetModel => new CriteriaDrivenBudgetEntity(id, criteriaDrivenBudgetModel)) .ToList() ); } db.SaveChanges(); return(new InvestmentLibraryModel(simulation)); }