Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F63004961
query.service.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, May 17, 02:15
Size
4 KB
Mime Type
text/x-java
Expires
Sun, May 19, 02:15 (2 d)
Engine
blob
Format
Raw Data
Handle
17719376
Attached To
rNIETZSCHEBETAAPP Nietzsche-Beta-App
query.service.ts
View Options
import { Injectable } from '@angular/core';
import { Parser, Generator, Wildcard } from 'sparqljs';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable()
export class QueryService {
constructor(private http: HttpClient) {
}
/**
* Gets the data from an endpoint via http post
*
* @param baseUrl: The url of the endpoint.
* @param query: The query to run.
* @param queryType: "CONSTRUCT" or "QUERY"
* @returns the response.
*/
public getData(baseUrl: string, query: string, queryType?: string ) {
let httpOptions;
if (queryType === 'CONSTRUCT') {
// A construct does contain a text as response, not a json, so responseType must be 'text' to avoid parse errors
httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/sparql-query', 'Accept': 'text/turtle'}),
responseType: 'text'};
return this.http.post(baseUrl, query, httpOptions).toPromise();
} if (queryType === 'SELECT' || queryType === 'ASK') {
httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/sparql-query',
'Accept': 'application/sparql-results+json; charset=UTF-8'})};
return this.http.post(baseUrl, query, httpOptions).toPromise();
}
}
public updateData(baseUrl: string, data: string) {
let query = `INSERT DATA {${data}}`;
console.log('query', query);
let httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/sparql-update',
'Authorization': 'Basic bmlldHpzY2hlX3VzZXI6bmlldHpzY2hl',
'Accept': 'application/sparql-results+json; charset=UTF-8'}), method: 'POST', body: query};
this.http.post(baseUrl, query, httpOptions).toPromise().then(fuu => console.log(fuu) );
}
/**
* Gets a text file by its name from the directory assets/queries.
*
* @param filename The name of the file + file name extension.
* @returns the text of the file.
*/
public getQueryfromFilename(filename) {
return this.http.get('../assets/queries/' + filename, {responseType: 'text'}).toPromise();
}
/**
* getQueryForItem()
* Parametrizes a given baseQuery with a iri passed, so the iri will be the resource of the where clause
*
* @param subjectIri: The iri of the selected resource
* @param baseQuery: name of the query to parametrize
* @param propertyIri: The iri of the selected property
* @param objectIri: The iri of the selected object
* @returns the query for the resource.
*/
public parametrizeQueryWithItem(baseQuery: string, subjectIri?: string, propertyIri?: string, objectIri?: string): string {
let qParametrizer = new TlnQueryParametrizer(baseQuery);
qParametrizer.parametrize(subjectIri, propertyIri, objectIri);
return qParametrizer.writeOut();
}
}
export class TlnQueryParametrizer {
parsedQuery;
parser: Parser;
sparqlGenerator = new Generator({});
constructor(baseQuery: string) {
this.parser = new Parser();
this.parsedQuery = this.parser.parse(baseQuery);
console.log(this.parsedQuery);
}
parametrize(subjectIri?: string, predicateIri?: string, objectIri?: string) {
this.setSubject(subjectIri);
this.setPredicate(predicateIri);
this.setObject(objectIri);
}
setSubject(subjectIri: string): void {
if (!subjectIri || subjectIri === '') { return }
this.parsedQuery.where[0].triples[0].subject = {'termType': 'NamedNode', 'value': decodeURI(subjectIri) };
if (this.parsedQuery.queryType === 'CONSTRUCT') { // in case of constructs we also change the property
this.parsedQuery.template[0].subject = {'termType': 'NamedNode', 'value': decodeURI(subjectIri) };
}
}
setPredicate(predicateIri: string): void {
if (!predicateIri || predicateIri === '') { return }
this.parsedQuery.where[0].triples[0].predicate = {'termType': 'NamedNode', 'value': decodeURI(predicateIri) };
if (this.parsedQuery.queryType === 'CONSTRUCT') { // in case of constructs we also change the property
this.parsedQuery.template[0].predicate = {'termType': 'NamedNode', 'value': decodeURI(predicateIri) };
}
}
setObject(objectIri: string): void {
if (!objectIri || objectIri === '') { return }
this.parsedQuery.where[0].triples[0].object = {'termType': 'NamedNode', 'value': decodeURI(objectIri) };
if (this.parsedQuery.queryType === 'CONSTRUCT') { // in case of constructs we also change the property
this.parsedQuery.template[0].object = {'termType': 'NamedNode', 'value': decodeURI(objectIri) };
}
}
writeOut() {
return this.sparqlGenerator.stringify(this.parsedQuery);
}
}
Event Timeline
Log In to Comment