Mercurial > hg > ng2-query-ismi
view app/query.service.ts @ 10:66dce99cef4e
attribute contains works now.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 20 Jan 2016 17:02:00 +0100 |
parents | 402c7229dc7c |
children | 6989cd00e8d7 |
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 neo4jBaseUrl = 'http://localhost:7474/db/data'; public state: QueryState; public ismiObjectTypes: any; public QUERY_MODES: QueryMode[] = [ {id: 'type_is', label:'Object type is'}, {id: 'att_contains', label: 'Attribute'}]; constructor(private _http: Http) { this.state = { 'steps': [], 'resultCypherQuery': '', 'resultCypherParams': {}, 'attributeCypherQuery': '', 'results': [], 'resultTypes': '', 'numResults': 0 }; } setup() { this.setupIsmiObjectTypes(); } getState() { return this.state; } getQueryModes(): QueryMode[] { return this.QUERY_MODES; } getQueryOptions(queryMode: QueryMode) { var options = ['a1', 'b1', 'c1']; if (queryMode.id === 'att_contains') { options = this.state.nextQueryAttributes; } 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.results[0].data.map(elem => elem.row[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.state.steps[index] = step; this.createCypherQuery(); } createCypherQuery() { var queryMatch = ''; var queryWhere = ''; var queryReturn = ''; var resultQuery = ''; var attQuery = ''; var returnType = ''; this.state.steps.forEach((step) => { // object type is if (step.mode.id === 'type_is') { queryMatch = `MATCH (n:${step.objectType})`; queryWhere = ''; queryReturn = 'RETURN n'; returnType = 'node'; } // attribute contains if (step.mode.id === 'att_contains') { queryWhere = `WHERE lower(n.${step.attribute}) CONTAINS lower('${step.value}')`; queryReturn = 'RETURN n'; returnType = 'node'; } }); resultQuery = queryMatch + ' ' + queryWhere + ' ' + queryReturn; attQuery = queryMatch + ' ' + queryWhere + ' WITH DISTINCT keys(n) AS atts UNWIND atts AS att RETURN DISTINCT att ORDER BY att'; this.state.resultCypherQuery = resultQuery; this.state.attributeCypherQuery = attQuery; this.state.resultTypes = returnType; } updateQuery() { this.createCypherQuery(); // run query for result table var resQuery = this.state.resultCypherQuery; var resParams = this.state.cypherParams; var resRes = this.fetchCypherResult(resQuery, resParams); resRes.subscribe( data => { console.debug("neo4j result data=", data); this.state.results = data.results[0].data.map(elem => elem.row[0]); this.state.numResults = this.state.results.length; }, err => console.error("neo4j result error=", err), () => console.debug('neo4j result query Complete') ); // run query for attribute list if (this.state.attributeCypherQuery) { var attRes = this.fetchCypherResult(this.state.attributeCypherQuery); attRes.subscribe( data => { console.debug("neo4j att data=", data); this.state.nextQueryAttributes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");; }, err => console.error("neo4j att error=", err), () => console.debug('neo4j att query Complete') ); } } fetchCypherResult(query: string, params = {}) { console.debug("fetching cypher query: ", query); 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({'statements': [ {'statement': query, 'parameters': params} ]}); // make post request asynchronously var resp = this._http.post(this.neo4jBaseUrl+'/transaction/commit', data, opts) // filter result as JSON .map(res => res.json()); // return Observable return resp; } }