From a84f39cd8fe670178256f04e3efb6b192a687a9c Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Tue, 4 Feb 2020 17:26:16 +0100
Subject: [PATCH] JditoFilter support for user-property selectionIgnoreCase

---
 process/JditoFilter_lib/process.js | 71 ++++++++++++++++++------------
 1 file changed, 43 insertions(+), 28 deletions(-)

diff --git a/process/JditoFilter_lib/process.js b/process/JditoFilter_lib/process.js
index eb2982d105e..e00af4d021c 100644
--- a/process/JditoFilter_lib/process.js
+++ b/process/JditoFilter_lib/process.js
@@ -1,3 +1,4 @@
+import("system.tools");
 import("system.logging");
 import("Sql_lib");
 
@@ -44,6 +45,8 @@ JditoFilterHelper.prototype.checkRecord = function (pRow)
     if (this.filter.length == 0)
         return true;
     
+    var regexFlags = JditoFilterUtils.isUserIgnoreCase() ? "i" : undefined;
+    
     return _testRecord.call(this, this.filter);
     
     /**
@@ -74,17 +77,17 @@ JditoFilterHelper.prototype.checkRecord = function (pRow)
         switch (pOperator)
         {
             case "CONTAINS":
-                return (new RegExp(pFilterValue)).test(pRowValue);
+                return (new RegExp(pFilterValue, regexFlags)).test(pRowValue);
             case "CONTAINSNOT":
-                return !(new RegExp(pFilterValue)).test(pRowValue);
+                return !(new RegExp(pFilterValue, regexFlags)).test(pRowValue);
             case "STARTSWITH":
-                return (new RegExp("^" + pFilterValue)).test(pRowValue);
+                return (new RegExp("^" + pFilterValue, regexFlags)).test(pRowValue);
             case "ENDSWITH":
-                return (new RegExp(pFilterValue + "$")).test(pRowValue);
+                return (new RegExp(pFilterValue + "$", regexFlags)).test(pRowValue);
             case "EQUAL":
-                return (new RegExp("^" + pFilterValue + "$")).test(pRowValue);
+                return (new RegExp("^" + pFilterValue + "$", regexFlags)).test(pRowValue);
             case "NOT_EQUAL":
-                return !(new RegExp("^" + pFilterValue + "$")).test(pRowValue);
+                return !(new RegExp("^" + pFilterValue + "$", regexFlags)).test(pRowValue);
             case "LESS":
                 return pRowValue < pFilterValue;
             case "LESS_OR_EQUAL":
@@ -192,7 +195,9 @@ JditoFilterUtils.filterRecords = function (pColumns, pRecords, pFilter, pCustomC
  */
 JditoFilterUtils.getSqlCondition = function (pFilter, pTable, pTableAlias, pColumnOrFnMap)
 {
-    var condition = newWhere();    
+    var condition = newWhere();
+    
+    var ignoreCase = JditoFilterUtils.isUserIgnoreCase();
     
     if (!pFilter)
         return condition;
@@ -229,11 +234,15 @@ JditoFilterUtils.getSqlCondition = function (pFilter, pTable, pTableAlias, pColu
             }
             else
             {
-                condition = _getCondition.call(pFilter, pFilter.value, pFilter.operator);
+                let isStringType, filterValue;
+                [condition, filterValue, isStringType] = _getCondition(pFilter.value, pFilter.operator);
+                if (isStringType && ignoreCase)
+                    condition = condition.replace("#", "UPPER(#)").replace("?", "UPPER(?)");
+                
                 if (pOperator == "AND")
-                    this.andIfSet(pFilter.name, pFilter.value, condition);
+                    this.andIfSet(pFilter.name, filterValue, condition);
                 else if (pOperator == "OR")
-                    this.orIfSet(pFilter.name, pFilter.value, condition);
+                    this.orIfSet(pFilter.name, filterValue, condition);
             }
         }
         else if (pFilter.type == "group")
@@ -251,43 +260,49 @@ JditoFilterUtils.getSqlCondition = function (pFilter, pTable, pTableAlias, pColu
         }
     }
     
-    //returns the condition depending on the operator and 
-    //adds wildcards to the value if necessary
+    //returns [condition, value with wildcards, is a string type] depending on the operator
     function _getCondition (pValue, pOperator)
     {
         switch (pOperator)
         {
             case "CONTAINS":
-                this.value = "%" + pValue + "%";
-                return SqlBuilder.LIKE();
+                return [SqlBuilder.LIKE(), "%" + pValue + "%", true];
             case "CONTAINSNOT":
-                this.value = "%" + pValue + "%";
-                return SqlBuilder.NOT_LIKE();
+                return [SqlBuilder.NOT_LIKE(), "%" + pValue + "%", true];
             case "STARTSWITH":
-                this.value = pValue + "%";
-                return SqlBuilder.LIKE();
+                return [SqlBuilder.LIKE(), pValue + "%", true];
             case "ENDSWITH":
-                this.value = "%" + pValue;
-                return SqlBuilder.LIKE();
+                return [SqlBuilder.LIKE(), "%" + pValue, true];
             case "EQUAL":
-                return SqlBuilder.EQUAL();
+                return [SqlBuilder.EQUAL(), pValue, true];
             case "NOT_EQUAL":
-                return SqlBuilder.NOT_EQUAL();
+                return [SqlBuilder.NOT_EQUAL(), pValue, true];
             case "LESS":
-                return SqlBuilder.LESS();
+                return [SqlBuilder.LESS(), pValue, false];
             case "LESS_OR_EQUAL":
-                return SqlBuilder.LESS_OR_EQUAL();
+                return [SqlBuilder.LESS_OR_EQUAL(), pValue, false];
             case "GREATER":
-                return SqlBuilder.GREATER();
+                return [SqlBuilder.GREATER(), pValue, false];
             case "GREATER_OR_EQUAL":
-                return SqlBuilder.GREATER_OR_EQUAL();
+                return [SqlBuilder.GREATER_OR_EQUAL(), pValue, false];
             case "ISNULL":
-                return "# is null";
+                return ["# is null", pValue, false];
             case "ISNOTNULL":
-                return "# is not null";
+                return ["# is not null", pValue, false];
         }
     }
 }
+
+/**
+ * @return {boolean} the selectionIgnoreCase property of the current user, defaults to true
+ */
+JditoFilterUtils.isUserIgnoreCase = function ()
+{
+    var user = tools.getCurrentUser();
+    var ignoreCase = user ? user[tools.PARAMS][tools.SELECTION_IGNORECASE] : "";
+    return ignoreCase == "" || /true/i.test(ignoreCase);
+}
+
     return JditoFilterUtils; //return only functions that should be public
     
 })();
-- 
GitLab