Exemple #1
0
 public FeasibilityModel(FeasibilityEntity feasibilityEntity, TreatmentsEntity treatmentsEntity)
 {
     Id              = feasibilityEntity.FEASIBILITYID.ToString();
     Criteria        = feasibilityEntity.CRITERIA;
     YearsBeforeAny  = treatmentsEntity.BEFOREANY;
     YearsBeforeSame = treatmentsEntity.BEFORESAME;
 }
Exemple #2
0
 public void UpdateTreatment(TreatmentsEntity entity)
 {
     entity.TREATMENT  = Name;
     entity.BUDGET     = Budgets.Count > 0 ? string.Join(",", Budgets) : null;
     entity.BEFOREANY  = Feasibility.YearsBeforeAny;
     entity.BEFORESAME = Feasibility.YearsBeforeSame;
 }
Exemple #3
0
        public TreatmentModel(TreatmentsEntity entity)
        {
            Id    = entity.TREATMENTID.ToString();
            Name  = entity.TREATMENT;
            Costs = entity.COSTS != null && entity.COSTS.Any()
                ? entity.COSTS.ToList().Select(c => new CostModel(c)).ToList()
                : new List <CostModel>();
            Consequences = entity.CONSEQUENCES != null && entity.CONSEQUENCES.Any()
                ? entity.CONSEQUENCES.ToList().Select(c => new ConsequenceModel(c)).ToList()
                : new List <ConsequenceModel>();
            var rawBudgets = entity.BUDGET != null
                ? entity.BUDGET.Split(',').ToList()
                : new List <string>();

            Budgets = new List <string>();
            if (rawBudgets.Count > 0)
            {
                rawBudgets.ForEach(item => {
                    Budgets.Add(item.Trim());
                });
            }
            Feasibility = new FeasibilityModel();

            if (entity.FEASIBILITIES != null && entity.FEASIBILITIES.Any())
            {
                var feasibilities = entity.FEASIBILITIES.ToList();

                feasibilities.ForEach(feasibilityEntity => {
                    var feasibilityModel = new FeasibilityModel(feasibilityEntity, entity);

                    if (feasibilities.Count > 1)
                    {
                        Feasibility.Aggregate(feasibilityModel);
                    }
                    else
                    {
                        Feasibility = feasibilityModel;
                    }
                });
            }
        }
