Exemplo n.º 1
0
        public IList <MetadataProperty> GetMetadataForEntityTypeInConfig(string entityType, string configIdentifier = null)
        {
            if (!entityType.IsValidBaseUri())
            {
                throw new InvalidFormatException(Constants.Messages.Identifier.IncorrectIdentifierFormat, entityType);
            }

            MetadataGraphConfiguration usedConfig;

            if (string.IsNullOrWhiteSpace(configIdentifier))
            {
                _logger.LogInformation($"Got request for entity type {entityType} with no config");
                usedConfig = _metadataGraphConfigurationRepository.GetLatestConfiguration();
            }
            else
            {
                _logger.LogInformation($"Got request for entity type {entityType} with config {configIdentifier}");
                usedConfig = _metadataGraphConfigurationRepository.GetEntityById(configIdentifier);
            }

            SparqlParameterizedString queryString = new SparqlParameterizedString();

            queryString.CommandText = @"
                SELECT *
                @fromMetadataNamedGraph
                @fromMetdataShaclNamedGraph
                @fromEnterpriseCoreOntologyNamedGraph
                @fromConsumerGroupNamedGraph
                WHERE
                {
                    {
                        @entityType rdfs:subClassOf*  ?resourceType.
                        @entityType rdfs:label ?typeLabel.
                        FILTER(lang(?typeLabel) IN (@language , """"))
                        Bind(@entityType as ?type).
                        ?resourceType sh:property ?shaclConstraint.
                        ?shaclConstraint ?shaclProperty ?shaclValue.
                        OPTIONAL
                        {
                            ?shaclValue rdf:type ?dataType.
                        }
                        OPTIONAL {
                            ?shaclConstraint @editWidget @nestedObjectEditor.
                            ?shaclConstraint sh:class ?nested
                        }
                        OPTIONAL
                        {
                            ?shaclValue rdf:type sh:PropertyGroup.
                            Bind(?shaclValue as ?group).
                            ?shaclValue sh:order ?groupOrder.
                            ?shaclValue rdfs:label ?grouplabel.
                            ?shaclValue tosh:editGroupDescription ?editGroupDescription.
                            ?shaclValue tosh:viewGroupDescription ?viewGroupDescription
                        }
                        OPTIONAL
                        {
                            ?shaclValue sh:class ?class.
                        }
                    }
                    UNION
                    {
                        @entityType rdfs:subClassOf* ?resourceType.
                        ?extraProperty rdfs:domain ?resourceType.
                        ?extraProperty ?shaclProperty ?shaclValue
                    }
                }";

            queryString.SetPlainLiteral("fromMetadataNamedGraph", usedConfig.GetMetadataGraphs().JoinAsFromNamedGraphs());
            queryString.SetPlainLiteral("fromMetdataShaclNamedGraph", usedConfig.GetShaclGraphs().JoinAsFromNamedGraphs());
            queryString.SetPlainLiteral("fromEnterpriseCoreOntologyNamedGraph", usedConfig.GetEcoGraphs().JoinAsFromNamedGraphs());
            queryString.SetPlainLiteral("fromConsumerGroupNamedGraph", usedConfig.GetConsumerGroupGraphs().JoinAsFromNamedGraphs());

            queryString.SetUri("entityType", new Uri(entityType));
            queryString.SetLiteral("language", Constants.I18n.DefaultLanguage);

            queryString.SetUri("editWidget", new Uri(Constants.TopBraid.EditWidget));
            queryString.SetUri("nestedObjectEditor", new Uri(Constants.TopBraid.NestedObjectEditor));

            SparqlResultSet results = _tripleStoreRepository.QueryTripleStoreResultSet(queryString);

            var whereNotNullResults = results.Where(r => GetDataFromNode(r, "shaclConstraint")?.Value != null);
            var whereNullResults    = results.Where(r => GetDataFromNode(r, "shaclConstraint").Value == null).ToList();
            var groupedResults      = whereNotNullResults.GroupBy(res => GetDataFromNode(res, "shaclConstraint")?.Value);
            var dict = GroupedSparqResultToDictionary(groupedResults);

            foreach (var r in whereNullResults)
            {
                var key = GetDataFromNode(r, "extraProperty")?.Value;

                if (dict.ContainsKey(key))
                {
                    dict[key].Add(r);
                }
            }

            var metaDataPropertyDictionary = dict.ToDictionary(r => r.Key, r => CreateMetadataPropertyFromList(r.Key, r.Value, configIdentifier));

            // MainDistribution is a subproperty of distribution and gets no nested metadata
            if (metaDataPropertyDictionary.TryGetValue(Constants.Resource.MainDistribution, out var mainProperties) && metaDataPropertyDictionary.TryGetValue(Constants.Resource.Distribution, out var properties))
            {
                mainProperties.NestedMetadata = properties.NestedMetadata;
            }

            var metaDataPropertyList = metaDataPropertyDictionary.Values.ToList();

            // Remove all properties without label -> must have for property
            // metaDataPropertyList = metaDataPropertyList.Where(r => r.Properties.Any(t => t.Key == Shacl.Name)).ToList();

            return(metaDataPropertyList);
        }