123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.PaginatedRequest;
- using Luticate2.Utils.Dbo.Result;
- using Microsoft.Extensions.Logging;
- using VDS.RDF.Nodes;
- using VDS.RDF.Query;
- using WebApiWebSem.Dbo.Articles;
- using WebApiWebSem.Dbo.ArticlesFields;
-
- namespace WebApiWebSem.DataAccess
- {
- public class DbPediaDataAccess
- {
- private readonly SparqlRemoteEndpoint _sparqlRemoteEndpoint;
- private readonly ILogger<DbPediaDataAccess> _logger;
-
- protected string Prefixes = @"
- PREFIX owl: <http://www.w3.org/2002/07/owl#>
- PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
- PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
- PREFIX foaf: <http://xmlns.com/foaf/0.1/>
- PREFIX dc: <http://purl.org/dc/elements/1.1/>
- PREFIX : <http://dbpedia.org/resource/>
- PREFIX dbpedia2: <http://dbpedia.org/property/>
- PREFIX dbpedia: <http://dbpedia.org/>
- PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
- PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
- ";
-
- public DbPediaDataAccess(SparqlRemoteEndpoint sparqlRemoteEndpoint, ILogger<DbPediaDataAccess> logger)
- {
- _sparqlRemoteEndpoint = sparqlRemoteEndpoint;
- _logger = logger;
- }
-
- protected string Escape(string str)//TODO
- {
- return str.Replace('\'', ' ').Replace('"', ' ');
- }
-
- protected LuResult<SparqlResultSet> Query(string query)
- {
- _logger.LogInformation(query);
- try
- {
- var results = _sparqlRemoteEndpoint.QueryWithResultSet(query);
- return LuResult<SparqlResultSet>.Ok(results);
- }
- catch (Exception e)
- {
- return LuResult<SparqlResultSet>.Error(LuStatus.BackendError, e);
- }
- }
-
- protected ArticlesDbo ModelToDbo(SparqlResult result)
- {
- var dbo = new ArticlesDbo
- {
- Fields = new List<ArticlesFieldsDbo>()
- };
- foreach (var variable in result.Variables)
- {
- string data = null;
- var v = result[variable];
- if (v != null)
- {
- data = v.AsValuedNode().AsString();
- }
- if (variable == "id")
- {
- dbo.Id = data.Split('/').Last();
- }
- else if (variable == "abstract")
- {
- dbo.Text = data;
- }
- else if (variable == "pictureUrl")
- {
- dbo.PictureUrl = data;
- }
- else if (variable == "pictureDesc")
- {
- dbo.PictureCaption = data;
- }
- else
- {
- dbo.Fields.Add(new ArticlesFieldsDbo
- {
- Property = variable,
- Type = "text/plain",
- Value = data
- });
- }
- }
- return dbo;
- }
-
- public LuResult<LuPaginatedDbo<ArticlesDbo>> GetMultiple(LuPaginatedRequestDbo request)
- {
- var type = ArticlesDataAccess.GetFilterType(request.Filter);
- string query = null;
- string querySelect = null;
- string queryWhereId = null;
- string queryWhereVars = null;
- if (type == "home")
- {
- querySelect = "?id ?abstract ?pictureUrl ?pictureDesc";
- queryWhereId = @"?id foaf:isPrimaryTopicOf wikipedia-en:World_War_II .";
- queryWhereVars = @"
- OPTIONAL { ?id dbo:abstract ?abstract FILTER(lang(?abstract) = 'en') }
- OPTIONAL { ?id foaf:depiction ?pictureUrl }
- OPTIONAL { ?id dbpedia-owl:depictionDescription ?pictureDesc }
- BIND ((?abstract) AS ?search)";
- }
- else if (type == "persons")
- {
- querySelect = "?id ?abstract ?pictureUrl ?pictureDesc (MIN(?name) AS ?name) (str(?birthDate) AS ?birthDate) (str(?deathDate) AS ?deathDate)";
- queryWhereId = @"?id rdf:type dbo:Person ;
- ?type dbr:World_War_II .";
- queryWhereVars = @"
- OPTIONAL { ?id dbo:abstract ?abstract FILTER(lang(?abstract) = 'en') }
- OPTIONAL { ?id foaf:depiction ?pictureUrl }
- OPTIONAL { ?id dbpedia-owl:depictionDescription ?pictureDesc }
- OPTIONAL { ?id dbp:name ?name . FILTER (lang(?name) = 'en') }
- OPTIONAL { ?id dbp:birthDate ?birthDate }
- OPTIONAL { ?id dbp:deathDate ?deathDate }
- BIND (CONCAT(?abstract, ' ', ?id) AS ?search)";
- }
-
- var filter = $"FILTER regex(?search, '{request.Filter.Query}', 'i')";
-
- var count = Query(string.Format(@"{0}
- SELECT (COUNT(?id) AS ?count) WHERE {{
- {1}
- {2}
- {3}
- }}
- ", Prefixes, queryWhereId, queryWhereVars, filter));
- if (!count)
- {
- return count.To<LuPaginatedDbo<ArticlesDbo>>();
- }
-
- var res = Query(string.Format(@"{0}
- SELECT DISTINCT {1} WHERE {{
- {2}
- {3}
- {4}
- }}
- ORDER BY {5}
- LIMIT {6}
- OFFSET {7}", Prefixes, querySelect, queryWhereId, queryWhereVars, filter, request.OrderBy.ToDbPediaModel(),
- request.PerPage, request.Page * request.PerPage));
- if (!res)
- {
- return res.To<LuPaginatedDbo<ArticlesDbo>>();
- }
- var results = new LuPaginatedDbo<ArticlesDbo>
- {
- Count = count.Data.Results.First()["count"].AsValuedNode().AsInteger(),
- Data = res.Data.Results.Select(ModelToDbo).Select(dbo =>
- {
- dbo.Type = type;
- return dbo;
- }).ToList()
- };
- return LuResult<LuPaginatedDbo<ArticlesDbo>>.Ok(results);
- }
- }
- }
|