From ddef830fb8bef54ebef8a1859021f9f688a3bc4c Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 17 Jan 2019 11:40:39 +0100
Subject: [PATCH] treetable lib

---
 entity/Prod2prod_entity/Prod2prod_entity.aod  |   2 +-
 .../recordcontainers/jdito/contentProcess.js  |   2 +
 .../Prod2prodFilter_view.aod                  |  36 +----
 process/Data_lib/Data_lib.aod                 |   9 ++
 process/Data_lib/process.js                   | 124 ++++++++++++++++++
 process/Product_lib/process.js                | 104 +++++++++++----
 6 files changed, 217 insertions(+), 60 deletions(-)
 create mode 100644 process/Data_lib/Data_lib.aod
 create mode 100644 process/Data_lib/process.js

diff --git a/entity/Prod2prod_entity/Prod2prod_entity.aod b/entity/Prod2prod_entity/Prod2prod_entity.aod
index 29507f9fe4..71cc2643aa 100644
--- a/entity/Prod2prod_entity/Prod2prod_entity.aod
+++ b/entity/Prod2prod_entity/Prod2prod_entity.aod
@@ -70,6 +70,7 @@
     <entityField>
       <name>PARENTID</name>
       <title>Parent</title>
+      <groupable v="true" />
     </entityField>
     <entityField>
       <name>PROD2PRODID</name>
@@ -98,7 +99,6 @@
       <name>jdito</name>
       <jDitoRecordAlias>Data_alias</jDitoRecordAlias>
       <contentProcess>%aditoprj%/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js</contentProcess>
-      <isPageable v="false" />
       <isSortable v="false" />
       <rowCountProcess>%aditoprj%/entity/Prod2prod_entity/recordcontainers/jdito/rowCountProcess.js</rowCountProcess>
       <onInsert>%aditoprj%/entity/Prod2prod_entity/recordcontainers/jdito/onInsert.js</onInsert>
diff --git a/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js b/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js
index 912ab1f560..cc59867417 100644
--- a/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/Prod2prod_entity/recordcontainers/jdito/contentProcess.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("system.result");
 import("system.vars");
 import("system.db");
@@ -10,5 +11,6 @@ var prodid = vars.exists("$param.ProductId_param")
 if(prodid != "")
 {
     var p2pUtils = new Prod2ProdUtils(prodid);
+    logging.log("casllllldsfasdf")
     result.object(p2pUtils.getPartsListForRecordContainer());
 }
\ No newline at end of file
diff --git a/neonView/Prod2prodFilter_view/Prod2prodFilter_view.aod b/neonView/Prod2prodFilter_view/Prod2prodFilter_view.aod
index 3057c5439e..c458b6242b 100644
--- a/neonView/Prod2prodFilter_view/Prod2prodFilter_view.aod
+++ b/neonView/Prod2prodFilter_view/Prod2prodFilter_view.aod
@@ -9,36 +9,12 @@
     </boxLayout>
   </layout>
   <children>
-    <tableViewTemplate>
-      <name>Prod2prod_table</name>
-      <autoNewRow v="true" />
+    <treetableViewTemplate>
+      <name>partlist</name>
+      <parentField>PARENTID</parentField>
+      <titleField>PRODUCTCODE</titleField>
+      <descriptionField>QUANTITY</descriptionField>
       <entityField>#ENTITY</entityField>
-      <columns>
-        <neonTableColumn>
-          <name>1c681134-4741-4dd6-b4c3-899d98216b72</name>
-          <entityField>PARENTID</entityField>
-        </neonTableColumn>
-        <neonTableColumn>
-          <name>5a884b06-880a-4ec4-9405-909548cedd41</name>
-          <entityField>PRODUCTCODE</entityField>
-        </neonTableColumn>
-        <neonTableColumn>
-          <name>723600d0-7d3b-4d96-9880-cb5b9ea90002</name>
-          <entityField>SOURCE_ID</entityField>
-        </neonTableColumn>
-        <neonTableColumn>
-          <name>3975b280-3f16-413c-8613-4de277cd8ece</name>
-          <entityField>QUANTITY</entityField>
-        </neonTableColumn>
-        <neonTableColumn>
-          <name>295adb8f-c639-4e5a-b02d-7f61756992ff</name>
-          <entityField>OPTIONAL</entityField>
-        </neonTableColumn>
-        <neonTableColumn>
-          <name>892a56f5-4417-4ece-9d94-70b1d9f0c4d4</name>
-          <entityField>TAKEPRICE</entityField>
-        </neonTableColumn>
-      </columns>
-    </tableViewTemplate>
+    </treetableViewTemplate>
   </children>
 </neonView>
