private HashSet <int> ValidateNoIncompletePerformanceMeasureActualRow(int relevantPerformanceMeasureID)
        {
            if (PerformanceMeasureActualSimples == null)
            {
                return(new HashSet <int>());
            }

            var performanceMeasureIDs = PerformanceMeasureActualSimples.Select(x => x.PerformanceMeasureID.GetValueOrDefault()).Distinct();

            var performanceMeasuresIDsAndSubcategoryCounts =
                HttpRequestStorage.DatabaseEntities.PerformanceMeasures.Where(x =>
                                                                              performanceMeasureIDs.Contains(x.PerformanceMeasureID)).Select(x => new { x.PerformanceMeasureID, SubcategoryCount = x.PerformanceMeasureSubcategories.Count });

            var performanceMeasureActualsWithMissingSubcategoryOptions =
                PerformanceMeasureActualSimples.Where(
                    x => x.PerformanceMeasureID == relevantPerformanceMeasureID && (!x.ActualValue.HasValue || performanceMeasuresIDsAndSubcategoryCounts.Single(y => x.PerformanceMeasureID == y.PerformanceMeasureID).SubcategoryCount != x.PerformanceMeasureActualSubcategoryOptions.Count || x.PerformanceMeasureActualSubcategoryOptions.Any(y => y.PerformanceMeasureSubcategoryOptionID == null))).ToList();

            return(new HashSet <int>(performanceMeasureActualsWithMissingSubcategoryOptions.Select(x => x.PerformanceMeasureActualID.GetValueOrDefault())));
        }
        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;
            }
        }