comparison app/query.service.ts @ 41:5353b2dffb0f

added local configuration file app/app-config.ts.
author casties
date Mon, 15 Feb 2016 17:05:07 +0100
parents 896ae7eefb33
children dc4f0541f04d
comparison
equal deleted inserted replaced
40:896ae7eefb33 41:5353b2dffb0f
2 import {Http, Headers} from 'angular2/http'; 2 import {Http, Headers} from 'angular2/http';
3 3
4 import 'rxjs/Rx'; // import all RxJS operators 4 import 'rxjs/Rx'; // import all RxJS operators
5 //import 'rxjs/add/operator/map'; 5 //import 'rxjs/add/operator/map';
6 6
7 import {NEO4J_BASE_URL, NEO4J_AUTHENTICATION} from './app-config';
7 import {QueryMode, QUERY_MODES, FIRST_QUERY_MODES} from './query-mode'; 8 import {QueryMode, QUERY_MODES, FIRST_QUERY_MODES} from './query-mode';
8 import {QueryState} from './query-state'; 9 import {QueryState} from './query-state';
9 import {QueryStep} from './query-step'; 10 import {QueryStep} from './query-step';
10 import {getResultType} from './result-type'; 11 import {getResultType} from './result-type';
11 import {ISMI_RESULT_TYPES} from './ismi-result-types'; 12 import {ISMI_RESULT_TYPES} from './ismi-result-types';
12 import {getRelationType} from './ismi-relation-types'; 13 import {getRelationType} from './ismi-relation-types';
13 14
14 @Injectable() 15 @Injectable()
15 export class QueryService { 16 export class QueryService {
16 17
17 //public neo4jBaseUrl = 'https://ismi-dev.mpiwg-berlin.mpg.de/neo4j-ismi/db/data';
18 public neo4jBaseUrl = 'http://localhost:7474/db/data';
19 public neo4jAuthentication = {'user': 'neo4j', 'password': 'neo5j'};
20 public typeAttribute = '_type'; 18 public typeAttribute = '_type';
21 public excludedAttributes = {}; 19 public excludedAttributes = {};
22 public state: QueryState; 20 public state: QueryState;
23 public objectTypes: string[]; 21 public objectTypes: string[];
24 22
45 43
46 /** 44 /**
47 * return the first set of options for the given query mode. 45 * return the first set of options for the given query mode.
48 */ 46 */
49 getQueryOptions(queryMode: QueryMode) { 47 getQueryOptions(queryMode: QueryMode) {
50 var options = []; 48 let options = [];
51 if (queryMode == null) return options; 49 if (queryMode == null) return options;
52 if (queryMode.id === 'type_is') { 50 if (queryMode.id === 'type_is') {
53 options = this.objectTypes; 51 options = this.objectTypes;
54 } else if (queryMode.id === 'relation_is') { 52 } else if (queryMode.id === 'relation_is') {
55 options = this.state.resultRelations; 53 options = this.state.resultRelations;
66 64
67 /** 65 /**
68 * fetch all object types from Neo4j and store in this.objectTypes. 66 * fetch all object types from Neo4j and store in this.objectTypes.
69 */ 67 */
70 setupObjectTypes() { 68 setupObjectTypes() {
71 var query = `MATCH (n) WITH DISTINCT labels(n) AS labels 69 let query = `MATCH (n) WITH DISTINCT labels(n) AS labels
72 UNWIND labels AS label 70 UNWIND labels AS label
73 RETURN DISTINCT label ORDER BY label`; 71 RETURN DISTINCT label ORDER BY label`;
74 72
75 var res = this.fetchCypherResults([query]); 73 let res = this.fetchCypherResults([query]);
76 res.subscribe( 74 res.subscribe(
77 data => { 75 data => {
78 console.debug("neo4j data=", data); 76 console.debug("neo4j data=", data);
79 this.objectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); 77 this.objectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");
80 console.debug("object types=", this.objectTypes); 78 console.debug("object types=", this.objectTypes);
95 * Create the cypher queries for the current query state. 93 * Create the cypher queries for the current query state.
96 * 94 *
97 * Updates the queries for results, attributes and relations. 95 * Updates the queries for results, attributes and relations.
98 */ 96 */
99 createCypherQuery() { 97 createCypherQuery() {
100 var queryMatch = ''; 98 let queryMatch = '';
101 var queryWhere = ''; 99 let queryWhere = '';
102 var queryReturn = ''; 100 let queryReturn = '';
103 var queryParams = {}; 101 let queryParams = {};
104 var resultQuery = ''; 102 let resultQuery = '';
105 var attributesQuery = ''; 103 let attributesQuery = '';
106 var outRelsQuery = ''; 104 let outRelsQuery = '';
107 var inRelsQuery = ''; 105 let inRelsQuery = '';
108 var returnType = ''; 106 let returnType = '';
109 var nIdx = 1; 107 let nIdx = 1;
110 this.state.steps.forEach((step, stepIdx) => { 108 this.state.steps.forEach((step, stepIdx) => {
111 var mode = step.mode.id; 109 let mode = step.mode.id;
112 var params = step.params; 110 let params = step.params;
113 111
114 /* 112 /*
115 * step: object type is 113 * step: object type is
116 */ 114 */
117 if (mode === 'type_is') { 115 if (mode === 'type_is') {
226 this.createCypherQuery(); 224 this.createCypherQuery();
227 this.state.resultInfo = 'loading...'; 225 this.state.resultInfo = 'loading...';
228 /* 226 /*
229 * run query for result table 227 * run query for result table
230 */ 228 */
231 var queries = [this.state.resultCypherQuery]; 229 let queries = [this.state.resultCypherQuery];
232 var params = [this.state.cypherQueryParams]; 230 let params = [this.state.cypherQueryParams];
233 if (this.state.attributesCypherQuery) { 231 if (this.state.attributesCypherQuery) {
234 queries.push(this.state.attributesCypherQuery); 232 queries.push(this.state.attributesCypherQuery);
235 params.push(this.state.cypherQueryParams); 233 params.push(this.state.cypherQueryParams);
236 } 234 }
237 if (this.state.outRelsCypherQuery) { 235 if (this.state.outRelsCypherQuery) {
240 } 238 }
241 if (this.state.inRelsCypherQuery) { 239 if (this.state.inRelsCypherQuery) {
242 queries.push(this.state.inRelsCypherQuery); 240 queries.push(this.state.inRelsCypherQuery);
243 params.push(this.state.cypherQueryParams); 241 params.push(this.state.cypherQueryParams);
244 } 242 }
245 var res = this.fetchCypherResults(queries, params); 243 let res = this.fetchCypherResults(queries, params);
246 res.subscribe( 244 res.subscribe(
247 data => { 245 data => {
248 console.debug("neo4j result data=", data); 246 console.debug("neo4j result data=", data);
249 var resIdx = 0; 247 let resIdx = 0;
250 /* 248 /*
251 * results for result table 249 * results for result table
252 */ 250 */
253 this.state.results = data.results[resIdx].data.map(elem => elem.row[0]); 251 this.state.results = data.results[resIdx].data.map(elem => elem.row[0]);
254 this.state.numResults = this.state.results.length; 252 this.state.numResults = this.state.results.length;
261 } else { 259 } else {
262 resTypes[t] += 1; 260 resTypes[t] += 1;
263 } 261 }
264 }); 262 });
265 let info = ''; 263 let info = '';
266 for (var t in resTypes) { 264 for (let t in resTypes) {
267 info += t + '(' + resTypes[t] + ') '; 265 info += t + '(' + resTypes[t] + ') ';
268 } 266 }
269 info = info.substr(0, info.length-1); 267 info = info.substr(0, info.length-1);
270 this.state.resultInfo = info; 268 this.state.resultInfo = info;
271 // save info also in last step 269 // save info also in last step
309 ); 307 );
310 } 308 }
311 309
312 310
313 filterAttributes(attributes: string[], normalized=false) { 311 filterAttributes(attributes: string[], normalized=false) {
314 var atts = []; 312 let atts = [];
315 if (normalized) { 313 if (normalized) {
316 attributes.forEach((att) => { 314 attributes.forEach((att) => {
317 if (att.substr(0, 3) == "_n_") { 315 if (att.substr(0, 3) == "_n_") {
318 atts.push(att.substr(3)); 316 atts.push(att.substr(3));
319 } 317 }
329 * 327 *
330 * Returns an Observable with the results. 328 * Returns an Observable with the results.
331 */ 329 */
332 fetchCypherResults(queries: string[], params=[{}]) { 330 fetchCypherResults(queries: string[], params=[{}]) {
333 console.debug("fetching cypher queries: ", queries); 331 console.debug("fetching cypher queries: ", queries);
334 var headers = new Headers(); 332 let headers = new Headers();
335 var auth = this.neo4jAuthentication; 333 let auth = NEO4J_AUTHENTICATION;
336 headers.append('Authorization', 'Basic ' + btoa(`${auth.user}:${auth.password}`)); 334 headers.append('Authorization', 'Basic ' + btoa(`${auth.user}:${auth.password}`));
337 headers.append('Content-Type', 'application/json'); 335 headers.append('Content-Type', 'application/json');
338 headers.append('Accept', 'application/json'); 336 headers.append('Accept', 'application/json');
339 // put headers in options 337 // put headers in options
340 var opts = {'headers': headers}; 338 let opts = {'headers': headers};
341 // unpack queries into statements 339 // unpack queries into statements
342 var statements = queries.map((q, i) => { 340 let statements = queries.map((q, i) => {
343 return {'statement': q, 'parameters': (params[i])?params[i]:{}}; 341 return {'statement': q, 'parameters': (params[i])?params[i]:{}};
344 }); 342 });
345 // create POST data from query 343 // create POST data from query
346 var data = JSON.stringify({'statements': statements}); 344 let data = JSON.stringify({'statements': statements});
347 // make post request asynchronously 345 // make post request asynchronously
348 var resp = this._http.post(this.neo4jBaseUrl+'/transaction/commit', data, opts) 346 let resp = this._http.post(NEO4J_BASE_URL+'/transaction/commit', data, opts)
349 // filter result as JSON 347 // filter result as JSON
350 .map(res => res.json()); 348 .map(res => res.json());
351 // return Observable 349 // return Observable
352 return resp; 350 return resp;
353 } 351 }