From 07283023b8019635303b2f97a442740399c74b22 Mon Sep 17 00:00:00 2001
From: "d.buechler" <d.buechler@adito.de>
Date: Thu, 31 Oct 2019 13:00:09 +0100
Subject: [PATCH] Temporarily added view to Organisation which calculates
 duplciates on each main-view-open and doesnt cache the results. Same as on
 Person

---
 .../Organisation_entity.aod                   | 106 +++++++++++++++---
 .../valueProcess.js                           |   2 +
 .../valueProcess.js                           |   3 +
 .../onlyshowcontactids_param/valueProcess.js  |  56 +++++++++
 .../onlyshowcontactids_param/valueProcess.js  |  16 ++-
 .../OrganisationMain_view.aod                 |   5 +
 6 files changed, 165 insertions(+), 23 deletions(-)
 create mode 100644 entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicateactionscontrol_param/valueProcess.js
 create mode 100644 entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicatecurrentcontactid_param/valueProcess.js
 create mode 100644 entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js

diff --git a/entity/Organisation_entity/Organisation_entity.aod b/entity/Organisation_entity/Organisation_entity.aod
index 404e434495..c31e2ab18b 100644
--- a/entity/Organisation_entity/Organisation_entity.aod
+++ b/entity/Organisation_entity/Organisation_entity.aod
@@ -192,6 +192,10 @@
           <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/communications/children/contactid_param/valueProcess.js</valueProcess>
           <expose v="false" />
         </entityParameter>
+        <entityParameter>
+          <name>ContactsMainCountry_param</name>
+          <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/communications/children/contactsmaincountry_param/valueProcess.js</valueProcess>
+        </entityParameter>
       </children>
     </entityConsumer>
     <entityConsumer>
@@ -1019,6 +1023,50 @@
         </entityActionField>
       </children>
     </entityActionGroup>
+    <entityField>
+      <name>STANDARD_COUNTRY</name>
+    </entityField>
+    <entityField>
+      <name>IndexCommunication</name>
+    </entityField>
+    <entityField>
+      <name>IndexAddress</name>
+    </entityField>
+    <entityField>
+      <name>IndexCountry</name>
+    </entityField>
+    <entityField>
+      <name>IndexZIP</name>
+    </entityField>
+    <entityField>
+      <name>IndexCity</name>
+    </entityField>
+    <entityParameter>
+      <name>OnlyOwnSupervised_param</name>
+      <expose v="true" />
+    </entityParameter>
+    <entityConsumer>
+      <name>SelfDuplicatesUncached</name>
+      <dependency>
+        <name>dependency</name>
+        <entityName>Organisation_entity</entityName>
+        <fieldName>SelfDuplicatesProvider</fieldName>
+      </dependency>
+      <children>
+        <entityParameter>
+          <name>DuplicateActionsControl_param</name>
+          <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicateactionscontrol_param/valueProcess.js</valueProcess>
+        </entityParameter>
+        <entityParameter>
+          <name>DuplicateCurrentContactId_param</name>
+          <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicatecurrentcontactid_param/valueProcess.js</valueProcess>
+        </entityParameter>
+        <entityParameter>
+          <name>OnlyShowContactIds_param</name>
+          <valueProcess>%aditoprj%/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js</valueProcess>
+        </entityParameter>
+      </children>
+    </entityConsumer>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
@@ -1187,6 +1235,10 @@
           <isFilterable v="true" />
           <filtertype>BASIC</filtertype>
         </consumerMapping>
+        <dbRecordFieldMapping>
+          <name>STANDARD_COUNTRY.value</name>
+          <recordfield>ADDRESS.COUNTRY</recordfield>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
       <filterExtensions>
         <filterExtensionSet>
@@ -1210,11 +1262,6 @@
         <element>ORGANISATION</element>
       </affectedTables>
       <affectedIds>%aditoprj%/entity/Organisation_entity/recordcontainers/index/affectedIds.js</affectedIds>
