From 92d33922ae776c637bf2c952077b359aa75199d1 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 4 Apr 2019 15:18:43 +0200
Subject: [PATCH] refactor tree

---
 .../recordcontainers/jdito/contentProcess.js  | 75 ++++++++++---------
 1 file changed, 40 insertions(+), 35 deletions(-)

diff --git a/entity/ObjectTree_entity/recordcontainers/jdito/contentProcess.js b/entity/ObjectTree_entity/recordcontainers/jdito/contentProcess.js
index 5f9715750d..c290fd12d8 100644
--- a/entity/ObjectTree_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/ObjectTree_entity/recordcontainers/jdito/contentProcess.js
@@ -1,16 +1,12 @@
-import("system.translate");
-import("system.util");
 import("system.db");
-import("system.text");
+import("system.translate");
 import("system.result");
 import("system.vars");
-import("system.logging");
 import("ObjectRelation_lib");
 import("Context_lib");
 import("Sql_lib");
 import("system.tools");
 
-var relationTypesCache = {};
 var tree = []
 var filter = JSON.parse(vars.get("$local.filter"))
 var selectedRelationType = null;
@@ -26,7 +22,7 @@ var originalObjectId = vars.get("$param.ObjectId_param");
 _loadObjectRelationTree(originalObjectId, vars.get("$param.ObjectType_param"), selectedRelationType);
 result.object(tree);
 
-function _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId, pNodeId, pLayer)
+function _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId, pNodeId, pLayer, pRelationTypeData)
 {
     // prevent stack overflows
     if (pLayer > 30)
@@ -56,10 +52,10 @@ function _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId,
                     currentObjectId = _getRootID(currentObjectId, relationTypeData);
                 }
                 
-                let uids = _insertEntry(tree, [[currentObjectId, "", "", "", ""]], pNodeId, pLayer, pObjectType, relationTypeData)
+                let uids = _insertEntry(tree, [[currentObjectId, "", "", "", ""]], pNodeId, pLayer, pObjectType)
                 for (let i = 0; i < uids.length; i++) 
                 {                    
-                    _loadObjectRelationTree(uids[i][0], uids[i][3], relationTypeData[0], uids[i], pLayer+1);
+                    _loadObjectRelationTree(uids[i][0], uids[i][3], relationTypeData[0], uids[i], pLayer+1, relationTypeData);
                 }
             }
             else // no ObjectType chosen
@@ -69,7 +65,7 @@ function _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId,
                 
                 for (let i=0; i<relationTypes.length; i++)
                 {   
-                    var data = _getEntryData(currentObjectId, relationTypes[i][0], relationTypes[i][3], relationTypes[i][7], relationTypes[i][8]);
+                    var data = _getEntryData(currentObjectId, relationTypes[i][3], relationTypes[i][7], relationTypes[i][8]);
                     
                     // if any subentry: show objectType
                     if (data.length > 0)
@@ -79,52 +75,56 @@ function _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId,
                         let uid = [currentObjectId, i, relationTypes[i]];
                         tree.push([JSON.stringify(uid), translate.text(relationTypes[i][1]), JSON.stringify(pNodeId), true, null, null, ""]);
                         
-                        _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId, uid, pLayer+1);
+                        _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId, uid, pLayer+1, relationTypeData);
                     }
                 }
             }
         }
         else if (pLayer >= 1)
         {
-            var typeData = pNodeId[2];
-            var typeId = typeData[0];
-            var hierarchy = typeData[4];
-            var destObjectType = typeData[6];
-            var relationType1 = typeData[7];
-            var relationType2 = typeData[8];
-            var direction = typeData[3];
+            // if no relationType given, load from nodeId
+            if (!pRelationTypeData)
+                pRelationTypeData = pNodeId[2];
+            
+            var typeId = pRelationTypeData[0];
+            var hierarchy = pRelationTypeData[4];
+            var destObjectType = pRelationTypeData[6];
+            var relationType1 = pRelationTypeData[7];
+            var relationType2 = pRelationTypeData[8];
+            var direction = pRelationTypeData[3];
             
             if (hierarchy == "1")
             {
-                var myData = _getEntryData(pNodeId[0], typeId, direction, relationType1, relationType2)
+                var myData = _getEntryData(pNodeId[0], direction, relationType1, relationType2)
                 
-                let uids = _insertEntry(tree, myData, pNodeId, pLayer, destObjectType, typeData)
+                let uids = _insertEntry(tree, myData, pNodeId, pLayer, destObjectType)
                 for (let i = 0; i < uids.length; i++) 
                 {                    
-                    _loadObjectRelationTree(uids[i][0], uids[i][3], pObjectRelationTypeId, uids[i], pLayer+1);
+                    _loadObjectRelationTree(uids[i][0], uids[i][3], pObjectRelationTypeId, uids[i], pLayer+1, pRelationTypeData);
                 }
             }
             else
             {
+                // pNodeId[4] is the previous NodeId and pNodeId[4][0] the previous ObjectId
                 var prevObjectId;
                 if (pNodeId[4] != undefined)
                 {
                     prevObjectId = pNodeId[4][0];
                 }
                 
-                var entryData = _getEntryData(pNodeId[0], typeId, direction, relationType1, relationType2, prevObjectId, true);
+                var entryData = _getEntryData(pNodeId[0], direction, relationType1, relationType2, prevObjectId, true);
 
                 // add both sides. Only one will succeed, because the prevObjectId will be filtered and it will just return []
-                let uids = _insertEntry(tree, entryData, pNodeId, pLayer, destObjectType, typeData, 0);
+                let uids = _insertEntry(tree, entryData, pNodeId, pLayer, destObjectType, 0);
                 if (direction == "same")
                 {
-                    var otherEntryData = _getEntryData(pNodeId[0], typeId, "normal", relationType1, relationType2, prevObjectId, true);
-                    uids  =uids.concat(_insertEntry(tree, otherEntryData, pNodeId, pLayer, destObjectType, typeData, 1));
+                    var otherEntryData = _getEntryData(pNodeId[0], "normal", relationType1, relationType2, prevObjectId, true);
+                    uids  =uids.concat(_insertEntry(tree, otherEntryData, pNodeId, pLayer, destObjectType, 1));
                 }
                                 
                 for (let i = 0; i < uids.length; i++) 
                 {   
-                    _loadObjectRelationTree(uids[i][0], uids[i][3], pObjectRelationTypeId, uids[i], pLayer+1);
+                    _loadObjectRelationTree(uids[i][0], uids[i][3], pObjectRelationTypeId, uids[i], pLayer+1, pRelationTypeData);
                 }
             }
         }
