From 7eae3c72d0311031fb1eb55cb27b563291600df0 Mon Sep 17 00:00:00 2001
From: "a.voegl" <a.voegl@adito.de>
Date: Tue, 20 Oct 2020 11:19:37 +0200
Subject: [PATCH] add missing code piece

---
 .../recordcontainers/jdito/contentProcess.js  | 145 +++++++++++-------
 1 file changed, 86 insertions(+), 59 deletions(-)

diff --git a/entity/360Degree_entity/recordcontainers/jdito/contentProcess.js b/entity/360Degree_entity/recordcontainers/jdito/contentProcess.js
index c22aa1e7ac5..3014803809e 100644
--- a/entity/360Degree_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/360Degree_entity/recordcontainers/jdito/contentProcess.js
@@ -1,21 +1,36 @@
-import("JditoFilter_lib");
-import("system.eMath");
-import("system.logging");
 import("system.util");
 import("system.db");
 import("system.vars");
 import("system.result");
 import("Context_lib");
 import("system.translate");
-import("system.datetime");
 
-if (vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param") 
-    && vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param"))
-{
-    var contextList = JSON.parse(vars.get("$param.ObjectType_param"));
-    var contactId = JSON.parse(vars.get("$param.ObjectRowId_param"));
-    var data = filterData(_get360Data(contactId, contextList));
-    result.object(data);   
+if (vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param") && vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param"))
+{    
+    var active;
+    var selectMap = ContextUtils.getSelectMap();
+    if(vars.exists("$local.filter") && vars.get("$local.filter") )
+    {
+        var filter = vars.get("$local.filter");
+        if (filter.filter)
+            if(filter.filter.childs != null && filter.filter.childs.length > 0)
+            {
+                filter.filter.childs.forEach(function(child)
+                {
+                    if(child.name === "ACTIVE")
+                        active = child.key == 'true';
+                });  
+            }  
+    }
+    
+    var contextList = JSON.parse(vars.getString("$param.ObjectType_param"));
+    var contactId = vars.get("$param.ObjectRowId_param");
+    var idValues;
+    if (vars.exists("$local.idvalues"))
+        idValues = vars.get("$local.idvalues");
+    
+    var res =  _get360Data(selectMap, contactId, contextList, active, idValues);
+    result.object(res);   
 } 
 else
 {
@@ -24,66 +39,78 @@ else
 
 /** 
  * load all data for the 360Degree tree.
- * @param {String} pContactId the select map (result of ContextUtils.getContextDataSqlviaReadEntity())
- * @param {String[]} pContextList the Contactid the 360 Degree tree should be loaded for
+ * @param {Object} pSelectMap the select map (result of ContextUtils.getSelectMap())
+ * @param {String[]} pContactId the Contactid the 360 Degree tree should be loaded for
+ * @param {String[]} pContextList list of contexts to load. Note that subcontexts use their own list, defined in ContextUtils
+ * @param {Boolean} [pActive=undefined] if not undefined: it select only for active / inactive state
+ * @param {Array} [pUids=undefined] uids of the 360° entity that are used for filtering, each rowId needs the format: 
+ *                                    {id: "«rowid»", type: "«context id»"}
  *  
  *  @return {String[][]} the resulting data
  */
-function _get360Data(pContactId, pContextList)
+function _get360Data(pSelectMap, pContactId, pContextList, pActive, pUids)
 {   
+    //if there are uids for filtering, group them per context:
+    var uidContextMap = new Map();//Map where key is the type (contextname) and value is an array of the rowIds for that type
+    if (pUids)
+    {
+        pUids.forEach(function (uid){
+            uid = JSON.parse(uid);
+            if (uidContextMap.has(uid.type))
+                uidContextMap.get(uid.type).push(uid.id);
+            else
+                uidContextMap.set(uid.type, [uid.id]);
+        });
+    }
+
     var resultList = [];
-    for(var i = 0; i < pContextList.length; i++)
+    pContextList.forEach(function (context) 
     {
-        var res = ContextUtils.getContextDataSqlviaReadEntity(pContextList[i], pContactId);
-        res.forEach(function (row) 
+        var rowIds;
+        if (pUids)
         {
-            var targetid = row["#UID"];
-            var title = row["#CONTENTTITLE"];
-            var description = row["#CONTENTDESCRIPTION"];
-            var dataDate = row["DATE_NEW"];
-            var group = pContextList[i];
-
+            //when a Uid-filter exists, but there is no context for filtering we can skip that context, 
+            //otherwise we need to filter for the found row ids of that context
+            if (!uidContextMap.has(context))
+                return;
+            else
+                rowIds = uidContextMap.get(context);
+        }
+        var data = db.table(ContextUtils.getContextDataSql(context, JSON.parse(pContactId), true, pActive, true, true, rowIds));
+        data.forEach(function (row) 
+        {
+            var active;
+            if(pActive != undefined) //ACTIVE
+                active = translate.text(pActive);
+            else
+            {
+                if(pSelectMap[context].activeStates && pSelectMap[context].activeStates.indexOf(row[3]) > -1)    
+                    active = translate.text("true");
+                else
+                    active = translate.text("false");
+            }
+            
+            var groupBy = row[4];
+            if(groupBy == "")
+                groupBy = ContextUtils.getEntityTitle(context, true);
+            
+            var uid = JSON.stringify({id: row[0], type: context});
             resultList.push([
-                util.getNewUUID(),                      // UID
-                targetid,                               // TARGET_ID
-                pContextList[i],                        // TARGET_CONTEXT
-                translate.text(pContextList[i]),        // TARGET_CONTEXT.displayValue
-                title,                                  // TITLE
-                description,                            // DESCRIPTION
-                dataDate,                               // DATE
-                dataDate,                               // YEAR
-                ContextUtils.getEntity(pContextList[i]),// ENTITY_NAME
-                translate.text(group)                   // GROUP
+                uid, // UID
+                row[0], // TARGET_ID
+                context, // TARGET_CONTEXT
+                row[1], // TITLE
+                row[2], //DATE
+                active,
+                groupBy
                 ]); 
         });
-    }  
-
+    });  
+    
     resultList.sort(function (a,b) {
-        if (a[6] < b[6]) return  1;
-        if (a[6] > b[6]) return -1;
+        if (a[4] < b[4]) return  1;
+        if (a[4] > b[4]) return -1;
         return 0;
     })
     return resultList;
-}
-
-
-
-function filterData(data)
-{
-    var filter = vars.get("$local.filter")
-    var filterFields = [
-    "UID", 
-    "TARGET_ID",
-    "TARGET_CONTEXT", 
-    "", 
-    "TITLE", 
-    "DESCRIPTION", 
-    "DATE" ,
-    "YEAR", 
-    "ENTITY_NAME",
-    "GROUP"
-    ];
-    data = JditoFilterUtils.filterRecords(filterFields, data, filter.filter);
-    
-    return data;
 }
\ No newline at end of file
-- 
GitLab