From cf7d0a0ce05478e124f2a5aef93f5585e0dfe1de Mon Sep 17 00:00:00 2001
From: "d.buechler" <d.buechler@adito.de>
Date: Tue, 3 Dec 2019 10:07:59 +0100
Subject: [PATCH] In the selfDuplicatesUncached consumer the parameter
 "ExcludedContactIds" is now filled.  All relations defined as "unrelated" are
 now excluded. This is because the duplicate list is now a live view instead
 of the cached one.

---
 .../Organisation_entity.aod                   |  4 +++
 .../excludedcontactids_param/valueProcess.js  |  7 ++++
 .../onlyshowcontactids_param/valueProcess.js  |  5 ++-
 entity/Person_entity/Person_entity.aod        |  4 +++
 .../excludedcontactids_param/valueProcess.js  |  7 ++++
 .../onlyshowcontactids_param/valueProcess.js  |  4 +--
 process/DuplicateScanner_lib/process.js       | 36 ++++++++++++++++++-
 7 files changed, 61 insertions(+), 6 deletions(-)
 create mode 100644 entity/Organisation_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js
 create mode 100644 entity/Person_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js

diff --git a/entity/Organisation_entity/Organisation_entity.aod b/entity/Organisation_entity/Organisation_entity.aod
index 76cce88c02..3636e907bd 100644
--- a/entity/Organisation_entity/Organisation_entity.aod
+++ b/entity/Organisation_entity/Organisation_entity.aod
@@ -1010,6 +1010,10 @@
           <name>OnlyShowContactIds_param</name>
           <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js</valueProcess>
         </entityParameter>
+        <entityParameter>
+          <name>ExcludedContactIds_param</name>
+          <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js</valueProcess>
+        </entityParameter>
       </children>
     </entityConsumer>
     <entityConsumer>
diff --git a/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js
new file mode 100644
index 0000000000..30b7732937
--- /dev/null
+++ b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.logging");
+import("system.vars");
+import("system.result");
+import("DuplicateScanner_lib");
+
+let unrelatedIds = DuplicateScannerUtils.GetUnrelatedRelationsForDuplicate(vars.get("$field.CONTACTID"));
+result.string(JSON.stringify(unrelatedIds));
\ No newline at end of file
diff --git a/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
index 4024668e40..fb1fb5a8a3 100644
--- a/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
+++ b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
@@ -9,7 +9,6 @@ let scannerName = "OrganisationDuplicates";
 let targetEntity = "Organisation_entity";
 let valuesToCheck = {};
 var entityFieldsToLoad = DuplicateScannerUtils.GetEntityFieldsFromConfig(scannerName, targetEntity);
-//project.getEntityStructure(targetEntity);
 
  //Read the values of all available entity fields and write the fieldname7value combination
  //as key/value pairs into an object. This is used to trigger the scan for duplicates
@@ -18,11 +17,11 @@ let entityFields = [];
 for (fieldname in entityFieldsToLoad) 
 { 
     field = entityFieldsToLoad[fieldname]; 
-    fieldValue = vars.get("$field." + field.name);
+    fieldValue = vars.get("$field." + field);
 
     if(fieldValue != null && fieldValue != "")
     {
-        valuesToCheck[field.name] = fieldValue;
+        valuesToCheck[field] = fieldValue;
     }
 }
 
diff --git a/entity/Person_entity/Person_entity.aod b/entity/Person_entity/Person_entity.aod
index 5796e1ce02..1add27c57b 100644
--- a/entity/Person_entity/Person_entity.aod
+++ b/entity/Person_entity/Person_entity.aod
@@ -1077,6 +1077,10 @@ Usually this is used for filtering COMMUNICATION-entries by a specified contact
           <name>OnlyShowContactIds_param</name>
           <valueProcess>%aditoprj%/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js</valueProcess>
         </entityParameter>
+        <entityParameter>
+          <name>ExcludedContactIds_param</name>
+          <valueProcess>%aditoprj%/entity/Person_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js</valueProcess>
+        </entityParameter>
       </children>
     </entityConsumer>
     <entityField>
diff --git a/entity/Person_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js b/entity/Person_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js
new file mode 100644
index 0000000000..30b7732937
--- /dev/null
+++ b/entity/Person_entity/entityfields/selfduplicatesuncached/children/excludedcontactids_param/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.logging");
+import("system.vars");
+import("system.result");
+import("DuplicateScanner_lib");
+
+let unrelatedIds = DuplicateScannerUtils.GetUnrelatedRelationsForDuplicate(vars.get("$field.CONTACTID"));
+result.string(JSON.stringify(unrelatedIds));
\ No newline at end of file
diff --git a/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js b/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
index b352b88a57..eaa45a32c9 100644
--- a/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
+++ b/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
@@ -18,11 +18,11 @@ let entityFields = [];
 for (fieldname in entityFieldsToLoad) 
 { 
     field = entityFieldsToLoad[fieldname]; 
-    fieldValue = vars.get("$field." + field.name);
+    fieldValue = vars.get("$field." + field);
 
     if(fieldValue != null && fieldValue != "")
     {
-        valuesToCheck[field.name] = fieldValue;
+        valuesToCheck[field] = fieldValue;
     }
 }
 
diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js
index 190b3733aa..6e10377791 100644
--- a/process/DuplicateScanner_lib/process.js
+++ b/process/DuplicateScanner_lib/process.js
@@ -775,7 +775,41 @@ DuplicateScannerUtils.GetEntityFieldsFromConfig = function(pFilterName, pTargetE
 {
     let indexPattern = _DuplicateScannerUtils._loadIndexPattern(pFilterName, pTargetEntity);
     let fieldConfigs = _DuplicateScannerUtils._loadEntityFieldConfigsFromPattern(indexPattern);
-    return _DuplicateScannerUtils._loadEntityFieldsFromFieldConfigs(fieldConfigs);
+    let entityFields = _DuplicateScannerUtils._loadEntityFieldsFromFieldConfigs(fieldConfigs);
+    let entityIdField = _DuplicateScannerUtils._loadEntityIdField(pFilterName, pTargetEntity);
+    entityFields.push(entityIdField);
+    return entityFields;
+}
+
+DuplicateScannerUtils.GetUnrelatedRelationsForDuplicate = function(pDuplicateId)
+{
+    let unrelatedIds = [];
+    let duplicateIds = newSelect("SOURCEDUPLICATEID, UNRELATEDDUPLICATEID")
+                .from("UNRELATEDDUPLICATES")
+                .where("UNRELATEDDUPLICATES.SOURCEDUPLICATEID", pDuplicateId)
+                .or("UNRELATEDDUPLICATES.UNRELATEDDUPLICATEID", pDuplicateId)
+                .table();
+    
+    let sourceDuplicateId = "";
+    let unrelatedDuplicateId = "";
+    for (let i = 0; i < duplicateIds.length; i++) 
+    {
+        sourceDuplicateId = duplicateIds[0];
+        unrelatedDuplicateId = duplicateIds[1];
+        
+        if(sourceDuplicateId == pDuplicateId || unrelatedDuplicateId == pDuplicateId)
+            continue;
+        
+        if(sourceDuplicateId != null && sourceDuplicateId != "")
+        {
+            unrelatedIds.push(sourceDuplicateId);
+        }
+        else if(unrelatedDuplicateId != null && unrelatedDuplicateId != "")
+        {
+            unrelatedIds.push(unrelatedDuplicateId);
+        }
+    }
+    return unrelatedIds;
 }
 
 function _DuplicateScannerUtils() {}
-- 
GitLab