Mercurial > hg > ng2-query-ismi
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:402c7229dc7c | 10:66dce99cef4e |
---|---|
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'}, |
19 {id: 'att_contains', label: 'Attribute contains'}]; | 19 {id: 'att_contains', label: 'Attribute'}]; |
20 | 20 |
21 constructor(private _http: Http) { | 21 constructor(private _http: Http) { |
22 this.state = { | 22 this.state = { |
23 'steps': [], | 23 'steps': [], |
24 'cypherQuery': '', | 24 'resultCypherQuery': '', |
25 'cypherParams': {}, | 25 'resultCypherParams': {}, |
26 'attributeCypherQuery': '', | |
26 'results': [], | 27 'results': [], |
27 'resultTypes': '', | 28 'resultTypes': '', |
28 'numResults': 0 | 29 'numResults': 0 |
29 }; | 30 }; |
30 } | 31 } |
42 } | 43 } |
43 | 44 |
44 getQueryOptions(queryMode: QueryMode) { | 45 getQueryOptions(queryMode: QueryMode) { |
45 var options = ['a1', 'b1', 'c1']; | 46 var options = ['a1', 'b1', 'c1']; |
46 if (queryMode.id === 'att_contains') { | 47 if (queryMode.id === 'att_contains') { |
47 if (this.state.attributeCypherQuery) { | 48 options = this.state.nextQueryAttributes; |
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 } | |
59 options = ['d', 'e', 'f']; | |
60 } else if (queryMode.id === 'type_is') { | 49 } else if (queryMode.id === 'type_is') { |
61 options = this.ismiObjectTypes; | 50 options = this.ismiObjectTypes; |
62 } | 51 } |
63 console.debug("getQueryOptions returns: ", options); | 52 console.debug("getQueryOptions returns: ", options); |
64 return options; | 53 return options; |
85 this.state.steps[index] = step; | 74 this.state.steps[index] = step; |
86 this.createCypherQuery(); | 75 this.createCypherQuery(); |
87 } | 76 } |
88 | 77 |
89 createCypherQuery() { | 78 createCypherQuery() { |
90 var resultCypher = ''; | 79 var queryMatch = ''; |
91 var attCypher = ''; | 80 var queryWhere = ''; |
81 var queryReturn = ''; | |
82 var resultQuery = ''; | |
83 var attQuery = ''; | |
92 var returnType = ''; | 84 var returnType = ''; |
93 this.state.steps.forEach((step) => { | 85 this.state.steps.forEach((step) => { |
86 // object type is | |
94 if (step.mode.id === 'type_is') { | 87 if (step.mode.id === 'type_is') { |
95 resultCypher = `MATCH (e:${step.objectType}) return e`; | 88 queryMatch = `MATCH (n:${step.objectType})`; |
89 queryWhere = ''; | |
90 queryReturn = 'RETURN n'; | |
96 returnType = 'node'; | 91 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`; | |
100 } | 92 } |
93 | |
94 // attribute contains | |
95 if (step.mode.id === 'att_contains') { | |
96 queryWhere = `WHERE lower(n.${step.attribute}) CONTAINS lower('${step.value}')`; | |
97 queryReturn = 'RETURN n'; | |
98 returnType = 'node'; | |
99 } | |
100 | |
101 }); | 101 }); |
102 this.state.resultCypherQuery = resultCypher; | 102 resultQuery = queryMatch + ' ' + queryWhere + ' ' + queryReturn; |
103 this.state.attributeCypherQuery = attCypher; | 103 attQuery = queryMatch + ' ' + queryWhere + ' WITH DISTINCT keys(n) AS atts UNWIND atts AS att RETURN DISTINCT att ORDER BY att'; |
104 this.state.resultCypherQuery = resultQuery; | |
105 this.state.attributeCypherQuery = attQuery; | |
104 this.state.resultTypes = returnType; | 106 this.state.resultTypes = returnType; |
105 } | 107 } |
106 | 108 |
107 updateQuery() { | 109 updateQuery() { |
108 this.createCypherQuery(); | 110 this.createCypherQuery(); |
109 var query = this.state.resultCypherQuery; | 111 // run query for result table |
110 var params = this.state.cypherParams; | 112 var resQuery = this.state.resultCypherQuery; |
111 var res = this.fetchCypherResult(query, params); | 113 var resParams = this.state.cypherParams; |
112 res.subscribe( | 114 var resRes = this.fetchCypherResult(resQuery, resParams); |
115 resRes.subscribe( | |
113 data => { | 116 data => { |
114 console.debug("neo4j data=", data); | 117 console.debug("neo4j result data=", data); |
115 this.state.results = data.results[0].data.map(elem => elem.row[0]); | 118 this.state.results = data.results[0].data.map(elem => elem.row[0]); |
116 this.state.numResults = this.state.results.length; | 119 this.state.numResults = this.state.results.length; |
117 }, | 120 }, |
118 err => console.error("neo4j error=", err), | 121 err => console.error("neo4j result error=", err), |
119 () => console.debug('neo4j query Complete') | 122 () => console.debug('neo4j result query Complete') |
120 ); | 123 ); |
124 // run query for attribute list | |
125 if (this.state.attributeCypherQuery) { | |
126 var attRes = this.fetchCypherResult(this.state.attributeCypherQuery); | |
127 attRes.subscribe( | |
128 data => { | |
129 console.debug("neo4j att data=", data); | |
130 this.state.nextQueryAttributes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");; | |
131 }, | |
132 err => console.error("neo4j att error=", err), | |
133 () => console.debug('neo4j att query Complete') | |
134 ); | |
135 } | |
121 } | 136 } |
122 | 137 |
123 fetchCypherResult(query: string, params = {}) { | 138 fetchCypherResult(query: string, params = {}) { |
139 console.debug("fetching cypher query: ", query); | |
124 var headers = new Headers(); | 140 var headers = new Headers(); |
125 headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j')); | 141 headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j')); |
126 headers.append('Content-Type', 'application/json'); | 142 headers.append('Content-Type', 'application/json'); |
127 headers.append('Accept', 'application/json'); | 143 headers.append('Accept', 'application/json'); |
128 // put headers in options | 144 // put headers in options |