public void UpdateModel(ProjectFirmaModels.Models.Project project, FirmaSession currentFirmaSession)
        {
            project.ProjectName        = ProjectName;
            project.ProjectDescription = ProjectDescription;
            // TaxonomyLeaf gets special handling
            SetTaxonomyLeaf(project, currentFirmaSession);
            project.ProjectStageID          = ProjectStageID;
            project.ImplementationStartYear = ImplementationStartYear;
            project.PlanningDesignStartYear = PlanningDesignStartYear;
            project.CompletionYear          = CompletionYear;
            project.BpaProjectNumber        = BpaProjectNumber;

            var secondaryProjectTaxonomyLeavesToUpdate = (SecondaryProjectTaxonomyLeafIDs?.ToList() ?? new List <int>())
                                                         .Select(x => new SecondaryProjectTaxonomyLeaf(project.ProjectID, x)
            {
                TenantID = HttpRequestStorage.Tenant.TenantID
            })
                                                         .ToList();

            project.SecondaryProjectTaxonomyLeafs.Merge(
                secondaryProjectTaxonomyLeavesToUpdate,
                HttpRequestStorage.DatabaseEntities.AllSecondaryProjectTaxonomyLeafs.Local,
                (a, b) => a.TaxonomyLeafID == b.TaxonomyLeafID && a.ProjectID == b.ProjectID,
                HttpRequestStorage.DatabaseEntities);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var projects = HttpRequestStorage.DatabaseEntities.Projects.ToList();

            if (!ProjectModelExtensions.IsProjectNameUnique(projects, ProjectName, ProjectID))
            {
                yield return(new SitkaValidationResult <EditProjectViewModel, string>(
                                 FirmaValidationMessages.ProjectNameUnique, m => m.ProjectName));
            }

            if (ImplementationStartYear < PlanningDesignStartYear)
            {
                yield return(new SitkaValidationResult <EditProjectViewModel, int?>(
                                 FirmaValidationMessages.ImplementationStartYearGreaterThanPlanningDesignStartYear,
                                 m => m.ImplementationStartYear));
            }

            if (CompletionYear < ImplementationStartYear)
            {
                yield return(new SitkaValidationResult <EditProjectViewModel, int?>(
                                 FirmaValidationMessages.CompletionYearGreaterThanEqualToImplementationStartYear,
                                 m => m.CompletionYear));
            }

            var isCompletedOrPostImplementation = ProjectStageID == ProjectStage.Completed.ProjectStageID || ProjectStageID == ProjectStage.PostImplementation.ProjectStageID;

            if (isCompletedOrPostImplementation && !CompletionYear.HasValue)
            {
                yield return(new SitkaValidationResult <EditProjectViewModel, int?>($"Since the {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} is in the {ProjectStage.ToType(ProjectStageID).ProjectStageDisplayName} stage, the {FieldDefinitionEnum.CompletionYear.ToType().GetFieldDefinitionLabel()} is required", m => m.CompletionYear));
            }

            if (isCompletedOrPostImplementation && CompletionYear > DateTime.Now.Year)
            {
                var errorMessage = $"{FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} is in the {ProjectStage.ToType(ProjectStageID).ProjectStageDisplayName} stage: " +
                                   $"the {FieldDefinitionEnum.CompletionYear.ToType().GetFieldDefinitionLabel()} must be less than or equal to the current year";
                yield return(new SitkaValidationResult <EditProjectViewModel, int?>(errorMessage, m => m.CompletionYear));
            }

            if (HasExistingProjectUpdate && OldProjectStageID != ProjectStageID)
            {
                var errorMessage = $"There are updates to this {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} that have not been submitted. Please delete the update if you want to change this {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()}'s stage.";
                yield return(new SitkaValidationResult <EditProjectViewModel, int>(errorMessage, m => m.ProjectStageID));
            }

            if (TaxonomyLeafID != null && (SecondaryProjectTaxonomyLeafIDs?.ToList() ?? new List <int>()).Contains(TaxonomyLeafID.Value))
            {
                yield return(new SitkaValidationResult <EditProjectViewModel, IEnumerable <int> >(
                                 $"Cannot have a {FieldDefinitionEnum.SecondaryProjectTaxonomyLeaf.ToType().GetFieldDefinitionLabel()} " +
                                 $"that is the same as the Primary {FieldDefinitionEnum.TaxonomyLeaf.ToType().GetFieldDefinitionLabel()}.",
                                 m => m.SecondaryProjectTaxonomyLeafIDs));
            }
        }
        public void UpdateModel(ProjectFirmaModels.Models.Project project, FirmaSession currentFirmaSession)
        {
            project.ProjectName             = ProjectName;
            project.ProjectDescription      = ProjectDescription;
            project.TaxonomyLeafID          = TaxonomyLeafID ?? ModelObjectHelpers.NotYetAssignedID;
            project.ProjectStageID          = ProjectStageID;
            project.ImplementationStartYear = ImplementationStartYear;
            project.PlanningDesignStartYear = PlanningDesignStartYear;
            project.CompletionYear          = CompletionYear;
            project.SolicitationID          = SolicitationID;

            var secondaryProjectTaxonomyLeavesToUpdate = (SecondaryProjectTaxonomyLeafIDs?.ToList() ?? new List <int>())
                                                         .Select(x => new SecondaryProjectTaxonomyLeaf(project.ProjectID, x)
            {
                TenantID = HttpRequestStorage.Tenant.TenantID
            })
                                                         .ToList();

            project.SecondaryProjectTaxonomyLeafs.Merge(
                secondaryProjectTaxonomyLeavesToUpdate,
                HttpRequestStorage.DatabaseEntities.AllSecondaryProjectTaxonomyLeafs.Local,
                (a, b) => a.TaxonomyLeafID == b.TaxonomyLeafID && a.ProjectID == b.ProjectID,
                HttpRequestStorage.DatabaseEntities);
        }
