From d4450dad97ab55be65c7cbc553e9c01482ef7683 Mon Sep 17 00:00:00 2001
From: "d.buechler" <d.buechler@adito.de>
Date: Tue, 20 Aug 2019 16:59:01 +0200
Subject: [PATCH] =?UTF-8?q?Personen=20Dubletten=20Cluster=20werden=20nun?=
 =?UTF-8?q?=20korrekt=20angezeigt.=20Dabei=20werden=20"ignorierte=20Datens?=
 =?UTF-8?q?=C3=A4tze"=20nicht=20in=20der=20Auflistung=20angezeigt.=20Clust?=
 =?UTF-8?q?er=20mit=20zwei=20Eintr=C3=A4gen,=20wovon=20einer=20"ignoriert"?=
 =?UTF-8?q?=20wird,=20werden=20ebenso=20ausgeblendet.=20Es=20wurde=20ein?=
 =?UTF-8?q?=20neues=20Entity=20zum=20Anzeigen=20der=20ignorierten=20Dublet?=
 =?UTF-8?q?ten=20angelegt.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../DuplicatesUnrelated_entity.aod            |  63 +++++++++++
 .../recordcontainer/contentProcess.js         | 106 +++++++++++-------
 .../DuplicatesUnrelated.aod                   |  16 +++
 ...icatesUnrelatedOrganisationFilter_view.aod |   5 +
 .../DuplicatesUnrelatedPersonFilter_view.aod  |  10 ++
 process/DuplicateScanner_lib/process.js       |   2 +-
 6 files changed, 160 insertions(+), 42 deletions(-)
 create mode 100644 entity/DuplicatesUnrelated_entity/DuplicatesUnrelated_entity.aod
 create mode 100644 neonContext/DuplicatesUnrelated/DuplicatesUnrelated.aod
 create mode 100644 neonView/DuplicatesUnrelatedOrganisationFilter_view/DuplicatesUnrelatedOrganisationFilter_view.aod
 create mode 100644 neonView/DuplicatesUnrelatedPersonFilter_view/DuplicatesUnrelatedPersonFilter_view.aod

diff --git a/entity/DuplicatesUnrelated_entity/DuplicatesUnrelated_entity.aod b/entity/DuplicatesUnrelated_entity/DuplicatesUnrelated_entity.aod
new file mode 100644
index 0000000000..77c9ac4403
--- /dev/null
+++ b/entity/DuplicatesUnrelated_entity/DuplicatesUnrelated_entity.aod
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entity xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.3.11" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/entity/1.3.11">
+  <name>DuplicatesUnrelated_entity</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+  <recordContainer>recordContainer</recordContainer>
+  <entityFields>
+    <entityProvider>
+      <name>#PROVIDER</name>
+    </entityProvider>
+    <entityProvider>
+      <name>UnrelatedPersonsProvider</name>
+    </entityProvider>
+    <entityParameter>
+      <name>TargetEntity</name>
+      <expose v="true" />
+      <mandatory v="true" />
+    </entityParameter>
+    <entityProvider>
+      <name>UnrelatedOrganisationsProvider</name>
+    </entityProvider>
+    <entityConsumer>
+      <name>UnrelatedPersonsConsumer</name>
+      <dependency>
+        <name>dependency</name>
+      </dependency>
+    </entityConsumer>
+    <entityConsumer>
+      <name>UnrelatedOrganisationsConsumer</name>
+      <dependency>
+        <name>dependency</name>
+      </dependency>
+    </entityConsumer>
+  </entityFields>
+  <recordContainers>
+    <dbRecordContainer>
+      <name>recordContainer</name>
+      <alias>Data_alias</alias>
+      <linkInformation>
+        <linkInformation>
+          <name>c5e79738-a374-4f09-8bc3-aafe5cd7f582</name>
+          <tableName>UNRELATEDDUPLICATES</tableName>
+          <primaryKey>ID</primaryKey>
+          <isUIDTable v="true" />
+          <readonly v="false" />
+        </linkInformation>
+        <linkInformation>
+          <name>214774c2-bf2f-460e-9931-5cdc27be1b5d</name>
+          <tableName>CONTACT</tableName>
+          <primaryKey>CONTACTID</primaryKey>
+          <isUIDTable v="false" />
+          <readonly v="false" />
+        </linkInformation>
+        <linkInformation>
+          <name>820d9ef7-c3fe-4a0b-9170-5526efccfbae</name>
+          <tableName>PERSON</tableName>
+          <primaryKey>PERSONID</primaryKey>
+          <isUIDTable v="false" />
+          <readonly v="false" />
+        </linkInformation>
+      </linkInformation>
+    </dbRecordContainer>
+  </recordContainers>
+</entity>
diff --git a/entity/Duplicates_entity/recordcontainers/recordcontainer/contentProcess.js b/entity/Duplicates_entity/recordcontainers/recordcontainer/contentProcess.js
index 3626ae33b7..2c215355e5 100644
--- a/entity/Duplicates_entity/recordcontainers/recordcontainer/contentProcess.js
+++ b/entity/Duplicates_entity/recordcontainers/recordcontainer/contentProcess.js
@@ -10,54 +10,78 @@ var INDEX_LASTNAME = 3;
 let targetEntity = vars.get("$param.TargetEntity");
 let duplicates = [];
 
