示例#1
0
        /// <summary>
        /// Checks if organization id list is valid or not.
        /// </summary>
        /// <returns></returns>
        public override void Validate(ModelStateDictionary modelState)
        {
            if (Model == null)
            {
                return;
            }

            var i = 0;

            Model.ForEach(serviceProducer =>
            {
                var selfProduced = ProvisionTypeEnum.SelfProduced.ToString();
                // check provision type SelfProduced - only defined set of organizations can be used (availableOrganizations)
                if (serviceProducer.ProvisionType == selfProduced)
                {
                    serviceProducer.Organizations.ForEach(o =>
                    {
                        if (!availableOrganizations.Contains(o))
                        {
                            modelState.AddModelError(PropertyName, $"Field invalid. When 'ProvisionType' has value { selfProduced } only main responsible and other responsible organizations can be used: '{availableOrganizations.ConvertToString()}'.");
                            return;
                        }
                    });

                    // no additional information allowed when provision type is self produced
                    if (serviceProducer.AdditionalInformation?.Count > 0)
                    {
                        modelState.AddModelError(PropertyName, $"No AdditionalInformation accepted when 'ProvisionType' has value { selfProduced }.");
                        return;
                    }
                }

                // for provision type Other or PurchaseServices user needs to define either organizations or additional information
                var other = ProvisionTypeEnum.Other.ToString(); var purchaseServices = ProvisionTypeEnum.PurchaseServices.ToString();
                if (serviceProducer.ProvisionType == other || serviceProducer.ProvisionType == purchaseServices)
                {
                    if (serviceProducer.Organizations?.Count == 0 && serviceProducer.AdditionalInformation?.Count == 0)
                    {
                        modelState.AddModelError(PropertyName, $"Either AdditionalInformation or Organizations needs to be defined when 'ProvisionType' has value {other} or { purchaseServices }.");
                        return;
                    }
                }

                if (serviceProducer.Organizations?.Count > 0)
                {
                    var list = new List <string>();
                    serviceProducer.Organizations.ForEach(p => list.Add(p.ToString()));
                    var organizations = new OrganizationIdListValidator(list, commonService, $"{PropertyName}[{ i++ }].Organizations");
                    organizations.Validate(modelState);
                }
            });
        }
示例#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);
        }