@igea/oac_backend 1.0.55 → 1.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igea/oac_backend",
3
- "version": "1.0.55",
3
+ "version": "1.0.57",
4
4
  "description": "Backend service for the OAC project",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -262,6 +262,120 @@ router.post('/search/by-prefix', (req, res) => {
262
262
  });
263
263
 
264
264
  });
265
+ //---------------------------------------------------
266
+ router.get('/rdf/resourceOf', (req, res) => {
267
+ const iri = decodeURIComponent(req.query.iri);
268
+ const query = `
269
+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
270
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
271
+ PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
272
+ SELECT
273
+ ?predicate
274
+ ?object
275
+ (COALESCE(?skosLabel, ?rdfsLabel, STR(?object)) AS ?objectLabel)
276
+ (GROUP_CONCAT(DISTINCT STR(?objectClass); separator=", ") AS ?objectClasses)
277
+ WHERE {
278
+ BIND(${iri} AS ?subject)
279
+ ?subject ?predicate ?object .
280
+ # label dell’oggetto
281
+ OPTIONAL { ?object skos:prefLabel ?skosLabel }
282
+ OPTIONAL { ?object rdfs:label ?rdfsLabel }
283
+ # classi dell’oggetto (solo se è una IRI)
284
+ OPTIONAL {
285
+ FILTER(isIRI(?object))
286
+ ?object rdf:type ?objectClass .
287
+ }
288
+ }
289
+ GROUP BY ?predicate ?object ?skosLabel ?rdfsLabel
290
+ ORDER BY ?predicate
291
+ `
292
+ axios.post(fusekiUrl, `query=${encodeURIComponent(query)}`, {
293
+ headers: {
294
+ 'Content-Type': 'application/x-www-form-urlencoded',
295
+ 'Accept': 'application/sparql-results+json'
296
+ }
297
+ }).then(response => {
298
+ const bindings = response.data.results.bindings;
299
+ let results = []
300
+ bindings.forEach(result => {
301
+ if(result.predicate){
302
+ results.push({
303
+ predicate: result.predicate.value,
304
+ object: {
305
+ iri: result.object.value,
306
+ label: result.objectLabel ? result.objectLabel.value : result.object.value,
307
+ classes: result.objectClasses ? result.objectClasses.value : ""
308
+ }
309
+ });
310
+ }
311
+ });
312
+ res.json({
313
+ success: true,
314
+ data: results.sort((a, b) => a.predicate.localeCompare(b.predicate)),
315
+ message: null
316
+ });
317
+ }).catch(err => {
318
+ res.status(500).json({
319
+ success: false,
320
+ data: null,
321
+ message: `Error: ${err}`
322
+ });
323
+ });
324
+
325
+ })
326
+
327
+ router.get('/rdf/rootResource', (req, res) => {
328
+ const iri = decodeURIComponent(req.query.iri);
329
+ const query = `
330
+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
331
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
332
+ PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
333
+ SELECT
334
+ (COALESCE(?skosLabel, ?rdfsLabel, STR(?iri)) AS ?label)
335
+ (GROUP_CONCAT(DISTINCT STR(?class); separator=", ") AS ?classes)
336
+ WHERE {
337
+ BIND(${iri} AS ?iri)
338
+ OPTIONAL { ?iri rdf:type ?class }
339
+ OPTIONAL { ?iri skos:prefLabel ?skosLabel }
340
+ OPTIONAL { ?iri rdfs:label ?rdfsLabel }
341
+ }
342
+ GROUP BY ?iri ?skosLabel ?rdfsLabel`
343
+ console.log(query)
344
+ axios.post(fusekiUrl, `query=${encodeURIComponent(query)}`, {
345
+ headers: {
346
+ 'Content-Type': 'application/x-www-form-urlencoded',
347
+ 'Accept': 'application/sparql-results+json'
348
+ }
349
+ }).then(response => {
350
+ const bindings = response.data.results.bindings;
351
+ let results = []
352
+ bindings.forEach(result => {
353
+ results.push({
354
+ label: result.label.value,
355
+ classes: result.classes.value
356
+ });
357
+ });
358
+ if(results.length > 0){
359
+ res.json({
360
+ success: true,
361
+ data: results[0],
362
+ message: null
363
+ });
364
+ }else{
365
+ res.status(404).json({
366
+ success: false,
367
+ data: null,
368
+ message: 'Resource not found'
369
+ });
370
+ }
371
+ }).catch(err => {
372
+ res.status(500).json({
373
+ success: false,
374
+ data: null,
375
+ message: `Error: ${err}`
376
+ });
377
+ });
378
+ })
265
379
  //---------------------------------------------------------------
