Skip to content

Commit

Permalink
feat(ui): dimensions search form
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Dec 20, 2024
1 parent dd7b1c1 commit b9db92b
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 103 deletions.
2 changes: 1 addition & 1 deletion apis/shared-dimensions/bootstrap/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import type { BootstrappedResourceFactory } from './index'

export const entrypoint = (ptr: BootstrappedResourceFactory, ns: NamespaceBuilder) =>
ptr('').addOut(rdf.type, [hydra.Resource, md.Entrypoint])
.addOut(md.sharedDimensions, ns('_term-sets?pageSize=1000'))
.addOut(md.sharedDimensions, ns('_term-sets?pageSize=20'))
.addOut(md.hierarchies, ns('_hierarchies'))
5 changes: 5 additions & 0 deletions apis/shared-dimensions/bootstrap/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const SharedDimensionUpdate = clownface({ dataset: $rdf.dataset() })
.namedNode(shape('shape/shared-dimension-update'))
.addOut(rdf.type, sh.NodeShape)

const SharedDimensionSearch = clownface({ dataset: $rdf.dataset() })
.namedNode(shape('shape/shared-dimension-search'))
.addOut(rdf.type, sh.NodeShape)

const SharedDimensionTermCreate = clownface({ dataset: $rdf.dataset() })
.namedNode(shape('shape/shared-dimension-term-create'))
.addOut(rdf.type, sh.NodeShape)
Expand All @@ -30,6 +34,7 @@ const HierarchyCreate = clownface({ dataset: $rdf.dataset() })
export default [
SharedDimensionCreate,
SharedDimensionUpdate,
SharedDimensionSearch,
SharedDimensionTermCreate,
SharedDimensionTermUpdate,
Hierarchy,
Expand Down
15 changes: 10 additions & 5 deletions apis/shared-dimensions/hydra/index.ttl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PREFIX owl: <http://www.w3.org/2002/07/owl#>
BASE <urn:hydra-box:api>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <http://schema.org/> .
Expand Down Expand Up @@ -41,8 +42,9 @@ md:SharedDimensions
rdfs:subClassOf hydra:Collection ;
hydra:supportedOperation
[
a hydra:Operation ;
a hydra:Operation, schema:DownloadAction ;
hydra:method "GET" ;
hydra:expects <dimension/_shape/shared-dimension-search> ;
code:implementedBy
[
a code:EcmaScript ;
Expand All @@ -51,7 +53,7 @@ md:SharedDimensions
hydra-box:variables
[
a hydra:IriTemplate ;
hydra:template "/_term-sets{?q,pageSize,page}" ;
hydra:template "/_term-sets{?q,pageSize,page,includeDeprecated}" ;
hydra:mapping
[
a hydra:IriTemplateMapping ;
Expand All @@ -67,6 +69,11 @@ md:SharedDimensions
a hydra:IriTemplateMapping ;
hydra:property hydra:pageIndex ;
hydra:variable "page" ;
],
[
a hydra:IriTemplateMapping ;
hydra:property owl:deprecated ;
hydra:variable "includeDeprecated" ;
] ;
] ;
], [
Expand All @@ -85,6 +92,7 @@ md:SharedDimensions

<dimension/_shape/shared-dimension-create> a sh:NodeShape, sh:Shape .
<dimension/_shape/shared-dimension-update> a sh:NodeShape, sh:Shape .
<dimension/_shape/shared-dimension-search> a sh:NodeShape, sh:Shape .

md:Hierarchies
a hydra:Class ;
Expand Down Expand Up @@ -320,6 +328,3 @@ md:SharedDimensionTerm
] ;
] ;
.

<dimension/_shape/shared-dimension-term-create> a sh:Shape .
<dimension/_shape/shared-dimension-term-update> a sh:Shape .
10 changes: 6 additions & 4 deletions apis/shared-dimensions/lib/domain/shared-dimensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import type { Quad, Stream, Term } from '@rdfjs/types'
import type { Quad, Stream, Term, Literal } from '@rdfjs/types'
import { hydra, rdf, schema, sh } from '@tpluscode/rdf-ns-builders'
import $rdf from 'rdf-ext'
import { toRdf } from 'rdf-literal'
Expand All @@ -17,9 +17,10 @@ interface GetSharedDimensions {
freetextQuery?: string
limit?: number
offset?: number
includeDeprecated?: Literal
}

export async function getSharedDimensions(client: StreamClient, { freetextQuery = '', limit = 10, offset = 0 }: GetSharedDimensions = {}): Promise<Quad[]> {
export async function getSharedDimensions(client: StreamClient, { freetextQuery = '', limit = 10, offset = 0, includeDeprecated }: GetSharedDimensions = {}): Promise<Quad[]> {
const { constructQuery } = await shapeToQuery()

const shape = await loadShape('dimensions-query-shape')
Expand All @@ -30,11 +31,12 @@ export async function getSharedDimensions(client: StreamClient, { freetextQuery
limit,
offset,
freetextQuery,
includeDeprecated,
orderBy: schema.name,
}))
await rewriteTemplates(shape, variables)

const dataset = await $rdf.dataset().import(await constructQuery(shape).execute(client))
const dataset = await $rdf.dataset().import(await client.query.construct(constructQuery(shape)))
clownface({ dataset })
.has(rdf.type, schema.DefinedTermSet)
.forEach(termSet => {
Expand Down Expand Up @@ -86,7 +88,7 @@ export async function getSharedTerms<C extends StreamClient | ParsingClient>({ s
}

const { constructQuery } = await shapeToQuery()
return constructQuery(shape).execute(client, {
return client.query.construct(constructQuery(shape), {
operation: 'postDirect',
}) as any
}
Expand Down
23 changes: 15 additions & 8 deletions apis/shared-dimensions/lib/handlers/shared-dimensions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Term } from '@rdfjs/types'
import { hydra, oa, schema } from '@tpluscode/rdf-ns-builders'
import { hydra, oa, owl, schema, xsd } from '@tpluscode/rdf-ns-builders'
import { asyncMiddleware } from 'middleware-async'
import { protectedResource } from '@hydrofoil/labyrinth/resource'
import { Enrichment } from '@hydrofoil/labyrinth/lib/middleware/preprocessResource'
import httpError from 'http-errors'
import clownface, { GraphPointer } from 'clownface'
import $rdf from 'rdf-ext'
import { md } from '@cube-creator/core/namespace'
import * as ns from '@cube-creator/core/namespace'
import conditional from 'express-conditional-middleware'
import { isMultipart } from '@cube-creator/express/multipart'
import { shaclValidate } from '../middleware/shacl'
Expand All @@ -29,19 +29,26 @@ export const get = asyncMiddleware(async (req, res, next) => {
const offset = (page - 1) * pageSize
const queryParams = {
freetextQuery: query.has(hydra.freetextQuery).out(hydra.freetextQuery).value,
validThrough: query.has(md.onlyValidTerms, query.literal(true)).terms.length ? new Date() : undefined,
validThrough: query.has(ns.md.onlyValidTerms, query.literal(true)).terms.length ? new Date() : undefined,
limit: pageSize,
offset,
includeDeprecated: $rdf.literal(query.has(owl.deprecated).out(owl.deprecated).value || 'false', xsd.boolean),
}

const collection = getCollection({
view: $rdf.namedNode(req.absoluteUrl()),
memberQuads: await getSharedDimensions(streamClient, queryParams),
collectionType: md.SharedDimensions,
collectionType: ns.md.SharedDimensions,
memberType: schema.DefinedTermSet,
collection: req.hydra.resource.term,
})

collection.addOut(ns.query.templateMappings, templateMappings => {
for (const { predicate, object } of query.dataset) {
templateMappings.addOut(predicate, object)
}
})

return res.dataset(collection.dataset)
})

Expand Down Expand Up @@ -83,15 +90,15 @@ export const getTerms = asyncMiddleware(async (req, res, next) => {
const queryParams = {
sharedDimensions: sharedDimensions.map(rewriteTerm),
freetextQuery: query.has(hydra.freetextQuery).out(hydra.freetextQuery).value,
validThrough: query.has(md.onlyValidTerms, query.literal(true)).terms.length ? new Date() : undefined,
validThrough: query.has(ns.md.onlyValidTerms, query.literal(true)).terms.length ? new Date() : undefined,
limit: pageSize,
offset,
}

const collection = getCollection({
memberQuads: await getSharedTerms(queryParams, parsingClient),
memberType: schema.DefinedTerm,
collectionType: md.SharedDimensionTerms,
collectionType: ns.md.SharedDimensionTerms,
collection: termsCollectionId(sharedDimensions, queryParams.freetextQuery),
})

Expand Down Expand Up @@ -119,9 +126,9 @@ const postDirect = protectedResource(shaclValidate, asyncMiddleware(async (req,
export const post = conditional(isMultipart, postImportedDimension, postDirect)

export const injectTermsLink: Enrichment = async (req, pointer) => {
pointer.deleteOut(md.terms).addOut(md.terms, termsCollectionId([pointer.term]))
pointer.deleteOut(ns.md.terms).addOut(ns.md.terms, termsCollectionId([pointer.term]))
}

export const injectExportLink: Enrichment = async (req, pointer) => {
pointer.deleteOut(md.export).addOut(md.export, pointer.namedNode(`${env.MANAGED_DIMENSIONS_BASE}dimension/_export?dimension=${pointer.value}`))
pointer.deleteOut(ns.md.export).addOut(ns.md.export, pointer.namedNode(`${env.MANAGED_DIMENSIONS_BASE}dimension/_export?dimension=${pointer.value}`))
}
1 change: 1 addition & 0 deletions apis/shared-dimensions/lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import env from './env'

type Shapes = 'shape/shared-dimension-create'
| 'shape/shared-dimension-update'
| 'shape/shared-dimension-search'
| 'shape/shared-dimension-term-create'
| 'shape/shared-dimension-term-update'
| 'shape/hierarchy-create'
Expand Down
8 changes: 5 additions & 3 deletions apis/shared-dimensions/lib/shapeToQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import onetime from 'onetime'
import { md } from '@cube-creator/core/namespace'
import { AnyPointer, GraphPointer } from 'clownface'
import { isGraphPointer } from 'is-graph-pointer'
import { hydra } from '@tpluscode/rdf-ns-builders'
import { hydra, sh } from '@tpluscode/rdf-ns-builders'
import { Parameters, PropertyShape } from '@hydrofoil/shape-to-query/model/constraint/ConstraintComponent'
import evalTemplateLiteral from 'rdf-loader-code/evalTemplateLiteral.js'
import namespace from '@rdfjs/namespace'
Expand Down Expand Up @@ -58,11 +58,13 @@ export async function rewriteTemplates(shape: AnyPointer, variables: Map<string,
return
}

const value = variables.get(variableName) as any
const value: any = variables.get(variableName) || templateNode.out(sh.defaultValue).term

;[...shape.dataset.match(null, null, templateNode.term)].forEach(quad => {
shape.dataset.delete(quad)
shape.dataset.add($rdf.quad(quad.subject, quad.predicate, value, quad.graph))
if (value) {
shape.dataset.add($rdf.quad(quad.subject, quad.predicate, value, quad.graph))
}
})

templateNode.deleteOut()
Expand Down
16 changes: 16 additions & 0 deletions apis/shared-dimensions/lib/shapes/dimensions-query-shape.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ prefix md: <https://cube-creator.zazuko.com/shared-dimensions/vocab#>
sh:path rdf:type ;
sh:hasValue schema:DefinedTermSet, meta:SharedDimension ;
] ;
sh:expression
[
sh:deactivated [ s2q:variable "includeDeprecated" ; sh:defaultValue false ] ;
sparql:or
(
[ sparql:not ( [ sparql:bound ( _:validThrough ) ] ) ]
[ sparql:gt ( _:validThrough [ sparql:now () ] ) ]
) ;
] ;
] ;
] ;
] ;
Expand Down Expand Up @@ -101,3 +110,10 @@ prefix md: <https://cube-creator.zazuko.com/shared-dimensions/vocab#>
]
] ;
] .

_:validThrough
s2q:optional
[
sh:path schema:validThrough ;
] ;
.
1 change: 1 addition & 0 deletions apis/shared-dimensions/lib/shapes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function entry(id: NamedNode, init: (graph: AnyPointer) => Initializer<NodeShape
export default new TermMap<NamedNode, ShapeFactory>([
entry(shape['shape/shared-dimension-create'], sharedDimension.create),
entry(shape['shape/shared-dimension-update'], sharedDimension.update),
entry(shape['shape/shared-dimension-search'], sharedDimension.search),
entry(shape['shape/shared-dimension-term-create'], dimensionTerm.create),
entry(shape['shape/shared-dimension-term-update'], dimensionTerm.update),
entry(shape['shape/hierarchy-create'], hierarchy()),
Expand Down
22 changes: 22 additions & 0 deletions apis/shared-dimensions/lib/shapes/shared-dimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { editor, iso6391, md, meta, sh1 } from '@cube-creator/core/namespace'
import { fromPointer as nodeShape } from '@rdfine/shacl/lib/NodeShape'
import { fromPointer as propertyGroup } from '@rdfine/shacl/lib/PropertyGroup'
import { fromPointer as resource } from '@rdfine/rdfs/lib/Resource'
import { owl } from '@tpluscode/rdf-ns-builders'

const defaultGroup = $rdf.namedNode('#default-group')
const datatypeUri = [xsd.anyURI, ['URI']]
Expand Down Expand Up @@ -387,3 +388,24 @@ export const update = (): Initializer<NodeShape> => ({
},
],
})

export const search = (): Initializer<NodeShape> => ({
property: [
{
path: hydra.freetextQuery,
name: 'Name',
minCount: 1,
maxCount: 1,
order: 1,
},
{
path: owl.deprecated,
name: 'Show deprecated',
minCount: 1,
maxCount: 1,
defaultValue: false,
datatype: xsd.boolean,
order: 2,
},
],
})
5 changes: 3 additions & 2 deletions apis/shared-dimensions/lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ export default class implements SharedDimensionsStore {
const { constructQuery } = await shapeToQuery()
const query = constructQuery(shape, {
focusNode: term,
}).FROM(this.graph)
})

const quads = await query.execute(this.client, {
const quads = await this.client.query.construct(query, {
operation: 'postDirect',
defaultGraphUri: [this.graph],
})
return clownface({
dataset: $rdf.dataset(quads),
Expand Down
2 changes: 1 addition & 1 deletion apis/shared-dimensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@cube-creator/core": "1.0.0",
"@cube-creator/express": "0.0.0",
"@hydrofoil/labyrinth": "^0.4.2",
"@hydrofoil/shape-to-query": "^0.12",
"@hydrofoil/shape-to-query": "^0.13.2",
"@rdfine/hydra": "^0.8.2",
"@rdfine/rdfs": "^0.6.4",
"@rdfine/schema": "^0.6.3",
Expand Down
1 change: 1 addition & 0 deletions fuseki/shared-dimensions.trig
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ graph <http://example.com/dimension/colors> {
<http://example.com/dimension/colors>
a schema:DefinedTermSet, meta:SharedDimension ;
schema:validFrom "2021-01-20T23:59:59Z"^^xsd:dateTime ;
schema:validThrough "2021-04-20T23:59:59Z"^^xsd:dateTime ;
schema:name "Colors"@en, "Farben"@de, "I colori"@it, "Les couleurs"@fr ;
.

Expand Down
Loading

0 comments on commit b9db92b

Please sign in to comment.