From a9b53c185ede4e4ca9d52911a231640a3b6ee1f1 Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Fri, 2 Oct 2020 11:51:13 +0200
Subject: [PATCH] =?UTF-8?q?[Projekt:=20Entwicklung=20-=20Neon][TicketNr.:?=
 =?UTF-8?q?=201066467][Fehler=20Importer=5Flib=20f=C3=BCr=20iAttribute=20u?=
 =?UTF-8?q?nd=20iComm]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../ImporterMappingFunctions_lib/process.js   | 57 +++++++++++++------
 1 file changed, 40 insertions(+), 17 deletions(-)

diff --git a/process/ImporterMappingFunctions_lib/process.js b/process/ImporterMappingFunctions_lib/process.js
index 27a4b5725f..a78f54d956 100644
--- a/process/ImporterMappingFunctions_lib/process.js
+++ b/process/ImporterMappingFunctions_lib/process.js
@@ -184,19 +184,29 @@ function iAttribute(pObject)
         
         if (value && objectId) 
         {
-            id = newSelect(ab_attributeRelationId, alias).from(ab_attributeRelation).where(attrRel_ab_attribute_id, attributeId)
-                    .and(object_rowId, objectId).and(attrRel_object_type, objectType).cell();
-            
-            if (id == "" || id == null) 
+            var exisingAttrRelation = newSelect([ab_attributeRelationId, valueColumn], alias)
+                .from(ab_attributeRelation)
+                .where(attrRel_ab_attribute_id, attributeId)
+                .and(object_rowId, objectId)
+                .and(attrRel_object_type, objectType)
+                .arrayRow();
+             
+            if (exisingAttrRelation.length == 0)
             {
                 columns = [ab_attributeRelationId, attrRel_ab_attribute_id, attrRel_object_type, object_rowId, valueColumn];
                 values = [util.getNewUUID(), attributeId, objectType, objectId, value];
                 this.insertData(ab_attributeRelation, columns, null, values, alias);
-            } 
-            else if (this.Config.ImportCommand.indexOf("update") != -1)
+            }
+            else
             {
-                cond = ab_attributeRelationId + " = '" + id + "'";
-                this.updateData(ab_attributeRelation, [valueColumn], null, [value], cond, alias);
+                var existingValue;
+                [id, existingValue] = exisingAttrRelation;
+                //new value has to differ from the old (existing) value to prevent unneccesary updates
+                 if (this.Config.ImportCommand.indexOf("update") != -1 && value != null && value.toString() != existingValue)
+                {
+                    cond = ab_attributeRelationId + " = '" + id + "'";
+                    this.updateData(ab_attributeRelation, [valueColumn], null, [value], cond, alias);
+                }
             }
         }
     }    
@@ -358,23 +368,36 @@ function iComm(pObject) {
     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(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;
     
-    var sql = "select " + this.getColumnCase("communicationid") + " from " + this.getTableCase("communication")
+    
+    var sql = "select " + this.getColumnCase("communicationid") + ", " + this.getColumnCase("addr") + " from " + this.getTableCase("communication")
         +" where " + this.getColumnCase("contact_id") + " = ? and " + this.getColumnCase("medium_id") + " = ? and "
-        + this.getColumnCase("isstandard") + " = ? and " + this.getColumnCase("addr") + " = ?"
-    var id = db.cell([sql, [[contact, SQLTYPES.CHAR], [medium, SQLTYPES.CHAR], //TODO: define types dynamically and not hardcoded
-        [standard, SQLTYPES.SMALLINT], [address, SQLTYPES.VARCHAR]]], this.Config.AliasTo);
-    if (id == "" || id == null) {
+        + this.getColumnCase("isstandard") + " = ?";
+    var existingData = db.array(db.ROW, [sql, [[contact, SQLTYPES.CHAR], [medium, SQLTYPES.CHAR], //TODO: define types dynamically and not hardcoded
+        [standard, SQLTYPES.SMALLINT]]], this.Config.AliasTo, 0, this.getConfiguredTimeout());
+        
+    if (existingData.length == 0)
+    {
         var columns = [this.getColumnCase("communicationid"), this.getColumnCase("addr"), 
-            this.getColumnCase("medium_id"), this.getColumnCase("contact_id"), this.getColumnCase("isstandard")];
+                this.getColumnCase("medium_id"), this.getColumnCase("contact_id"), this.getColumnCase("isstandard")];
         this.insertData(this.getTableCase("communication"), columns, null, [util.getNewUUID(), address, medium, contact, standard], this.Config.AliasTo);       
-    }    
+    }
+    else
+    {
+        var existingId = existingData[0];
+        var existingAddress = existingData[1];
+        if (address != existingAddress)
+        {
+            var cond = this.getColumnCase("communicationid") + " = '" + existingId + "'";
+            this.updateData(this.getTableCase("communication"), [this.getColumnCase("addr")], null, [address], cond, this.Config.AliasTo);
+        }
+    }
     return true;
 }
 
-- 
GitLab