public IEnumerable <ValidationResult> GetValidationResults()
        {
            var errors = new List <ValidationResult>();

            if (ProjectExemptReportingYearSimples != null && ProjectExemptReportingYearSimples.Any(x => x.IsExempt) && string.IsNullOrWhiteSpace(Explanation))
            {
                errors.Add(new SitkaValidationResult <PerformanceMeasuresViewModel, string>(FirmaValidationMessages.ExplanationNecessaryForProjectExemptYears, x => x.Explanation));
            }
            if (!string.IsNullOrWhiteSpace(Explanation) && Explanation.Length > ProjectFirmaModels.Models.Project.FieldLengths.PerformanceMeasureActualYearsExemptionExplanation)
            {
                errors.Add(new SitkaValidationResult <PerformanceMeasuresViewModel, string>(
                               FirmaValidationMessages.ExplanationForProjectExemptYearsExceedsMax(ProjectFirmaModels.Models.Project.FieldLengths.PerformanceMeasureActualYearsExemptionExplanation),
                               x => x.Explanation));
            }

            var pmValidationResults = ValidatePerformanceMeasures();
            var allWarningMessages  = pmValidationResults.SelectMany(pmvr => pmvr.GetWarningMessages());

            errors.AddRange(allWarningMessages.Select(m => new ValidationResult(m)));

            return(errors);
        }
        public void UpdateModel(List <ProjectFirmaModels.Models.PerformanceMeasureActual> currentPerformanceMeasureActuals,
                                IList <ProjectFirmaModels.Models.PerformanceMeasureActual> allPerformanceMeasureActuals,
                                IList <PerformanceMeasureActualSubcategoryOption> allPerformanceMeasureActualSubcategoryOptions,
                                ProjectFirmaModels.Models.Project project,
                                IList <PerformanceMeasureReportingPeriod> allPerformanceMeasureReportingPeriods)
        {
            var currentPerformanceMeasureActualSubcategoryOptions =
                currentPerformanceMeasureActuals.SelectMany(x => x.PerformanceMeasureActualSubcategoryOptions).ToList();
            var performanceMeasureActualsUpdated = new List <ProjectFirmaModels.Models.PerformanceMeasureActual>();

            if (PerformanceMeasureActualSimples != null)
            {
                // Renumber negative indexes for PerformanceMeasureActuals
                RenumberPerformanceMeasureActuals(PerformanceMeasureActualSimples);

                var performanceMeasureReportingPeriodsFromDatabase = HttpRequestStorage.DatabaseEntities.AllPerformanceMeasureReportingPeriods.Local;
                // Completely rebuild the list
                performanceMeasureActualsUpdated = PerformanceMeasureActualSimples.Select(x =>
                {
                    var performanceMeasureReportingPeriod = allPerformanceMeasureReportingPeriods.SingleOrDefault(y => y.PerformanceMeasureReportingPeriodCalendarYear == x.CalendarYear);
                    if (performanceMeasureReportingPeriod == null)
                    {
                        Check.EnsureNotNull(x.PerformanceMeasureID, "We need to have a performance measure.");
                        performanceMeasureReportingPeriod = new PerformanceMeasureReportingPeriod((int)x.PerformanceMeasureID, x.CalendarYear, x.CalendarYear.ToString());
                        performanceMeasureReportingPeriodsFromDatabase.Add(performanceMeasureReportingPeriod);
                        HttpRequestStorage.DatabaseEntities.SaveChanges();
                    }

                    var performanceMeasureActual = new ProjectFirmaModels.Models.PerformanceMeasureActual(x.PerformanceMeasureActualID.GetValueOrDefault(),
                                                                                                          x.ProjectID.GetValueOrDefault(),
                                                                                                          x.PerformanceMeasureID.GetValueOrDefault(),
                                                                                                          x.ActualValue.GetValueOrDefault(),
                                                                                                          performanceMeasureReportingPeriod.PerformanceMeasureReportingPeriodID);
                    if (x.PerformanceMeasureActualSubcategoryOptions != null)
                    {
                        performanceMeasureActual.PerformanceMeasureActualSubcategoryOptions =
                            x.PerformanceMeasureActualSubcategoryOptions.Where(pmavsou => ModelObjectHelpers.IsRealPrimaryKeyValue(pmavsou.PerformanceMeasureSubcategoryOptionID))
                            .Select(
                                y =>
                                new PerformanceMeasureActualSubcategoryOption(performanceMeasureActual.PerformanceMeasureActualID,
                                                                              y.PerformanceMeasureSubcategoryOptionID.GetValueOrDefault(),
                                                                              y.PerformanceMeasureID,
                                                                              y.PerformanceMeasureSubcategoryID))
                            .ToList();
                    }
                    return(performanceMeasureActual);
                }).ToList();
            }

            var databaseEntities = HttpRequestStorage.DatabaseEntities;

            currentPerformanceMeasureActuals.Merge(performanceMeasureActualsUpdated,
                                                   allPerformanceMeasureActuals,
                                                   (x, y) => x.PerformanceMeasureActualID == y.PerformanceMeasureActualID,
                                                   (x, y) =>
            {
                x.PerformanceMeasureReportingPeriodID = y.PerformanceMeasureReportingPeriodID;
                x.ActualValue = y.ActualValue;
            }, databaseEntities);

            currentPerformanceMeasureActualSubcategoryOptions.Merge(
                performanceMeasureActualsUpdated.SelectMany(x => x.PerformanceMeasureActualSubcategoryOptions).ToList(),
                allPerformanceMeasureActualSubcategoryOptions,
                (x, y) => x.PerformanceMeasureActualID == y.PerformanceMeasureActualID && x.PerformanceMeasureSubcategoryID == y.PerformanceMeasureSubcategoryID && x.PerformanceMeasureID == y.PerformanceMeasureID,
                (x, y) => x.PerformanceMeasureSubcategoryOptionID = y.PerformanceMeasureSubcategoryOptionID, databaseEntities);

            var currentProjectExemptYears = project.GetPerformanceMeasuresExemptReportingYears();

            databaseEntities.ProjectExemptReportingYears.Load();
            var allProjectExemptYears       = databaseEntities.AllProjectExemptReportingYears.Local;
            var projectExemptReportingYears = new List <ProjectExemptReportingYear>();

            if (ProjectExemptReportingYearSimples != null)
            {
                // Completely rebuild the list
                projectExemptReportingYears =
                    ProjectExemptReportingYearSimples.Where(x => x.IsExempt)
                    .Select(x => new ProjectExemptReportingYear(x.ProjectExemptReportingYearID, x.ProjectID, x.CalendarYear, ProjectExemptReportingType.PerformanceMeasures.ProjectExemptReportingTypeID))
                    .ToList();
            }
            currentProjectExemptYears.Merge(projectExemptReportingYears,
                                            allProjectExemptYears,
                                            (x, y) => x.ProjectID == y.ProjectID && x.CalendarYear == y.CalendarYear && x.ProjectExemptReportingTypeID == y.ProjectExemptReportingTypeID, HttpRequestStorage.DatabaseEntities);
            project.PerformanceMeasureActualYearsExemptionExplanation = Explanation;
            if (project.ProjectApprovalStatus == ProjectApprovalStatus.PendingApproval)
            {
                project.ReportedAccomplishmentsComment = Comments;
            }
        }