-      <includedConsumers>
-        <element>Addresses</element>
-        <element>Communications</element>
-      </includedConsumers>
-      <affectedConsumerIds>%aditoprj%/entity/Organisation_entity/recordcontainers/index/affectedConsumerIds.js</affectedConsumerIds>
       <indexFieldMappings>
         <indexRecordFieldMapping>
           <name>NAME.value</name>
@@ -1226,28 +1273,53 @@
           <indexFieldType>STRING</indexFieldType>
         </indexRecordFieldMapping>
         <indexRecordFieldMapping>
-          <name>STANDARD_EMAIL_COMMUNICATION.value</name>
-          <indexFieldType>EMAIL</indexFieldType>
+          <name>CONTACTID.value</name>
+          <indexFieldType>STRING</indexFieldType>
+        </indexRecordFieldMapping>
+        <indexRecordFieldMapping>
+          <name>CUSTOMERCODE.value</name>
+          <indexFieldType>TEXT_PLAIN</indexFieldType>
+          <additionalFieldNameAliases />
+        </indexRecordFieldMapping>
+        <indexRecordFieldMapping>
+          <name>IndexAddress.value</name>
+          <indexFieldType>ADDRESS</indexFieldType>
           <additionalFieldNameAliases>
-            <element>addr</element>
+            <element>address</element>
           </additionalFieldNameAliases>
-          <isStored v="true" />
+          <isMultiValued v="true" />
         </indexRecordFieldMapping>
         <indexRecordFieldMapping>
-          <name>STANDARD_PHONE_COMMUNICATION.value</name>
-          <indexFieldType>TELEPHONE</indexFieldType>
+          <name>IndexCity.value</name>
+          <indexFieldType>ADDRESS</indexFieldType>
           <additionalFieldNameAliases>
-            <element>addr</element>
+            <element>city</element>
           </additionalFieldNameAliases>
+          <isMultiValued v="true" />
         </indexRecordFieldMapping>
         <indexRecordFieldMapping>
-          <name>CONTACTID.value</name>
-          <indexFieldType>STRING</indexFieldType>
+          <name>IndexCommunication.value</name>
+          <indexFieldType>COMMUNICATION</indexFieldType>
+          <additionalFieldNameAliases>
+            <element>comm</element>
+          </additionalFieldNameAliases>
+          <isMultiValued v="true" />
         </indexRecordFieldMapping>
         <indexRecordFieldMapping>
-          <name>CUSTOMERCODE.value</name>
-          <indexFieldType>TEXT_PLAIN</indexFieldType>
-          <additionalFieldNameAliases />
+          <name>IndexCountry.value</name>
+          <indexFieldType>COMMUNICATION</indexFieldType>
+          <additionalFieldNameAliases>
+            <element>country</element>
+          </additionalFieldNameAliases>
+          <isMultiValued v="true" />
+        </indexRecordFieldMapping>
+        <indexRecordFieldMapping>
+          <name>IndexZIP.value</name>
+          <indexFieldType>ADDRESS</indexFieldType>
+          <additionalFieldNameAliases>
+            <element>zip</element>
+          </additionalFieldNameAliases>
+          <isMultiValued v="true" />
         </indexRecordFieldMapping>
       </indexFieldMappings>
     </indexRecordContainer>
