changeset 3:c741a00d38de

first list of object types :-)
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 13 Jan 2016 12:41:01 +0100
parents 80270f5a5735
children 351c3df28602
files app/query-select.component.ts app/query.service.ts
diffstat 2 files changed, 37 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/app/query-select.component.ts	Wed Jan 13 11:13:07 2016 +0100
+++ b/app/query-select.component.ts	Wed Jan 13 12:41:01 2016 +0100
@@ -9,7 +9,7 @@
     selector: 'query-select',
     template: `
         <p>Selected option: {{selectedQuery}}</p>
-        <select (change)="onSelectType($event)">
+        <select (change)="onSelectMode($event)">
             <option *ngFor="#mode of queryModes" [value]="mode.id">
                 {{mode.label}}
             </option>
@@ -36,12 +36,13 @@
     
     setupQueryModes() {
         this.queryModes = this._queryService.getQueryModes();
-        this._queryService.getIsmiObjectTypes();
+        this._queryService.setupIsmiObjectTypes();
     }
     
-    onSelectType(event: any) {
+    onSelectMode(event: any) {
         var selected = event.target.value;
-        this._queryService.getQueryOptions(selected).then(
+        var mode = this.queryModes.find(mode => mode.id === selected);
+        this._queryService.getQueryOptions(mode).then(
             options => this.query2Options = options);
     }
 }
--- a/app/query.service.ts	Wed Jan 13 11:13:07 2016 +0100
+++ b/app/query.service.ts	Wed Jan 13 12:41:01 2016 +0100
@@ -20,37 +20,47 @@
         return this.QUERY_MODES;
     }
     
-    getQueryOptions(queryMode: string) {
+    getQueryOptions(queryMode: QueryMode) {
         var options = ['a1', 'b1', 'c1'];
-        if (queryMode == 'Attribute contains') {
+        if (queryMode.id === 'att_contains') {
             options = ['d', 'e', 'f'];
+        } else if (queryMode.id === 'type_is') {
+            options = this.ismiObjectTypes;
         }
         return Promise.resolve(options);
     }
     
-    getIsmiObjectTypes() {
+    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')
+        );
+    }
+    
+    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');
-        var data = {
-            'query': `MATCH (n)
-WITH DISTINCT labels(n) AS labels
-UNWIND labels AS label
-RETURN DISTINCT label
-ORDER BY label`,
-            'params': {}
-        };
-
-        this._http.post('http://localhost:7474/db/data/cypher/', JSON.stringify(data), {'headers': headers})
-            .map(res => res.json())
-            .subscribe(
-            data => {
-                console.debug("neo4j data=", data);
-                this.ismiObjectTypes = data.data.map(elem => elem[0]).filter(elem => elem[0] != "_");
-                },
-            err => console.error("neo4j error=", err),
-            () => console.debug('neo4j query Complete')
-            );
+        // 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;
     }
 }
\ No newline at end of file