diff --git a/process/Data_lib/Data_lib.aod b/process/Data_lib/Data_lib.aod
new file mode 100644
index 0000000000..945a3460a8
--- /dev/null
+++ b/process/Data_lib/Data_lib.aod
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<process xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.1.7" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.1.7">
+  <name>Data_lib</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+  <process>%aditoprj%/process/Data_lib/process.js</process>
+  <variants>
+    <element>LIBRARY</element>
+  </variants>
+</process>
diff --git a/process/Data_lib/process.js b/process/Data_lib/process.js
new file mode 100644
index 0000000000..6f2b7fab9b
--- /dev/null
+++ b/process/Data_lib/process.js
@@ -0,0 +1,124 @@
+/**
+ *  
+ * @class
+ */
+function DataTree() 
+{
+    this._dataTree = {
+        root: {
+            ids: []
+        }
+    };
+    this._resultData = [];
+}
+
+/**
+ * @return {DataTree}
+ */
+DataTree.begin = function() 
+{
+    var tree = new DataTree();
+    return tree;
+}
+
+/**
+ *
+ * @param {String[]} pData
+ * @param {manipulateNodeCallback} pManipulateNodeFn
+ * 
+ */
+DataTree.prototype.getTreeObject = function() 
+{
+    return this._dataTree;
+}
+
+DataTree.prototype.add = function(pUid, pParentId, pData, pManipulateNodeFn) 
+{
+    logging.log(pUid + " " + pParentId + " ")
+    /*if (!pParentId || pParentId == "root")
+    {
+        pParentId = "root";
+        this._dataTree[pParentId].ids.push(pUid);
+        
+        // callback to provide a hook for manipulating the node. (e.g. for adding additional node data)
+        if (pManipulateNodeFn != undefined)
+            pManipulateNodeFn(pParentId, this._dataTree[pParentId]);
+    }
+    else 
+    {
+        if (this._dataTree[pParentId] == undefined)
+            this._dataTree[pParentId] = {
+                data: [],
+                ids: []
+            }
+
+        this._dataTree[pParentId].ids.push(pUid);
+    }
+
+
+    this._dataTree[pUid] = {
+        data: pData,
+        parent: pParentId
+    }
+
+    if (this._dataTree[pUid].ids == undefined)
+    {
+        this._dataTree[pUid].ids = [];
+    }
+
+    // callback to provide a hook for manipulating the node. (e.g. for adding additional node data)
+    if (pManipulateNodeFn != undefined)
+        pManipulateNodeFn(pUid, this._dataTree[pUid]);
+*/
+    return this;
+}
+
+/**
+ *
+ * @param {String[]} pData
+ * @param {manipulateNodeCallback} pManipulateNodeFn
+ * 
+ */
+DataTree.prototype.addArray = function(pData, pManipulateNodeFn) 
+{
+    logging.log(pData.toSource())
+    pData.forEach(function(pRow)
+    {
+        this.add(pRow[0], pRow[1], pRow.splice(2), pManipulateNodeFn);
+    }, this);
+    
+    return this;
+}
+
+DataTree.prototype.toArray = function(pRootId) 
+{   
+    var result = [];
+    var tree = this._dataTree;
+    // in this function Ids are just concatenated. This is bad, because it results in very long strings.
+    // Because of this the concatenated id's are maped to new uids in the resulting array
+    var uidMap = {};
+    logging.log(tree.toSource())
+    //__push(pRootId, tree.root);
+    
+    function __push(pParent, pNode) {   
+        for (var i = 0; i < pNode.ids.length; i++) {
+
+            var nextUid = pParent + pNode.ids[i];
+            __addRow(nextUid, pParent, tree[pNode.ids[i]].data)
+           logging.log(nextUid + "\n---- " + tree[pNode.ids[i]])
+            __push(nextUid, tree[pNode.ids[i]])
+        }
+    }
+    
+    function __addRow(pUid, pParentId, pRowdata)
+    {
+        uidMap[pUid] = util.getNewUUID();
+        if (uidMap[pParentId] == undefined) {
+            uidMap[pParentId] = util.getNewUUID();
+        }
+        
+        result.push([uidMap[pUid], uidMap[pParentId]].concat(pRowdata));
+    }
+    
+    return result;
+}
\ No newline at end of file
diff --git a/process/Product_lib/process.js b/process/Product_lib/process.js
index 6122cd8b2b..24652de922 100644
--- a/process/Product_lib/process.js
+++ b/process/Product_lib/process.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("system.util");
 import("system.SQLTYPES");
 import("system.datetime");
@@ -8,7 +9,7 @@ import("Util_lib");
 import("Binary_lib");
 import("Sql_lib");
 import("Keyword_lib");
