From ac4db2ebf39ef57b7afc4cefd0b3f87831f01ee5 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Tue, 23 Aug 2022 15:04:00 +0200 Subject: [PATCH] fix: Support legacy hierarchies described with hasHierarchy See https://github.com/zazuko/cube-creator/issues/1243 and ongoing discussion on https://zulip.zazuko.com/#narrow/stream/40-bafu-ext/topic/Hierarchie.20removed.20from.20cube/near/283252 --- app/rdf/queries.ts | 1 + app/rdf/query-hierarchies.ts | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/rdf/queries.ts b/app/rdf/queries.ts index bc92da827..a45e873aa 100644 --- a/app/rdf/queries.ts +++ b/app/rdf/queries.ts @@ -572,6 +572,7 @@ const buildFilters = ({ } const hasHierarchy = + cubeDimension.out(ns.cubeMeta.inHierarchy).values.length > 0 || cubeDimension.out(ns.cubeMeta.hasHierarchy).values.length > 0; const toRDFValue = (value: string): NamedNode | Literal => { return dataType && !hasHierarchy diff --git a/app/rdf/query-hierarchies.ts b/app/rdf/query-hierarchies.ts index cc285f81a..138cd7fb9 100644 --- a/app/rdf/query-hierarchies.ts +++ b/app/rdf/query-hierarchies.ts @@ -5,6 +5,7 @@ import { } from "@zazuko/cube-hierarchy-query/index"; import { AnyPointer } from "clownface"; import { isGraphPointer } from "is-graph-pointer"; +import { Cube } from "rdf-cube-view-query"; import rdf from "rdf-ext"; import { StreamClient } from "sparql-http-client"; import { ParsingClient } from "sparql-http-client/ParsingClient"; @@ -59,6 +60,27 @@ const toTree = ( return results.map((r) => serializeNode(r, 0)); }; +const findHierarchyForDimension = (cube: Cube, dimensionIri: string) => { + const newHierarchy = cube.ptr + .any() + .has(ns.sh.path, rdf.namedNode(dimensionIri)) + .has(ns.cubeMeta.inHierarchy) + .out(ns.cubeMeta.inHierarchy) + .toArray()[0]; + if (newHierarchy) { + return newHierarchy; + } + const legacyHierarchy = cube.ptr + .any() + .has(ns.sh.path, rdf.namedNode(dimensionIri)) + .has(ns.cubeMeta.hasHierarchy) + .out(ns.cubeMeta.hasHierarchy) + .toArray()[0]; + if (legacyHierarchy) { + return legacyHierarchy; + } +}; + export const queryHierarchy = async ( dimensionIri: string, sourceUrl: string, @@ -79,18 +101,13 @@ export const queryHierarchy = async ( if (cubeResults.length === 0) { throw new Error("Could not find cube"); } + const cubeIri = cubeResults[0].cube.value; const cube = await source.cube(cubeIri); if (!cube) { throw new Error("Could not find cube"); } - const hierarchy = cube.ptr - .any() - .has(ns.sh.path, rdf.namedNode(dimensionIri)) - .has(ns.cubeMeta.inHierarchy) - .out(ns.cubeMeta.inHierarchy) - .toArray() - .shift(); + const hierarchy = findHierarchyForDimension(cube, dimensionIri); // @ts-ignore if (!isGraphPointer(hierarchy)) {