Exemple #4
0
        /// <summary>
        /// Executes an upsert/delete operation on a simulation's treatment library data
        /// Throws a RowNotInTableException if no simulation is found
        /// </summary>
        /// <param name="model">TreatmentLibraryModel</param>
        /// <param name="db">BridgeCareContext</param>
        /// <returns>TreatmentLibraryModel</returns>
        public TreatmentLibraryModel SaveSimulationTreatmentLibrary(TreatmentLibraryModel model, BridgeCareContext db)
        {
            var id = int.Parse(model.Id);

            if (!db.Simulations.Any(s => s.SIMULATIONID == id))
            {
                throw new RowNotInTableException($"No scenario found with id {id}");
            }

            var simulation = db.Simulations.Include(s => s.TREATMENTS)
                             .Include(s => s.TREATMENTS.Select(t => t.FEASIBILITIES))
                             .Include(s => s.TREATMENTS.Select(t => t.COSTS))
                             .Include(s => s.TREATMENTS.Select(t => t.CONSEQUENCES))
                             .Single(s => s.SIMULATIONID == id);

            if (simulation.TREATMENTS.Any())
            {
                simulation.TREATMENTS.ToList().ForEach(treatmentEntity =>
                {
                    var treatmentModel = model.Treatments
                                         .SingleOrDefault(t => t.Id == treatmentEntity.TREATMENTID.ToString());

                    if (treatmentModel == null)
                    {
                        TreatmentsEntity.DeleteEntry(treatmentEntity, db);
                    }
                    else
                    {
                        treatmentModel.matched = true;
                        treatmentModel.UpdateTreatment(treatmentEntity);

                        if (treatmentEntity.FEASIBILITIES.Any())
                        {
                            treatmentEntity.FEASIBILITIES.ToList().ForEach(feasibilityEntity =>
                            {
                                var feasibilityModel = treatmentModel.Feasibility;

                                if (feasibilityModel.Id != feasibilityEntity.FEASIBILITYID.ToString())
                                {
                                    FeasibilityEntity.DeleteEntry(feasibilityEntity, db);
                                }
                                else
                                {
                                    feasibilityModel.matched   = true;
                                    feasibilityEntity.CRITERIA = feasibilityModel.Criteria;
                                }
                            });
                        }

                        if (!treatmentModel.Feasibility.matched)
                        {
                            db.Feasibilities
                            .Add(new FeasibilityEntity(treatmentEntity.TREATMENTID, treatmentModel.Feasibility));
                        }

                        if (treatmentEntity.COSTS.Any())
                        {
                            treatmentEntity.COSTS.ToList().ForEach(costEntity =>
                            {
                                var costModel = treatmentModel.Costs
                                                .SingleOrDefault(c => c.Id == costEntity.COSTID.ToString());

                                if (costModel == null)
                                {
                                    CostsEntity.DeleteEntry(costEntity, db);
                                }
                                else
                                {
                                    costModel.matched = true;
                                    costModel.UpdateCost(costEntity);
                                }
                            });
                        }

                        if (treatmentModel.Costs.Any(m => !m.matched))
                        {
                            db.Costs
                            .AddRange(treatmentModel.Costs
                                      .Where(costModel => !costModel.matched)
                                      .Select(costModel => new CostsEntity(treatmentEntity.TREATMENTID, costModel))
                                      .ToList()
                                      );
                        }

                        if (treatmentEntity.CONSEQUENCES.Any())
                        {
                            treatmentEntity.CONSEQUENCES.ToList().ForEach(consequenceEntity =>
                            {
                                var consequenceModel = treatmentModel.Consequences
                                                       .SingleOrDefault(c => c.Id == consequenceEntity.CONSEQUENCEID.ToString());

                                if (consequenceModel == null)
                                {
                                    ConsequencesEntity.DeleteEntry(consequenceEntity, db);
                                }
                                else
                                {
                                    consequenceModel.matched = true;
                                    consequenceModel.UpdateConsequence(consequenceEntity);
                                }
                            });
                        }

                        if (treatmentModel.Consequences.Any(m => !m.matched))
                        {
                            db.Consequences
                            .AddRange(treatmentModel.Consequences
                                      .Where(consequenceModel => !consequenceModel.matched)
                                      .Select(consequenceModel => new ConsequencesEntity(treatmentEntity.TREATMENTID, consequenceModel))
                                      .ToList()
                                      );
                        }
                    }
                });
            }

            if (model.Treatments.Any(m => !m.matched))
            {
                db.Treatments
                .AddRange(model.Treatments
                          .Where(treatmentModel => !treatmentModel.matched)
                          .Select(treatmentModel => new TreatmentsEntity(id, treatmentModel))
                          .ToList()
                          );
            }

            db.SaveChanges();

            return(new TreatmentLibraryModel(simulation));
        }
