diff --git a/src/client/actions/index.js b/src/client/actions/index.js index a29d11bfa120ce63578bd368f50e80d39f855d56..eebd86d960a762d7412eb930ce978db8b4ffa65a 100644 --- a/src/client/actions/index.js +++ b/src/client/actions/index.js @@ -20,13 +20,13 @@ export const OPEN_MARKER_POPUP = 'OPEN_MARKER_POPUP'; export const CLEAR_ERROR = 'CLEAR_ERROR'; -export const fetchResults = (resultClass, variant) => ({ +export const fetchResults = (resultClass, facetClass, variant) => ({ type: FETCH_RESULTS, - resultClass, variant + resultClass, facetClass, variant }); -export const updateResults = ({ data }) => ({ +export const updateResults = ({ resultClass, data }) => ({ type: UPDATE_RESULTS, - data + resultClass, data }); export const sortResults = sortBy => ({ type: SORT_RESULTS, @@ -41,13 +41,13 @@ export const fetchResultsFailed = error => ({ error }); -export const fetchByURI = uri => ({ +export const fetchByURI = (resultClass, uri) => ({ type: FETCH_BY_URI, - uri + resultClass, uri }); -export const updateInstance = ({ instance }) => ({ +export const updateInstance = ({ resultClass, instance }) => ({ type: UPDATE_INSTANCE, - instance + resultClass, instance }); export const fetchByURIFailed = error => ({ type: FETCH_BY_URI_FAILED, diff --git a/src/client/components/FacetBar.js b/src/client/components/FacetBar.js index 05922cf134c53766d4f6c85552a23e2d415e27ab..ec24426b915e7ed99721d60bc6fb30411c6335d2 100644 --- a/src/client/components/FacetBar.js +++ b/src/client/components/FacetBar.js @@ -69,7 +69,7 @@ class FacetBar extends React.Component { facetFilters={filters} updateFilter={this.props.updateFilter} updatedFacet={updatedFacet} - searchField={false} + searchField={true} /> </div> </Paper> diff --git a/src/client/components/LeafletMap.js b/src/client/components/LeafletMap.js index d4b0bd892342a20e1b0334bd3ce0950cc2776e3d..3f63f479a3f37255f31a820e3013dd4427b22fba 100644 --- a/src/client/components/LeafletMap.js +++ b/src/client/components/LeafletMap.js @@ -71,7 +71,7 @@ class LeafletMap extends React.Component { componentDidMount() { - //this.props.fetchPlaces(this.props.variant); + this.props.fetchResults(this.props.resultClass, this.props.facetClass, this.props.variant); // Base layers // const OSMBaseLayer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { @@ -146,7 +146,7 @@ class LeafletMap extends React.Component { // check if results data or mapMode have changed if (this.props.results !== results || this.props.mapMode !== mapMode) { if (this.props.mapMode === 'cluster') { - console.log(this.props.results) + //console.log(this.props.results) this.updateMarkersAndCluster(this.props.results); } else { this.updateMarkers(this.props.results); @@ -154,6 +154,7 @@ class LeafletMap extends React.Component { } if (this.props.instance !== instance) { + console.log(instance) this.markers[this.props.instance.id] .bindPopup(this.createPopUpContent(this.props.instance), { maxHeight: 300, @@ -189,49 +190,45 @@ class LeafletMap extends React.Component { } updateMarkersAndCluster(results) { + //console.log(results) this.resultMarkerLayer.clearLayers(); this.markers = {}; let clusterer = null; - if (this.props.variant === 'productionPlaces') { - clusterer = new L.MarkerClusterGroup({ - iconCreateFunction: (cluster) => { - //const childCount = cluster.getChildCount(); - let childCount = 0; + clusterer = new L.MarkerClusterGroup({ + iconCreateFunction: (cluster) => { + let childCount = 0; + if (this.props.showInstanceCountInClusters) { cluster.getAllChildMarkers().forEach(marker => { - childCount += parseInt(marker.options.manuscriptCount); + childCount += parseInt(marker.options.instanceCount); }); - let c = ' marker-cluster-'; - if (childCount < 10) { - c += 'small'; - } else if (childCount < 100) { - c += 'medium'; - } else { - c += 'large'; - } - return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) }); + } else { + childCount = cluster.getChildCount(); } - }); - } else { - clusterer = new L.MarkerClusterGroup({ - iconCreateFunction: cluster => { - const childCount = cluster.getChildCount(); - return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster marker-cluster-grey', iconSize: new L.Point(40, 40) }); + let c = ' marker-cluster-'; + if (childCount < 10) { + c += 'small'; + } else if (childCount < 100) { + c += 'medium'; + } else { + c += 'large'; } - }); - } - - results.forEach(value => { - //console.log(value) - const marker = this.createMarker(value); - this.markers[value.id] = marker; - marker == null ? null : clusterer.addLayer(marker); + return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) }); + //return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster marker-cluster-grey', iconSize: new L.Point(40, 40) }); + } }); + for (const result of results) { + const marker = this.createMarker(result); + if (marker !== null) { + this.markers[result.id] = marker; + clusterer.addLayer(marker); + } + } clusterer.addTo(this.resultMarkerLayer); } createMarker(result) { // const color = typeof result.markerColor === 'undefined' ? 'grey' : result.markerColor; - //const icon = new ColorIcon({iconUrl: 'img/markers/marker-icon-' + color + '.png'}); + // const icon = new ColorIcon({iconUrl: 'img/markers/marker-icon-' + color + '.png'}); if (!has(result, 'lat') || !has(result, 'long')) { return null; } @@ -241,18 +238,18 @@ class LeafletMap extends React.Component { const latLng = [+lat, +long]; let marker = null; - if (this.props.variant === 'productionPlaces') { + if (this.props.showInstanceCountInClusters) { // https://github.com/coryasilva/Leaflet.ExtraMarkers const icon = L.ExtraMarkers.icon({ icon: 'fa-number', - number: result.manuscriptCount, - markerColor: 'red', + number: result.instanceCount, + markerColor: 'blue', shape: 'circle', prefix: 'fa' }); marker = L.marker(latLng, { icon: icon, - manuscriptCount: result.manuscriptCount ? result.manuscriptCount : null, + instanceCount: result.instanceCount ? result.instanceCount : null, id: result.id }) .on('click', this.markerOnClick); @@ -288,7 +285,7 @@ class LeafletMap extends React.Component { } markerOnClick = event => { - this.props.fetchByURI(event.target.options.id); + this.props.fetchByURI('places', event.target.options.id,); }; createPopUpContent(result) { @@ -356,9 +353,13 @@ LeafletMap.propTypes = { results: PropTypes.array.isRequired, instance: PropTypes.object.isRequired, fetchResults: PropTypes.func, + resultClass: PropTypes.string, + facetClass: PropTypes.string, fetchByURI: PropTypes.func.isRequired, fetching: PropTypes.bool.isRequired, mapMode: PropTypes.string.isRequired, + variant: PropTypes.string.isRequired, + showInstanceCountInClusters: PropTypes.bool }; export default withStyles(styles)(LeafletMap); diff --git a/src/client/components/Manuscripts.js b/src/client/components/Manuscripts.js index 6d4bccab0352c960b664817fe4236736e6c2a508..bdb0c474b9d299fb43b000f7a8cbdb25b1180318 100644 --- a/src/client/components/Manuscripts.js +++ b/src/client/components/Manuscripts.js @@ -40,6 +40,7 @@ let Manuscripts = props => { data={props.manuscripts} filters={props.facetData.filters} resultClass='manuscripts' + facetClass='manuscripts' fetchResults={props.fetchResults} updatePage={props.updatePage} sortResults={props.sortResults} @@ -51,11 +52,16 @@ let Manuscripts = props => { path={'/manuscripts/production_places'} render={() => <LeafletMap - results={props.facetData.facets.productionPlace.flatValues} - instance={props.manuscripts.instance} + results={props.places.results} + resultClass='places' + facetClass='manuscripts' + instance={props.places.instance} + fetchResults={props.fetchResults} fetchByURI={props.fetchByURI} - fetching={props.facetData.fetching} + fetching={props.places.fetching} mapMode={'cluster'} + variant='productionPlaces' + showInstanceCountInClusters={true} />} /> {/*<Route @@ -73,6 +79,7 @@ let Manuscripts = props => { Manuscripts.propTypes = { manuscripts: PropTypes.object.isRequired, + places: PropTypes.object.isRequired, facetData: PropTypes.object.isRequired, fetchResults: PropTypes.func.isRequired, fetchByURI: PropTypes.func.isRequired, diff --git a/src/client/components/ResultTable.js b/src/client/components/ResultTable.js index 76a2bf8a6848f3990c2f1989fcbdd35ac96dbd23..daac7a16eec05f14106d23343aa66c16bdcce8d8 100644 --- a/src/client/components/ResultTable.js +++ b/src/client/components/ResultTable.js @@ -59,7 +59,7 @@ class ResultTable extends React.Component { componentDidUpdate = prevProps => { if (prevProps.data.page != this.props.data.page) { - this.props.fetchResults(this.props.resultClass); + this.props.fetchResults(this.props.resultClass, this.props.facetClass, null); this.props.routeProps.history.push({ pathname: `/${this.props.resultClass}/table`, data: `?page=${this.props.data.page}`, @@ -68,17 +68,17 @@ class ResultTable extends React.Component { if (prevProps.filters != this.props.filters) { this.props.updatePage(0); if (this.props.data.page == 0) { - this.props.fetchResults(this.props.resultClass); + this.props.fetchResults(this.props.resultClass, this.props.facetClass, null); } } if (prevProps.data.sortBy != this.props.data.sortBy) { this.props.updatePage(0); if (this.props.data.page == 0) { - this.props.fetchResults(this.props.resultClass); + this.props.fetchResults(this.props.resultClass, this.props.facetClass, null); } } if (prevProps.data.sortDirection != this.props.data.sortDirection) { - this.props.fetchResults(this.props.resultClass); + this.props.fetchResults(this.props.resultClass, this.props.facetClass, null); } } @@ -161,6 +161,7 @@ ResultTable.propTypes = { classes: PropTypes.object.isRequired, data: PropTypes.object.isRequired, resultClass: PropTypes.string.isRequired, + facetClass: PropTypes.string.isRequired, filters: PropTypes.object.isRequired, fetchResults: PropTypes.func.isRequired, sortResults: PropTypes.func.isRequired, diff --git a/src/client/containers/MapApp.js b/src/client/containers/MapApp.js index 240c5f30b04a9a5ad609a1bacab489c4ae130a47..d2139f0e6613423dc319263708e12a3d9dbcc1c3 100644 --- a/src/client/containers/MapApp.js +++ b/src/client/containers/MapApp.js @@ -100,6 +100,7 @@ let MapApp = (props) => { <Paper className={classes.resultsContainerPaper}> <Manuscripts manuscripts={props.manuscripts} + places={props.places} facetData={props.manuscriptsFacets} fetchResults={props.fetchResults} fetchByURI={props.fetchByURI} diff --git a/src/client/epics/index.js b/src/client/epics/index.js index 51931e6a25886571b429002423bfb8f641880928..8f66213f3fc34bf003321ba46787705ecf3c0d08 100644 --- a/src/client/epics/index.js +++ b/src/client/epics/index.js @@ -20,11 +20,11 @@ const fetchResultsEpic = (action$, state$) => action$.pipe( ofType(FETCH_RESULTS), withLatestFrom(state$), mergeMap(([action, state]) => { - const { resultClass } = action; - const params = stateSliceToUrl(state[resultClass], state[`${resultClass}Facets`]); + const { resultClass, facetClass, variant } = action; + const params = stateSliceToUrl(state[resultClass], state[`${facetClass}Facets`], variant); const requestUrl = `${apiUrl}${resultClass}/results?${params}`; return ajax.getJSON(requestUrl).pipe( - map(response => updateResults({ data: response })) + map(response => updateResults({ resultClass: resultClass, data: response })) ); }) ); @@ -32,10 +32,10 @@ const fetchResultsEpic = (action$, state$) => action$.pipe( const fetchByURIEpic = action$ => action$.pipe( ofType(FETCH_BY_URI), mergeMap(action => { - const searchUrl = apiUrl + 'places'; - const requestUrl = `${searchUrl}/${encodeURIComponent(action.placeId)}`; + const { uri, resultClass } = action; + const requestUrl = `${apiUrl}${resultClass}/instance/${encodeURIComponent(uri)}`; return ajax.getJSON(requestUrl).pipe( - map(response => updateInstance({ instance: response })) + map(response => updateInstance({ resultClass: resultClass, instance: response })) ); }) ); @@ -73,13 +73,16 @@ const fetchFacetEpic = (action$, state$) => action$.pipe( }) ); -export const stateSliceToUrl = (stateSlice, facets) => { +export const stateSliceToUrl = (stateSlice, facets, variant) => { let params = { page: stateSlice.page, pagesize: stateSlice.pagesize, sortBy: stateSlice.sortBy, - sortDirection: stateSlice.sortDirection + sortDirection: stateSlice.sortDirection, }; + if (variant !== null) { + params.variant = variant; + } let filters = {}; let activeFilters = false; for (const [key, value] of Object.entries(facets.filters)) { diff --git a/src/client/reducers/manuscripts.js b/src/client/reducers/manuscripts.js index 6405af87c8f645d4f939ad6f518579427cbda7d8..fd9bb270d7980e9df66486e4ef08e665325927d3 100644 --- a/src/client/reducers/manuscripts.js +++ b/src/client/reducers/manuscripts.js @@ -97,12 +97,14 @@ const manuscripts = (state = INITIAL_STATE, action) => { case SORT_RESULTS: return updateSortBy(state, action); case UPDATE_RESULTS: - return { - ...state, - resultCount: parseInt(action.data.resultCount), - results: action.data.results, - fetching: false - }; + if (action.resultClass === 'manuscripts') { + return { + ...state, + resultCount: parseInt(action.data.resultCount), + results: action.data.results, + fetching: false + }; + } else return state; case UPDATE_INSTANCE: return { ...state, diff --git a/src/client/reducers/places.js b/src/client/reducers/places.js index 08d547db15c20b906e8c64a86f7e0da55f7a987f..d28c2820f6b772aa88054a4fabb9a67a8a611f5a 100644 --- a/src/client/reducers/places.js +++ b/src/client/reducers/places.js @@ -16,7 +16,7 @@ export const INITIAL_STATE = { pagesize: 5, sortBy: 'prefLabel', sortDirection: 'asc', - fetchingResults: false, + fetching: false, tableColumns: [ { id: 'prefLabel', @@ -51,34 +51,37 @@ export const INITIAL_STATE = { }; const places = (state = INITIAL_STATE, action) => { - switch (action.type) { - case FETCH_RESULTS: - return { ...state, fetching: true }; - case FETCH_BY_URI: - return { ...state, fetching: true }; - case SORT_RESULTS: - return updateSortBy(state, action); - case UPDATE_RESULTS: - return { - ...state, - resultCount: parseInt(action.data.resultCount), - results: action.data.results, - fetching: false - }; - case UPDATE_INSTANCE: - return { - ...state, - instance: action.instance, - fetchingPlaces: false - }; - case UPDATE_PAGE: - return { - ...state, - page: action.page - }; - default: - return state; - } + if (action.resultClass === 'places') { + switch (action.type) { + case FETCH_RESULTS: + return { ...state, fetching: true }; + case FETCH_BY_URI: + return { ...state, fetching: true }; + case SORT_RESULTS: + return updateSortBy(state, action); + case UPDATE_RESULTS: + return { + ...state, + resultCount: parseInt(action.data.resultCount), + results: action.data.results, + fetching: false + }; + case UPDATE_INSTANCE: + console.log(action) + return { + ...state, + instance: action.instance, + fetching: false + }; + case UPDATE_PAGE: + return { + ...state, + page: action.page + }; + default: + return state; + } + } else return state; }; export default places; diff --git a/src/server/index.js b/src/server/index.js index f21dbfca0e74b2fbe62108949bca8e120c8a977e..315ffd527d38880dccebfa0c205a31b2ffb53d14 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -1,7 +1,8 @@ import express from 'express'; import path from 'path'; import bodyParser from 'body-parser'; -import { getManuscripts } from './sparql/Manuscripts'; +import { /*getManuscript,*/ getManuscripts } from './sparql/Manuscripts'; +import { getPlace, getPlaces } from './sparql/Places'; import { getFacet } from './sparql/Facets'; const DEFAULT_PORT = 3001; const app = express(); @@ -21,17 +22,22 @@ app.use(function(req, res, next) { app.use(express.static(path.join(__dirname, './../public/'))); app.get(`${apiPath}/:resultClass/results`, (req, res) => { - const page = parseInt(req.query.page) || 0; - const pagesize = parseInt(req.query.pagesize) || 5; - const sortBy = req.query.sortBy; - const sortDirection = req.query.sortDirection; + const page = parseInt(req.query.page) || null; + const pagesize = parseInt(req.query.pagesize) || null; + const sortBy = req.query.sortBy || null; + const sortDirection = req.query.sortDirection || null; const filters = req.query.filters == null ? null : JSON.parse(req.query.filters); + const variant = req.query.variant || null; let getResults = null; switch (req.params.resultClass) { case 'manuscripts': getResults = getManuscripts; + break; + case 'places': + getResults = getPlaces; + break; } - return getResults(page, pagesize, filters, sortBy, sortDirection).then(data => { + return getResults(variant, page, pagesize, filters, sortBy, sortDirection).then(data => { res.json(data); }) .catch(err => { @@ -40,6 +46,25 @@ app.get(`${apiPath}/:resultClass/results`, (req, res) => { }); }); +app.get(`${apiPath}/:resultClass/instance/:uri`, (req, res) => { + let getByURI = null; + switch (req.params.resultClass) { + // case 'manuscripts': + // getByURI = getManuscript; + // break; + case 'places': + getByURI = getPlace; + break; + } + return getByURI(req.params.uri).then(data => { + res.json(data[0]) + }) + .catch(err => { + console.log(err); + return res.sendStatus(500); + }); +}); + app.get(`${apiPath}/facet/:id`, (req, res) => { const filters = req.query.filters == null ? null : JSON.parse(req.query.filters); return getFacet(req.params.id, req.query.sortBy, req.query.sortDirection, filters).then(data => { diff --git a/src/server/sparql/Datasets.js b/src/server/sparql/Datasets.js index 00d825b8dda471d4b4c84ac67e2b12978558b034..8cb01fd5e7518df004ba49d5ccb80c394fcb24e0 100644 --- a/src/server/sparql/Datasets.js +++ b/src/server/sparql/Datasets.js @@ -119,18 +119,20 @@ module.exports = { PREFIX frbroo: <http://erlangen-crm.org/efrbroo/> PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/> PREFIX mmm-schema: <http://ldf.fi/mmm/schema/> - SELECT ?id ?lat ?long ?prefLabel ?dataProviderUrl - (COUNT(DISTINCT ?manuscript) as ?manuscriptCount) + SELECT ?id ?lat ?long ?prefLabel ?source ?dataProviderUrl + (COUNT(DISTINCT ?manuscript) as ?instanceCount) WHERE { ?manuscript ^crm:P108_has_produced/crm:P7_took_place_at ?id . + <FILTER> ?id skos:prefLabel ?prefLabel . + ?id dct:source ?source . OPTIONAL { ?id mmm-schema:data_provider_url ?dataProviderUrl } OPTIONAL { ?id wgs84:lat ?lat ; wgs84:long ?long . } } - GROUP BY ?id ?lat ?long ?prefLabel ?dataProviderUrl + GROUP BY ?id ?lat ?long ?prefLabel ?source ?dataProviderUrl `, 'allPlacesQuery': ` PREFIX skos: <http://www.w3.org/2004/02/skos/core#> @@ -235,10 +237,11 @@ module.exports = { PREFIX frbroo: <http://erlangen-crm.org/efrbroo/> PREFIX mmm-schema: <http://ldf.fi/mmm/schema/> PREFIX gvp: <http://vocab.getty.edu/ontology#> - SELECT DISTINCT ?id ?prefLabel ?selected ?source ?parent ?instanceCount { + PREFIX wgs84: <http://www.w3.org/2003/01/geo/wgs84_pos#> + SELECT DISTINCT ?id ?prefLabel ?selected ?source ?parent ?lat ?long ?instanceCount { { { - SELECT DISTINCT (count(DISTINCT ?instance) as ?instanceCount) ?id ?selected ?parent ?source { + SELECT DISTINCT (count(DISTINCT ?instance) as ?instanceCount) ?id ?selected ?source ?lat ?long ?parent { { ?instance a frbroo:F4_Manifestation_Singleton . <FILTER> @@ -246,15 +249,16 @@ module.exports = { <SELECTED_VALUES> BIND(COALESCE(?selected_, false) as ?selected) OPTIONAL { ?id dct:source ?source } + OPTIONAL { ?id gvp:broaderPreferred ?parent_ . } OPTIONAL { - ?id gvp:broaderPreferred ?parent_ . - #FILTER(?parent_ != <http://ldf.fi/mmm/places/tgn_7029392>) + ?id wgs84:lat ?lat ; + wgs84:long ?long . } BIND(COALESCE(?parent_, '0') as ?parent) } <PARENTS> } - GROUP BY ?id ?selected ?source ?parent + GROUP BY ?id ?selected ?source ?lat ?long ?parent } FILTER(BOUND(?id)) OPTIONAL { ?id skos:prefLabel ?prefLabel_ } diff --git a/src/server/sparql/Helpers.js b/src/server/sparql/Helpers.js new file mode 100644 index 0000000000000000000000000000000000000000..af04d72a0d54fa01de716f363b760d4561755b31 --- /dev/null +++ b/src/server/sparql/Helpers.js @@ -0,0 +1,12 @@ +import { facetConfigs } from './FacetConfigs'; + +export const generateResultFilter = filters => { + let filterStr = ''; + for (let property in filters) { + filterStr += ` + VALUES ?${property}Filter { <${filters[property].join('> <')}> } + ?id ${facetConfigs[property].predicate} ?${property}Filter . + `; + } + return filterStr; +}; diff --git a/src/server/sparql/Manuscripts.js b/src/server/sparql/Manuscripts.js index b7b23d8b4c6bbbeb971fa7dd10ba3625632c0268..5ad3a863cdb2582e85986fcac3b5f4373d1496ad 100644 --- a/src/server/sparql/Manuscripts.js +++ b/src/server/sparql/Manuscripts.js @@ -3,13 +3,14 @@ import datasetConfig from './Datasets'; import { mapCount } from './Mappers'; import { makeObjectList } from './SparqlObjectMapper'; import { facetConfigs } from './FacetConfigs'; +import { generateResultFilter } from './Helpers'; const sparqlSearchEngine = new SparqlSearchEngine(); -export const getManuscripts = (page, pagesize, filters, sortBy, sortDirection) => { +export const getManuscripts = (variant, page, pagesize, filters, sortBy, sortDirection) => { return Promise.all([ getManuscriptCount(filters), - getManuscriptData(page, pagesize, filters, sortBy, sortDirection), + getManuscriptData(variant, page, pagesize, filters, sortBy, sortDirection), ]) .then(data => { return { @@ -22,7 +23,7 @@ export const getManuscripts = (page, pagesize, filters, sortBy, sortDirection) = .catch(err => console.log(err)); }; -const getManuscriptData = (page, pagesize, filters, sortBy, sortDirection) => { +const getManuscriptData = (variant, page, pagesize, filters, sortBy, sortDirection) => { let { endpoint, manuscriptQuery } = datasetConfig['mmm']; if (filters == null) { manuscriptQuery = manuscriptQuery.replace('<FILTER>', ''); @@ -41,28 +42,3 @@ const getManuscriptCount = filters => { countQuery = countQuery.replace('<FILTER>', generateResultFilter(filters)); return sparqlSearchEngine.doSearch(countQuery, endpoint, mapCount); }; - -export const getPlaces = variant => { - const config = datasetConfig['mmm']; - const query = config[`${variant}Query`]; - //console.log(query) - return sparqlSearchEngine.doSearch(query, config.endpoint, makeObjectList); -}; - -export const getPlace = id => { - let { endpoint, placeQuery } = datasetConfig['mmm']; - placeQuery = placeQuery.replace('<PLACE_ID>', `<${id}>`); - // console.log(placeQuery) - return sparqlSearchEngine.doSearch(placeQuery, endpoint, makeObjectList); -}; - -const generateResultFilter = filters => { - let filterStr = ''; - for (let property in filters) { - filterStr += ` - VALUES ?${property}Filter { <${filters[property].join('> <')}> } - ?id ${facetConfigs[property].predicate} ?${property}Filter . - `; - } - return filterStr; -}; diff --git a/src/server/sparql/Mappers.js b/src/server/sparql/Mappers.js index cd039da097a2bd84580cbc95d22e3b5410845b4f..86a0c4f19e65fae3841143e43d8f61316a3ce2d3 100644 --- a/src/server/sparql/Mappers.js +++ b/src/server/sparql/Mappers.js @@ -37,7 +37,7 @@ export const mapFacet = sparqlBindings => { export const mapHierarchicalFacet = sparqlBindings => { const results = makeObjectList(sparqlBindings); - //console.log(results) + const flatResults = results; results.push({ id: 'http://ldf.fi/mmm/places/sdbm_not_linked', prefLabel: 'SDBM places not linked to TGN', @@ -72,7 +72,7 @@ export const mapHierarchicalFacet = sparqlBindings => { treeData.forEach(node => sumUp(node)); return { distinctValueCount: results.length, - flatValues: results, + flatValues: flatResults, values: treeData }; }; diff --git a/src/server/sparql/Places.js b/src/server/sparql/Places.js new file mode 100644 index 0000000000000000000000000000000000000000..bdbe69049b8b4af0a4c9eed383904c376da8253e --- /dev/null +++ b/src/server/sparql/Places.js @@ -0,0 +1,43 @@ +import SparqlSearchEngine from './SparqlSearchEngine'; +import datasetConfig from './Datasets'; +import { makeObjectList } from './SparqlObjectMapper'; +import { generateResultFilter } from './Helpers'; + +const sparqlSearchEngine = new SparqlSearchEngine(); + +export const getPlaces = (variant, page, pagesize, filters, sortBy, sortDirection) => { + return Promise.all([ + getPlacesData(variant, page, pagesize, filters, sortBy, sortDirection) + ]).then(data => { + return { + resultCount: data[0].count, + results: data[0] + }; + }); +}; + +export const getPlace = uri => { + const config = datasetConfig['mmm']; + let query = config['placeQuery']; + query = query.replace('<PLACE_ID>', `<${uri}>`); + return sparqlSearchEngine.doSearch(query, config.endpoint, makeObjectList); +}; + +const getPlacesData = (variant, page, pagesize, filters, sortBy, sortDirection) => { + const config = datasetConfig['mmm']; + let query = config[`${variant}Query`]; + if (filters == null) { + query = query.replace('<FILTER>', ''); + } else { + query = query.replace('<FILTER>', generateResultFilter(filters)); + } + //console.log(query) + return sparqlSearchEngine.doSearch(query, config.endpoint, makeObjectList); +}; + +// export const getPlace = id => { +// let { endpoint, placeQuery } = datasetConfig['mmm']; +// placeQuery = placeQuery.replace('<PLACE_ID>', `<${id}>`); +// // console.log(placeQuery) +// return sparqlSearchEngine.doSearch(placeQuery, endpoint, makeObjectList); +// };