diff --git a/src/sparql/distribution.ts b/src/sparql/distribution.ts index 2f850d0da85d2b0343d114c829e1d115cdea4e2b..4ead4c6b03b90e2b8b848e0a5a355d02e0fb541a 100644 --- a/src/sparql/distribution.ts +++ b/src/sparql/distribution.ts @@ -14,6 +14,7 @@ import { executeSparqlConstruct } from './sparql' import { resourceContext } from './resource' +import { ContextDefinition } from 'jsonld' export async function insertDistribution( distribution: Partial<OcDistribution>, @@ -124,104 +125,74 @@ export async function getDistribution( identifier: string, auth?: Credentials ): Promise<OcDistribution> { - /** - * First, get the @id of the catalog - * to better filtering requests. - */ - const distributionIdResponse = await executeSparqlConstruct<OcDistribution>( - ` - CONSTRUCT { - ?distribution ?p ?o. - ?distribution oct:graph ?g. - } - WHERE { - ?distribution ?p ?o; - a dcat:Distribution. - GRAPH ?g { - ?distribution dct:identifier ?identifier. - } - FILTER (str(?identifier) = "${identifier}"). + const context: ContextDefinition = { + ...resourceContext, + accessURL: { '@id': 'http://www.w3.org/ns/dcat#accessURL', '@type': '@id' }, + license: { '@id': 'http://purl.org/dc/terms/license', '@type': '@id' }, + type: { '@id': 'http://purl.org/dc/terms/type', '@type': '@id' }, + accessRights: { '@id': 'http://purl.org/dc/terms/accessRights', '@type': '@id' }, + format: { '@id': 'http://purl.org/dc/terms/format', '@type': '@id' }, + label: { + '@id': 'http://www.w3.org/2004/02/skos/core#label', + '@container': '@language' + }, + prefLabel: { + '@id': 'http://www.w3.org/2004/02/skos/core#prefLabel', + '@container': '@language' } - - `, - { - auth, - context: { - ...resourceContext, - accessURL: { '@id': 'http://www.w3.org/ns/dcat#accessURL', '@type': '@id' }, - license: { '@id': 'http://purl.org/dc/terms/license', '@type': '@id' }, - type: { '@id': 'http://purl.org/dc/terms/type', '@type': '@id' }, - accessRights: { '@id': 'http://purl.org/dc/terms/accessRights', '@type': '@id' }, - format: { '@id': 'http://purl.org/dc/terms/format', '@type': '@id' } + } + + const frame = { + '@context': context, + '@type': [ + 'http://www.w3.org/ns/dcat#Distribution', + ], + contains: { + accessRights: { + '@embed': '@always', + }, + type: { + '@embed': '@always' + }, + format: { + '@embed': '@always' + }, + license: { + '@embed': '@always' } } - ) - const distribution = distributionIdResponse?.[0] - const distributionUri = distribution['@id'] + } - /** - * access rights, License, type, access URL and format - */ - const distributionRelatedResponse = await executeSparqlConstruct<OcConcept>( + const distributionResponse = await executeSparqlConstruct<OcDistribution>( ` CONSTRUCT { - ?accessRights ?p1 ?o1. - ?license ?p2 ?o2. - ?type ?p3 ?o3. - ?format ?p5 ?o5. + ?distribution ?p ?o. + ?distribution oct:graph ?g. + ?o2 ?p3 ?o3. } WHERE { - OPTIONAL { - <${distributionUri}> dct:accessRights ?accessRights. - ?accessRights ?p1 ?o1. - } - OPTIONAL { - <${distributionUri}> dct:license ?license. - ?license ?p2 ?o2. - } - OPTIONAL { - <${distributionUri}> dct:type ?type. - ?type ?p3 ?o3. - } - OPTIONAL { - <${distributionUri}> dct:format ?format. - ?format ?p5 ?o5. + GRAPH ?g { + ?distribution a dcat:Distribution; + dct:identifier ?identifier. + FILTER (str(?identifier) = "${identifier}") } + + ?distribution ?p ?o. + + VALUES ?p2 { dct:accessRights dct:license dct:type dct:format } + ?distribution ?p2 ?o2. + ?o2 ?p3 ?o3. } `, { auth, - context: { - ...resourceContext, - label: { - '@id': 'http://www.w3.org/2004/02/skos/core#label', - '@container': '@language' - }, - prefLabel: { - '@id': 'http://www.w3.org/2004/02/skos/core#prefLabel', - '@container': '@language' - } - } + context: context, + frame: frame } ) + const distribution = distributionResponse?.[0] - const distributionResponse = await Promise.all([ - distributionRelatedResponse // 0 - ]) - - distribution.license = distributionResponse[0].find( - (d) => d['@id'] === (distribution.license as unknown as string) - ) - distribution.type = distributionResponse[0].find( - (d) => d['@id'] === (distribution.type as unknown as string) - ) - distribution.accessRights = distributionResponse[0].find( - (d) => d['@id'] === (distribution.accessRights as unknown as string) - ) - distribution.format = distributionResponse[0].find( - (d) => d['@id'] === (distribution.format as unknown as string) - ) return distribution }