From 76e50707bdd1eb5d068bcb9a4c9569005bd42c9f Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Sat, 27 Jul 2019 13:17:06 +0200
Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1041584][Filter -
 Attribute]

---
 .../filterConditionProcess.js                 |  9 +----
 .../attribute_filter/filterFieldsProcess.js   |  7 +---
 .../attribute_filter/filterValuesProcess.js   |  4 +-
 process/AttributeFilter_lib/process.js        | 40 +++++++++++++++----
 process/Attribute_lib/process.js              | 14 ++++++-
 process/Entity_lib/process.js                 | 24 +++++++++++
 6 files changed, 75 insertions(+), 23 deletions(-)

diff --git a/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterConditionProcess.js b/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterConditionProcess.js
index 7297d0af27d..65d6b67c6e0 100644
--- a/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterConditionProcess.js
+++ b/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterConditionProcess.js
@@ -1,10 +1,5 @@
-import("system.vars");
-import("Context_lib");
 import("system.result");
 import("AttributeFilter_lib");
 
-var objectType = ContextUtils.getCurrentContextId();
-var filterName = vars.get("$local.name");
-var filterCond = vars.get("$local.condition");
-var resSql = AttributeFilterExtensionMaker.getFilterCondition(objectType, filterName, filterCond);
-result.string(resSql);
\ No newline at end of file
+var sqlCond = AttributeFilterExtensionMaker.makeFilterConditionSql();
+result.string(sqlCond);
\ No newline at end of file
diff --git a/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterFieldsProcess.js b/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterFieldsProcess.js
index 73d0e5b681e..2625d5ed4d4 100644
--- a/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterFieldsProcess.js
+++ b/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterFieldsProcess.js
@@ -1,8 +1,5 @@
-import("Context_lib");
 import("AttributeFilter_lib");
 import("system.result");
 
-var objectType = ContextUtils.getCurrentContextId();
-var res = AttributeFilterExtensionMaker.getFilterFields(objectType);
-result.string(res);
-
+var fields = AttributeFilterExtensionMaker.makeFilterFields();
+result.string(fields);
\ No newline at end of file
diff --git a/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterValuesProcess.js b/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterValuesProcess.js
index 071eda0778b..942b0a0495a 100644
--- a/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterValuesProcess.js
+++ b/entity/Person_entity/recordcontainers/db/filterextensions/attribute_filter/filterValuesProcess.js
@@ -1,7 +1,5 @@
-import("system.vars");
 import("system.result");
 import("AttributeFilter_lib");
 
-var filter = vars.getString("$local.filter");
-var values = AttributeFilterExtensionMaker.getFilterValues(filter);
+var values = AttributeFilterExtensionMaker.makeFilterValues();
 result.object(values);
\ No newline at end of file
diff --git a/process/AttributeFilter_lib/process.js b/process/AttributeFilter_lib/process.js
index d64aa4c759c..6278c014c9e 100644
--- a/process/AttributeFilter_lib/process.js
+++ b/process/AttributeFilter_lib/process.js
@@ -1,3 +1,6 @@
+import("Entity_lib");
+import("system.vars");
+import("Context_lib");
 import("system.db");
 import("Sql_lib");
 import("Attribute_lib");
@@ -48,9 +51,9 @@ AttributeFilterExtensionMaker.getFilterFields = function(pObjectType)
             res.push({
                 name: name,
                 title: row["FULL_ATTRIBUTE_NAME"],
-                //TODO: comment why this workaround is necessary
+                //workaround since we do not have a "UNKNOWN"-contentType in the filter-definition
                 contentType: contentType == "UNKNOWN" ? "TEXT" : contentType,
-                hasDropDownValues: contentType == "UNKNOWN" || contentType == "BOOLEAN" ? true : false
+                hasDropDownValues: contentType == "UNKNOWN" || contentType == "BOOLEAN" ? true : false//TODO: determine this somehow else
             });
         }
     });
@@ -59,6 +62,13 @@ AttributeFilterExtensionMaker.getFilterFields = function(pObjectType)
     return res;
 };
 
+AttributeFilterExtensionMaker.makeFilterFields = function()
+{
+    var objectType = ContextUtils.getCurrentContextId();
+    var res = AttributeFilterExtensionMaker.getFilterFields(objectType);
+    return res;
+};
+
 AttributeFilterExtensionMaker.getFilterValues = function(pFilter) 
 {
     var filter = JSON.parse(pFilter);
@@ -73,7 +83,14 @@ AttributeFilterExtensionMaker.getFilterValues = function(pFilter)
     return res;
 };
 
