From b8d5a7eafd3597c72611f4c4153a5fe732cdd50a Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <legac@cppm.in2p3.fr>
Date: Sun, 26 Jun 2016 12:33:46 +0200
Subject: [PATCH] Update direct protocol (proxy, store and filter).

---
 modules/plugin_dbui/converter.py            |   5 +-
 static/plugin_dbui/src/data/DirectStore.js  |  30 +---
 static/plugin_dbui/src/data/proxy/Direct.js | 145 ++++++++++----------
 static/plugin_dbui/src/grid/Filter.js       |   2 +-
 4 files changed, 81 insertions(+), 101 deletions(-)

diff --git a/modules/plugin_dbui/converter.py b/modules/plugin_dbui/converter.py
index 0b318f78..659a7d61 100644
--- a/modules/plugin_dbui/converter.py
+++ b/modules/plugin_dbui/converter.py
@@ -716,10 +716,7 @@ def to_jsonstore(table, **kwargs):
                       extraParams=base_params,
                       model=tablename,
                       pageParam=NOPAGE,
-                      rootProperty=ROOT,
-                      successProperty=SUCCESS,
-                      storeId=get_store_id(tablename),
-                      totalProperty=TOTAL)
+                      storeId=get_store_id(tablename))
 
     for field in table:
         fieldname = field.name
diff --git a/static/plugin_dbui/src/data/DirectStore.js b/static/plugin_dbui/src/data/DirectStore.js
index 14b8cad8..d73b2858 100644
--- a/static/plugin_dbui/src/data/DirectStore.js
+++ b/static/plugin_dbui/src/data/DirectStore.js
@@ -7,20 +7,6 @@
  *
  * It also provide a set of method to filter the store passing by the server.
  *
