From 654fb1539da0a93594df9fcd387b2e1ba53e13eb Mon Sep 17 00:00:00 2001
From: "d.buechler" <d.buechler@adito.de>
Date: Thu, 18 Jul 2019 16:51:55 +0200
Subject: [PATCH] =?UTF-8?q?Zwischenstand=20Dublettenermittlung=20Die=20Fil?=
 =?UTF-8?q?terung=20anhand=20der=20konfigurierten=20Filter=20funktioniert?=
 =?UTF-8?q?=20nun=20grunds=C3=A4tzlich=20und=20es=20werden=20die=20korrekt?=
 =?UTF-8?q?en=20Daten=20zur=C3=BCckgegeben.=20Aktuell=20werden=20noch=20di?=
 =?UTF-8?q?e=20bei=20der=20Konfiguration=20der=20Filter=20gesetzten=20Wert?=
 =?UTF-8?q?e=20zur=20Filterung=20genutzt.=20Es=20bestehen=20Verhaltsnsprob?=
 =?UTF-8?q?leme=20bei=20der=20Kombination=20GenericMultiple=20+=20Feld=20m?=
 =?UTF-8?q?it=20Typ=20Filter=5FTree?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../DuplicateScanConditionConfig_entity.aod   |  7 ++
 .../entityfields/condition/onValueChange.js   |  3 +
 .../entityfields/condition/valueProcess.js    |  2 +-
 .../dbrecordcontainer/onDBUpdate.js           |  3 +
 .../testduplicatescan/onActionProcess.js      |  5 +-
 process/DuplicateScanner_lib/process.js       | 76 ++++++++++++++-----
 6 files changed, 74 insertions(+), 22 deletions(-)
 create mode 100644 entity/DuplicateScanConditionConfig_entity/entityfields/condition/onValueChange.js
 create mode 100644 entity/DuplicateScanConditionConfig_entity/recordcontainers/dbrecordcontainer/onDBUpdate.js

diff --git a/entity/DuplicateScanConditionConfig_entity/DuplicateScanConditionConfig_entity.aod b/entity/DuplicateScanConditionConfig_entity/DuplicateScanConditionConfig_entity.aod
index f692c1451d..dd41eac070 100644
--- a/entity/DuplicateScanConditionConfig_entity/DuplicateScanConditionConfig_entity.aod
+++ b/entity/DuplicateScanConditionConfig_entity/DuplicateScanConditionConfig_entity.aod
@@ -18,6 +18,12 @@
       <title>Condition</title>
       <contentType>FILTER_TREE</contentType>
       <valueProcess>%aditoprj%/entity/DuplicateScanConditionConfig_entity/entityfields/condition/valueProcess.js</valueProcess>
+      <onValueChange>%aditoprj%/entity/DuplicateScanConditionConfig_entity/entityfields/condition/onValueChange.js</onValueChange>
+      <onValueChangeTypes>
+        <element>MASK</element>
+        <element>PROCESS</element>
+        <element>PROCESS_SETVALUE</element>
+      </onValueChangeTypes>
     </entityField>
     <entityProvider>
       <name>ScanConditionConfigsProvider</name>
@@ -79,6 +85,7 @@
     <dbRecordContainer>
       <name>DBRecordContainer</name>
       <alias>Data_alias</alias>
+      <onDBUpdate>%aditoprj%/entity/DuplicateScanConditionConfig_entity/recordcontainers/dbrecordcontainer/onDBUpdate.js</onDBUpdate>
       <linkInformation>
         <linkInformation>
           <name>22217bb2-6fbf-4a7f-94f3-b200ef54c49f</name>
