Newer
Older
import { runSelectQuery } from './SparqlApi';
import { endpoint, facetValuesQuery } from './SparqlQueriesGeneral';
import { prefixes } from './SparqlQueriesPrefixes';
import {
mapFacet,
mapHierarchicalFacet,
} from './Mappers';
export const getFacet = ({
facetClass,
facetID,
sortBy,
sortDirection,
uriFilters,
spatialFilters,
}) => {
let q = facetValuesQuery;
const facetConfig = facetConfigs[facetClass][facetID];
let selectedNoHitsBlock = '# no selections';
let filterBlock = '# no filters';
let parentBlock = '# no parents';
let mapper = mapFacet;
if (uriFilters !== null || spatialFilters !== null) {
filterBlock = generateFilter({
facetClass: facetClass,
uriFilters: uriFilters,
spatialFilters: spatialFilters,
filterTarget: 'instance',
facetID: facetID,
inverse: false,
});
}
if (uriFilters !== null && has(uriFilters, facetID)) {
selectedBlock = `
OPTIONAL {
FILTER(?id IN ( <${uriFilters[facetID].join('>, <')}> ))
BIND(true AS ?selected_)
}
`;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const facetIDs = Object.keys(uriFilters);
// get selected values with no hits, only when there are filters from
// other facets
if (facetIDs.length > 1) {
const noHitsFilter = generateFilter({
facetClass: facetClass,
uriFilters: uriFilters,
spatialFilters: spatialFilters,
filterTarget: 'instance',
facetID: facetID,
inverse: true,
});
selectedNoHitsBlock = `
UNION
{
{
SELECT DISTINCT (count(DISTINCT ?instance) as ?instanceCount) ?id ?selected ?parent {
{
VALUES ?id { <${uriFilters[facetID].join('> <')}> }
${noHitsFilter}
BIND(true AS ?selected)
OPTIONAL {
?id gvp:broaderPreferred ?parent_
}
BIND(COALESCE(?parent_, '0') as ?parent)
}
<PARENTS>
}
GROUP BY ?id ?selected ?parent
}
FILTER(BOUND(?id))
<FACET_VALUE_FILTER>
OPTIONAL { ?id skos:prefLabel ?prefLabel_ }
BIND(COALESCE(STR(?prefLabel_), STR(?id)) AS ?prefLabel)
}
`;
}
}
if (facetConfig.type === 'hierarchical') {
mapper = mapHierarchicalFacet;
const parentFilterStr = generateFilter({
facetClass: facetClass,
uriFilters: uriFilters,
spatialFilters: spatialFilters,
UNION
{
${parentFilterStr}
?instance2 ${facetConfig.parentPredicate} ?id .
OPTIONAL { ?id skos:prefLabel ?prefLabel_ }
BIND(COALESCE(STR(?prefLabel_), STR(?id)) AS ?prefLabel)
OPTIONAL {
?id gvp:broaderPreferred ?parent_
BIND(COALESCE(?selected_, false) as ?selected)
BIND(COALESCE(?parent_, '0') as ?parent)
}
q = q.replace('<SELECTED_VALUES>', selectedBlock);
q = q.replace('<SELECTED_VALUES_NO_HITS>', selectedNoHitsBlock);
q = q.replace(/<FACET_VALUE_FILTER>/g, facetConfig.facetValueFilter);
q = q.replace(/<PARENTS>/g, parentBlock);
q = q.replace('<ORDER_BY>', `ORDER BY ${sortDirection}(?${sortBy})` );
q = q.replace(/<RDF_TYPE>/g, facetConfigs[facetClass].rdfType);
q = q.replace(/<FILTER>/g, filterBlock );
q = q.replace(/<PREDICATE>/g, facetConfig.predicate);
// if (facetID == 'productionPlace') {
// // console.log(uriFilters)
// console.log(prefixes + q)
// }
return runSelectQuery(prefixes + q, endpoint, mapper);