Exemple #4
0
        public IEnumerable <ValidationResult> GetValidationResults()
        {
            var projects = HttpRequestStorage.DatabaseEntities.Projects.ToList();

            if (!Enum.IsDefined(typeof(ProjectCategoryEnum), ProjectCategoryEnum))
            {
                yield return(new SitkaValidationResult <BasicsViewModel, ProjectCategoryEnum>($"A valid value for {FieldDefinitionEnum.ProjectCategory.ToType().GetFieldDefinitionLabel()} is required.", m => m.ProjectCategoryEnum));
            }

            if (TaxonomyLeafID == -1)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>($"{MultiTenantHelpers.GetTaxonomyLeafDisplayNameForProject()} is required.", m => m.TaxonomyLeafID));
            }

            if (!ProjectModelExtensions.IsProjectNameUnique(projects, ProjectName, ProjectID))
            {
                yield return(new SitkaValidationResult <BasicsViewModel, string>(FirmaValidationMessages.ProjectNameUnique, m => m.ProjectName));
            }

            if (ImplementationStartYear < PlanningDesignStartYear)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>(FirmaValidationMessages.ImplementationStartYearGreaterThanPlanningDesignStartYear, m => m.ImplementationStartYear));
            }

            if (CompletionYear < ImplementationStartYear)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>(FirmaValidationMessages.CompletionYearGreaterThanEqualToImplementationStartYear, m => m.CompletionYear));
            }

            if (CompletionYear < PlanningDesignStartYear)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>(FirmaValidationMessages.CompletionYearGreaterThanEqualToPlanningDesignStartYear, m => m.CompletionYear));
            }

            var currentYear = FirmaDateUtilities.CalculateCurrentYearToUseForUpToAllowableInputInReporting();

            if (ProjectStageID == ProjectStage.Implementation.ProjectStageID)
            {
                if (ImplementationStartYear > currentYear)
                {
                    yield return(new SitkaValidationResult <BasicsViewModel, int?>(
                                     FirmaValidationMessages.ImplementationYearMustBePastOrPresentForImplementationProjects,
                                     m => m.ImplementationStartYear));
                }
            }

            if (ImplementationStartYear == null && ProjectStageID != ProjectStage.Terminated.ProjectStageID && ProjectStageID != ProjectStage.Deferred.ProjectStageID)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>(
                                 $"Implementation year is required when the {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} stage is not Deferred or Terminated",
                                 m => m.ImplementationStartYear));
            }

            if (ProjectStageID == ProjectStage.Completed.ProjectStageID && !CompletionYear.HasValue)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>($"Since the {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} is in the Completed stage, the Completion year is required", m => m.CompletionYear));
            }

            if (ProjectStageID == ProjectStage.PostImplementation.ProjectStageID && !CompletionYear.HasValue)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>($"Since the {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} is in the Post-Implementation stage, the Completion year is required", m => m.CompletionYear));
            }

            if ((ProjectStageID == ProjectStage.Completed.ProjectStageID || ProjectStageID == ProjectStage.PostImplementation.ProjectStageID) && CompletionYear > currentYear)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>(FirmaValidationMessages.CompletionYearMustBePastOrPresentForCompletedProjects, m => m.CompletionYear));
            }

            if (ProjectStageID == ProjectStage.PlanningDesign.ProjectStageID && PlanningDesignStartYear > currentYear)
            {
                yield return(new SitkaValidationResult <BasicsViewModel, int?>(
                                 $"Since the {FieldDefinitionEnum.Project.ToType().GetFieldDefinitionLabel()} is in the Planning / Design stage, the Planning / Design start year must be less than or equal to the current year",
                                 m => m.PlanningDesignStartYear));
            }

            if (TaxonomyLeafID != null && SecondaryProjectTaxonomyLeafIDs.ToList().Contains(TaxonomyLeafID.Value))
            {
                yield return(new SitkaValidationResult <BasicsViewModel, IEnumerable <int> >(
                                 $"Cannot have a {FieldDefinitionEnum.SecondaryProjectTaxonomyLeaf.ToType().GetFieldDefinitionLabel()} " +
                                 $"that is the same as the Primary {FieldDefinitionEnum.TaxonomyLeaf.ToType().GetFieldDefinitionLabel()}.",
                                 m => m.SecondaryProjectTaxonomyLeafIDs));
            }
        }