From 7f9cafbcde6b6e4910c114fa9285c5b20a58f18e Mon Sep 17 00:00:00 2001
From: Sebastian Listl <s.listl@adito.de>
Date: Thu, 26 Aug 2021 17:03:32 +0200
Subject: [PATCH] 1083903 importer standard communications

---
 .../ImporterMappingFunctions_lib/process.js   | 80 ++++++++++++++-----
 1 file changed, 60 insertions(+), 20 deletions(-)

diff --git a/process/ImporterMappingFunctions_lib/process.js b/process/ImporterMappingFunctions_lib/process.js
index ed103e907b..97f5f2f5fa 100644
--- a/process/ImporterMappingFunctions_lib/process.js
+++ b/process/ImporterMappingFunctions_lib/process.js
@@ -1,3 +1,5 @@
+import("Keyword_lib");
+import("Communication_lib");
 import("system.fileIO");
 import("system.SQLTYPES");
 import("system.text");
@@ -480,41 +482,79 @@ function iKeywordAttribute (pObject)
  * @return {Boolean} true
  * */
 
-function iComm(pObject) {
-    if (! this.doIfCheck(pObject)) return true;
+function iComm (pObject) 
+{
+    if (!this.doIfCheck(pObject)) 
+        return true;
     var address = this.InputRecord[pObject.Address];
     var medium = this.InputRecord[pObject.Medium];
     var contact = this.InputRecord[pObject.ContactID];
     var standard = "0";
     
-    if(address == undefined) address = this.resolveSymbol(pObject, pObject.Address);
-    if(medium == undefined) medium = this.resolveSymbol(pObject, pObject.Medium);
-    if(contact == undefined) contact = this.resolveSymbol(pObject, pObject.ContactID);
-    if(pObject.Standard) standard = "1";
+    if (address == undefined) 
+        address = this.resolveSymbol(pObject, pObject.Address);
+    if (medium == undefined) 
+        medium = this.resolveSymbol(pObject, pObject.Medium);
+    if (contact == undefined) 
+        contact = this.resolveSymbol(pObject, pObject.ContactID);
+    if (pObject.Standard) 
+        standard = "1";
     
-    if(!address || !medium || !contact) return true;
+    if (!address || !medium || !contact) 
+        return true;
+    
+    const COMMUNICATION = this.getTableCase("communication");
+    const COMMUNICATIONID = this.getColumnCase("communicationid");
+    const OBJECT_ROWID = this.getColumnCase("object_rowid");
+    const OBJECT_TYPE = this.getColumnCase("object_type");
+    const ISSTANDARD = this.getColumnCase("isstandard");
+    const MEDIUM_ID = this.getColumnCase("medium_id");
+    const ADDR = this.getColumnCase("addr");
     
+    var keywordAttr = new KeywordAttribute($KeywordRegistry.communicationMedium(), "category");
+    mediumCategory = new SqlBuilder(this.Config.AliasTo)
+        .select("AB_KEYWORD_ATTRIBUTERELATION." + keywordAttr.dbField)
+        .from("AB_KEYWORD_ATTRIBUTERELATION")
+        .join("AB_KEYWORD_ENTRY", "AB_KEYWORD_ATTRIBUTERELATION.AB_KEYWORD_ENTRY_ID = AB_KEYWORD_ENTRY.AB_KEYWORD_ENTRYID")
+        .where("AB_KEYWORD_ATTRIBUTERELATION.AB_KEYWORD_ATTRIBUTE_ID", keywordAttr.id)
+        .and("AB_KEYWORD_ENTRY.KEYID", medium)
+        .cell();
     
-    var sql = "select " + this.getColumnCase("communicationid") + ", " + this.getColumnCase("addr") + " from " + this.getTableCase("communication")
-        +" where " + this.getColumnCase("object_rowid") + " = ? and " + this.getColumnCase("medium_id") + " = ? and "
-        + this.getColumnCase("isstandard") + " = ? and " + this.getColumnCase("object_type") + " = ?";
-    var existingData = db.array(db.ROW, [sql, [[contact, SQLTYPES.CHAR], [medium, SQLTYPES.CHAR], //TODO: define types dynamically and not hardcoded
-        [standard, SQLTYPES.SMALLINT], ["Contact", SQLTYPES.CHAR]]], this.Config.AliasTo, 0, this.getConfiguredTimeout());
+    var sql = new SqlBuilder(this.Config.AliasTo)
+        .select([COMMUNICATIONID, ADDR, MEDIUM_ID, ISSTANDARD])
+        .from(COMMUNICATION)
+        .where(COMMUNICATION + "." + OBJECT_ROWID, contact)
+        .and(COMMUNICATION + "." + OBJECT_TYPE, "Contact")
+        .and(COMMUNICATION + "." + MEDIUM_ID, CommUtil.getMediumIdsByCategory(mediumCategory), SqlBuilder.IN());
+        
+    var existingData = sql.table(null, null, this.getConfiguredTimeout());
+    var hasStandardAddr = existingData.some(function (commData)
+    {
+        return commData[3] == "1"; //check if there is already a standard address for the medium category
+    });
+    if (hasStandardAddr)
+    {
+        standard = "0";
+    }
+    
+    var existingComm = existingData.find(function (commData)
+    {
+        return commData[2] == medium; //check if communication with the same medium exists
+    });
         
-    if (existingData.length == 0)
+    if (!existingComm)
     {
-        var columns = [this.getColumnCase("communicationid"), this.getColumnCase("addr"), 
-                this.getColumnCase("medium_id"), this.getColumnCase("object_rowid"), this.getColumnCase("isstandard")];
-        this.insertData(this.getTableCase("communication"), columns, null, [util.getNewUUID(), address, medium, contact, standard], this.Config.AliasTo);       
+        var columns = [COMMUNICATIONID, ADDR, MEDIUM_ID, OBJECT_ROWID, ISSTANDARD];
+        this.insertData(COMMUNICATION, columns, null, [util.getNewUUID(), address, medium, contact, standard], this.Config.AliasTo);       
     }
     else
     {
-        var existingId = existingData[0];
-        var existingAddress = existingData[1];
+        var existingId = existingComm[0];
+        var existingAddress = existingComm[1];
         if (address != existingAddress)
         {
-            var cond = this.getColumnCase("communicationid") + " = '" + existingId + "'";
-            this.updateData(this.getTableCase("communication"), [this.getColumnCase("addr")], null, [address], cond, this.Config.AliasTo);
+            var cond = COMMUNICATIONID + " = '" + existingId + "'";
+            this.updateData(COMMUNICATION, [ADDR], null, [address], cond, this.Config.AliasTo);
         }
     }
     return true;
-- 
GitLab