- * **Note**: this class inherits of the config options from:
- *
- * - **{@link Ext.data.proxy.Direct proxy}**
- *
- *   - **{@link Ext.data.proxy.Direct#extraParams extraParams}**
- *   - **{@link Ext.data.proxy.Direct#pageParam pageParam}**
- *
- * - **{@link Ext.data.reader.Json JsonReader}**
- *
- *   - **{@link Ext.data.reader.Json#cfg-root rootProperty}**
- *   - **{@link Ext.data.reader.Json#idProperty idProperty}**
- *   - **{@link Ext.data.reader.Json#successProperty successProperty}**
- *   - **{@link Ext.data.reader.Json#totalProperty totalProperty}**
-
  */
 Ext.define('Dbui.data.DirectStore', {
 
@@ -52,15 +38,7 @@ Ext.define('Dbui.data.DirectStore', {
                 type: 'xdirect'
             };
 
-            Ext.copyTo(proxy, config, [
-            	'extraParams',
-                'idProperty',
-                'pageParam',
-                'rootProperty',
-                'successProperty',
-                'totalProperty'
-            ]);
-
+            Ext.copyTo(proxy, config, ['extraParams', 'pageParam']);
             config.proxy = proxy;
         }
 
@@ -125,17 +103,17 @@ Ext.define('Dbui.data.DirectStore', {
             conditions = [],
             i;
 
-        if (me.extraParams.where) {
+        if (me.proxy.extraParams.where) {
 
             if (me.initialWhere) {
 
                 for (i = 0; i < me.initialWhere.length; i += 1) {
                     conditions.push(me.initialWhere[i]);
                 }
-                me.getProxy().setExtraParam('where', conditions);
+                me.proxy.setExtraParam('where', conditions);
 
             } else {
-                delete me.getProxy().extraParams.where;
+                delete me.proxy.extraParams.where;
             }
         }
     }
diff --git a/static/plugin_dbui/src/data/proxy/Direct.js b/static/plugin_dbui/src/data/proxy/Direct.js
index 11d440ad..fc14e439 100644
--- a/static/plugin_dbui/src/data/proxy/Direct.js
+++ b/static/plugin_dbui/src/data/proxy/Direct.js
@@ -1,14 +1,5 @@
 /**
- * Implement the server protocol.
- *
- * **Note**: this class inherits of the config options from the JsonReader:
- *
- * - **{@link Ext.data.reader.Json JsonReader}**
- *
- *   - **{@link Ext.data.reader.Json#cfg-root rootProperty}**
- *   - **{@link Ext.data.reader.Json#idProperty idProperty}**
- *   - **{@link Ext.data.reader.Json#successProperty successProperty}**
- *   - **{@link Ext.data.reader.Json#totalProperty totalProperty}**
+ * Direct proxy implementing the DbSvc protocol.
  *
  * * **Note**: each operation contains at least 3 parameters:
  *
@@ -35,6 +26,21 @@ Ext.define('Dbui.data.proxy.Direct', {
     extend: 'Ext.data.proxy.Direct',
     alias: 'proxy.xdirect',
 
+    // predefined JSON reader adapted to the DbSvc service
+    reader: {
+        type: 'json',
+        rootProperty: 'records',
+        successProperty: 'success',
+        totalProperty: 'count'
+    },
+
+    // predeined JSON writer adapted to the DbSvc service
+    writer: {
+        type: 'json',
+        allowSingle: false,
+        writeAllFields: false
+    },
+
     // jshint strict: false
 
     /**
@@ -42,8 +48,7 @@ Ext.define('Dbui.data.proxy.Direct', {
      */
     constructor: function (config) {
 
-        var me = this,
-            reader;
+        var me = this;
 
         // the direct API tu run with the DbSvc service
         if (!config.api) {
@@ -55,24 +60,6 @@ Ext.define('Dbui.data.proxy.Direct', {
             };
         }
 
-        // the JSON reader
-        if (!config.reader) {
-            reader = {
-                type: 'json'
-            };
-            Ext.copyTo(reader, config, 'rootProperty,idProperty,successProperty,totalProperty');
-            config.reader = reader;
-        }
-
-        // the JSOn writer
-        if (!config.writer) {
-            config.writer = {
-                type: 'json',
-                allowSingle: false,
-                writeAllFields: false
-            };
-        }
-
         // initialise the base class
         me.callParent(arguments);
     },
@@ -91,81 +78,99 @@ Ext.define('Dbui.data.proxy.Direct', {
      * @param {Object} scope
      *
      */
-    doRequest: function (operation, callback, scope) {
+    doRequest: function (operation) {
 
         "use strict";
 
         var me = this,
-            writer = me.getWriter(),
-            request = me.buildRequest(operation),
-            params = request.params,
-            args = [],
-            i,
-            fieldId = Dbui.encodeField(operation.params.tableName, 'id'),
-            fn,
-            method,
-            records;
+            action, api, args, data, fieldId, i, fn, params, records,
+            request, writer;
 
         if (!me.methodsResolved) {
             me.resolveMethods();
         }
 
-        fn = me.api[request.action] || me.directFn;
+        request = me.buildRequest(operation);
+        action  = request.getAction();
+        api     = me.getApi();
+
+        if (api) {
+            fn = api[action];
+        }
+
+        fn = fn || me.getDirectFn();
+
+        writer = me.getWriter();
 
-        if (operation.allowWrite()) {
+        if (writer && operation.allowWrite()) {
             request = writer.write(request);
         }
 
-        switch (operation.action) {
+        // DvSvc Protocol ....................................................
 
+        params = request.getParams();
+
+        switch (action) {
         case 'read':
-            method = fn.directCfg.method;
-            args = method.getArgs(params, me.paramOrder, me.paramsAsHash);
             break;
 
         case 'create':
-            params.records = request.jsonData;
-            args.push(params);
-            break;
-
-        case 'update':
-            params.records = request.jsonData;
+            fieldId = Dbui.encodeField(params.tableName, 'id');
+            params.records = [];
+            records = request.getRecords();
 
-            // abandon the update if the proxy is not yet ready
-            for (i = 0; i < params.records; i += 1) {
-                if (!params.records[fieldId]) {
-                    return;
-                }
+            for (i = 0; i < records.length; i += 1) {
+                data = records[i].getData();
+                delete data[fieldId];
+                params.records.push(data);
             }
-
-            args.push(params);
             break;
 
-        case 'destroy':
-
-            // build the list of id to be destroy
+         case 'update':
             params.records = [];
+            records = request.getRecords();
 
-            records = operation.getRecords();
             for (i = 0; i < records.length; i += 1) {
-                params.records.push(records[i].get(fieldId));
+                data = records[i].getData();
+                params.records.push(data);
             }
+            break;
 
-            // abandon the transaction if the proxy is not yet ready
-            if (records === null || params.records.length === 0) {
-                return;
-            }
+         case 'destroy':
+            fieldId = Dbui.encodeField(params.tableName, 'id');
+            params.records = [];
+            records = request.getRecords();
 
-            args.push(params);
+            for (i = 0; i < records.length; i += 1) {
+                data = records[i].getData();
+                params.records.push(data[fieldId]);
+            }
             break;
         }
 
-        Ext.apply(request, {
+        //....................................................................
+
+        request.setParams(params);
+
+        args = fn.directCfg.method.getArgs({
+            params: params,
+            paramOrder: me.getParamOrder(),
+            paramsAsHash: me.getParamsAsHash(),
+            metadata: me.getMetadata(),
+            callback: me.createRequestCallback(request, operation),
+            scope: me
+        });
+
+        request.setConfig({
             args: args,
             directFn: fn
         });
 
-        args.push(me.createRequestCallback(request, operation, callback, scope), me);
         fn.apply(window, args);
+
+        // Store expects us to return something to indicate that the request
+        // is pending; not doing so will make a buffered Store repeat the
+        // requests over and over. See https://sencha.jira.com/browse/EXTJSIV-11757
+        return request;
     }
 });
diff --git a/static/plugin_dbui/src/grid/Filter.js b/static/plugin_dbui/src/grid/Filter.js
index 544c19dd..c26923c7 100644
--- a/static/plugin_dbui/src/grid/Filter.js
+++ b/static/plugin_dbui/src/grid/Filter.js
@@ -339,7 +339,7 @@ Ext.define('Dbui.grid.Filter', {
 
         // push new condition in the where clause of the store
         if (conditions.length) {
-            store.extraParams.where = conditions;
+            store.proxy.extraParams.where = conditions;
 
             // go back to the initial where setting when conditions are empty
         } else {
-- 
GitLab