diff --git a/entity/DuplicateScanConditionConfig_entity/entityfields/condition/onValueChange.js b/entity/DuplicateScanConditionConfig_entity/entityfields/condition/onValueChange.js
new file mode 100644
index 0000000000..b47529a485
--- /dev/null
+++ b/entity/DuplicateScanConditionConfig_entity/entityfields/condition/onValueChange.js
@@ -0,0 +1,3 @@
+import("system.vars");
+import("system.logging");
+logging.log("condition onValueChange -> " + vars.get("$field.CONDITION"));
\ No newline at end of file
diff --git a/entity/DuplicateScanConditionConfig_entity/entityfields/condition/valueProcess.js b/entity/DuplicateScanConditionConfig_entity/entityfields/condition/valueProcess.js
index a69cee128a..0f811becde 100644
--- a/entity/DuplicateScanConditionConfig_entity/entityfields/condition/valueProcess.js
+++ b/entity/DuplicateScanConditionConfig_entity/entityfields/condition/valueProcess.js
@@ -2,7 +2,7 @@ import("system.logging");
 import("system.vars");
 import("system.result");
 
-logging.log("condition -> " + JSON.stringify(vars.get("$field.CONDITION")));
+logging.log("condition value process -> " + JSON.stringify(vars.get("$field.CONDITION")));
 if(vars.get("$field.CONDITION") == "")
 {
     let entityToScan = vars.get("$param.EntityToFilter_param");
diff --git a/entity/DuplicateScanConditionConfig_entity/recordcontainers/dbrecordcontainer/onDBUpdate.js b/entity/DuplicateScanConditionConfig_entity/recordcontainers/dbrecordcontainer/onDBUpdate.js
new file mode 100644
index 0000000000..113fc36d8f
--- /dev/null
+++ b/entity/DuplicateScanConditionConfig_entity/recordcontainers/dbrecordcontainer/onDBUpdate.js
@@ -0,0 +1,3 @@
+import("system.vars");
+import("system.logging");
+logging.log("onDbUpdate condition -> " + vars.get("$field.CONDITION"));
\ No newline at end of file
diff --git a/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js b/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js
index dd1346996c..96cb4de75f 100644
--- a/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js
+++ b/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js
@@ -1,2 +1,5 @@
+import("system.logging");
 import("DuplicateScanner_lib");
-DuplicateScannerUtils.ScanForDuplicates("PersonenDub", "");
\ No newline at end of file
+
+var duplicates = DuplicateScannerUtils.ScanForDuplicates("FilterName", "Person_entity", "", ["PERSONID", "LASTNAME"]);
+logging.log("duplicates -> " + duplicates);
\ No newline at end of file
diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js
index ffc05eb37c..2bb7a2da1a 100644
--- a/process/DuplicateScanner_lib/process.js
+++ b/process/DuplicateScanner_lib/process.js
@@ -10,55 +10,89 @@ import("system.entities");
  */
 function DuplicateScannerUtils() {}
 
-DuplicateScannerUtils.ScanForDuplicates = function(pFilterName, pFilterValues)
+DuplicateScannerUtils.ScanForDuplicates = function(pFilterName, pTargetEntity,  pFilterValues, pTargetEntityResultFields)
 {
-    let configuredFilters = _DuplicateScannerUtils._loadFiltersByName(pFilterName);
+    let configuredFilters = _DuplicateScannerUtils._loadFilters(pFilterName, pTargetEntity);
     
-    logging.log("configuredFilters filter -> " + configuredFilters[0][0]);
-    logging.log("configuredFilters COUNT_CHARACTERS_TO_USE -> " + configuredFilters[0][1]);
-    logging.log("configuredFilters EXTERNAL_SERVICE_USAGE_ALLOWED -> " + configuredFilters[0][2]);
-    
-    let preFilteredContacts = _DuplicateScannerUtils._applyPreFilter(configuredFilters);
+    logging.log("configuredFilters filter -> " + configuredFilters);
+    logging.log("configuredFilters 0 -> " + configuredFilters[0]);
+
+
+    let possibleDuplicates = _DuplicateScannerUtils._applyPreFilter(pTargetEntity, configuredFilters, pTargetEntityResultFields);
     
 //    if(_DuplicateScannerUtils._isExternalScanServiceAvailable())
 //    {
 //        
 //    }
+    
+    return possibleDuplicates;
 }
 
 
 function _DuplicateScannerUtils() {}
 
-_DuplicateScannerUtils._applyPreFilter = function(pTargetEntity, pFilterCountCharactersToUseRay)
+_DuplicateScannerUtils._applyPreFilter = function(pTargetEntity, pFilterCountCharactersToUseRay, pTargetEntityResultFields)
 {
-    var maxResultsTreshold = 3;
-    
+    var maxResultsTreshold = 4;
+    var combinedFilter = {};
     logging.log("pFilterCountCharactersToUseRay.length -> " + pFilterCountCharactersToUseRay.length);
-    //entities.createConfigForLoadingRows().filter()
+
     for (i = 0; i < pFilterCountCharactersToUseRay.length; i++) 
     {
         let filter = pFilterCountCharactersToUseRay[i][0];
         let countCharsOfValueToUse = pFilterCountCharactersToUseRay[i][1];
         
+        if(filter == null || filter == "")
+            continue;
+        
+        filter = JSON.parse(filter).filter;
+        
+        logging.log("filter -> " + JSON.stringify(filter));
+        logging.log("filter.childs -> " + JSON.stringify(filter.childs));
+        logging.log("filter.childs count -> " + filter.childs.length);
+        
+        if(i == 0)
+            combinedFilter = filter;
+        else
+        {
+            //Extend the current combined filter with the next filter condition to further refine the results
+            logging.log("aktueller combinedFilter -> " + JSON.stringify(combinedFilter));
+            combinedFilter.childs.push(filter.childs[0]);
+            logging.log("filter.childs hinzufügen in combinedFilter -> " + JSON.stringify(filter.childs));
+        }
+        
+        logging.log("combinedFilter -> " + JSON.stringify(combinedFilter));
+        
         //todo insert value into filter
         
         let loadRowsConfig = entities.createConfigForLoadingRows()
                              .entity(pTargetEntity)
-                             .filter(filter);
+                             .filter(JSON.stringify(combinedFilter));
                              
         let rowCount = entities.getRowCount(loadRowsConfig);                 
         
-        if(rowCount <= maxResultsTreshold && rowCount > 0)
+        logging.log("rowCount -> " + rowCount);
+        
+        if(rowCount > maxResultsTreshold)
         {
-            return entities.getRows(loadRowsConfig)
-            //we're in the treshold, return the data
+            logging.log("zu viele rows gefundenn nächsten Filter anwenden -> rowCount:" + rowCount + " maxResultsTreshold:" + maxResultsTreshold);
+            //Found more rows than allowed by the threshold, run next filter to narrow the results
+            continue;
         }
         else if(rowCount <= 0)
         {
-            //no rows found, probably use the current combined filter or return nothing?
+            return [];
+        }
+        else
+        {
+            //we're in the treshold, return the data
+            
+            loadRowsConfig = loadRowsConfig.fields(pTargetEntityResultFields);
+            
+            let resultRows = entities.getRows(loadRowsConfig);
+            logging.log("foundRows -> " + JSON.stringify(resultRows));
+            return resultRows;
         }
-
-
     }
 }
 
@@ -73,12 +107,14 @@ _DuplicateScannerUtils._callExternalScanService = function(pFormattedJsonValues)
     //todo load config and call ws
 }
 
-_DuplicateScannerUtils._loadFiltersByName = function(pFilterName)
+_DuplicateScannerUtils._loadFilters = function(pFilterName, pTargetEntity)
 {
     //entities.createConfigForLoadingRows().entity("DuplicateScanConditionConfig_entity")..fields(["$field.CONDITION", "$field.CONDITION_CONFIG_NAME", "$field.COUNT_CHARACTERS_TO_USE"])
     let query = "select \"CONDITION\", COUNT_CHARACTERS_TO_USE, EXTERNAL_SERVICE_USAGE_ALLOWED from DUPLICATESCANCONDITIONCONFIG"
                 + " join DUPLICATESCANNER on DUPLICATESCANNER.ID = DUPLICATESCANCONDITIONCONFIG.DUPLICATESCAN_ID"
-                + " where FILTER_NAME = '" + pFilterName + "'";
+                + " where FILTER_NAME = '" + pFilterName + "'"
+            + " and ENTITY_TO_SCAN_NAME = '" + pTargetEntity + "'";
             
+    logging.log("_loadFilters query -> " + query);
     return db.table(query);
 }
\ No newline at end of file
-- 
GitLab