@@ -134,8 +134,17 @@ function _loadObjectRelationTree(pObjectId, pObjectType, pObjectRelationTypeId,
 /**
  * load data for a relation.
  * OBJECT_ROWID, AB_OBJECTRELATIONID, OBJECT_TYPE, RELATION_TITLE
+ * 
+ * @param {String} pObjectId
+ * @param {String} pDirection
+ * @param {String} pRelationType1
+ * @param {String} pRelationType2
+ * @param {String} pPrevId Id of the previous node to exclude it
+ * @param {String} pNoRecursion if false: select for direction "same" the other direction, if result is empty.
+ * 
+ * @return {[][]}
  */
-function _getEntryData(pObjectId, pRelationTypeId, pDirection, pRelationType1, pRelationType2, pPrevId, pRecursion)
+function _getEntryData(pObjectId, pDirection, pRelationType1, pRelationType2, pPrevId, pNoRecursion)
 {
     if (pRelationType1 == undefined || pRelationType2 == undefined) 
         return [];
@@ -154,7 +163,6 @@ function _getEntryData(pObjectId, pRelationTypeId, pDirection, pRelationType1, p
         myNum = 1;
     }
         
-        
     var cond = SqlCondition.begin()
                            .andPrepare("AB_OBJECTRELATION.AB_OBJECTRELATIONTYPE1", pRelationType1)
                            .andPrepare("AB_OBJECTRELATION.AB_OBJECTRELATIONTYPE2", pRelationType2)
@@ -177,9 +185,9 @@ function _getEntryData(pObjectId, pRelationTypeId, pDirection, pRelationType1, p
                  from AB_OBJECTRELATION \n\
                  join AB_OBJECTRELATIONTYPE on AB_OBJECTRELATIONTYPEID = AB_OBJECTRELATIONTYPE" + myNum + " and ","1=2", "", false));
 
-    if (data.length == 0 && pDirection == "same" && !pRecursion)
+    if (data.length == 0 && pDirection == "same" && !pNoRecursion)
     {
-         return _getEntryData (pObjectId, pRelationTypeId, "normal", pRelationType1, pRelationType2, pPrevId, true)
+         return _getEntryData(pObjectId, "normal", pRelationType1, pRelationType2, pPrevId, true)
     }
     
     // TODO: BINDATA?
@@ -190,13 +198,10 @@ function _getEntryData(pObjectId, pRelationTypeId, pDirection, pRelationType1, p
 function _getRelationTypes(pObjectType)
 {
     // TODO: load from entity when possible
-    if (relationTypesCache[pObjectType] == undefined)
-        relationTypesCache[pObjectType] = ObjectRelationUtils.getPossibleRelationTypes(pObjectType, true);
-    
-    return relationTypesCache[pObjectType];
+    return ObjectRelationUtils.getPossibleRelationTypes(pObjectType, true);
 }
 
-function _insertEntry (pTree, pEntryData, pNodeId, pLayer, pObjectType, pRelationTypeData, pNum)
+function _insertEntry(pTree, pEntryData, pNodeId, pLayer, pObjectType, pNum)
 {
     var expanded = true;
     if (pLayer > 10) expanded = false;
@@ -206,7 +211,7 @@ function _insertEntry (pTree, pEntryData, pNodeId, pLayer, pObjectType, pRelatio
     {
         var display = db.cell(ContextUtils.getNameSql(pObjectType, pEntryData[i][0]));
         // TODO: Icon
-        var uid = [pEntryData[i][0], i, pRelationTypeData, pObjectType, pNodeId, pEntryData[i][2], pEntryData[i][1]]
+        var uid = [pEntryData[i][0], i, "", pObjectType, pNodeId, pEntryData[i][2], pEntryData[i][1]]
         if (pNum)
             uid.push(pNum);
         uids.push(uid);
-- 
GitLab