changeset 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
files app/query-app.component.ts app/query-select.component.ts app/query.service.ts
diffstat 3 files changed, 82 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/app/query-app.component.ts	Wed Jan 20 11:32:31 2016 +0100
+++ b/app/query-app.component.ts	Wed Jan 20 17:02:00 2016 +0100
@@ -38,12 +38,11 @@
     
     addQueryStep() {
         this.querySteps.push({'mode': null});
-        //this.queryState = this._queryService.getState();
     }
     
     removeQueryStep() {
+        this.querySteps.pop();
         this._queryService.state.steps.pop();
-        //this.queryState = this._queryService.getState();
     }
     
     onQueryChanged(event: any) {
--- a/app/query-select.component.ts	Wed Jan 20 11:32:31 2016 +0100
+++ b/app/query-select.component.ts	Wed Jan 20 17:02:00 2016 +0100
@@ -10,18 +10,33 @@
     selector: 'query-select',
     template: `
         <div>
+        <form (ngSubmit)="onSubmit()">
         <select (change)="onSelectMode($event)">
             <option></option>
             <option *ngFor="#mode of queryModes" [value]="mode.id">
                 {{mode.label}}
             </option>
         </select>
-        <select *ngIf="query2Options" (change)="onSelectOption($event)">
+
+        <select *ngIf="selectedMode.id=='type_is' && queryOptions" (change)="onSelectOption($event)">
             <option></option>
-            <option *ngFor="#option of query2Options" [value]="option">
+            <option *ngFor="#option of queryOptions" [value]="option">
                 {{option}}
             </option>
         </select>
+
+        <span *ngIf="selectedMode.id=='att_contains'">
+        <select [(ngModel)]="selectedOption">
+            <option></option>
+            <option *ngFor="#option of queryOptions" [value]="option">
+                {{option}}
+            </option>
+        </select>
+        <span>contains</span>
+        <input type="text" [(ngModel)]="queryInput"/>
+        </span>
+        <button type="submit">Submit</button>
+        </form>
         </div>
         `,
     inputs: ['index']
@@ -33,7 +48,9 @@
     public index: number;
     public queryModes: QueryMode[];
     public selectedMode: QueryMode;
-    public query2Options: string[];
+    public queryOptions: string[];
+    public selectedOption: string;
+    public queryInput: string;
     
     @Output('queryChanged') queryChanged = new EventEmitter<QueryState>();
     
@@ -54,7 +71,7 @@
     onSelectMode(event: any) {
         var selected = event.target.value;
         this.selectedMode = this.queryModes.find(mode => mode.id === selected);
-        this.query2Options = this._queryService.getQueryOptions(this.selectedMode);
+        this.queryOptions = this._queryService.getQueryOptions(this.selectedMode);
     }
     
     onSelectOption(event: any) {
@@ -64,4 +81,17 @@
         this._queryService.setQueryStep(this.index, step);
         this.queryChanged.emit(this._queryService.getState());
     }
+    
+    onSubmit() {
+        console.debug("Submit! selectedMode=", this.selectedMode, " selectedOption=", this.selectedOption, " queryInput=", this.queryInput);
+        if (this.selectedMode.id == 'att_contains') {
+            var att = this.selectedOption;
+            var val = this.queryInput;
+            if (att && val) {
+                var step = {'mode': this.selectedMode, 'attribute': att, 'value': val};
+                this._queryService.setQueryStep(this.index, step);
+                this.queryChanged.emit(this._queryService.getState());
+           }
+        }
+    }
 }
--- a/app/query.service.ts	Wed Jan 20 11:32:31 2016 +0100
+++ b/app/query.service.ts	Wed Jan 20 17:02:00 2016 +0100
@@ -16,13 +16,14 @@
     
     public QUERY_MODES: QueryMode[] = [
         {id: 'type_is', label:'Object type is'},
-        {id: 'att_contains', label: 'Attribute contains'}];
+        {id: 'att_contains', label: 'Attribute'}];
         
     constructor(private _http: Http) {
         this.state = {
             'steps': [],
-            'cypherQuery': '',
-            'cypherParams': {}, 
+            'resultCypherQuery': '',
+            'resultCypherParams': {},
+            'attributeCypherQuery': '',
             'results': [],
             'resultTypes': '',
             'numResults': 0
@@ -44,19 +45,7 @@
     getQueryOptions(queryMode: QueryMode) {
         var options = ['a1', 'b1', 'c1'];
         if (queryMode.id === 'att_contains') {
-            if (this.state.attributeCypherQuery) {
-                var res = this.fetchCypherResult(this.state.attributeCypherQuery);
-                res.subscribe(
-                    data => {
-                        console.debug("neo4j data=", data);
-                        var atts = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");
-                        console.debug("ismi attributes=", atts);
-                    },
-                    err => console.error("neo4j error=", err),
-                    () => console.debug('neo4j query Complete')
-                );
-            }
-            options = ['d', 'e', 'f'];
+            options = this.state.nextQueryAttributes;
         } else if (queryMode.id === 'type_is') {
             options = this.ismiObjectTypes;
         }
@@ -87,40 +76,67 @@
     }
     
     createCypherQuery() {
-        var resultCypher = '';
-        var attCypher = '';
+        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') {
-                resultCypher = `MATCH (e:${step.objectType}) return e`;
+                queryMatch = `MATCH (n:${step.objectType})`;
+                queryWhere = '';
+                queryReturn =  'RETURN n';
                 returnType = 'node';
-                attCypher = `MATCH (e:${step.objectType}) WITH DISTINCT keys(e) AS atts
-                    UNWIND atts AS att 
-                    RETURN DISTINCT att ORDER BY att`;
             }
+            
+            // attribute contains
+            if (step.mode.id === 'att_contains') {
+                queryWhere = `WHERE lower(n.${step.attribute}) CONTAINS lower('${step.value}')`;
+                queryReturn =  'RETURN n';
+                returnType = 'node';
+            }
+            
         });
-        this.state.resultCypherQuery = resultCypher;
-        this.state.attributeCypherQuery = attCypher;
+        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();
-        var query = this.state.resultCypherQuery;
-        var params = this.state.cypherParams;
-        var res = this.fetchCypherResult(query, params);
-        res.subscribe(
+        // 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 data=", 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 error=", err),
-            () => console.debug('neo4j query Complete')
+            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');