+
+let duplicateInfosQuery = "";
+
 if(targetEntity == "Person_entity")
+    duplicateInfosQuery = "select DUPLICATECLUSTERS.ID, CLUSTERID, FIRSTNAME, LASTNAME"
+                        + " from DUPLICATECLUSTERS"
+                        + " join CONTACT on CONTACT.CONTACTID = DUPLICATEID"
+                        + " join PERSON on PERSON.PERSONID = CONTACT.PERSON_ID"
+                        + " where DUPLICATEID not in (select UNRELATEDDUPLICATES.UNRELATEDDUPLICATEID from UNRELATEDDUPLICATES)"
+                        + " ORDER BY CLUSTERID";
+else
+    duplicateInfosQuery = "orgquery";
+                    
+let duplicateInfos = db.table(duplicateInfosQuery);
+
+let recordClusterId = "";
+let recordDescription = "";
+let recordDuplicateInClusterCount = 0;
+
+for (let i = 0; i < duplicateInfos.length; i++) 
 {
-    //duplicates.push([123, "Description of person cluster", 4, "Person_entity"]);
+    let currentRecordId = duplicateInfos[i][INDEX_ID];
+    let currentClusterId = duplicateInfos[i][INDEX_CLUSTERID];
+    let currentDescription = "";
     
-    let personInfosQuery = "select DUPLICATECLUSTERS.ID, CLUSTERID, FIRSTNAME, LASTNAME"
-                            + " from DUPLICATECLUSTERS"
-                            + " join CONTACT on CONTACT.CONTACTID = DUPLICATEID"
-                            + " join PERSON on PERSON.PERSONID = CONTACT.PERSON_ID"
-                            + " ORDER BY CLUSTERID";
-    let personInfos = db.table(personInfosQuery);
+    //Build the description depending on the targetEntity
+    if(targetEntity == "Person_entity")
+        currentDescription = duplicateInfos[i][INDEX_FIRSTNAME] + " " + duplicateInfos[i][INDEX_LASTNAME];
+    else
+        currentDescription = "orgfields";
     
-    let recordClusterId = "";
-    let recordDescription = "";
-    let recordDuplicateInClusterCount = 0;
-    
-    for (let i = 0; i < personInfos.length; i++) 
+    if(i == 0)
     {
-        let currentClusterId = personInfos[i][INDEX_CLUSTERID];
-        let currentDescription = personInfos[i][INDEX_FIRSTNAME] + " " + personInfos[i][INDEX_LASTNAME];
-        
-        if(i == 0)
-        {
-            recordClusterId = currentClusterId;
-            recordDescription = currentDescription;
-            recordDuplicateInClusterCount = 1;
-            continue;
-        }
+        recordClusterId = currentClusterId;
+        recordDescription = currentDescription;
+        recordDuplicateInClusterCount = 1;
+        continue;
+    }
+    //If the record belongs to the same Cluster as the one before, append its value and increase the counter
+    //otherwise write the clusters record an start a new record.
+    if(recordClusterId == currentClusterId)
+    {
+        recordDescription += ", " + currentDescription;
+        recordDuplicateInClusterCount++;
         
-        if(recordClusterId == currentClusterId)
-        {
-            recordDescription += ", " + currentDescription;
-            recordDuplicateInClusterCount++;
-        }
-        else
-        {
-            let currentRecordId = personInfos[i][INDEX_ID];
-            duplicates.push([currentRecordId, recordDescription, recordDuplicateInClusterCount, "Person_entity"]);
-            
-            recordClusterId = currentClusterId
-            recordDescription = currentDescription
-            recordDuplicateInClusterCount = 1;
-        }
+        /*
+         * Finish the current record if its the last duplicate.
+         * It has to be checked wether or not more than one element is currently in the cluster:
+         * Normally, there are always at least 2 elements in a cluster. If then a duplicate relation
+         * is beign ignored, there's only one record left in this particluar cluster. 
+         * As there are then no interactions possible (and a cluster of one is no cluster), this cluster musn't be shown in the list.
+         */
+        if(i == duplicateInfos.length-1 && recordDuplicateInClusterCount > 1)
+            duplicates.push([currentRecordId, recordDescription, recordDuplicateInClusterCount, targetEntity]);
+    }
+    else
+    {
+        /*
+         * Finish the current record if its the next cluster.
+         * It has to be checked wether or not more than one element is currently in the cluster:
+         * Normally, there are always at least 2 elements in a cluster. If then a duplicate relation
+         * is beign ignored, there's only one record left in this particluar cluster. 
+         * As there would be no interactions possible (and a cluster of one is no cluster), this cluster musn't be shown in the list.
+         */
+        if(recordDuplicateInClusterCount > 1)
+            duplicates.push([currentRecordId, recordDescription, recordDuplicateInClusterCount, targetEntity]);
+
+        recordClusterId = currentClusterId
+        recordDescription = currentDescription
+        recordDuplicateInClusterCount = 1;
     }
-    
-}
-else if(targetEntity == "Organisation_entity")
-{
-    duplicates.push([124, "Description of org cluster", 2, "Organisation_entity"]);
 }