266
380
  router.get('/count/entities', (req, res) => {
267
381
  const query = `
@@ -266,20 +266,26 @@ router.post('/convert/:from/:to', (req, res) => {
266
266
  }
267
267
  }
268
268
  let files = [inputFile.name];
269
- conversionFunction(inputFile.name, outputFile.name).then(() => {
269
+ let input_data = inputFile.name;
270
+ if(from === 'ttl' && to === 'xml'){
271
+ input_data = content;
272
+ }
273
+ conversionFunction(input_data, outputFile.name).then(() => {
270
274
  let dt = new Date();
271
275
  let filename = `investigation-${dt.toISOString()}.${to}`;
272
276
  res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
273
277
  res.sendFile(outputFile.name, (err) => {
274
278
  if (err) {
279
+ console.log(err)
275
280
  res.status(500).json({
276
281
  success: false,
277
282
  data: null,
278
283
  message: `Error sending file: ${err}`
279
284
  });
280
- files = [inputFile]
285
+ files = [inputFile.name]
286
+ }else{
287
+ files.push(outputFile.name)
281
288
  }
282
- files.push(outputFile.name)
283
289
  removeFiles(files)
284
290
  });
285
291
  }).catch(err => {
@@ -158,56 +158,63 @@ class Converter {
158
158
  static turtle2RdfXmlCustom(turtle, outRdfXmlPath=null) {
159
159
 
160
160
  return new Promise((resolve, reject) => {
161
- const parser = new Parser();
162
- const quads = parser.parse(turtle);
163
-
164
- const dataset = rdf.dataset();
165
- for (const quad of quads) {
166
- dataset.add(quad);
167
- }
161
+ try{
162
+ const parser = new Parser();
163
+ const quads = parser.parse(turtle);
164
+
165
+ const dataset = rdf.dataset();
166
+ for (const quad of quads) {
167
+ dataset.add(quad);
168
+ }
168
169
 
169
- const root = create({ version: '1.0', encoding: 'UTF-8' })
170
- .ele('rdf:RDF', {
171
- 'xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
172
- 'xmlns:rdfs': 'http://www.w3.org/2000/01/rdf-schema#'
173
- });
174
-
175
- const visited = new Set();
176
-
177
- const subjects = Array.from(
178
- new Set(
179
- dataset.toArray().map(q => q.subject.value)
180
- )
181
- ).map(uri => rdf.namedNode(uri))
182
- .sort((a, b) => a.value.localeCompare(b.value));
183
-
184
- const objects = Array.from(
185
- new Set(
186
- dataset.toArray().map(q => q.object.value)
187
- )
188
- ).map(uri => rdf.namedNode(uri))
189
- .sort((a, b) => a.value.localeCompare(b.value));
190
-
191
- for (let s of subjects) {
192
- s["_isRoot"] = false;
193
- if (objects.findIndex(o => o.value === s.value) == -1) {
194
- s["_isRoot"] = true;
195
- //console.log("Root subject: " + s.value);
170
+ const root = create({ version: '1.0', encoding: 'UTF-8' })
171
+ .ele('rdf:RDF', {
172
+ 'xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
173
+ 'xmlns:rdfs': 'http://www.w3.org/2000/01/rdf-schema#'
174
+ });
175
+
176
+ const visited = new Set();
177
+
178
+ const subjects = Array.from(
179
+ new Set(
180
+ dataset.toArray().map(q => q.subject.value)
181
+ )
182
+ ).map(uri => rdf.namedNode(uri))
183
+ .sort((a, b) => a.value.localeCompare(b.value));
184
+
185
+ const objects = Array.from(
186
+ new Set(
187
+ dataset.toArray().map(q => q.object.value)
188
+ )
189
+ ).map(uri => rdf.namedNode(uri))
190
+ .sort((a, b) => a.value.localeCompare(b.value));
191
+
192
+ for (let s of subjects) {
193
+ s["_isRoot"] = false;
194
+ if (objects.findIndex(o => o.value === s.value) == -1) {
195
+ s["_isRoot"] = true;
196
+ //console.log("Root subject: " + s.value);
197
+ }
198
+ }
199
+
200
+ for (let s of subjects) {
201
+ if(s["_isRoot"])
202
+ Converter.buildNode(dataset, s, root, visited);
196
203
  }
197
- }
198
-
199
- for (let s of subjects) {
200
- if(s["_isRoot"])
201
- Converter.buildNode(dataset, s, root, visited);
202
- }
203
204
 
204
- const xml = root.end({ prettyPrint: true });
205
-
206
- if(outRdfXmlPath){
207
- fs.writeFileSync(outRdfXmlPath, xml, 'utf8');
208
- }
205
+ const xml = root.end({ prettyPrint: true });
206
+
207
+ console.log(xml)
208
+ if(outRdfXmlPath){
209
+ fs.writeFileSync(outRdfXmlPath, xml, 'utf8');
210
+ }
209
211
 
210
- resolve(xml)
212
+ resolve(xml)
213
+ }catch(e){
214
+ console.log(e)
215
+ reject(e)
216
+ }
217
+
211
218
  })
212
219
 
213
220
  }