Mercurial > hg > ng2-query-ismi
comparison app/query.service.ts @ 9:402c7229dc7c
more query generation.
author | casties |
---|---|
date | Wed, 20 Jan 2016 11:32:31 +0100 |
parents | fa646ee46c19 |
children | 66dce99cef4e |
comparison
equal
deleted
inserted
replaced
8:fa646ee46c19 | 9:402c7229dc7c |
---|---|
8 import {QueryStep} from './query-step'; | 8 import {QueryStep} from './query-step'; |
9 | 9 |
10 @Injectable() | 10 @Injectable() |
11 export class QueryService { | 11 export class QueryService { |
12 | 12 |
13 public neo4jBaseUrl = 'http://localhost:7474/db/data/cypher/'; | 13 public neo4jBaseUrl = 'http://localhost:7474/db/data'; |
14 public state: QueryState; | 14 public state: QueryState; |
15 public ismiObjectTypes: any; | 15 public ismiObjectTypes: any; |
16 | 16 |
17 public QUERY_MODES: QueryMode[] = [ | 17 public QUERY_MODES: QueryMode[] = [ |
18 {id: 'type_is', label:'Object type is'}, | 18 {id: 'type_is', label:'Object type is'}, |
42 } | 42 } |
43 | 43 |
44 getQueryOptions(queryMode: QueryMode) { | 44 getQueryOptions(queryMode: QueryMode) { |
45 var options = ['a1', 'b1', 'c1']; | 45 var options = ['a1', 'b1', 'c1']; |
46 if (queryMode.id === 'att_contains') { | 46 if (queryMode.id === 'att_contains') { |
47 if (this.state.attributeCypherQuery) { | |
48 var res = this.fetchCypherResult(this.state.attributeCypherQuery); | |
49 res.subscribe( | |
50 data => { | |
51 console.debug("neo4j data=", data); | |
52 var atts = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); | |
53 console.debug("ismi attributes=", atts); | |
54 }, | |
55 err => console.error("neo4j error=", err), | |
56 () => console.debug('neo4j query Complete') | |
57 ); | |
58 } | |
47 options = ['d', 'e', 'f']; | 59 options = ['d', 'e', 'f']; |
48 } else if (queryMode.id === 'type_is') { | 60 } else if (queryMode.id === 'type_is') { |
49 options = this.ismiObjectTypes; | 61 options = this.ismiObjectTypes; |
50 } | 62 } |
51 console.debug("getQueryOptions returns: ", options); | 63 console.debug("getQueryOptions returns: ", options); |
59 | 71 |
60 var res = this.fetchCypherResult(query); | 72 var res = this.fetchCypherResult(query); |
61 res.subscribe( | 73 res.subscribe( |
62 data => { | 74 data => { |
63 console.debug("neo4j data=", data); | 75 console.debug("neo4j data=", data); |
64 this.ismiObjectTypes = data.data.map(elem => elem[0]).filter(elem => elem[0] != "_"); | 76 this.ismiObjectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); |
65 console.debug("ismi types=", this.ismiObjectTypes); | 77 console.debug("ismi types=", this.ismiObjectTypes); |
66 }, | 78 }, |
67 err => console.error("neo4j error=", err), | 79 err => console.error("neo4j error=", err), |
68 () => console.debug('neo4j query Complete') | 80 () => console.debug('neo4j query Complete') |
69 ); | 81 ); |
73 this.state.steps[index] = step; | 85 this.state.steps[index] = step; |
74 this.createCypherQuery(); | 86 this.createCypherQuery(); |
75 } | 87 } |
76 | 88 |
77 createCypherQuery() { | 89 createCypherQuery() { |
78 var cypher = ''; | 90 var resultCypher = ''; |
91 var attCypher = ''; | |
79 var returnType = ''; | 92 var returnType = ''; |
80 for (var i=0; i < this.state.steps.length; ++i) { | 93 this.state.steps.forEach((step) => { |
81 var step = this.state.steps[i]; | |
82 if (step.mode.id === 'type_is') { | 94 if (step.mode.id === 'type_is') { |
83 cypher = `MATCH (e:${step.objectType}) return e`; | 95 resultCypher = `MATCH (e:${step.objectType}) return e`; |
84 returnType = 'node'; | 96 returnType = 'node'; |
97 attCypher = `MATCH (e:${step.objectType}) WITH DISTINCT keys(e) AS atts | |
98 UNWIND atts AS att | |
99 RETURN DISTINCT att ORDER BY att`; | |
85 } | 100 } |
86 } | 101 }); |
87 this.state.cypherQuery = cypher; | 102 this.state.resultCypherQuery = resultCypher; |
103 this.state.attributeCypherQuery = attCypher; | |
88 this.state.resultTypes = returnType; | 104 this.state.resultTypes = returnType; |
89 } | 105 } |
90 | 106 |
91 updateQuery() { | 107 updateQuery() { |
92 this.createCypherQuery(); | 108 this.createCypherQuery(); |
93 var query = this.state.cypherQuery; | 109 var query = this.state.resultCypherQuery; |
94 var params = this.state.cypherParams; | 110 var params = this.state.cypherParams; |
95 var res = this.fetchCypherResult(query, params); | 111 var res = this.fetchCypherResult(query, params); |
96 res.subscribe( | 112 res.subscribe( |
97 data => { | 113 data => { |
98 console.debug("neo4j data=", data); | 114 console.debug("neo4j data=", data); |
99 this.state.results = data.data.map(elem => elem[0]); | 115 this.state.results = data.results[0].data.map(elem => elem.row[0]); |
100 this.state.numResults = this.state.results.length; | 116 this.state.numResults = this.state.results.length; |
101 }, | 117 }, |
102 err => console.error("neo4j error=", err), | 118 err => console.error("neo4j error=", err), |
103 () => console.debug('neo4j query Complete') | 119 () => console.debug('neo4j query Complete') |
104 ); | 120 ); |
110 headers.append('Content-Type', 'application/json'); | 126 headers.append('Content-Type', 'application/json'); |
111 headers.append('Accept', 'application/json'); | 127 headers.append('Accept', 'application/json'); |
112 // put headers in options | 128 // put headers in options |
113 var opts = {'headers': headers}; | 129 var opts = {'headers': headers}; |
114 // create POST data from query | 130 // create POST data from query |
115 var data = JSON.stringify({'query': query, 'params': params}); | 131 var data = JSON.stringify({'statements': [ |
132 {'statement': query, 'parameters': params} | |
133 ]}); | |
116 // make post request asynchronously | 134 // make post request asynchronously |
117 var resp = this._http.post(this.neo4jBaseUrl, data, opts) | 135 var resp = this._http.post(this.neo4jBaseUrl+'/transaction/commit', data, opts) |
118 // filter result as JSON | 136 // filter result as JSON |
119 .map(res => res.json()); | 137 .map(res => res.json()); |
120 // return Observable | 138 // return Observable |
121 return resp; | 139 return resp; |
122 } | 140 } |