import { Injectable } from '@angular/core' ; import { HttpClient , HttpParams , HttpHeaders } from '@angular/common/http' ; import { Observable , BehaviorSubject } from 'rxjs' ; import { map } from 'rxjs/operators' ; import * as urljoin from 'url-join' ; import { ProjectSettingsService } from './project-settings.service' ; import { StardogService } from './stardog.service' ; import { QueryService } from './query.service' ; export interface ProjectSettings { endpoint : string ; username ? : string ; password ? : string ; } @ Injectable ( ) export class SPARQLService { public endpoint : string ; public updateEndpoint : string ; public dataEndpoint : string ; public auth : string ; public triplestore : string ; constructor ( private _http , private _ps , private _ss , private _qs ) { } getTriplestoreSettings ( ) { const tss = this . _ps . getTriplestoreSettings ( ) ; this . endpoint = tss . endpoint ; this . updateEndpoint = tss . updateEndpoint ; this . dataEndpoint = tss . dataEndpoint ; this . triplestore = tss . tripleStore . toLowerCase ( ) ; this . auth = `Basic ${ window . btoa ( tss . username + ':' + tss . password ) }` ; } public getQuery ( query , reasoning ? , mimeType ? ) { this . getTriplestoreSettings ( ) ; query = query . replace ( /\+/g , "%2B" ) ; var options = { } ; if ( ! mimeType ) { const queryType = this . _qs . getQuerytype ( query ) ; if ( queryType == 'construct' ) { options . responseType = 'text' ; options . headers = { 'Accept' : 'text/turtle' } ; } else { options . headers = { 'Accept' : 'application/sparql-results+json' } ; } } if ( this . triplestore == 'stardog' ) { return this . _ss . query ( query , reasoning ) ; } options . headers [ 'Authorization' ] = this . auth ; options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ; const body = `query=${ query }` ; return this . _http . post ( this . endpoint , body , options ) . toPromise ( ) ; } public updateQuery ( query ) { this . getTriplestoreSettings ( ) ; query = query . replace ( /\+/g , "%2B" ) ; if ( this . triplestore == 'stardog' ) { return this . _ss . query ( query ) ; } var headers = { 'Authorization' : this . auth , 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } ; var options = { observe : 'response' , responseType : 'text' , headers } ; const body = `update=${ encodeURIComponent ( query ) }` ; return this . _http . post ( this . updateEndpoint , body , options ) . toPromise ( ) ; } public loadTriples ( triples , graphURI ? ) { this . getTriplestoreSettings ( ) ; if ( ! this . dataEndpoint ) return new Promise ( ( resolve , reject ) => reject ( "No data endpoint provided!" ) ) ; if ( this . triplestore == 'stardog' ) { return this . _ss . loadTriples ( triples , graphURI ) . toPromise ( ) ; } var options = { params : { } , headers : { 'Content-type' : 'text/turtle' } } ; if ( graphURI ) options . params = { graph : graphURI } ; return this . _http . post ( this . dataEndpoint , triples , options ) . toPromise ( ) ; } getTriplesFromURL ( url ) { var options = { responseType : 'text' as 'text' , observe : 'response' as 'response' , headers : { Accept : 'text/turtle' } } ; return this . _http . get ( url , options ) . toPromise ( ) ; } /**
     * QUERIES
     */ wipeDB ( namedGraph ? ) { if ( ! namedGraph ) { var q = "DELETE WHERE { ?s ?p ?o }" ; } else { var q = `DELETE WHERE { Graph <${ namedGraph }> { ?s ?p ?o }}` ; } return this . updateQuery ( q ) ; } async getNamedGraphs ( ) { var q = 'SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o}}' ; const res = await this . getQuery ( q ) ; return res . results . bindings . map ( obj => { var x = obj ; return x . g . value ; } ) ; } }