-
+import("Data_lib");
 
 /**
  * utility functions for products
@@ -408,7 +409,7 @@ function Prod2ProdUtils(productId) {
  *                  } }
  */
 Prod2ProdUtils.prototype.getPartsListObject = function() {
-    return this._relateChilds();
+    return this._relateChilds().getTreeObject();
 }
 
 /**
@@ -430,24 +431,44 @@ Prod2ProdUtils.prototype.getPartsListObject = function() {
  *                    , "PRODUCTCODE"] ]
  */
 Prod2ProdUtils.prototype.getPartsListForRecordContainer = function() {
-    var ret = [];
-    var childs = this._relateChilds();
-
-    __push(childs.root);
-
-    function __push(obj) {
-        for (var i = 0; i < obj.ids.length; i++) {
-            var rowdata = childs[obj.ids[i]].rowdata;
-            var UID = util.getNewUUID();
-            var PARENTID = childs[obj.ids[i]].destid;
-
-            rowdata = [UID, PARENTID].concat(rowdata);
-            ret.push(rowdata);
-            __push(childs[obj.ids[i]]);
-        }
-    }
-
-    return ret;
+    var tree = this._relateChilds();
+    return tree.toArray("");
+//    var ret = [];
+//    var childs = this._relateChilds();
+//    
+//    // map internal, (uids used in this function) concatenated ids to new random generated uuids. 
+//    // Without this every depth would make the uid significantly longer.
+//    var uidMap = {};
+//    
+//    __push("", childs.root, 0);
+//    
+//
+//    
+//    
+//    function __push(parent, obj) {
+//        logging.log(obj.toSource())
+//        //
+//        
+//        for (var i = 0; i < obj.ids.length; i++) {
+//            logging.log(childs[obj.ids[i]].toSource())
+//            var nextUid = parent + obj.ids[i];
+//            __addRow(nextUid, parent, childs[obj.ids[i]].rowdata)
+//           
+//            __push(nextUid, childs[obj.ids[i]])
+//        }
+//    }
+//    
+//    function __addRow(uid, parentId, rowdata)
+//    {
+//        uidMap[uid] = util.getNewUUID();
+//        if (uidMap[parentId] == undefined) {
+//            uidMap[parentId] = util.getNewUUID();
+//        }
+//        
+//        ret.push([uidMap[uid], uidMap[parentId]].concat(rowdata));
+//    }
+//    
+//    return ret;
 }
 
 /**
@@ -459,7 +480,7 @@ Prod2ProdUtils.prototype.getPartsListForRecordContainer = function() {
 */
 Prod2ProdUtils.prototype.getPartsListProdIds = function() {
     var ret = [];
-    var childs = this._relateChilds();
+    var childs = this._relateChilds().getTreeObject();
 
     __push(childs.root);
 
@@ -515,6 +536,29 @@ Prod2ProdUtils.prototype._initProd2ProdData = function() {
  **/
 Prod2ProdUtils.prototype._buildTree = function(supervised) {
     this._initProd2ProdData();
+    var tree = DataTree.begin().addArray(this.data,
+        function(pUid, pNode)
+        {
+            if (pUid == "root")
+            {
+                pNode["sourceid"] = this.productId;
+                if (supervised) 
+                {
+                    pNode["destid"] = this.productId;
+                }
+            } 
+            else
+            {
+                pNode["destid"] = pNode.parent;
+                pNode["sourceid"] = pNode.data[0];
+                pNode["quantity"] = pNode.data[1];
+                pNode["optional"] = pNode.data[2];
+                pNode["takeprice"] = pNode.data[3];
+                pNode["productcode"] = pNode.data[4];
+            }
+        }
+    );
+    /*
 
     var tree = { root: {ids: [], sourceid: this.productId } };
 
@@ -536,7 +580,7 @@ Prod2ProdUtils.prototype._buildTree = function(supervised) {
             };
         }
     }
-
+*/
     return tree;
 }
 
@@ -547,11 +591,12 @@ Prod2ProdUtils.prototype._relateChilds = function() {
 
     return tree;
 
-
     function __relate(id) {
-        for (var treeId in tree) {
-            if (tree[treeId].destid == tree[id].sourceid && tree[id].ids.indexOf(treeId) == -1) {   
-                tree[id].ids.push(treeId);
+        var treeObject = tree.getTreeObject();
+        for (var treeId in treeObject) {
+            if (treeObject[treeId].destid == treeObject[id].sourceid && treeObject[id].ids.indexOf(treeId) == -1) { 
+                if (treeId != "root")
+                    treeObject[id].ids.push(treeId);
                 __relate(treeId);
             }    
         }
@@ -567,9 +612,10 @@ Prod2ProdUtils.prototype._relateParents = function() {
 
 
     function __relate(id) {
-        for (var treeId in tree) {
-            if (tree[treeId].sourceid == tree[id].destid && tree[id].ids.indexOf(treeId) == -1) {   
-                tree[id].ids.push(treeId);
+        var treeObject = tree.getTreeObject();
+        for (var treeId in treeObject) {
+            if (treeObject[treeId].sourceid == treeObject[id].destid && treeObject[id].ids.indexOf(treeId) == -1) {   
+                treeObject[id].ids.push(treeId);
                 __relate(treeId);
             }    
         }
-- 
GitLab