/// <summary> /// Ctor - service validator /// </summary> /// <param name="model">Service model</param> /// <param name="generalDescriptionService">General description service</param> /// <param name="codeService">Code service</param> /// <param name="fintoService">Finto item service</param> /// <param name="commonService">Common service</param> /// <param name="channelService">Channel service</param> /// <param name="newLanguages">Languages that should be validated within lists</param> /// <param name="userRole">User role</param> public ServiceValidator( IVmOpenApiServiceInVersionBase model, IGeneralDescriptionService generalDescriptionService, ICodeService codeService, IFintoService fintoService, ICommonService commonService, IChannelService channelService, IList <string> newLanguages, UserRoleEnum userRole ) : base(model, "Service") { this.generalDescriptionService = generalDescriptionService; if (model == null) { throw new ArgumentNullException(PropertyName, $"{PropertyName} must be defined."); } names = new LocalizedListValidator(model.ServiceNames, "ServiceNames", newLanguages, new List <string>() { NameTypeEnum.Name.ToString() }); languages = new LanguageListValidator(model.Languages, codeService); serviceClasses = new ServiceClassListValidator(model.ServiceClasses, fintoService); ontologyTerms = new OntologyTermListValidator(model.OntologyTerms, fintoService); targetGroups = new TargetGroupListValidator(model.TargetGroups, fintoService); lifeEvents = new LifeEventListValidator(model.LifeEvents, fintoService); industrialClasses = new IndustrialClassListValidator(model.IndustrialClasses, fintoService); organizations = new OrganizationIdListValidator(model.OtherResponsibleOrganizations?.Select(o => o.ToString()).ToList(), commonService, "OtherResponsibleOrganizations"); status = new PublishingStatusValidator(model.PublishingStatus, model.CurrentPublishingStatus); areas = new AreaAndTypeValidator(model.Areas, model.AreaType, codeService); var channelList = model.ServiceServiceChannels?.Count > 0 ? model.ServiceServiceChannels.Select(i => i.ChannelGuid).ToList() : new List <Guid>(); channels = new ServiceChannelIdListValidator(channelList, channelService, userRole, "ServiceChannels"); var availableOrganizations = new List <Guid>(); if (!model.MainResponsibleOrganization.IsNullOrEmpty()) { availableOrganizations.Add(model.MainResponsibleOrganization.ParseToGuidWithExeption()); } if (model.OtherResponsibleOrganizations?.Count > 0) { availableOrganizations.AddRange(model.OtherResponsibleOrganizations); } serviceProducers = new ServiceProducerListValidator(model.ServiceProducers, availableOrganizations, commonService); mainOrganization = new OrganizationIdValidator(model.MainResponsibleOrganization, commonService, "MainResponsibleOrganization"); generalDescriptionAttached = !string.IsNullOrEmpty(model.StatutoryServiceGeneralDescriptionId); this.newLanguages = newLanguages; }
/// <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); } }); }