From 00f0cd1eb73030ef421e08dabf0d5b8baaba3e0d Mon Sep 17 00:00:00 2001
From: "d.buechler" <d.buechler@adito.de>
Date: Tue, 27 Aug 2019 11:36:56 +0200
Subject: [PATCH] =?UTF-8?q?RefreshUnrelatedDuplicateRelations:=20Diverse?=
 =?UTF-8?q?=20Fehler=20behoben.=20Die=20ClusterIds=20werden=20nun=20korrek?=
 =?UTF-8?q?t=20aktualisiert=20und=20veraltete=20gel=C3=B6scht.=20Org=20Dub?=
 =?UTF-8?q?letten=20werden=20nun=20in=20der=20Testaktion=20ermittelt?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../testduplicatescan/onActionProcess.js      | 34 +++++++++++++++----
 process/DuplicateScanner_lib/process.js       | 12 +++----
 2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js b/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js
index a7272d7517..96cb00f646 100644
--- a/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js
+++ b/entity/DuplicateScan_entity/entityfields/testactiongroup/children/testduplicatescan/onActionProcess.js
@@ -1,11 +1,7 @@
 import("system.logging");
 import("DuplicateScanner_lib");
 
-//var filterName = "PersonenDubletten";
-//var targetEntity = "Person_entity";
-//var values = {FIRSTNAME: "Anja", LASTNAME: "Lindner", GENDER: "f", CONTACTID: ""};
-//var resultFields = ["PERSONID", "LASTNAME", "FIRSTNAME"];
-//var duplicates = DuplicateScannerUtils.ScanForDuplicates(filterName, targetEntity, values, resultFields);
+
 
 var filterName = "PersonDuplicates";
 var targetEntity = "Person_entity";
@@ -14,7 +10,31 @@ var queryPersonContactIds = "select CONTACTID, FIRSTNAME, LASTNAME, GENDER from
                             + " join PERSON on PERSONID = PERSON_ID";
 var tmpFieldsInFilterRay = ["CONTACTID", "FIRSTNAME", "LASTNAME", "GENDER"];
 
-logging.log("in der action -> ");
+logging.log("Löschen von PERSON Dubletten -> ");
+DuplicateScannerUtils.DeleteDuplicateClustersByTargetEntity("Person_entity");
 
+logging.log("Neu berechnen von PERSON Dubletten -> ");
 DuplicateScannerUtils.RebuildDuplicatesCache(filterName, targetEntity, queryPersonContactIds,
-tmpFieldsInFilterRay, resultFieldsIdFieldName);
\ No newline at end of file
+tmpFieldsInFilterRay, resultFieldsIdFieldName);
+
+DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations(targetEntity);
+
+//##############################################################################
+
+filterName = "OrganisationDuplicates";
+targetEntity = "Organisation_entity";
+resultFieldsIdFieldName = "CONTACTID";
+queryPersonContactIds = "select CONTACTID, ORGANISATION.\"NAME\" from ORGANISATION"
+                            + " join CONTACT on CONTACT.CONTACTID = ORGANISATION.ORGANISATIONID"
+                            + " where CONTACTID != '0'";
+tmpFieldsInFilterRay = ["CONTACTID", "NAME"];
+
+
+logging.log("Löschen von ORGANISATION Dubletten -> ");
+DuplicateScannerUtils.DeleteDuplicateClustersByTargetEntity(targetEntity)
+
+logging.log("Neu berechnen von ORGANISATION Dubletten -> ");
+DuplicateScannerUtils.RebuildDuplicatesCache(filterName, targetEntity, queryPersonContactIds,
+tmpFieldsInFilterRay, resultFieldsIdFieldName);
+
+DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations(targetEntity);
\ No newline at end of file
diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js
index e2deb7a14a..955b9f2e5e 100644
--- a/process/DuplicateScanner_lib/process.js
+++ b/process/DuplicateScanner_lib/process.js
@@ -85,7 +85,6 @@ DuplicateScannerUtils.DeleteDuplicateClustersByTargetEntity = function(pTargetEn
  * Afterwards, all records which contain a nonexistend clusterId are being deleted
  * 
  * @param {String} pTargetEntity Name of Entity whose duplicates should be updated
- * 
  */
 DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations = function(pTargetEntity)
 {
@@ -102,11 +101,12 @@ DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations = function(pTargetEntit
             
     let newIdOldIdRay = db.table(query);
     let updateStatements = [];
-    
+
+    //Build update statements to set new clusterIds
     for (let i = 0; i < newIdOldIdRay.length; i++) 
     {
-        let newClusterId = newIdOldIdRay[INDEX_NEW_CLUSTERID];
-        let oldClusterId = newIdOldIdRay[INDEX_OLD_CLUSTERID];
+        let newClusterId = newIdOldIdRay[i][INDEX_NEW_CLUSTERID];
+        let oldClusterId = newIdOldIdRay[i][INDEX_OLD_CLUSTERID];
         let updateColumns = ["CLUSTERID"];
         let condition = "UNRELATEDDUPLICATES.CLUSTERID = '" + oldClusterId + "'";
         
@@ -114,16 +114,14 @@ DuplicateScannerUtils.RefreshUnrelatedDuplicateRelations = function(pTargetEntit
         
         updateStatements.push(updateStatement);
     }
-    
     db.updates(updateStatements);
     
     /* 
      * All unrelated duplicate ids that still exist in a cluster, have been updated with the new cluster id.
      * All records with a nonexistend clusterid can now be deleted because they haven't been detected as a duplicate any more.
      */
-    let deleteCondition = "CLUSTERID not in (select dc1.CLUSTERID from DUPLICATECLUSTERS dc1 where dc1.TARGET_ENTITY = '" + pTargetEntity + "')";
+    let deleteCondition = "CLUSTERID not in (select dc1.CLUSTERID from DUPLICATECLUSTERS dc1)";
     db.deleteData("UNRELATEDDUPLICATES", deleteCondition);
-
 }
 
 DuplicateScannerUtils.CreateUnrelatedDuplicateRelation = function(pSourceContactId, pUnrelatedContactId, pClusterId)
-- 
GitLab