-AttributeFilterExtensionMaker.getFilterCondition = function(pObjectType, pFilterName, pCondition) 
+AttributeFilterExtensionMaker.makeFilterValues = function()
+{
+    var filter = vars.getString("$local.filter");
+    var res = AttributeFilterExtensionMaker.getFilterValues(filter);
+    return res;
+};
+
+AttributeFilterExtensionMaker.getFilterCondition = function(pObjectType, pFilterName, pCondition, pIdTableName, pIdColumnName) 
 {
     var name = pFilterName;
     name = AttributeSearchNameCoder.decode(name);
@@ -87,14 +104,23 @@ AttributeFilterExtensionMaker.getFilterCondition = function(pObjectType, pFilter
         [pObjectType, SqlUtils.getSingleColumnType("AB_ATTRIBUTERELATION.OBJECT_TYPE")],
         [attributeId, SqlUtils.getSingleColumnType("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID")]
     ];
-    //TODO: dynmic mapping of UID and TABLE -> sys.uidcolumn
-    var resSql = "CONTACTID in (select CONTACT.CONTACTID \n\
-                         from CONTACT \n\
-                         left join AB_ATTRIBUTERELATION on (AB_ATTRIBUTERELATION.OBJECT_ROWID = CONTACT.CONTACTID \n\
+    var resSql = pIdColumnName + " in (select " + pIdTableName + "." + pIdColumnName + " \n\
+                         from " + pIdTableName + " \n\
+                         left join AB_ATTRIBUTERELATION on (AB_ATTRIBUTERELATION.OBJECT_ROWID = " + pIdTableName + "." + pIdColumnName + " \n\
                                                             and AB_ATTRIBUTERELATION.OBJECT_TYPE = ? \n\
                                                             and AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID = ?) \n\
                          where " + condition + " )";
 
     resSql = db.translateCondition([resSql, preparedValues]);
     return resSql;
+};
+
+AttributeFilterExtensionMaker.makeFilterConditionSql = function()
+{
+    var objectType = ContextUtils.getCurrentContextId();
+    var filterName = vars.get("$local.name");
+    var filterCond = vars.get("$local.condition");
+    var uidInfo = EntityUtils.parseUidColumn(vars.get("$sys.uidcolumn"));
+    var res = AttributeFilterExtensionMaker.getFilterCondition(objectType, filterName, filterCond, uidInfo.table, uidInfo.column);
+    return res;
 };
\ No newline at end of file
diff --git a/process/Attribute_lib/process.js b/process/Attribute_lib/process.js
index aa14cfb5aea..7d02717f1fd 100644
--- a/process/Attribute_lib/process.js
+++ b/process/Attribute_lib/process.js
@@ -1,4 +1,5 @@
 import("KeywordData_lib");
+import("KeywordData_lib");
 import("Context_lib");
 import("system.util");
 import("system.datetime");
@@ -74,7 +75,18 @@ AttributeUtil.getPossibleAttributes = function (pObjectType, pIncludeGroups, pFi
     return attributes;
 }
 
-//TODO: comment function
+/**
+ * searches for possiblevalues for a atttribute and returns these. The values depend on the attributeType
+ * 
+ * @param {String} pAttributeId the id of the attribute
+ * @param {Boolean} pAttributeType type of the attribute that is specified with pAttributeId;
+ *                                 The type needs to be passed to the function for better performance 
+ *                                 (loading the type via attribute several times would be too slow)
+ * @param {Boolean} [pIncludeInactives=false] specifies if only active attributevalues or actives + inactives shall be returned, 
+ *                                            this is important when you want to search for attributevalues 
+ * 
+ * @return {Array} 2D-array with [ID, value] als elements if the given attributeType has possible items. if not null is returned
+ */
 AttributeUtil.getPossibleListValues = function (pAttributeId, pAttributeType, pIncludeInactives)
 {
     var attributeId = pAttributeId;
diff --git a/process/Entity_lib/process.js b/process/Entity_lib/process.js
index 24029d5a797..1c75a8c472e 100644
--- a/process/Entity_lib/process.js
+++ b/process/Entity_lib/process.js
@@ -2,6 +2,30 @@ import("system.result");
 import("system.neon");
 import("system.vars");
 
+/**
+* provides static methods for special handling of entities in JDito-Processes
+* do not create an instance of this
+* 
+* @class
+*/
+function EntityUtils(){}
+
+/**
+ * parses a full-databnase-uid-name into a table and a column identifier
+ * 
+ * @param {String} pFullUidName full-databnase-uid-name like "ORGANISATION.ORGANISATIONID", the variable "$sys.uidcolumn" returns this for example
+ * 
+ * @return {Object} new object with 2 properties: - table: contains the tablename, - column: contains the columnname
+ */
+EntityUtils.parseUidColumn = function(pFullUidName)
+{
+    var pos = pFullUidName.lastIndexOf(".");
+    return {
+        table: pFullUidName.substring(0, pos),
+        column: pFullUidName.substring(++pos)
+    };
+};
+
 /**
 * provides static methods for special handling of entities in JDito-Processes
 * do not create an instance of this
-- 
GitLab