/// <summary>
        /// Validates general description model.
        /// </summary>
        public override void Validate(ModelStateDictionary modelState)
        {
            names.Validate(modelState);
            if (Model.Descriptions?.Where(d => d.Type == DescriptionTypeEnum.Description.ToString()).FirstOrDefault() == null &&
                Model.Descriptions?.Where(d => d.Type == DescriptionTypeEnum.BackgroundDescription.ToString()).FirstOrDefault() == null && newLanguages?.Count > 0)
            {
                modelState.AddModelError("Descriptions", string.Format(CoreMessages.OpenApi.RequiredValueWithLanguageAndTypeNotFound,
                                                                       $"{ DescriptionTypeEnum.Description.ToString()} or { DescriptionTypeEnum.BackgroundDescription.ToString()}", newLanguages.First()));

                descriptions = new LocalizedListValidator(Model.Descriptions, "Descriptions", newLanguages,
                                                          new List <string>()
                {
                    DescriptionTypeEnum.ShortDescription.ToString()
                });
            }
            else
            {
                var descriptionType = Model.Descriptions?.Where(d => d.Type != DescriptionTypeEnum.ShortDescription.ToString()).Select(d => d.Type).FirstOrDefault();
                descriptions = new LocalizedListValidator(Model.Descriptions, "Descriptions", newLanguages,
                                                          new List <string>()
                {
                    DescriptionTypeEnum.ShortDescription.ToString(), descriptionType
                });
            }
            descriptions.Validate(modelState);
            languages.Validate(modelState);
            serviceClasses.Validate(modelState);
            ontologyTerms.Validate(modelState);
            targetGroups.Validate(modelState);
            lifeEvents.Validate(modelState);
            industrialClasses.Validate(modelState);
            status.Validate(modelState);
        }
Esempio n. 2
0
        /// <summary>
        /// Checks if service model is valid or not.
        /// </summary>
        public override void Validate(ModelStateDictionary modelState)
        {
            if (!generalDescriptionAttached)
            {
                // validate names if general description is not set (name is taken from general description if not set).
                names.Validate(modelState);
                // Validate all required descriptions
                descriptions = new LocalizedListValidator(Model.ServiceDescriptions, "ServiceDescriptions", newLanguages,
                                                          new List <string>()
                {
                    DescriptionTypeEnum.ShortDescription.ToString(), DescriptionTypeEnum.Description.ToString()
                });
            }
            else
            {
                // General description was defined.
                var gdId = Model.StatutoryServiceGeneralDescriptionId.ParseToGuid();
                if (gdId.HasValue)
                {
                    var gd = generalDescriptionService.GetGeneralDescriptionVersionBase(gdId.Value, 0);
                    if (gd == null || !gd.Id.HasValue)
                    {
                        modelState.AddModelError("StatutoryServiceGeneralDescriptionId", CoreMessages.OpenApi.RecordNotFound);
                    }
                    else
                    {
                        if (gd.Descriptions == null || gd.Descriptions.Where(d => d.Type == DescriptionTypeEnum.Description.ToString() && d.Value != null && d.Value.Length > 0).FirstOrDefault() == null)
                        {
                            // Description was not defined within GD - service model need to include all required description, so let's validate both ShortDescription and Description
                            descriptions = new LocalizedListValidator(Model.ServiceDescriptions, "ServiceDescriptions", newLanguages,
                                                                      new List <string>()
                            {
                                DescriptionTypeEnum.ShortDescription.ToString(), DescriptionTypeEnum.Description.ToString()
                            });
                        }
                        else
                        {
                            // Validate only ShortDescription for Descriptions. Description is not required if it is defined within GD.
                            descriptions = new LocalizedListValidator(Model.ServiceDescriptions, "ServiceDescriptions", newLanguages,
                                                                      new List <string>()
                            {
                                DescriptionTypeEnum.ShortDescription.ToString()
                            });
                        }
                    }
                }
                else
                {
                    modelState.AddModelError("StatutoryServiceGeneralDescriptionId", CoreMessages.OpenApi.RecordNotFound);
                }
            }

            if (descriptions != null)
            {
                descriptions.Validate(modelState);
            }
            //municipalities.Validate(modelState); // We do not need to validate municipalities anymore - they are mapped into Model.Areas in VmOpenApiServiceInVersionBase
            languages.Validate(modelState);
            serviceClasses.Validate(modelState);
            ontologyTerms.Validate(modelState);
            targetGroups.Validate(modelState);
            lifeEvents.Validate(modelState);
            industrialClasses.Validate(modelState);
            organizations.Validate(modelState);
            status.Validate(modelState);

            // Validate area type, service areas and municipalities
            areas.Validate(modelState);

            channels.Validate(modelState);
            serviceProducers.Validate(modelState);
            mainOrganization.Validate(modelState);
        }