Mercurial > hg > ng2-query-ismi
view app/query.service.ts @ 6:9f5473536a98
more query generation.
author | casties |
---|---|
date | Thu, 14 Jan 2016 17:19:35 +0100 |
parents | b06a5d61afed |
children | 6cd6c09032aa |
line wrap: on
line source
import {Injectable} from 'angular2/core'; import {Http, Headers} from 'angular2/http'; import 'rxjs/Rx'; // import all RxJS operators import {QueryMode} from './query-mode'; import {QueryState} from './query-state'; import {QueryStep} from './query-step'; @Injectable() export class QueryService { public queryState: QueryState; public ismiObjectTypes; public QUERY_MODES: QueryMode[] = [ {id: 'type_is', label:'Object type is'}, {id: 'att_contains', label: 'Attribute contains'}]; constructor(private _http: Http) { this.queryState = { 'steps': [], 'cypherQuery': '', 'cyperParams': {}, 'results': [], numResults: 0}; } getQueryModes(): QueryMode[] { return this.QUERY_MODES; } getQueryOptions(queryMode: QueryMode) { var options = ['a1', 'b1', 'c1']; if (queryMode.id === 'att_contains') { options = ['d', 'e', 'f']; } else if (queryMode.id === 'type_is') { options = this.ismiObjectTypes; } console.debug("getQueryOptions returns: ", options); return options; } setupIsmiObjectTypes() { var query = `MATCH (n) WITH DISTINCT labels(n) AS labels UNWIND labels AS label RETURN DISTINCT label ORDER BY label`; var res = this.fetchCypherResult(query); res.subscribe( data => { console.debug("neo4j data=", data); this.ismiObjectTypes = data.data.map(elem => elem[0]).filter(elem => elem[0] != "_"); console.debug("ismi types=", this.ismiObjectTypes); }, err => console.error("neo4j error=", err), () => console.debug('neo4j query Complete') ); } setQueryStep(index: number, step: QueryStep) { this.queryState.steps[index] = step; this.createCypherQuery(); } createCypherQuery() { var cypher = ''; var step = this.queryState.steps[0]; if (step.mode.id === 'type_is') { cypher = `MATCH (e:${step.objectType}) return e`; } this.queryState.cypherQuery = cypher; } fetchCypherResult(query: string, params = {}) { var headers = new Headers(); headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j')); headers.append('Content-Type', 'application/json'); headers.append('Accept', 'application/json'); // put headers in options var opts = {'headers': headers}; // create POST data from query var data = JSON.stringify({'query': query, 'params': params}); // make post request asynchronously var resp = this._http.post('http://localhost:7474/db/data/cypher/', data, opts) // filter result as JSON .map(res => res.json()); // return Observable return resp; } }