Exemple #5
0
        /// <summary>
        /// Executes an upsert/delete operation on a simulation's treatment library data
        /// Throws a RowNotInTableException if no simulation is found
        /// </summary>
        /// <param name="model">TreatmentLibraryModel</param>
        /// <param name="db">BridgeCareContext</param>
        /// <returns>TreatmentLibraryModel</returns>
        private TreatmentLibraryModel SaveSimulationTreatmentLibrary(TreatmentLibraryModel model, BridgeCareContext db)
        {
            var id = int.Parse(model.Id);

            var simulation = db.Simulations.Include(s => s.TREATMENTS)
                             .Include(s => s.TREATMENTS.Select(t => t.FEASIBILITIES))
                             .Include(s => s.TREATMENTS.Select(t => t.COSTS))
                             .Include(s => s.TREATMENTS.Select(t => t.CONSEQUENCES))
                             .Single(s => s.SIMULATIONID == id);

            if (simulation.TREATMENTS.Any())
            {
                simulation.TREATMENTS.ToList().ForEach(treatmentEntity =>
                {
                    var treatmentModel = model.Treatments
                                         .SingleOrDefault(t => t.Id == treatmentEntity.TREATMENTID.ToString());

                    if (treatmentModel == null)
                    {
                        TreatmentsEntity.DeleteEntry(treatmentEntity, db);
                    }
                    else
                    {
                        treatmentModel.matched = true;
                        treatmentModel.UpdateTreatment(treatmentEntity);

                        if (treatmentEntity.FEASIBILITIES.Any())
                        {
                            var feasibilityModel = treatmentModel.Feasibility;

                            var feasibilitiesToDelete = treatmentEntity.FEASIBILITIES
                                                        .Where(f => f.FEASIBILITYID.ToString() != feasibilityModel.Id).ToList();
                            feasibilitiesToDelete.ForEach(feasibilityToDelete => FeasibilityEntity.DeleteEntry(feasibilityToDelete, db));

                            if (treatmentEntity.FEASIBILITIES.Any(
                                    f => f.FEASIBILITYID.ToString() == feasibilityModel.Id))
                            {
                                feasibilityModel.matched = true;
                                var feasibilityEntity    = treatmentEntity.FEASIBILITIES
                                                           .Single(f => f.FEASIBILITYID.ToString() == feasibilityModel.Id);
                                feasibilityEntity.CRITERIA = feasibilityModel.Criteria;
                            }
                        }

                        if (!treatmentModel.Feasibility.matched)
                        {
                            treatmentEntity.FEASIBILITIES
                            .Add(new FeasibilityEntity(treatmentEntity.TREATMENTID, treatmentModel.Feasibility));
                        }

                        if (treatmentEntity.COSTS.Any())
                        {
                            treatmentEntity.COSTS.ToList().ForEach(costEntity =>
                            {
                                var costModel = treatmentModel.Costs
                                                .SingleOrDefault(c => c.Id == costEntity.COSTID.ToString());

                                if (costModel == null)
                                {
                                    CostsEntity.DeleteEntry(costEntity, db);
                                }
                                else
                                {
                                    costModel.matched = true;
                                    costModel.UpdateCost(costEntity);
                                }
                            });
                        }

                        if (treatmentModel.Costs.Any(m => !m.matched))
                        {
                            treatmentModel.Costs
                            .Where(costModel => !costModel.matched)
                            .Select(costModel => new CostsEntity(treatmentEntity.TREATMENTID, costModel))
                            .ToList().ForEach(costsEntity => treatmentEntity.COSTS.Add(costsEntity));
                        }

                        if (treatmentEntity.CONSEQUENCES.Any())
                        {
                            treatmentEntity.CONSEQUENCES.ToList().ForEach(consequenceEntity =>
                            {
                                var consequenceModel = treatmentModel.Consequences
                                                       .SingleOrDefault(c => c.Id == consequenceEntity.CONSEQUENCEID.ToString());

                                if (consequenceModel == null)
                                {
                                    ConsequencesEntity.DeleteEntry(consequenceEntity, db);
                                }
                                else
                                {
                                    consequenceModel.matched = true;
                                    consequenceModel.UpdateConsequence(consequenceEntity);
                                }
                            });
                        }

                        if (treatmentModel.Consequences.Any(m => !m.matched))
                        {
                            treatmentModel.Consequences
                            .Where(consequenceModel => !consequenceModel.matched)
                            .Select(consequenceModel => new ConsequencesEntity(treatmentEntity.TREATMENTID, consequenceModel))
                            .ToList().ForEach(consequencesEntity => treatmentEntity.CONSEQUENCES.Add(consequencesEntity));
                        }
                    }
                });
            }

            if (model.Treatments.Any(m => !m.matched))
            {
                model.Treatments
                .Where(treatmentModel => !treatmentModel.matched)
                .Select(treatmentModel =>
                {
                    var treatment = new TreatmentsEntity(id, treatmentModel)
                    {
                        FEASIBILITIES = new List <FeasibilityEntity>()
                        {
                            new FeasibilityEntity(treatmentModel.Feasibility)
                        }
                    };
                    if (treatmentModel.Costs.Count > 0)
                    {
                        treatment.COSTS = treatmentModel.Costs.Select(c => new CostsEntity(c)).ToList();
                    }
                    if (treatmentModel.Consequences.Count > 0)
                    {
                        treatment.CONSEQUENCES =
                            treatmentModel.Consequences.Select(c => new ConsequencesEntity(c)).ToList();
                    }
                    return(treatment);
                })
                .ToList().ForEach(treatmentEntity => simulation.TREATMENTS.Add(treatmentEntity));
            }

            db.SaveChanges();

            return(new TreatmentLibraryModel(simulation));
        }