diff --git a/process/ImporterMappingFunctions_lib/process.js b/process/ImporterMappingFunctions_lib/process.js index 27a4b5725fbfc5be68e9d42c5a498c3d132d8a25..a78f54d9563d23ddd9e2a508c57ff6ca9c7a9806 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; }