+    
 
 result.object(duplicates);
\ No newline at end of file
diff --git a/neonContext/DuplicatesUnrelated/DuplicatesUnrelated.aod b/neonContext/DuplicatesUnrelated/DuplicatesUnrelated.aod
new file mode 100644
index 0000000000..88f2b1a2c5
--- /dev/null
+++ b/neonContext/DuplicatesUnrelated/DuplicatesUnrelated.aod
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<neonContext xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.1.0" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/neonContext/1.1.0">
+  <name>DuplicatesUnrelated</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+  <entity>DuplicatesUnrelated_entity</entity>
+  <references>
+    <neonViewReference>
+      <name>0b5cbd31-cfa1-4ee8-8bd2-b4772ac95953</name>
+      <view>DuplicatesUnrelatedPersonFilter_view</view>
+    </neonViewReference>
+    <neonViewReference>
+      <name>433a665e-63ac-4281-8f7c-08603362daf7</name>
+      <view>DuplicatesUnrelatedOrganisationFilter_view</view>
+    </neonViewReference>
+  </references>
+</neonContext>
diff --git a/neonView/DuplicatesUnrelatedOrganisationFilter_view/DuplicatesUnrelatedOrganisationFilter_view.aod b/neonView/DuplicatesUnrelatedOrganisationFilter_view/DuplicatesUnrelatedOrganisationFilter_view.aod
new file mode 100644
index 0000000000..0962b89c7f
--- /dev/null
+++ b/neonView/DuplicatesUnrelatedOrganisationFilter_view/DuplicatesUnrelatedOrganisationFilter_view.aod
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<neonView xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.1.2" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/neonView/1.1.2">
+  <name>DuplicatesUnrelatedOrganisationFilter_view</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+</neonView>
diff --git a/neonView/DuplicatesUnrelatedPersonFilter_view/DuplicatesUnrelatedPersonFilter_view.aod b/neonView/DuplicatesUnrelatedPersonFilter_view/DuplicatesUnrelatedPersonFilter_view.aod
new file mode 100644
index 0000000000..324006b6d7
--- /dev/null
+++ b/neonView/DuplicatesUnrelatedPersonFilter_view/DuplicatesUnrelatedPersonFilter_view.aod
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<neonView xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.1.2" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/neonView/1.1.2">
+  <name>DuplicatesUnrelatedPersonFilter_view</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+  <layout>
+    <boxLayout>
+      <name>layout</name>
+    </boxLayout>
+  </layout>
+</neonView>
diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js
index 6b23e51adf..2665f5dd73 100644
--- a/process/DuplicateScanner_lib/process.js
+++ b/process/DuplicateScanner_lib/process.js
@@ -55,7 +55,7 @@ DuplicateScannerUtils.DeleteCachedDuplicate = function(pContactId)
     DuplicateScannerUtils.DeleteAllUnrelatedDuplicateRelations(pContactId);
 }
 
-DuplicateScannerUtils.CreateUnrelatedDuplicateRelation = function(dd, pUnrelatedContactId)
+DuplicateScannerUtils.CreateUnrelatedDuplicateRelation = function(pSourceContactId, pUnrelatedContactId)
 {
     let newUid = util.getNewUUID();
     let columns = ["ID", "SOURCEDUPLICATEID", "UNRELATEDDUPLICATEID"];
-- 
GitLab