diff --git a/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicateactionscontrol_param/valueProcess.js b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicateactionscontrol_param/valueProcess.js
new file mode 100644
index 0000000000..81570217c3
--- /dev/null
+++ b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicateactionscontrol_param/valueProcess.js
@@ -0,0 +1,2 @@
+import("system.result");
+result.string("1");//todo use keyword
\ No newline at end of file
diff --git a/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicatecurrentcontactid_param/valueProcess.js b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicatecurrentcontactid_param/valueProcess.js
new file mode 100644
index 0000000000..821415ae69
--- /dev/null
+++ b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/duplicatecurrentcontactid_param/valueProcess.js
@@ -0,0 +1,3 @@
+import("system.vars");
+import("system.result");
+result.string(vars.get("$field.CONTACTID"));
\ 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
new file mode 100644
index 0000000000..0578e1b08a
--- /dev/null
+++ b/entity/Organisation_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
@@ -0,0 +1,56 @@
+import("system.project");
+import("system.indexsearch");
+import("system.logging");
+import("system.vars");
+import("DuplicateScanner_lib");
+import("system.result");
+
+let targetEntity = "Organisation_entity";
+let valuesToCheck = {};
+var entityModel = 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
+let fieldValue = "";
+let entityFields = [];
+for (fieldname in entityModel.fields) 
+{ 
+    field = entityModel.fields[fieldname]; 
+    if(field.fieldType == project.ENTITYFIELDTYPE_FIELD)
+    {
+        fieldValue = vars.get("$field." + field.name);
+        
+        if(fieldValue != null && fieldValue != "")
+        {
+//            logging.log("field.name -> " + field.name);
+            valuesToCheck[field.name] = fieldValue;
+//            logging.log("field value -> " + fieldValue);
+        }
+    }
+}
+
+let scanResults = DuplicateScannerUtils.ScanForDuplicates("OrganisationDuplicates", targetEntity,
+valuesToCheck, null);
+
+let duplicateIds = [];
+
+if(scanResults != null)
+{
+    //Run thru every duplicate result and read out the id. 
+    //Do it now to have a simple array on all usages lateron.
+    for (let i = 0; i < scanResults.length; i++)
+    {
+        let duplicateContactId = scanResults[i][indexsearch.FIELD_ID];
+        duplicateIds.push(duplicateContactId);
+    }
+}
+
+/*
+ * To achieve that if there are no duplicates, no contacts should be shown and therefore returned by the 
+ * recordcontainer, an invalid id gets returned. It then is used in the conditionProcess to load the duplicates.
+ * Because of its invalidity, no records are shown.
+*/
+if(duplicateIds.length == 0)
+    result.string(JSON.stringify(["nodata"]));
+else
+    result.string(JSON.stringify(duplicateIds));
\ 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 e6e230e458..476e86c009 100644
--- a/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
+++ b/entity/Person_entity/entityfields/selfduplicatesuncached/children/onlyshowcontactids_param/valueProcess.js
@@ -33,14 +33,18 @@ let scanResults = DuplicateScannerUtils.ScanForDuplicates("PersonDuplicates", ta
 valuesToCheck, null);
 
 let duplicateIds = [];
-//Run thru every duplicate result and read out the id. 
-//Do it now to have a simple array on all usages lateron.
-for (let i = 0; i < scanResults.length; i++)
+
+if(scanResults != null)
 {
-    let duplicateContactId = scanResults[i][indexsearch.FIELD_ID];
-    duplicateIds.push(duplicateContactId);
+    //Run thru every duplicate result and read out the id. 
+    //Do it now to have a simple array on all usages lateron.
+    for (let i = 0; i < scanResults.length; i++)
+    {
+        let duplicateContactId = scanResults[i][indexsearch.FIELD_ID];
+        duplicateIds.push(duplicateContactId);
+    }
 }
-logging.log("duplicateIds.length -> " + duplicateIds.length);
+
 /*
  * To achieve that if there are no duplicates, no contacts should be shown and therefore returned by the 
  * recordcontainer, an invalid id gets returned. It then is used in the conditionProcess to load the duplicates.
diff --git a/neonView/OrganisationMain_view/OrganisationMain_view.aod b/neonView/OrganisationMain_view/OrganisationMain_view.aod
index b7faebeb14..1acc8fcfd3 100644
--- a/neonView/OrganisationMain_view/OrganisationMain_view.aod
+++ b/neonView/OrganisationMain_view/OrganisationMain_view.aod
@@ -65,5 +65,10 @@
       <entityField>SelfDuplicatesConsumer</entityField>
       <view>OrganisationFilter_view</view>
     </neonViewReference>
+    <neonViewReference>
+      <name>c84abb91-0af3-42d0-825b-78ea4de324ea</name>
+      <entityField>SelfDuplicatesUncached</entityField>
+      <view>OrganisationFilter_view</view>
+    </neonViewReference>
   </children>
 </neonView>
-- 
GitLab