diff --git a/process/ImporterMappingFunctions_lib/process.js b/process/ImporterMappingFunctions_lib/process.js index a7c2ddaf30f6f6b136c938af767e55cb40c67334..f267488b4e2ada4f55de7738ff4239cccd152c7d 100644 --- a/process/ImporterMappingFunctions_lib/process.js +++ b/process/ImporterMappingFunctions_lib/process.js @@ -61,116 +61,142 @@ function iKeyword(pObject) { /* * Values of the mapping line: - * Attribute req -- the new attribute name - * AType req -- the type of the attribute - * OType opt -- the type of the object (AB_ATTRIBUTEUSAGE) - * OID opt -- the row id for the object instance (AB_ATTRIBUTERELATION) - * Value opt -- the value for the object instance (AB_ATTRIBUTERELATION) + * attribute (required): The new attribute name. + * attributeType (required): The type of the attribute e.g 'GROUP'. + * objectType (optional): The type of the object (AB_ATTRIBUTEUSAGE). + * objectId (optional): The OBJECT_ROWID for the object instance (AB_ATTRIBUTERELATION). + * value (optional): The value for the object instance (AB_ATTRIBUTERELATION). * * @name iAttribute * @param {Object} pObject req the mapping line * @return {Boolean} true * */ -function iAttribute(pObject) { + +function iAttribute(pObject) +{ if (!this.doIfCheck(pObject)) return true; - var attribute = this.InputRecord[pObject.Attribute]; - if(attribute == undefined) attribute = this.resolveSymbol(pObject, pObject.Attribute); - var atype = this.InputRecord[pObject.AType]; - if(atype == undefined) atype = this.resolveSymbol(pObject, pObject.AType); - var otype = this.InputRecord[pObject.OType]; - if(otype == undefined) otype = this.resolveSymbol(pObject, pObject.OType); - var oid = this.InputRecord[pObject.OID]; - if(oid == undefined) oid = this.resolveSymbol(pObject, pObject.OID); - var value = this.InputRecord[pObject.Value]; - if(value == undefined) value = this.resolveSymbol(pObject, pObject.Value); + var attribute = this.InputRecord[pObject.attribute]; + var attributeType = this.InputRecord[pObject.attributeType]; + var objectType = this.InputRecord[pObject.objectType]; + var objectId = this.InputRecord[pObject.objectId]; + var value = this.InputRecord[pObject.value]; + + var alias = this.Config.AliasTo; + var funcBuffer = this.FuncBuffer.iAttribute; + var ab_attribute = this.getTableCase("ab_attribute"); + var ab_attributeId = this.getColumnCase("ab_attribute.ab_attributeid"); + var attribute_parent_id = this.getColumnCase("ab_attribute.attribute_parent_id"); + var attribute_name = this.getColumnCase("ab_attribute.attribute_name"); + var attribute_type = this.getColumnCase("ab_attribute.attribute_type"); + var attribute_active = this.getColumnCase("ab_attribute.attribute_active"); + var ab_attributeUsage = this.getTableCase("ab_attributeusage"); + var ab_attributeUsageId = this.getColumnCase("ab_attributeusage.ab_attributeusageid"); + var attrU_ab_attribute_id = this.getColumnCase("ab_attributeusage.ab_attribute_id"); + var attrU_object_type = this.getColumnCase("ab_attributeusage.object_type"); + var ab_attributeRelation = this.getTableCase("ab_attributerelation"); + var attrRel_ab_attribute_id = this.getColumnCase("ab_attributerelation.ab_attribute_id"); + var ab_attributeRelationId = this.getColumnCase("ab_attributerelation.ab_attributerelationid"); + var object_rowId = this.getColumnCase("ab_attributerelation.object_rowid"); + var attrRel_object_type = this.getColumnCase("ab_attributerelation.object_type") - if (!attribute || !atype) return true; - atype = atype.toUpperCase(); + if(attribute == undefined) attribute = this.resolveSymbol(pObject, pObject.attribute); + if(attributeType == undefined) attributeType = this.resolveSymbol(pObject, pObject.attributeType); + if(objectType == undefined) objectType = this.resolveSymbol(pObject, pObject.objectType); + if(objectId == undefined) objectId = this.resolveSymbol(pObject, pObject.objectId); + if(value == undefined) value = this.resolveSymbol(pObject, pObject.value); + + if (!attribute || !attributeType) return true; + attributeType = attributeType.toUpperCase(); var valueColumn = ""; var attributes = attribute.split("."); - var columns = [this.getColumnCase("ab_attributeid"), this.getColumnCase("attribute_parent_id"), this.getColumnCase("attribute_name"), - this.getColumnCase("attribute_type"), this.getColumnCase("attribute_level"), this.getColumnCase("attribute_active")]; + var columns = [ab_attributeId, attribute_parent_id, attribute_name, attribute_type, attribute_active]; var type = $AttributeTypes.GROUP.toString(); - switch (atype) { - case $AttributeTypes.TEXT.toString(): - valueColumn = this.getColumnCase("char_value"); - break; - case $AttributeTypes.DATE.toString(): - valueColumn = this.getColumnCase("date_value"); - break; - case $AttributeTypes.NUMBER.toString(): - valueColumn = this.getColumnCase("number_value"); - break; - case $AttributeTypes.BOOLEAN.toString(): - valueColumn = this.getColumnCase("bool_value"); - break; + + if ($AttributeTypes[attributeType] == undefined) + { + this.writeLog(this.LogLevels.Error, "iAttribute: " + attributeType + " isn't a valid attribute type."); + return false; + } + + switch (attributeType) + { case $AttributeTypes.COMBO.toString(): - valueColumn = this.getColumnCase("id_value"); + valueColumn = $AttributeTypes[attributeType].databaseField; type = $AttributeTypes.COMBO.toString(); break; default: - return true; + valueColumn = $AttributeTypes[attributeType].databaseField; } - if (this.FuncBuffer.iAttribute == undefined) this.FuncBuffer.iAttribute = { - childs: {} - }; - var pathToFollow = this.FuncBuffer.iAttribute; - for (var i = 0; i < attributes.length; i++) { - if (pathToFollow["childs"][attributes[i]] != undefined) { - var id = pathToFollow["childs"][attributes[i]]["id"]; - } else { - pathToFollow["childs"][attributes[i]] = { - id: id, - childs: {} - }; - if (i == 0) { - var parent = "NULL"; - var sql = "select " + this.getColumnCase("ab_attributeid") + " from " + this.getTableCase("ab_attribute") + " where " - + this.getColumnCase("attribute_name") + " = ? and " + this.getColumnCase("attribute_level") + " = 0"; - id = db.cell([sql, [[attributes[i], SQLTYPES.VARCHAR]]], this.Config.AliasTo); - } else { + if (funcBuffer == undefined) funcBuffer = {childs: {}}; + var pathToFollow = funcBuffer; + + for (var i = 0; i < attributes.length; i++) + { + if (pathToFollow["childs"][attributes[i]] != undefined) var id = pathToFollow["childs"][attributes[i]]["id"]; + else + { + pathToFollow["childs"][attributes[i]] = {id: id, childs: {}}; + + if (i == 0) + { + var parent = "NULL"; + // select ab_attributeid from AB_ATTRIBUTE where ATTRIBUTE_NAME = 'Subordinate campaign of' and attribute_parent_id is null + id = newSelect(ab_attributeId, alias).from(ab_attribute).where(attribute_name, attributes[i]) + .and(attribute_parent_id, "is null").cell(); + } + else + { parent = pathToFollow["id"]; - sql = "select " + this.getColumnCase("ab_attributeid") + " from " + this.getTableCase("ab_attribute") + " where " - + this.getColumnCase("attribute_name") + " = ? and " + this.getColumnCase("attribute_parent_id") + " = ?"; - id = db.cell([sql, [[attributes[i], SQLTYPES.VARCHAR], [parent, SQLTYPES.CHAR]]], this.Config.AliasTo); - } - if (id == "" || id == null) { + id = newSelect(ab_attributeId, alias).from(ab_attribute).where(attribute_name, attributes[i]) + .and(attribute_parent_id, parent).cell(); + } + + if (id == "" || id == null) + { id = util.getNewUUID(); - if (attributes.length == i+1) type = atype; - //TODO: add insertNoWait to instantly add AB_ATTRIBUTE records; this ensures that nothing is in the funcBuffer that does not exist in the database + if (attributes.length == i+1) type = attributeType; + //TODO: add insertNoWait to instantly add AB_ATTRIBUTE records; + //this ensures that nothing is in the funcBuffer that does not exist in the database //TODO: check: are COMOB-values added automatically? - this.insertData(this.getTableCase("ab_attribute"), columns, null, [id, parent, attributes[i], type, i.toString(), "1"], this.Config.AliasTo); - } - pathToFollow["childs"][attributes[i]]["id"] = id; + var values = [id, parent, attributes[i], type, "1"]; + this.insertData(ab_attribute, columns, null, values, alias); + } + pathToFollow["childs"][attributes[i]]["id"] = id; } pathToFollow = pathToFollow["childs"][attributes[i]]; - } + } - if (otype) { - var aid = id; - sql = "select " + this.getColumnCase("ab_attributeusageid") + " from " + this.getTableCase("ab_attributeusage") + " where " - + this.getColumnCase("ab_attribute_id") + " = ? and " + this.getColumnCase("object_type") + " = ?"; - id = db.cell([sql, [[aid, SQLTYPES.CHAR], [otype, SQLTYPES.VARCHAR]]], this.Config.AliasTo); - if (id == "" || id == null) { - columns = [this.getColumnCase("ab_attributeusageid"), this.getColumnCase("ab_attribute_id"), this.getColumnCase("object_type")]; - this.insertData(this.getTableCase("ab_attributeusage"), columns, null, [util.getNewUUID(), aid, otype], this.Config.AliasTo); + if (objectType) + { + var attributeId = id; + id = newSelect(ab_attributeUsageId, alias).from(ab_attributeUsage).where(attrU_ab_attribute_id, attributeId) + .and(attrU_object_type, objectType).cell(); + + if (id == "" || id == null) + { + columns = [ab_attributeUsageId, attrU_ab_attribute_id, attrU_object_type]; + values = [util.getNewUUID(), attributeId, objectType]; + this.insertData(ab_attributeUsage, columns, null, values, alias); } - if (value && oid) { - sql = "select " + this.getColumnCase("ab_attributerelationid") + " from " + this.getTableCase("ab_attributerelation") + " where " - + this.getColumnCase("ab_attribute_id") + " = ? and " + this.getColumnCase("object_rowid") + " = ? and " - + this.getColumnCase("object_type") + " = ?"; - id = db.cell([sql, [[aid, SQLTYPES.CHAR], [oid, SQLTYPES.CHAR], [otype, SQLTYPES.VARCHAR]]], this.Config.AliasTo); - if (id == "" || id == null) { - columns = [this.getColumnCase("ab_attributerelationid"), this.getColumnCase("ab_attribute_id"), this.getColumnCase("object_type"), - this.getColumnCase("object_rowid"), valueColumn]; - this.insertData(this.getTableCase("ab_attributerelation"), columns, null, [util.getNewUUID(), aid, otype, oid, value], this.Config.AliasTo); - } else if (this.Config.ImportCommand.indexOf("update") != -1) { - cond = this.getColumnCase("ab_attributerelationid") + " = '" + id + "'"; - this.updateData(this.getTableCase("ab_attributerelation"), [valueColumn], null, [value], cond, this.Config.AliasTo); + 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) + { + 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) + { + cond = ab_attributeRelationId + " = '" + id + "'"; + this.updateData(ab_attributeRelation, [valueColumn], null, [value], cond, alias); } } } @@ -231,9 +257,9 @@ function iKeywordAttribute(pObject) { } var sql = "select " + this.getColumnCase("ab_keyword_attributeid") - + " from " + this.getTableCase("ab_keyword_attribute") - + " where " + this.getColumnCase("name") - + " = ? and " + this.getColumnCase("container") + " = ?"; + + " from " + this.getTableCase("ab_keyword_attribute") + + " where " + this.getColumnCase("name") + + " = ? and " + this.getColumnCase("container") + " = ?"; var attributeId = db.cell([sql, [[keywordAttribute, SQLTYPES.VARCHAR], [keywordContainer, SQLTYPES.VARCHAR]]], this.Config.AliasTo); @@ -241,36 +267,36 @@ function iKeywordAttribute(pObject) { if (attributeId == "" || attributeId == null) { attributeId = util.getNewUUID(); var columns = [this.getColumnCase("ab_keyword_attributeid"), - this.getColumnCase("name"), - this.getColumnCase("container"), - this.getColumnCase("kind")]; + this.getColumnCase("name"), + this.getColumnCase("container"), + this.getColumnCase("kind")]; this.insertData(this.getTableCase("ab_keyword_attribute"), columns, null, - [attributeId, keywordAttribute, keywordContainer, keywordAttributeType], this.Config.AliasTo); + [attributeId, keywordAttribute, keywordContainer, keywordAttributeType], this.Config.AliasTo); } // Creates the entry in AB_KEYWORD_ENTRY and AB_KEYWORD_ATTRIBUTERELATION, case if it not exists. if (keyword && keywordAttrRelValue) { sql = "select " + this.getColumnCase("ab_keyword_entryid") - + " from " + this.getTableCase("ab_keyword_entry") - + " where " + this.getColumnCase("keyid") + " = ?"; + + " from " + this.getTableCase("ab_keyword_entry") + + " where " + this.getColumnCase("keyid") + " = ?"; var keywordId = db.cell([sql, [[keyword, SQLTYPES.CHAR]]], this.Config.AliasTo); if (keywordId == "" || keywordId == null) { sql = "select " + this.getColumnCase("keyid") - + " from " + this.getTableCase("ab_keyword_entry") - + " where " + this.getColumnCase("container") - + " = ? and " + this.getColumnCase("title") + " = ?"; + + " from " + this.getTableCase("ab_keyword_entry") + + " where " + this.getColumnCase("container") + + " = ? and " + this.getColumnCase("title") + " = ?"; keywordId = db.cell([sql, [[keywordContainer, SQLTYPES.VARCHAR], [keyword, SQLTYPES.VARCHAR]]], this.Config.AliasTo); if (keywordId == "" || keywordId == null) { columns = [this.getColumnCase("ab_keyword_entryid"), this.getColumnCase("keyid"), this.getColumnCase("container"), - this.getColumnCase("title"), this.getColumnCase("sorting"), this.getColumnCase("isactive"), - this.getColumnCase("isessential")]; + this.getColumnCase("title"), this.getColumnCase("sorting"), this.getColumnCase("isactive"), + this.getColumnCase("isessential")]; sql = "select max(coalesce(sorting, 0))+1 from ab_keyword_entry where container = ?"; @@ -287,21 +313,21 @@ function iKeywordAttribute(pObject) { // Creates or updates the keyword attributerelation. sql = "select " + this.getColumnCase("ab_keyword_attributerelationid") - + " from " + this.getTableCase("ab_keyword_attributerelation") - + " where " + this.getColumnCase("ab_keyword_entry_id") - + " = ? and " + this.getColumnCase("ab_keyword_attribute_id") + " = ?"; + + " from " + this.getTableCase("ab_keyword_attributerelation") + + " where " + this.getColumnCase("ab_keyword_entry_id") + + " = ? and " + this.getColumnCase("ab_keyword_attribute_id") + " = ?"; id = db.cell([sql, [[keywordId, SQLTYPES.CHAR], [attributeId, SQLTYPES.CHAR]]], this.Config.AliasTo); if (id == "" || id == null) { columns = [this.getColumnCase("ab_keyword_attributerelationid"), - this.getColumnCase("ab_keyword_entry_id"), - this.getColumnCase("ab_keyword_attribute_id"), - valueColumn]; + this.getColumnCase("ab_keyword_entry_id"), + this.getColumnCase("ab_keyword_attribute_id"), + valueColumn]; id = util.getNewUUID(); this.insertData(this.getTableCase("ab_keyword_attributerelation"), columns, null, - [id, keywordId, attributeId, keywordAttrRelValue], this.Config.AliasTo); + [id, keywordId, attributeId, keywordAttrRelValue], this.Config.AliasTo); } else { if (this.Config.ImportCommand.indexOf("update") != -1) { diff --git a/process/Importer_lib/process.js b/process/Importer_lib/process.js index ad800c6581ee472cf3a7e1e90ed9b4f6a67be8a7..430c8e0aa7734b65247a12799a2181d0610594a8 100644 --- a/process/Importer_lib/process.js +++ b/process/Importer_lib/process.js @@ -1050,6 +1050,17 @@ function Importer(pConfig) this.insertData = function(pTable, pColumns, pTypes, pValues, pAlias) { + // Checks wether the given columns (pColumns) are in the following format: tablename.columnname + // and case when then the following code cuts away the string "tablename."-String to get only the column name. + // Its required for the continueing processes otherwise a exception will be thrown. + for (i = 0; i < pColumns.length; i++) + { + if (pColumns[i].indexOf(".", 0) > -1) + { + pColumns[i] = pColumns[i].substring(pColumns[i].indexOf(".", 0)+1, pColumns[i].length); + } + } + var tt = datetime.date(); var resData = true; if(this.Preview == false) @@ -1089,14 +1100,17 @@ function Importer(pConfig) pValues.push(datetime.date()); } - this.insertArray.push([this.getTableCase(pTable), pColumns, pTypes, pValues, pAlias]); + var types = pTypes != null ? pTypes : pColumns.map(function (columnName) { + return this.getColumnDataType(this.getTableCase(pTable), this.getColumnCase(columnName)); + }, this); + this.insertArray.push([this.getTableCase(pTable), pColumns, types, pValues, pAlias]); } } } catch(ex) { this.writeLog(this.LogLevels.Error, "Exception at insertData for record: " + getRecordString(pColumns, pValues)); - logging.log(ex); + logging.log(ex["rhinoException"] != undefined ? ex["rhinoException"] : ex); resData = false; } } @@ -1195,16 +1209,20 @@ function Importer(pConfig) theValues.push(datetime.date()); minchanges++; } + var types = theTypes != null ? theTypes : pColumns.map(function (columnName){ + return this.getColumnDataType(this.getTableCase(pTable), this.getColumnCase(columnName)) + }, this); + if(this.CompleteUpdate == false) { if(theCols.length > minchanges) { - this.updateArray.push([this.getTableCase(pTable), theCols, theTypes, theValues, pCondition, pAlias]); + this.updateArray.push([this.getTableCase(pTable), theCols, types, theValues, pCondition, pAlias]); } } else { - this.updateArray.push([this.getTableCase(pTable), theCols, theTypes, theValues, pCondition, pAlias]); + this.updateArray.push([this.getTableCase(pTable), theCols, types, theValues, pCondition, pAlias]); } } } @@ -1212,7 +1230,7 @@ function Importer(pConfig) catch(ex) { resultData = false; - this.writeLog(this.LogLevels.Error, "Exception at updateData for record: " + getRecordString(pColumns, pValues)); + logging.log(ex["rhinoException"] != undefined ? ex["rhinoException"] : ex); logging.log(ex); } } @@ -1481,7 +1499,7 @@ function Importer(pConfig) return pName.toLowerCase(); else return pName; - } + }; //Converts a string with the columnname in upper or lower case this.getColumnCase = function(pName) @@ -1494,6 +1512,20 @@ function Importer(pConfig) return pName; } + //Converts a string with the columnname in upper or lower case + this.getColumnDataType = function(pTableName, pColumnName) + { + if (this.DataType[pTableName] == undefined) + throw new Error("Importer: getColumnDataType: could not retrieve column data type for table: " + pTableName ); + + var dataType = this.DataType[pTableName][pColumnName]; + + if (dataType == undefined) + throw new Error("Importer: getColumnDataType: could not retrieve column data type for table.column: " + pTableName + "." + pColumnName); + return dataType; + }; + + this.doIfCheck = function(pObject) { if (pObject.DoIf == undefined) return true;