diff --git a/src/AssortmentHandler.cs b/src/AssortmentHandler.cs new file mode 100644 index 0000000..a320b18 --- /dev/null +++ b/src/AssortmentHandler.cs @@ -0,0 +1,279 @@ +using Dynamicweb.Core; +using Dynamicweb.Data; +using Dynamicweb.DataIntegration.Integration; +using Dynamicweb.DataIntegration.ProviderHelpers; +using Dynamicweb.Ecommerce.Assortments; +using Dynamicweb.Extensibility.Notifications; +using Dynamicweb.Logging; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; + +namespace Dynamicweb.DataIntegration.Providers.EcomProvider; + +public class AssortmentHandler +{ + private Dictionary> assortmentsToRebuild = new Dictionary>(); + private readonly string assortmentsProductsTempTableName = "AssortmentsProductsTempTable"; + private readonly ILogger logger = null; + private SqlCommand sqlCommand = null; + private readonly string CurrentContextLanguageId = Ecommerce.Common.Context.LanguageID; + + public SqlCommand SqlCommand + { + get + { + return sqlCommand; + } + set + { + sqlCommand = value; + } + } + + public AssortmentHandler(SqlCommand sqlCommand, ILogger logger) + { + this.sqlCommand = sqlCommand; + this.logger = logger; + } + + #region Assortments rebuild + public void ProcessAssortments(DataRow dataRow, Mapping mapping) + { + if (dataRow != null && mapping.DestinationTable != null) + { + string assortmentID = null; + string assortmentLanguageId = CurrentContextLanguageId; + string relationID = null; + AssortmentRelationType relationType = AssortmentRelationType.Product; + + switch (mapping.DestinationTable.Name) + { + case "EcomAssortments": + if (dataRow.Table.Columns.Contains("AssortmentRebuildRequired") && dataRow["AssortmentRebuildRequired"] != DBNull.Value && Converter.ToBoolean(dataRow["AssortmentRebuildRequired"]) && + dataRow.Table.Columns.Contains("AssortmentID") && dataRow["AssortmentID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentID"] as string)) + { + assortmentID = (string)dataRow["AssortmentID"]; + if (dataRow.Table.Columns.Contains("AssortmentLanguageID") && dataRow["AssortmentLanguageID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentLanguageID"] as string)) + { + assortmentLanguageId = (string)dataRow["AssortmentLanguageID"]; + } + } + break; + case "EcomAssortmentGroupRelations": + if (dataRow.Table.Columns.Contains("AssortmentGroupRelationAssortmentID") && dataRow["AssortmentGroupRelationAssortmentID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentGroupRelationAssortmentID"] as string) && + dataRow.Table.Columns.Contains("AssortmentGroupRelationGroupID") && dataRow["AssortmentGroupRelationGroupID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentGroupRelationGroupID"] as string)) + { + assortmentID = (string)dataRow["AssortmentGroupRelationAssortmentID"]; + relationID = (string)dataRow["AssortmentGroupRelationGroupID"]; + relationType = AssortmentRelationType.Group; + } + break; + case "EcomAssortmentProductRelations": + if (dataRow.Table.Columns.Contains("AssortmentProductRelationAssortmentID") && dataRow["AssortmentProductRelationAssortmentID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentProductRelationAssortmentID"] as string) && + dataRow.Table.Columns.Contains("AssortmentProductRelationProductID") && dataRow["AssortmentProductRelationProductID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentProductRelationProductID"] as string)) + { + assortmentID = (string)dataRow["AssortmentProductRelationAssortmentID"]; + relationID = (string)dataRow["AssortmentProductRelationProductID"]; + relationType = AssortmentRelationType.Product; + } + break; + case "EcomAssortmentShopRelations": + if (dataRow.Table.Columns.Contains("AssortmentShopRelationAssortmentID") && dataRow["AssortmentShopRelationAssortmentID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentShopRelationAssortmentID"] as string) && + dataRow.Table.Columns.Contains("AssortmentShopRelationShopID") && dataRow["AssortmentShopRelationShopID"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["AssortmentShopRelationShopID"] as string)) + { + assortmentID = (string)dataRow["AssortmentShopRelationAssortmentID"]; + relationID = (string)dataRow["AssortmentShopRelationShopID"]; + relationType = AssortmentRelationType.Shop; + } + break; + } + if (!string.IsNullOrEmpty(assortmentID)) + { + AssortmentInfo info = new AssortmentInfo(); + info.AssortmentID = assortmentID; + info.RelationID = relationID; + info.RelationType = relationType; + if (string.IsNullOrEmpty(relationID) //this happens when AssortmentRebuildRequired = true + || IsAssortmentToRebuild(info)) + { + if (!assortmentsToRebuild.Keys.Contains(assortmentID)) + { + assortmentsToRebuild.Add(assortmentID, new List()); + } + if (!string.IsNullOrEmpty(relationID)) + { + assortmentsToRebuild[assortmentID].Add(info); + } + } + } + } + } + + public void RebuildAssortments() + { + var assortmentsForBuild = new List(); + var failedAssortments = new List(); + + if (assortmentsToRebuild.Count > 0) + { + CreateAssortmentProductsTempTable(); + InsertAssortmentProductsToTempTable(assortmentsToRebuild); + UpdateAssortmentsProducts(false); + Assortment assortment = null; + string msg = ""; + + foreach (string assortmentId in assortmentsToRebuild.Keys) + { + try + { + assortment = Ecommerce.Services.Assortments.GetAssortmentById(assortmentId); + if (assortment is object) + { + assortmentsForBuild.Add(assortment); + Ecommerce.Services.Assortments.BuildAssortment(assortment); + } + } + catch (Exception ex) + { + msg += string.Format("Error rebulding assortments: {0}. ", ex.Message); + if (assortment != null) + { + failedAssortments.Add(assortment); + msg += string.Format("AssortmentID: {0}. ", assortment.ID); + } + } + } + + UpdateAssortmentsProducts(true); + DeleteAssortmentProductsTempTable(); + + if (!string.IsNullOrEmpty(msg)) + { + logger.Log(msg); + } + } + NotificationManager.Notify(Dynamicweb.Ecommerce.Notifications.Ecommerce.Assortment.AssortmentsBuildFinished, + new Dynamicweb.Ecommerce.Notifications.Ecommerce.Assortment.AssortmentsBuildFinishedArgs(assortmentsForBuild, failedAssortments)); + } + + private bool IsAssortmentToRebuild(AssortmentInfo info) + { + bool rebuild = false; + if (info is object && !string.IsNullOrEmpty(info.RelationID)) + { + Assortment assortment = Ecommerce.Services.Assortments.GetAssortmentById(info.AssortmentID); + if (assortment is object) + { + if (info.RelationType == AssortmentRelationType.Product) + { + rebuild = !assortment.ProductRelations.Values.Any(relation => + string.Compare(relation.ProductID, info.RelationID, true) == 0); + } + else if (info.RelationType == AssortmentRelationType.Group) + { + rebuild = !assortment.GroupRelations.Keys.Any(groupID => string.Compare(groupID, info.RelationID, true) == 0); + } + else if (info.RelationType == AssortmentRelationType.Shop) + { + rebuild = !assortment.ShopRelations.Keys.Any(shopID => string.Compare(shopID, info.RelationID, true) == 0); + } + } + } + return rebuild; + } + + private void InsertAssortmentProductsToTempTable(Dictionary> assortments) + { + if (assortments != null && assortments.Count > 0) + { + string sql = string.Format(@"insert into {0}(ProductAutoID) select ProductAutoID from EcomProducts + where ProductActive = 1 and ( ProductID in ( ", assortmentsProductsTempTableName); + + string groupIDs = string.Join("','", assortments.SelectMany(v => v.Value).Where(x => x.RelationType == AssortmentRelationType.Group && !string.IsNullOrEmpty(x.RelationID)).Select(i => i.RelationID).Distinct()); + string shopIDs = string.Join("','", assortments.SelectMany(v => v.Value).Where(x => x.RelationType == AssortmentRelationType.Shop && !string.IsNullOrEmpty(x.RelationID)).Select(i => i.RelationID).Distinct()); + if (!string.IsNullOrEmpty(groupIDs) || !string.IsNullOrEmpty(shopIDs)) + { + StringBuilder sb = new StringBuilder(); + sb.Append(sql); + sb.Append("select GroupProductRelationProductID from EcomGroupProductRelation "); + sb.Append("where "); + if (!string.IsNullOrEmpty(shopIDs)) + { + sb.AppendFormat("GroupProductRelationGroupID in( select ShopGroupGroupID from EcomShopGroupRelation where ShopGroupShopID in ( '{0}' )) or ", + shopIDs); + } + if (!string.IsNullOrEmpty(groupIDs)) + { + sb.AppendFormat("GroupProductRelationGroupID in ( '{0}' )", groupIDs); + } + else + { + sb.Remove(sb.Length - 4, 3); //remove or + } + sb.Append(") )"); + ExecuteInsertAssortmentProductsToTempTable(sb.ToString()); + } + + List productIDs = assortments.SelectMany(v => v.Value).Where(x => x.RelationType == AssortmentRelationType.Product && !string.IsNullOrEmpty(x.RelationID)).Select(i => i.RelationID).Distinct().ToList(); + if (productIDs.Count > 0) + { + int maxProductIDsInQuery = 3000; + if (productIDs.Count > maxProductIDsInQuery) + { + int chunks = productIDs.Count / maxProductIDsInQuery; + if (productIDs.Count % maxProductIDsInQuery != 0) + { + chunks++; + } + for (int i = 0; i < chunks; i++) + { + string pds = string.Join("','", productIDs.Skip(i * maxProductIDsInQuery).Take(maxProductIDsInQuery)); + ExecuteInsertAssortmentProductsToTempTable(string.Format("{0} '{1}' ) )", sql, string.Join("','", productIDs))); + } + } + else + { + ExecuteInsertAssortmentProductsToTempTable(string.Format("{0} '{1}' ) )", sql, string.Join("','", productIDs))); + } + } + } + } + + private void ExecuteInsertAssortmentProductsToTempTable(string sql) + { + sqlCommand.CommandText = sql; + try + { + sqlCommand.ExecuteScalar(); + } + catch (Exception ex) + { + throw new Exception(string.Format("Error insert assortments products to temp table: {0}. Sql: {1}.", ex.Message, sql)); + } + } + + private void CreateAssortmentProductsTempTable() + { + List columns = new List(); + columns.Add(new SqlColumn("ProductAutoID", "bigint", null, 0, true, true)); + SQLTable.CreateTempTable(sqlCommand, null, assortmentsProductsTempTableName, null, columns, logger); + } + + private void DeleteAssortmentProductsTempTable() + { + sqlCommand.CommandText = "if exists (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + assortmentsProductsTempTableName + "') AND type in (N'U')) drop table " + assortmentsProductsTempTableName; + sqlCommand.ExecuteScalar(); + } + + private void UpdateAssortmentsProducts(bool setActive) + { + sqlCommand.CommandText = string.Format("update EcomProducts set ProductActive = {0} where ProductAutoID in (select distinct(ProductAutoID) from {1})", + Database.SqlBool(setActive), assortmentsProductsTempTableName); + sqlCommand.ExecuteScalar(); + } + #endregion Assortments rebuild +} diff --git a/src/AssortmentInfo.cs b/src/AssortmentInfo.cs new file mode 100644 index 0000000..dcd544e --- /dev/null +++ b/src/AssortmentInfo.cs @@ -0,0 +1,10 @@ +using Dynamicweb.Ecommerce.Assortments; + +namespace Dynamicweb.DataIntegration.Providers.EcomProvider; + +public class AssortmentInfo +{ + public string AssortmentID; + public AssortmentRelationType RelationType; + public string RelationID; +} diff --git a/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj index 736d0bd..66db541 100644 --- a/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj +++ b/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj @@ -1,6 +1,6 @@  - 10.0.5 + 10.0.6 1.0.0.0 Ecommerce Provider @@ -10,7 +10,7 @@ Dynamicweb CMS Dynamicweb Software A/S Dynamicweb Software A/S - Copyright © 2020 Dynamicweb Software A/S + Copyright © 2023 Dynamicweb Software A/S net7.0 @@ -22,12 +22,8 @@ snupkg - - - - - - + + - \ No newline at end of file + diff --git a/src/EcomDestinationWriter.cs b/src/EcomDestinationWriter.cs index 429634f..e1ec96b 100644 --- a/src/EcomDestinationWriter.cs +++ b/src/EcomDestinationWriter.cs @@ -3,8 +3,6 @@ using Dynamicweb.Data; using Dynamicweb.DataIntegration.Integration; using Dynamicweb.DataIntegration.ProviderHelpers; -using Dynamicweb.DataIntegration.Providers.DynamicwebProvider; -using Dynamicweb.DataIntegration.Providers.SqlProvider; using Dynamicweb.Logging; using System; using System.Collections; @@ -14,4171 +12,4013 @@ using System.Linq; using System.Text; -namespace Dynamicweb.DataIntegration.Providers.EcomProvider +namespace Dynamicweb.DataIntegration.Providers.EcomProvider; + +internal class EcomDestinationWriter : BaseSqlWriter { - internal class EcomDestinationWriter - { - private readonly Job job; - private readonly SqlConnection connection; - private readonly SqlCommand sqlCommand; - private readonly bool deactivateMissingProducts; - private readonly bool updateOnlyExistingProducts; - protected internal DataSet DataToWrite = new DataSet(); - private readonly ILogger logger; - //Used for fast searching of VariantOptionsProductRelation combinations - private Hashtable ecomVariantOptionsProductRelationKeys = new Hashtable(); - //Used for fast searching of GroupProductRelation combinations - private Hashtable ecomGroupProductRelationKeys = new Hashtable(); - //Used for fast searching of VariantgroupProductrelation combinations - private Hashtable ecomVariantgroupProductrelationKeys = new Hashtable(); - private AssortmentHandler assortmentHandler = null; - private bool isParentGroupSortingInEcomGroupsMapping = false; - private readonly bool discardDuplicates; - protected DuplicateRowsHandler duplicateRowsHandler; - private Hashtable _processedProductsKeys = new Hashtable(); - private SortedList ProductVariantsCountDictionary = new SortedList(); - private SortedList ProductVariantGroupsCountDictionary = new SortedList(); - private readonly bool partialUpdate; - private readonly Dictionary> MappingIdEcomProductsPKColumns; - private readonly bool removeMissingAfterImportDestinationTablesOnly; - private readonly bool useStrictPrimaryKeyMatching; - private readonly bool _createMissingGoups; - private readonly bool _skipFailingRows; - private readonly bool _useProductIdFoundByNumber; - private readonly bool _ignoreEmptyCategoryFieldValues; - private List> _rowsWithMissingGroups = new List>(); - internal const string EcomProductsMissingGroupsErrorMessage = "Failed at importing EcomProducts rows with missing Groups"; - - private Dictionary> Mappings = new Dictionary>(StringComparer.OrdinalIgnoreCase); - private Dictionary> DestinationColumns = new Dictionary>(StringComparer.OrdinalIgnoreCase); - private Dictionary> DestinationColumnMappings = new Dictionary>(StringComparer.OrdinalIgnoreCase); - private Dictionary> SourceColumnMappings = new Dictionary>(); - - private Dictionary ColumnMappingsByMappingId = new Dictionary(); - - private long RowAutoId = 0; - protected internal Dictionary>> DataRowsToWrite = new Dictionary>>(StringComparer.OrdinalIgnoreCase); - private Dictionary ImportedProductsByNumber = new Dictionary(StringComparer.OrdinalIgnoreCase); - private List _addedMappingsForMoveToMainTables = new List(); - - internal int RowsToWriteCount - { - get; - private set; - } - - public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, - bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate) - : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, - updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, false) - { - } - - public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, - bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly) - : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, - updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, false, true) - { - } - - public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, - bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, - bool useStrictPrimaryKeyMatching, bool createMissingGoups) - : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, - updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, useStrictPrimaryKeyMatching, createMissingGoups, false) - { - } - - public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, - bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, - bool useStrictPrimaryKeyMatching, bool createMissingGoups, bool skipFailingRows) - : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, - updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, useStrictPrimaryKeyMatching, createMissingGoups, skipFailingRows, false) - { - } - public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, - bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, - bool useStrictPrimaryKeyMatching, bool createMissingGoups, bool skipFailingRows, bool useProductIdFoundByNumber) - : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, - updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, useStrictPrimaryKeyMatching, createMissingGoups, skipFailingRows, useProductIdFoundByNumber, false) - { - } - - public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, - bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, - bool useStrictPrimaryKeyMatching, bool createMissingGoups, bool skipFailingRows, bool useProductIdFoundByNumber, bool ignoreEmptyCategoryFieldValues) - { - deactivateMissingProducts = deactivateMissinProducts; - this.updateOnlyExistingProducts = updateOnlyExistingProducts; - this.deleteExcess = deleteExcess; - this.job = job; - bool removeMissing = ((EcomProvider)this.job.Destination).RemoveMissingAfterImport || ((EcomProvider)this.job.Destination).RemoveMissingAfterImportDestinationTablesOnly; - _removeFromEcomGroups = removeMissing; - _removeFromEcomVariantGroups = removeMissing; - this.connection = connection; - this.logger = logger; - if (commandForTest == null) - { - sqlCommand = connection.CreateCommand(); - } - else - { - sqlCommand = commandForTest; - } - InitMappings(); - CreateTempTables(); + private readonly Job job; + private readonly SqlConnection connection; + private readonly SqlCommand sqlCommand; + private readonly bool deactivateMissingProducts; + private readonly bool updateOnlyExistingProducts; + protected internal DataSet DataToWrite = new DataSet(); + private readonly ILogger logger; + //Used for fast searching of VariantOptionsProductRelation combinations + private Hashtable ecomVariantOptionsProductRelationKeys = new Hashtable(); + //Used for fast searching of GroupProductRelation combinations + private Hashtable ecomGroupProductRelationKeys = new Hashtable(); + //Used for fast searching of VariantgroupProductrelation combinations + private Hashtable ecomVariantgroupProductrelationKeys = new Hashtable(); + private AssortmentHandler assortmentHandler = null; + private bool isParentGroupSortingInEcomGroupsMapping = false; + private readonly bool discardDuplicates; + protected DuplicateRowsHandler duplicateRowsHandler; + private Hashtable _processedProductsKeys = new Hashtable(); + private SortedList ProductVariantsCountDictionary = new SortedList(); + private SortedList ProductVariantGroupsCountDictionary = new SortedList(); + private readonly bool partialUpdate; + private readonly Dictionary> MappingIdEcomProductsPKColumns; + private readonly bool removeMissingAfterImportDestinationTablesOnly; + private readonly bool useStrictPrimaryKeyMatching; + private readonly bool _createMissingGoups; + private readonly bool _skipFailingRows; + private readonly bool _useProductIdFoundByNumber; + private readonly bool _ignoreEmptyCategoryFieldValues; + private List> _rowsWithMissingGroups = new List>(); + internal const string EcomProductsMissingGroupsErrorMessage = "Failed at importing EcomProducts rows with missing Groups"; + + private Dictionary> Mappings = new Dictionary>(StringComparer.OrdinalIgnoreCase); + private Dictionary> DestinationColumns = new Dictionary>(StringComparer.OrdinalIgnoreCase); + private Dictionary> DestinationColumnMappings = new Dictionary>(StringComparer.OrdinalIgnoreCase); + private Dictionary> SourceColumnMappings = new Dictionary>(); + + private Dictionary ColumnMappingsByMappingId = new Dictionary(); + + private long RowAutoId = 0; + protected internal Dictionary>> DataRowsToWrite = new Dictionary>>(StringComparer.OrdinalIgnoreCase); + private Dictionary ImportedProductsByNumber = new Dictionary(StringComparer.OrdinalIgnoreCase); + private List _addedMappingsForMoveToMainTables = new List(); + + internal int RowsToWriteCount + { + get; + private set; + } + + public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, + bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate) + : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, + updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, false) + { + } + + public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, + bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly) + : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, + updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, false, true) + { + } + + public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, + bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, + bool useStrictPrimaryKeyMatching, bool createMissingGoups) + : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, + updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, useStrictPrimaryKeyMatching, createMissingGoups, false) + { + } - if (string.IsNullOrEmpty(defaultLanguageId)) + public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, + bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, + bool useStrictPrimaryKeyMatching, bool createMissingGoups, bool skipFailingRows) + : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, + updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, useStrictPrimaryKeyMatching, createMissingGoups, skipFailingRows, false) + { + } + public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, + bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, + bool useStrictPrimaryKeyMatching, bool createMissingGoups, bool skipFailingRows, bool useProductIdFoundByNumber) + : this(job, connection, deactivateMissinProducts, commandForTest, deleteExcess, logger, + updateOnlyExistingProducts, defaultLanguageId, discardDuplicates, partialUpdate, removeMissingAfterImportDestinationTablesOnly, useStrictPrimaryKeyMatching, createMissingGoups, skipFailingRows, useProductIdFoundByNumber, false) + { + } + + public EcomDestinationWriter(Job job, SqlConnection connection, bool deactivateMissinProducts, SqlCommand commandForTest, bool deleteExcess, ILogger logger, + bool updateOnlyExistingProducts, string defaultLanguageId, bool discardDuplicates, bool partialUpdate, bool removeMissingAfterImportDestinationTablesOnly, + bool useStrictPrimaryKeyMatching, bool createMissingGoups, bool skipFailingRows, bool useProductIdFoundByNumber, bool ignoreEmptyCategoryFieldValues) + { + deactivateMissingProducts = deactivateMissinProducts; + this.updateOnlyExistingProducts = updateOnlyExistingProducts; + this.deleteExcess = deleteExcess; + this.job = job; + bool removeMissing = ((EcomProvider)this.job.Destination).RemoveMissingAfterImport || ((EcomProvider)this.job.Destination).RemoveMissingAfterImportDestinationTablesOnly; + _removeFromEcomGroups = removeMissing; + _removeFromEcomVariantGroups = removeMissing; + this.connection = connection; + this.logger = logger; + if (commandForTest == null) + { + sqlCommand = connection.CreateCommand(); + } + else + { + sqlCommand = commandForTest; + } + InitMappings(); + CreateTempTables(); + + if (string.IsNullOrEmpty(defaultLanguageId)) + { + sqlCommand.CommandText = + "select top(1) ecomlanguages.languageid from ecomlanguages where ecomlanguages.languageisdefault=1"; + var result = sqlCommand.ExecuteReader(); + if (result.Read()) { - sqlCommand.CommandText = - "select top(1) ecomlanguages.languageid from ecomlanguages where ecomlanguages.languageisdefault=1"; - var result = sqlCommand.ExecuteReader(); - if (result.Read()) - { - _defaultLanguageId = (string)result["languageid"]; - } - else - { - _defaultLanguageId = "LANG1"; - } - result.Close(); + _defaultLanguageId = (string)result["languageid"]; } else { - _defaultLanguageId = defaultLanguageId; - } - assortmentHandler = new AssortmentHandler(sqlCommand, this.logger); - if (this.job != null && this.job.Mappings != null) - { - Dictionary ecomGroupsMapping = null; - if (DestinationColumnMappings.TryGetValue("EcomGroups", out ecomGroupsMapping) && ecomGroupsMapping.ContainsKey("ParentGroupsSorting")) - { - isParentGroupSortingInEcomGroupsMapping = true; - } - } - this.discardDuplicates = discardDuplicates; - bool discardDuplicatesFromMapping = false; - if (!discardDuplicates) - { - discardDuplicatesFromMapping = job.Mappings.Where(m => m != null && m.Active).Any(m => - { - bool? v = m.GetOptionValue("DiscardDuplicates"); - return v.HasValue && v.Value; - }); + _defaultLanguageId = "LANG1"; } - if (discardDuplicates || discardDuplicatesFromMapping) + result.Close(); + } + else + { + _defaultLanguageId = defaultLanguageId; + } + assortmentHandler = new AssortmentHandler(sqlCommand, this.logger); + if (this.job != null && this.job.Mappings != null) + { + Dictionary ecomGroupsMapping = null; + if (DestinationColumnMappings.TryGetValue("EcomGroups", out ecomGroupsMapping) && ecomGroupsMapping.ContainsKey("ParentGroupsSorting")) { - duplicateRowsHandler = new DuplicateRowsHandler(logger, job.Mappings); + isParentGroupSortingInEcomGroupsMapping = true; } - if (connection.State != ConnectionState.Open) + } + this.discardDuplicates = discardDuplicates; + bool discardDuplicatesFromMapping = false; + if (!discardDuplicates) + { + discardDuplicatesFromMapping = job.Mappings.Where(m => m != null && m.Active).Any(m => { - connection.Open(); - } + bool? v = m.GetOptionValue("DiscardDuplicates"); + return v.HasValue && v.Value; + }); + } + if (discardDuplicates || discardDuplicatesFromMapping) + { + duplicateRowsHandler = new DuplicateRowsHandler(logger, job.Mappings); + } + if (connection.State != ConnectionState.Open) + { + connection.Open(); + } - this.partialUpdate = partialUpdate; - MappingIdEcomProductsPKColumns = GetEcomProductsPKColumns(); - this.removeMissingAfterImportDestinationTablesOnly = removeMissingAfterImportDestinationTablesOnly; - this.useStrictPrimaryKeyMatching = useStrictPrimaryKeyMatching; - _createMissingGoups = createMissingGoups; - _skipFailingRows = skipFailingRows; - if (useProductIdFoundByNumber && DestinationColumnMappings.TryGetValue("EcomProducts", out Dictionary mapping) - && mapping.ContainsKey("ProductNumber") && mapping.ContainsKey("ProductVariantID")) - { - _useProductIdFoundByNumber = true; - } - _ignoreEmptyCategoryFieldValues = ignoreEmptyCategoryFieldValues; + this.partialUpdate = partialUpdate; + MappingIdEcomProductsPKColumns = GetEcomProductsPKColumns(); + this.removeMissingAfterImportDestinationTablesOnly = removeMissingAfterImportDestinationTablesOnly; + this.useStrictPrimaryKeyMatching = useStrictPrimaryKeyMatching; + _createMissingGoups = createMissingGoups; + _skipFailingRows = skipFailingRows; + if (useProductIdFoundByNumber && DestinationColumnMappings.TryGetValue("EcomProducts", out Dictionary mapping) + && mapping.ContainsKey("ProductNumber") && mapping.ContainsKey("ProductVariantID")) + { + _useProductIdFoundByNumber = true; } + _ignoreEmptyCategoryFieldValues = ignoreEmptyCategoryFieldValues; + } - private void InitMappings() + private void InitMappings() + { + foreach (var mapping in job.Mappings) { - foreach (var mapping in job.Mappings) - { - var mappingColumns = mapping.GetColumnMappings(); - ColumnMappingsByMappingId.Add(mapping.GetId(), mappingColumns); - InitMappingsByTableName(mapping); - InitColumnMappings(mapping, mappingColumns); - InitDestinationColumns(mapping.DestinationTable.Name, mapping.DestinationTable.Columns); - } + var mappingColumns = mapping.GetColumnMappings(); + ColumnMappingsByMappingId.Add(mapping.GetId(), mappingColumns); + InitMappingsByTableName(mapping); + InitColumnMappings(mapping, mappingColumns); + InitDestinationColumns(mapping.DestinationTable.Name, mapping.DestinationTable.Columns); } + } - private void InitMappingsByTableName(Mapping mapping) + private void InitMappingsByTableName(Mapping mapping) + { + List mappingsList = null; + if (!Mappings.TryGetValue(mapping.DestinationTable.Name, out mappingsList)) { - List mappingsList = null; - if (!Mappings.TryGetValue(mapping.DestinationTable.Name, out mappingsList)) - { - mappingsList = new List(); - Mappings.Add(mapping.DestinationTable.Name, mappingsList); - } - mappingsList.Add(mapping); + mappingsList = new List(); + Mappings.Add(mapping.DestinationTable.Name, mappingsList); } + mappingsList.Add(mapping); + } - private void InitColumnMappings(Mapping mapping, ColumnMappingCollection mappings) + private void InitColumnMappings(Mapping mapping, ColumnMappingCollection mappings) + { + string tableName = mapping.DestinationTable.Name; + Dictionary destinationColumnMappingDictionary = null; + Dictionary sourceColumnMappingDictionary = null; + if (!SourceColumnMappings.TryGetValue(mapping.GetId(), out sourceColumnMappingDictionary)) { - string tableName = mapping.DestinationTable.Name; - Dictionary destinationColumnMappingDictionary = null; - Dictionary sourceColumnMappingDictionary = null; - if (!SourceColumnMappings.TryGetValue(mapping.GetId(), out sourceColumnMappingDictionary)) - { - sourceColumnMappingDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); - SourceColumnMappings.Add(mapping.GetId(), sourceColumnMappingDictionary); - } + sourceColumnMappingDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + SourceColumnMappings.Add(mapping.GetId(), sourceColumnMappingDictionary); + } - if (!DestinationColumnMappings.TryGetValue(tableName, out destinationColumnMappingDictionary)) + if (!DestinationColumnMappings.TryGetValue(tableName, out destinationColumnMappingDictionary)) + { + destinationColumnMappingDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + DestinationColumnMappings.Add(tableName, destinationColumnMappingDictionary); + } + foreach (var columnMapping in mappings) + { + if (!destinationColumnMappingDictionary.ContainsKey(columnMapping.DestinationColumn.Name)) { - destinationColumnMappingDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); - DestinationColumnMappings.Add(tableName, destinationColumnMappingDictionary); + destinationColumnMappingDictionary.Add(columnMapping.DestinationColumn.Name, columnMapping.DestinationColumn); } - foreach (var columnMapping in mappings) + if (!sourceColumnMappingDictionary.ContainsKey(columnMapping.DestinationColumn.Name)) { - if (!destinationColumnMappingDictionary.ContainsKey(columnMapping.DestinationColumn.Name)) - { - destinationColumnMappingDictionary.Add(columnMapping.DestinationColumn.Name, columnMapping.DestinationColumn); - } - if (!sourceColumnMappingDictionary.ContainsKey(columnMapping.DestinationColumn.Name)) - { - sourceColumnMappingDictionary.Add(columnMapping.DestinationColumn.Name, columnMapping); - } + sourceColumnMappingDictionary.Add(columnMapping.DestinationColumn.Name, columnMapping); } } + } - private void InitDestinationColumns(string tableName, ColumnCollection columns) + private void InitDestinationColumns(string tableName, ColumnCollection columns) + { + Dictionary destinationColumnDictionary = null; + if (!DestinationColumns.TryGetValue(tableName, out destinationColumnDictionary)) { - Dictionary destinationColumnDictionary = null; - if (!DestinationColumns.TryGetValue(tableName, out destinationColumnDictionary)) - { - destinationColumnDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); - DestinationColumns.Add(tableName, destinationColumnDictionary); - } - foreach (var destinationColumn in columns) + destinationColumnDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + DestinationColumns.Add(tableName, destinationColumnDictionary); + } + foreach (var destinationColumn in columns) + { + if (!destinationColumnDictionary.ContainsKey(destinationColumn.Name)) { - if (!destinationColumnDictionary.ContainsKey(destinationColumn.Name)) - { - destinationColumnDictionary.Add(destinationColumn.Name, destinationColumn); - } + destinationColumnDictionary.Add(destinationColumn.Name, destinationColumn); } } + } - public void CreateTempTable(string tempTableSchema, string tempTableName, string tempTablePrefix, List destinationColumns, ILogger logger) - { - SQLTable.CreateTempTable(sqlCommand, tempTableSchema, tempTableName, tempTablePrefix, destinationColumns, logger); - } - internal void CreateTempTables() + public void CreateTempTable(string tempTableSchema, string tempTableName, string tempTablePrefix, List destinationColumns, ILogger logger) + { + SQLTable.CreateTempTable(sqlCommand, tempTableSchema, tempTableName, tempTablePrefix, destinationColumns, logger); + } + internal void CreateTempTables() + { + foreach (Table table in job.Destination.GetSchema().GetTables()) { - foreach (Table table in job.Destination.GetSchema().GetTables()) + Dictionary columnMappingDictionary = null; + if (DestinationColumnMappings.TryGetValue(table.Name, out columnMappingDictionary)) { - Dictionary columnMappingDictionary = null; - if (DestinationColumnMappings.TryGetValue(table.Name, out columnMappingDictionary)) - { - List destColumns = new List(); - Dictionary destinationTableColumns = DestinationColumns[table.Name]; - foreach (Column column in columnMappingDictionary.Values) - { - destColumns.Add((SqlColumn)column); - } - switch (table.Name) - { - case "EcomVariantGroups": - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantGroupID"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantGroupLanguageID"); - break; - case "EcomVariantsOptions": - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantOptionID"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantOptionLanguageID"); - break; - case "EcomProducts": - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantID"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductID"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductLanguageID"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantProdCounter"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantGroupCounter"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantCounter"); - break; - case "EcomProductCategoryFieldValue": - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueProductId"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueProductVariantId"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueProductLanguageId"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueFieldCategoryId"); - break; - case "EcomPrices": - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "PriceID"); - EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "PriceCurrency"); - break; - case "EcomGroups": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "GroupLanguageID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "GroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - break; - case "EcomProductsRelated": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedProductID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedProductRelID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedGroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedProductRelVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - break; - case "EcomStockUnit": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "StockUnitProductID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "StockUnitVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "StockUnitID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - break; - case "EcomManufacturers": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "ManufacturerID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "ManufacturerName", typeof(string), SqlDbType.NVarChar, null, 252, false, false, false); - break; - case "EcomDetails": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "DetailID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - break; - case "EcomAssortmentPermissions": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "AssortmentPermissionAccessUserID", typeof(string), SqlDbType.Int, null, -1, false, true, false); - break; - case "EcomVariantOptionsProductRelation": - EnsureDestinationColumn(columnMappingDictionary, destColumns, "VariantOptionsProductRelationProductID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - EnsureDestinationColumn(columnMappingDictionary, destColumns, "VariantOptionsProductRelationVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); - break; - } - List tableMappings = null; - if (Mappings.TryGetValue(table.Name, out tableMappings)) - { - foreach (var mapping in tableMappings) - { - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport" + mapping.GetId(), destColumns, logger); - AddTableToDataset(destColumns, GetTableName(table.Name, mapping)); - } - } + List destColumns = new List(); + Dictionary destinationTableColumns = DestinationColumns[table.Name]; + foreach (Column column in columnMappingDictionary.Values) + { + destColumns.Add((SqlColumn)column); } - else + switch (table.Name) + { + case "EcomVariantGroups": + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantGroupID"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantGroupLanguageID"); + break; + case "EcomVariantsOptions": + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantOptionID"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "VariantOptionLanguageID"); + break; + case "EcomProducts": + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantID"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductID"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductLanguageID"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantProdCounter"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantGroupCounter"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "ProductVariantCounter"); + break; + case "EcomProductCategoryFieldValue": + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueProductId"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueProductVariantId"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueProductLanguageId"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "FieldValueFieldCategoryId"); + break; + case "EcomPrices": + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "PriceID"); + EnsureDestinationColumn(columnMappingDictionary, destColumns, destinationTableColumns, "PriceCurrency"); + break; + case "EcomGroups": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "GroupLanguageID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "GroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + break; + case "EcomProductsRelated": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedProductID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedProductRelID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedGroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "ProductRelatedProductRelVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + break; + case "EcomStockUnit": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "StockUnitProductID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "StockUnitVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "StockUnitID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + break; + case "EcomManufacturers": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "ManufacturerID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "ManufacturerName", typeof(string), SqlDbType.NVarChar, null, 252, false, false, false); + break; + case "EcomDetails": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "DetailID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + break; + case "EcomAssortmentPermissions": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "AssortmentPermissionAccessUserID", typeof(string), SqlDbType.Int, null, -1, false, true, false); + break; + case "EcomVariantOptionsProductRelation": + EnsureDestinationColumn(columnMappingDictionary, destColumns, "VariantOptionsProductRelationProductID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + EnsureDestinationColumn(columnMappingDictionary, destColumns, "VariantOptionsProductRelationVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false); + break; + } + List tableMappings = null; + if (Mappings.TryGetValue(table.Name, out tableMappings)) { - List destColumns = new List(); - switch (table.Name) + foreach (var mapping in tableMappings) { - //add columns to destTables for current Table, if it's needed when the table is not included in the mapping - case "EcomProducts": - break; - case "EcomGroups": - destColumns.Add(new SqlColumn("GroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("GroupLanguageID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("GroupName", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomVariantGroups": - destColumns.Add(new SqlColumn("VariantGroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("VariantGroupLanguageID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("VariantGroupName", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomVariantsOptions": - destColumns.Add(new SqlColumn("VariantOptionID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("VariantOptionLanguageID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("VariantOptionName", typeof(string), SqlDbType.NVarChar, null, 255, false, false, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomManufacturers": - destColumns.Add(new SqlColumn("ManufacturerID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("ManufacturerName", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomProductsRelated": - destColumns.Add(new SqlColumn("ProductRelatedProductID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("ProductRelatedProductRelID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("ProductRelatedGroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("ProductRelatedProductRelVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomLanguages": - destColumns.Add(new SqlColumn("LanguageID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("LanguageCode2", typeof(string), SqlDbType.NVarChar, null, 50, false, false, false)); - destColumns.Add(new SqlColumn("LanguageName", typeof(string), SqlDbType.NVarChar, null, 255, false, false, false)); - destColumns.Add(new SqlColumn("LanguageNativeName", typeof(string), SqlDbType.NVarChar, null, 255, false, false, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomVariantOptionsProductRelation": - destColumns.Add(new SqlColumn("VariantOptionsProductRelationProductID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("VariantOptionsProductRelationVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; - case "EcomProductCategoryFieldValue": - destColumns.Add(new SqlColumn("FieldValueFieldId", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("FieldValueFieldCategoryId", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("FieldValueProductId", typeof(string), SqlDbType.NVarChar, null, 30, false, true, false)); - destColumns.Add(new SqlColumn("FieldValueProductVariantId", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); - destColumns.Add(new SqlColumn("FieldValueProductLanguageId", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); - destColumns.Add(new SqlColumn("FieldValueValue", typeof(string), SqlDbType.NVarChar, null, -1, false, true, false)); - CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); - AddTableToDataset(destColumns, table.Name); - break; + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport" + mapping.GetId(), destColumns, logger); + AddTableToDataset(destColumns, GetTableName(table.Name, mapping)); } } } - - //Create product group relation temp table - List groupProductRelationColumns = new List + else { - new SqlColumn("GroupProductRelationGroupId",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("GroupProductRelationProductId",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("GroupProductRelationSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false), - new SqlColumn("GroupProductRelationIsPrimary",typeof(bool),SqlDbType.Bit,null,-1,false,false,false) - }; - CreateTempTable(null, "EcomGroupProductRelation", "TempTableForBulkImport", groupProductRelationColumns, logger); - AddTableToDataset(groupProductRelationColumns, "EcomGroupProductRelation"); + List destColumns = new List(); + switch (table.Name) + { + //add columns to destTables for current Table, if it's needed when the table is not included in the mapping + case "EcomProducts": + break; + case "EcomGroups": + destColumns.Add(new SqlColumn("GroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("GroupLanguageID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("GroupName", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomVariantGroups": + destColumns.Add(new SqlColumn("VariantGroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("VariantGroupLanguageID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("VariantGroupName", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomVariantsOptions": + destColumns.Add(new SqlColumn("VariantOptionID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("VariantOptionLanguageID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("VariantOptionName", typeof(string), SqlDbType.NVarChar, null, 255, false, false, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomManufacturers": + destColumns.Add(new SqlColumn("ManufacturerID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("ManufacturerName", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomProductsRelated": + destColumns.Add(new SqlColumn("ProductRelatedProductID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("ProductRelatedProductRelID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("ProductRelatedGroupID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("ProductRelatedProductRelVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomLanguages": + destColumns.Add(new SqlColumn("LanguageID", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("LanguageCode2", typeof(string), SqlDbType.NVarChar, null, 50, false, false, false)); + destColumns.Add(new SqlColumn("LanguageName", typeof(string), SqlDbType.NVarChar, null, 255, false, false, false)); + destColumns.Add(new SqlColumn("LanguageNativeName", typeof(string), SqlDbType.NVarChar, null, 255, false, false, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomVariantOptionsProductRelation": + destColumns.Add(new SqlColumn("VariantOptionsProductRelationProductID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("VariantOptionsProductRelationVariantID", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + case "EcomProductCategoryFieldValue": + destColumns.Add(new SqlColumn("FieldValueFieldId", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("FieldValueFieldCategoryId", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("FieldValueProductId", typeof(string), SqlDbType.NVarChar, null, 30, false, true, false)); + destColumns.Add(new SqlColumn("FieldValueProductVariantId", typeof(string), SqlDbType.NVarChar, null, 255, false, true, false)); + destColumns.Add(new SqlColumn("FieldValueProductLanguageId", typeof(string), SqlDbType.NVarChar, null, 50, false, true, false)); + destColumns.Add(new SqlColumn("FieldValueValue", typeof(string), SqlDbType.NVarChar, null, -1, false, true, false)); + CreateTempTable(table.SqlSchema, table.Name, "TempTableForBulkImport", destColumns, logger); + AddTableToDataset(destColumns, table.Name); + break; + } + } + } - //create product variantgroup relation temp table - List variantGroupProductRelation = new List - { - new SqlColumn("VariantgroupProductRelationProductID",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), - new SqlColumn("VariantgroupProductRelationVariantGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), - new SqlColumn("VariantgroupProductRelationID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("VariantGroupProductRelationSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false) - }; - CreateTempTable(null, "EcomVariantgroupProductRelation", "TempTableForBulkImport", variantGroupProductRelation, logger); - AddTableToDataset(variantGroupProductRelation, "EcomVariantgroupProductRelation"); + //Create product group relation temp table + List groupProductRelationColumns = new List + { + new SqlColumn("GroupProductRelationGroupId",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("GroupProductRelationProductId",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("GroupProductRelationSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false), + new SqlColumn("GroupProductRelationIsPrimary",typeof(bool),SqlDbType.Bit,null,-1,false,false,false) + }; + CreateTempTable(null, "EcomGroupProductRelation", "TempTableForBulkImport", groupProductRelationColumns, logger); + AddTableToDataset(groupProductRelationColumns, "EcomGroupProductRelation"); + + //create product variantgroup relation temp table + List variantGroupProductRelation = new List + { + new SqlColumn("VariantgroupProductRelationProductID",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), + new SqlColumn("VariantgroupProductRelationVariantGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), + new SqlColumn("VariantgroupProductRelationID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("VariantGroupProductRelationSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false) + }; + CreateTempTable(null, "EcomVariantgroupProductRelation", "TempTableForBulkImport", variantGroupProductRelation, logger); + AddTableToDataset(variantGroupProductRelation, "EcomVariantgroupProductRelation"); + + //Create ShopGroupRelation temp table + List shopGroupRelations = new List + { + new SqlColumn("ShopGroupShopID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("ShopGroupGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("ShopGroupRelationsSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false) + }; + CreateTempTable(null, "EcomShopGroupRelation", "TempTableForBulkImport", shopGroupRelations, logger); + AddTableToDataset(shopGroupRelations, "EcomShopGroupRelation"); + + //Create Shop relation table + List shops = new List + { + new SqlColumn("ShopID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("ShopName",typeof(string),SqlDbType.NVarChar,null,255,false,true,false) + }; + CreateTempTable(null, "EcomShops", "TempTableForBulkImport", shops, logger); + AddTableToDataset(shops, "EcomShops"); + + //Create Product-relatedGroup temp table + List productsRelatedGroups = new List + { + new SqlColumn("RelatedGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("RelatedGroupName",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), + new SqlColumn("RelatedGroupLanguageID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false) + }; + CreateTempTable(null, "EcomProductsRelatedGroups", "TempTableForBulkImport", productsRelatedGroups, logger); + AddTableToDataset(productsRelatedGroups, "EcomProductsRelatedGroups"); + + //Create EcomGroupRelations temp table + List groupRelations = new List + { + new SqlColumn("GroupRelationsGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), + new SqlColumn("GroupRelationsParentID",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), + new SqlColumn("GroupRelationsSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false) + }; + CreateTempTable(null, "EcomGroupRelations", "TempTableForBulkImport", groupRelations, logger); + AddTableToDataset(groupRelations, "EcomGroupRelations"); + } - //Create ShopGroupRelation temp table - List shopGroupRelations = new List - { - new SqlColumn("ShopGroupShopID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("ShopGroupGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("ShopGroupRelationsSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false) - }; - CreateTempTable(null, "EcomShopGroupRelation", "TempTableForBulkImport", shopGroupRelations, logger); - AddTableToDataset(shopGroupRelations, "EcomShopGroupRelation"); + private static void EnsureDestinationColumn(Dictionary columnMappingDictionary, List destColumns, string columnName, Type type, SqlDbType dbType, Table dbtable, int limit, bool isIdentity, bool isPrimaryKey, bool isNew) + { + if (!columnMappingDictionary.ContainsKey(columnName)) + { + destColumns.Add(new SqlColumn(columnName, type, dbType, dbtable, limit, isIdentity, isPrimaryKey, isNew)); + } + } - //Create Shop relation table - List shops = new List - { - new SqlColumn("ShopID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("ShopName",typeof(string),SqlDbType.NVarChar,null,255,false,true,false) - }; - CreateTempTable(null, "EcomShops", "TempTableForBulkImport", shops, logger); - AddTableToDataset(shops, "EcomShops"); + private static void EnsureDestinationColumn(Dictionary columnMappingDictionary, List destColumns, Dictionary destinationTableColumns, string columnName) + { + if (!columnMappingDictionary.ContainsKey(columnName)) + { + destColumns.Add((SqlColumn)destinationTableColumns[columnName]); + } + } - //Create Product-relatedGroup temp table - List productsRelatedGroups = new List - { - new SqlColumn("RelatedGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("RelatedGroupName",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), - new SqlColumn("RelatedGroupLanguageID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false) - }; - CreateTempTable(null, "EcomProductsRelatedGroups", "TempTableForBulkImport", productsRelatedGroups, logger); - AddTableToDataset(productsRelatedGroups, "EcomProductsRelatedGroups"); + private string GetTableName(string name, Mapping mapping) + { + return $"{name}${mapping.GetId()}"; + } - //Create EcomGroupRelations temp table - List groupRelations = new List - { - new SqlColumn("GroupRelationsGroupID",typeof(string),SqlDbType.NVarChar,null,255,false,true,false), - new SqlColumn("GroupRelationsParentID",typeof(string),SqlDbType.NVarChar,null,255,false,false,false), - new SqlColumn("GroupRelationsSorting",typeof(int),SqlDbType.Int,null,-1,false,false,false) - }; - CreateTempTable(null, "EcomGroupRelations", "TempTableForBulkImport", groupRelations, logger); - AddTableToDataset(groupRelations, "EcomGroupRelations"); + private string GetTableNameWithoutPrefix(string name) + { + if (name.Contains("$")) + { + return name.Split(new char[] { '$' })[0]; } - - private static void EnsureDestinationColumn(Dictionary columnMappingDictionary, List destColumns, string columnName, Type type, SqlDbType dbType, Table dbtable, int limit, bool isIdentity, bool isPrimaryKey, bool isNew) + else { - if (!columnMappingDictionary.ContainsKey(columnName)) - { - destColumns.Add(new SqlColumn(columnName, type, dbType, dbtable, limit, isIdentity, isPrimaryKey, isNew)); - } + return name; } + } - private static void EnsureDestinationColumn(Dictionary columnMappingDictionary, List destColumns, Dictionary destinationTableColumns, string columnName) + private string GetPrefixFromTableName(string name) + { + if (name.Contains("$")) { - if (!columnMappingDictionary.ContainsKey(columnName)) - { - destColumns.Add((SqlColumn)destinationTableColumns[columnName]); - } + return name.Split(new char[] { '$' })[1]; + } + else + { + return string.Empty; } + } - private string GetTableName(string name, Mapping mapping) + private void AddTableToDataset(IEnumerable groupProductRelationColumns, string tableName) + { + var newTable = DataToWrite.Tables.Add(tableName); + foreach (SqlColumn destColumn in groupProductRelationColumns) { - return $"{name}${mapping.GetId()}"; + newTable.Columns.Add(destColumn.Name, destColumn.Type); } + DataRowsToWrite.Add(tableName, new Dictionary>()); + } - private string GetTableNameWithoutPrefix(string name) + private readonly string _defaultLanguageId; + private int _lastVariantGroupProductRelationID = -1; + private int LastVariantGroupProductRelationID + { + get { - if (name.Contains("$")) - { - return name.Split(new char[] { '$' })[0]; - } - else + if (_lastVariantGroupProductRelationID == -1) { - return name; + _lastVariantGroupProductRelationID = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING(VariantgroupProductRelationID,22,LEN(VariantgroupProductRelationID)-21) as int)+1))) as lastID from EcomVariantGroupProductRelation where VariantgroupProductRelationID like 'ImportedVARGRPPRODREL%')")); } + return _lastVariantGroupProductRelationID; } + set { _lastVariantGroupProductRelationID = value; } + } + + private int _variantGroupProductRelationSortingCounter = 0; - private string GetPrefixFromTableName(string name) + private int _lastRelatedGroupID = -1; + protected int LastRelatedGroupID + { + get { - if (name.Contains("$")) - { - return name.Split(new char[] { '$' })[1]; - } - else + if (_lastRelatedGroupID == -1) { - return string.Empty; + _lastRelatedGroupID = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(RelatedGroupID,15,LEN(RelatedGroupID)-14) as int))) as lastID from EcomProductsRelatedGroups where RelatedGroupID like 'ImportedRELGRP%')")); } + return _lastRelatedGroupID; } + set { _lastRelatedGroupID = value; } + } - private void AddTableToDataset(IEnumerable groupProductRelationColumns, string tableName) + private int _lastShopId = -1; + private int LastShopId + { + get { - var newTable = DataToWrite.Tables.Add(tableName); - foreach (SqlColumn destColumn in groupProductRelationColumns) + if (_lastShopId == -1) { - newTable.Columns.Add(destColumn.Name, destColumn.Type); + _lastShopId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( ShopID ,13, LEN(ShopID)-12) as int)+1))) as lastID from EcomShops where ShopID like 'ImportedSHOP%')")); } - DataRowsToWrite.Add(tableName, new Dictionary>()); + return _lastShopId; } + set { _lastShopId = value; } + } - private readonly string _defaultLanguageId; - private int _lastVariantGroupProductRelationID = -1; - private int LastVariantGroupProductRelationID + private int _lastProductId = -1; + private int LastProductId + { + get { - get + if (_lastProductId == -1) { - if (_lastVariantGroupProductRelationID == -1) - { - _lastVariantGroupProductRelationID = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING(VariantgroupProductRelationID,22,LEN(VariantgroupProductRelationID)-21) as int)+1))) as lastID from EcomVariantGroupProductRelation where VariantgroupProductRelationID like 'ImportedVARGRPPRODREL%')")); - } - return _lastVariantGroupProductRelationID; + _lastProductId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(ProductID,13,LEN(ProductID)-12) as int))) as lastID from EcomProducts where ProductID like 'ImportedPROD%')")); } - set { _lastVariantGroupProductRelationID = value; } + return _lastProductId; } + set { _lastProductId = value; } + } - private int _variantGroupProductRelationSortingCounter = 0; - - private int _lastRelatedGroupID = -1; - protected int LastRelatedGroupID + private int _lastGroupId = -1; + protected int LastGroupId + { + get { - get + if (_lastGroupId == -1) { - if (_lastRelatedGroupID == -1) - { - _lastRelatedGroupID = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(RelatedGroupID,15,LEN(RelatedGroupID)-14) as int))) as lastID from EcomProductsRelatedGroups where RelatedGroupID like 'ImportedRELGRP%')")); - } - return _lastRelatedGroupID; + _lastGroupId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( GroupID ,14,LEN(GroupID)-13) as int)+1))) as lastID from EcomGroups where GroupID like 'ImportedGROUP%')")); } - set { _lastRelatedGroupID = value; } + return _lastGroupId; + } + set { _lastGroupId = value; } + } - private int _lastShopId = -1; - private int LastShopId + private int _lastManufacturerId = -1; + protected int LastManufacturerId + { + get { - get + if (_lastManufacturerId == -1) { - if (_lastShopId == -1) - { - _lastShopId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( ShopID ,13, LEN(ShopID)-12) as int)+1))) as lastID from EcomShops where ShopID like 'ImportedSHOP%')")); - } - return _lastShopId; + _lastManufacturerId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( ManufacturerID ,13,LEN(ManufacturerID)-12) as int)))) as lastID from EcomManufacturers where ManufacturerID like 'ImportedMANU%')")); } - set { _lastShopId = value; } + return _lastManufacturerId; + } + set { _lastManufacturerId = value; } + } - private int _lastProductId = -1; - private int LastProductId + private int _lastVariantGroupId = -1; + private int LastVariantGroupId + { + get { - get + if (_lastVariantGroupId == -1) { - if (_lastProductId == -1) - { - _lastProductId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(ProductID,13,LEN(ProductID)-12) as int))) as lastID from EcomProducts where ProductID like 'ImportedPROD%')")); - } - return _lastProductId; + _lastVariantGroupId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( variantGroupId ,15,LEN(variantGroupId)-14) as int)+1))) as lastID from EcomVariantGroups where VariantGroupID like 'ImportedVARGRP%')")); } - set { _lastProductId = value; } + return _lastVariantGroupId; } + set { _lastVariantGroupId = value; } + } - private int _lastGroupId = -1; - protected int LastGroupId + private int _lastVariantOptionId = -1; + private int LastVariantOptionId + { + get { - get + if (_lastVariantOptionId == -1) { - if (_lastGroupId == -1) - { - _lastGroupId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( GroupID ,14,LEN(GroupID)-13) as int)+1))) as lastID from EcomGroups where GroupID like 'ImportedGROUP%')")); - } - return _lastGroupId; - + _lastVariantOptionId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( variantOptionId ,11,LEN(variantOptionId)-10) as int)+1))) as lastID from EcomVariantsOptions where VariantOptionID like 'ImportedVO%')")); } - set { _lastGroupId = value; } + return _lastVariantOptionId; } + set { _lastVariantOptionId = value; } + } - private int _lastManufacturerId = -1; - protected int LastManufacturerId + private int _lastLanguageId = -1; + protected int LastLanguageId + { + get { - get + if (_lastLanguageId == -1) { - if (_lastManufacturerId == -1) - { - _lastManufacturerId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( ManufacturerID ,13,LEN(ManufacturerID)-12) as int)))) as lastID from EcomManufacturers where ManufacturerID like 'ImportedMANU%')")); - } - return _lastManufacturerId; - + _lastLanguageId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( LanguageID ,13,LEN(LanguageID)-12) as int)+1))) as lastID from EcomLanguages where LanguageID like 'ImportedLANG%')")); } - set { _lastManufacturerId = value; } + return _lastLanguageId; + } + set { _lastLanguageId = value; } + } - private int _lastVariantGroupId = -1; - private int LastVariantGroupId + private int _lastPriceId = -1; + private int LastPriceId + { + get { - get + if (_lastPriceId == -1) { - if (_lastVariantGroupId == -1) - { - _lastVariantGroupId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( variantGroupId ,15,LEN(variantGroupId)-14) as int)+1))) as lastID from EcomVariantGroups where VariantGroupID like 'ImportedVARGRP%')")); - } - return _lastVariantGroupId; + _lastPriceId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(PriceID,14,LEN(PriceID)-13) as int))) as lastID from EcomPrices where PriceID like 'ImportedPRICE%')")); } - set { _lastVariantGroupId = value; } + return _lastPriceId; } + set { _lastPriceId = value; } + } - private int _lastVariantOptionId = -1; - private int LastVariantOptionId + private int _lastDetailId = -1; + private int LastDetailId + { + get { - get + if (_lastDetailId == -1) { - if (_lastVariantOptionId == -1) - { - _lastVariantOptionId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( variantOptionId ,11,LEN(variantOptionId)-10) as int)+1))) as lastID from EcomVariantsOptions where VariantOptionID like 'ImportedVO%')")); - } - return _lastVariantOptionId; + _lastDetailId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(DetailID,15,LEN(DetailID)-14) as int))) as lastID from EcomDetails where DetailID like 'ImportedDETAIL%')")); } - set { _lastVariantOptionId = value; } + return _lastDetailId; } + set { _lastDetailId = value; } + } - private int _lastLanguageId = -1; - protected int LastLanguageId + private int GetLastId(CommandBuilder commandBuilder) + { + using (var reader = Database.CreateDataReader(commandBuilder, sqlCommand.Connection)) { - get + if (reader.Read()) { - if (_lastLanguageId == -1) - { - _lastLanguageId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,(MAX(CAST(SUBSTRING( LanguageID ,13,LEN(LanguageID)-12) as int)+1))) as lastID from EcomLanguages where LanguageID like 'ImportedLANG%')")); - } - return _lastLanguageId; - + return reader["lastID"] != DBNull.Value ? int.Parse((string)reader["lastID"]) : 0; } - set { _lastLanguageId = value; } + return 0; } + } - private int _lastPriceId = -1; - private int LastPriceId + internal Dictionary _variantGroups = null; + private Dictionary VariantGroups + { + get { - get + if (_variantGroups == null) { - if (_lastPriceId == -1) + _variantGroups = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select variantGroupID, variantgroupName, VariantGroupLanguageID from EcomVariantGroups"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _lastPriceId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(PriceID,14,LEN(PriceID)-13) as int))) as lastID from EcomPrices where PriceID like 'ImportedPRICE%')")); + var rowId = Converter.ToString(row["variantGroupID"]); + if (!_variantGroups.ContainsKey(rowId)) + { + _variantGroups.Add(rowId, row); + } } - return _lastPriceId; } - set { _lastPriceId = value; } + return _variantGroups; } + } - private int _lastDetailId = -1; - private int LastDetailId + internal HashSet _existingVariantOptionsList; + private HashSet GetVariantOptionList() + { + if (_existingVariantOptionsList == null) { - get + _existingVariantOptionsList = new HashSet(); + using (var reader = Database.CreateDataReader(CommandBuilder.Create("select VariantOptionID from EcomVariantsOptions"), sqlCommand.Connection)) { - if (_lastDetailId == -1) + while (reader.Read()) { - _lastDetailId = GetLastId(CommandBuilder.Create("(select convert(nvarchar,MAX(CAST(SUBSTRING(DetailID,15,LEN(DetailID)-14) as int))) as lastID from EcomDetails where DetailID like 'ImportedDETAIL%')")); + _existingVariantOptionsList.Add(reader["VariantOptionID"].ToString()); } - return _lastDetailId; } - set { _lastDetailId = value; } } + return _existingVariantOptionsList; + } - private int GetLastId(CommandBuilder commandBuilder) + internal Dictionary _ecomLanguages; + private Dictionary EcomLanguages + { + get { - using (var reader = Database.CreateDataReader(commandBuilder, sqlCommand.Connection)) + if (_ecomLanguages == null) { - if (reader.Read()) + _ecomLanguages = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select LanguageID, LanguageCode2, LanguageName from EcomLanguages"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - return reader["lastID"] != DBNull.Value ? int.Parse((string)reader["lastID"]) : 0; + var languageId = Converter.ToString(row["LanguageID"]); + _ecomLanguages.Add(languageId, row); } - return 0; } + return _ecomLanguages; } + } - internal Dictionary _variantGroups = null; - private Dictionary VariantGroups + internal Dictionary> _productsRelatedGroups; + protected Dictionary> ProductsRelatedGroups + { + get { - get + if (_productsRelatedGroups == null) { - if (_variantGroups == null) + _productsRelatedGroups = new Dictionary>(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select RelatedGroupID, RelatedGroupName, RelatedGroupLanguageID from EcomProductsRelatedGroups"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _variantGroups = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select variantGroupID, variantgroupName, VariantGroupLanguageID from EcomVariantGroups"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) + var rowId = Converter.ToString(row["RelatedGroupID"]); + List rows = null; + if (!_productsRelatedGroups.TryGetValue(rowId, out rows)) { - var rowId = Converter.ToString(row["variantGroupID"]); - if (!_variantGroups.ContainsKey(rowId)) - { - _variantGroups.Add(rowId, row); - } + rows = new List(); + _productsRelatedGroups.Add(rowId, rows); } + rows.Add(row); } - return _variantGroups; } + return _productsRelatedGroups; + } + } - internal HashSet _existingVariantOptionsList; - private HashSet GetVariantOptionList() + internal Dictionary _productManufacturers; + protected Dictionary ProductManufacturers + { + get { - if (_existingVariantOptionsList == null) + if (_productManufacturers == null) { - _existingVariantOptionsList = new HashSet(); - using (var reader = Database.CreateDataReader(CommandBuilder.Create("select VariantOptionID from EcomVariantsOptions"), sqlCommand.Connection)) + _productManufacturers = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select ManufacturerID, ManufacturerName from EcomManufacturers"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - while (reader.Read()) + var rowId = Converter.ToString(row["ManufacturerID"]); + if (!_productManufacturers.ContainsKey(rowId)) { - _existingVariantOptionsList.Add(reader["VariantOptionID"].ToString()); + _productManufacturers.Add(rowId, row); } } } - return _existingVariantOptionsList; + return _productManufacturers; + } + } - internal Dictionary _ecomLanguages; - private Dictionary EcomLanguages + internal Dictionary _ecomShops; + private Dictionary EcomShops + { + get { - get + if (_ecomShops == null) { - if (_ecomLanguages == null) + _ecomShops = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select ShopID, ShopName from EcomShops"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _ecomLanguages = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select LanguageID, LanguageCode2, LanguageName from EcomLanguages"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - var languageId = Converter.ToString(row["LanguageID"]); - _ecomLanguages.Add(languageId, row); - } + var shopId = Converter.ToString(row["ShopID"]); + _ecomShops.Add(shopId, row); } - return _ecomLanguages; } + return _ecomShops; } + } - internal Dictionary> _productsRelatedGroups; - protected Dictionary> ProductsRelatedGroups + internal Dictionary> _productGroups; + protected Dictionary> ProductGroups + { + get { - get + if (_productGroups == null) { - if (_productsRelatedGroups == null) + _productGroups = new Dictionary>(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupID, GroupName, GroupNumber from EcomGroups"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _productsRelatedGroups = new Dictionary>(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select RelatedGroupID, RelatedGroupName, RelatedGroupLanguageID from EcomProductsRelatedGroups"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) + var rowId = Converter.ToString(row["GroupID"]); + List rows = null; + if (!_productGroups.TryGetValue(rowId, out rows)) { - var rowId = Converter.ToString(row["RelatedGroupID"]); - List rows = null; - if (!_productsRelatedGroups.TryGetValue(rowId, out rows)) - { - rows = new List(); - _productsRelatedGroups.Add(rowId, rows); - } - rows.Add(row); + rows = new List(); + _productGroups.Add(rowId, rows); } + rows.Add(row); } - return _productsRelatedGroups; - } + return _productGroups; + } + } - internal Dictionary _productManufacturers; - protected Dictionary ProductManufacturers + internal Dictionary _productCategoryFields; + protected Dictionary ProductCategoryFields + { + get { - get + if (_productCategoryFields == null) { - if (_productManufacturers == null) + _productCategoryFields = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("SELECT FieldId, FieldCategoryId FROM EcomProductCategoryField"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _productManufacturers = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select ManufacturerID, ManufacturerName from EcomManufacturers"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) + var fieldId = Converter.ToString(row["FieldId"]); + var categoryId = Converter.ToString(row["FieldCategoryId"]); + + var rowId = $"{categoryId}|{fieldId}"; + if (!_productCategoryFields.ContainsKey(rowId)) { - var rowId = Converter.ToString(row["ManufacturerID"]); - if (!_productManufacturers.ContainsKey(rowId)) - { - _productManufacturers.Add(rowId, row); - } + _productCategoryFields.Add(rowId, row); } } - return _productManufacturers; - } + return _productCategoryFields; } + } - internal Dictionary _ecomShops; - private Dictionary EcomShops + internal readonly string _sortingKeySeparator = ";"; + internal Dictionary _shopGroupRelationSorting = null; + protected Dictionary ShopGroupRelationSorting + { + get { - get + if (_shopGroupRelationSorting == null) { - if (_ecomShops == null) + _shopGroupRelationSorting = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select ShopGroupShopID, ShopGroupGroupID, ShopGroupRelationsSorting from EcomShopGroupRelation"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _ecomShops = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select ShopID, ShopName from EcomShops"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - var shopId = Converter.ToString(row["ShopID"]); - _ecomShops.Add(shopId, row); - } + var shopId = Converter.ToString(row["ShopGroupShopID"]); + var groupId = Converter.ToString(row["ShopGroupGroupID"]); + var sort = Converter.ToInt32(row["ShopGroupRelationsSorting"]); + _shopGroupRelationSorting.Add($"{shopId}{_sortingKeySeparator}{groupId}", sort); } - return _ecomShops; } + return _shopGroupRelationSorting; } + } - internal Dictionary> _productGroups; - protected Dictionary> ProductGroups + internal Dictionary _groupRelationSorting = null; + protected Dictionary GroupRelationSorting + { + get { - get + if (_groupRelationSorting == null) { - if (_productGroups == null) + _groupRelationSorting = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupRelationsGroupId, GroupRelationsParentId, GroupRelationsSorting from EcomGroupRelations where GroupRelationsSorting > 0"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _productGroups = new Dictionary>(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupID, GroupName, GroupNumber from EcomGroups"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - var rowId = Converter.ToString(row["GroupID"]); - List rows = null; - if (!_productGroups.TryGetValue(rowId, out rows)) - { - rows = new List(); - _productGroups.Add(rowId, rows); - } - rows.Add(row); - } + var groupId = Converter.ToString(row["GroupRelationsGroupId"]); + var parentId = Converter.ToString(row["GroupRelationsParentId"]); + var sort = Converter.ToInt32(row["GroupRelationsSorting"]); + _groupRelationSorting.Add($"{groupId}{_sortingKeySeparator}{parentId}", sort); } - return _productGroups; - } + return _groupRelationSorting; } + } - internal Dictionary _productCategoryFields; - protected Dictionary ProductCategoryFields + internal DataTable _existingProducts; + protected DataTable ExistingProducts + { + get { - get + if (_existingProducts == null) { - if (_productCategoryFields == null) + List columnsToSelect = new List() { + "ProductID", "ProductLanguageID", "ProductVariantID","ProductNumber", "ProductName", + "ProductVariantCounter", "ProductVariantGroupCounter","ProductVariantProdCounter" + }; + IEnumerable ecomProductsPKColumns = MappingIdEcomProductsPKColumns.Values.SelectMany(i => i); + if (ecomProductsPKColumns != null) { - _productCategoryFields = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("SELECT FieldId, FieldCategoryId FROM EcomProductCategoryField"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - var fieldId = Converter.ToString(row["FieldId"]); - var categoryId = Converter.ToString(row["FieldCategoryId"]); - - var rowId = $"{categoryId}|{fieldId}"; - if (!_productCategoryFields.ContainsKey(rowId)) - { - _productCategoryFields.Add(rowId, row); - } - } + IEnumerable columnsToAdd = ecomProductsPKColumns.Where(c => + !columnsToSelect.Any(cs => string.Equals(c, cs, StringComparison.OrdinalIgnoreCase))); + columnsToSelect.AddRange(columnsToAdd); } - return _productCategoryFields; + string sql = "select " + string.Join(",", columnsToSelect) + " from EcomProducts"; + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create(sql), sqlCommand.Connection); + _existingProducts = dataSet.Tables[0]; } + return _existingProducts; } + } - internal readonly string _sortingKeySeparator = ";"; - internal Dictionary _shopGroupRelationSorting = null; - protected Dictionary ShopGroupRelationSorting + private Dictionary _productNumberVariantIds; + protected Dictionary ProductNumberVariantIds + { + get { - get + if (_productNumberVariantIds == null) { - if (_shopGroupRelationSorting == null) + _productNumberVariantIds = new Dictionary(); + + if (_useProductIdFoundByNumber) { - _shopGroupRelationSorting = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select ShopGroupShopID, ShopGroupGroupID, ShopGroupRelationsSorting from EcomShopGroupRelation"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) + foreach (var row in ExistingProducts.Select("ProductNumber <> '' and ProductVariantID <> '' and ProductLanguageID = '" + _defaultLanguageId + "'")) { - var shopId = Converter.ToString(row["ShopGroupShopID"]); - var groupId = Converter.ToString(row["ShopGroupGroupID"]); - var sort = Converter.ToInt32(row["ShopGroupRelationsSorting"]); - _shopGroupRelationSorting.Add($"{shopId}{_sortingKeySeparator}{groupId}", sort); + string number = row["ProductNumber"].ToString(); + if (!_productNumberVariantIds.ContainsKey(number)) + { + _productNumberVariantIds.Add(number, (row["ProductID"].ToString(), row["ProductVariantID"].ToString())); + } } } - return _shopGroupRelationSorting; } + return _productNumberVariantIds; } + } - internal Dictionary _groupRelationSorting = null; - protected Dictionary GroupRelationSorting + internal Dictionary> _existingGroupProductRelations = null; + protected Dictionary> ExistingGroupProductRelations + { + get { - get + if (_existingGroupProductRelations == null) { - if (_groupRelationSorting == null) + _existingGroupProductRelations = new Dictionary>(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupProductRelationGroupID, GroupProductRelationProductID, GroupProductRelationIsPrimary, GroupProductRelationSorting from EcomGroupProductRelation where GroupProductRelationSorting <> 0"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - _groupRelationSorting = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupRelationsGroupId, GroupRelationsParentId, GroupRelationsSorting from EcomGroupRelations where GroupRelationsSorting > 0"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - var groupId = Converter.ToString(row["GroupRelationsGroupId"]); - var parentId = Converter.ToString(row["GroupRelationsParentId"]); - var sort = Converter.ToInt32(row["GroupRelationsSorting"]); - _groupRelationSorting.Add($"{groupId}{_sortingKeySeparator}{parentId}", sort); - } + string productId = Converter.ToString(row["GroupProductRelationProductId"]); + string groupId = Converter.ToString(row["GroupProductRelationGroupId"]); + bool isPrimary = Converter.ToBoolean(row["GroupProductRelationIsPrimary"]); + int sort = Converter.ToInt32(row["GroupProductRelationSorting"]); + string key = $"{groupId}{_sortingKeySeparator}{productId}"; + _existingGroupProductRelations.Add(key, new Tuple(isPrimary, sort)); } - return _groupRelationSorting; } + return _existingGroupProductRelations; } + } - internal DataTable _existingProducts; - protected DataTable ExistingProducts + internal Dictionary _primaryGroupProductRelations = null; + protected Dictionary PrimaryGroupProductRelations + { + get { - get + if (_primaryGroupProductRelations == null) { - if (_existingProducts == null) + _primaryGroupProductRelations = new Dictionary(StringComparer.OrdinalIgnoreCase); + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupProductRelationGroupID, GroupProductRelationProductID, GroupProductRelationSorting from EcomGroupProductRelation where GroupProductRelationIsPrimary = 1"), sqlCommand.Connection); + foreach (DataRow row in dataSet.Tables[0].Rows) { - List columnsToSelect = new List() { - "ProductID", "ProductLanguageID", "ProductVariantID","ProductNumber", "ProductName", - "ProductVariantCounter", "ProductVariantGroupCounter","ProductVariantProdCounter" - }; - IEnumerable ecomProductsPKColumns = MappingIdEcomProductsPKColumns.Values.SelectMany(i => i); - if (ecomProductsPKColumns != null) + var rowId = Converter.ToString(row["GroupProductRelationProductID"]); + if (!_primaryGroupProductRelations.ContainsKey(rowId)) { - IEnumerable columnsToAdd = ecomProductsPKColumns.Where(c => - !columnsToSelect.Any(cs => string.Equals(c, cs, StringComparison.OrdinalIgnoreCase))); - columnsToSelect.AddRange(columnsToAdd); + _primaryGroupProductRelations.Add(rowId, row); } - string sql = "select " + string.Join(",", columnsToSelect) + " from EcomProducts"; - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create(sql), sqlCommand.Connection); - _existingProducts = dataSet.Tables[0]; } - return _existingProducts; } + return _primaryGroupProductRelations; + } + } - private Dictionary _productNumberVariantIds; - protected Dictionary ProductNumberVariantIds + private DataTable _existingUsers; + private DataTable ExistingUsers + { + get { - get + if (_existingUsers == null) { - if (_productNumberVariantIds == null) - { - _productNumberVariantIds = new Dictionary(); - - if (_useProductIdFoundByNumber) - { - foreach (var row in ExistingProducts.Select("ProductNumber <> '' and ProductVariantID <> '' and ProductLanguageID = '" + _defaultLanguageId + "'")) - { - string number = row["ProductNumber"].ToString(); - if (!_productNumberVariantIds.ContainsKey(number)) - { - _productNumberVariantIds.Add(number, (row["ProductID"].ToString(), row["ProductVariantID"].ToString())); - } - } - } - } - return _productNumberVariantIds; + DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select AccessUserID, AccessUserUserName, AccessUserCustomerNumber, AccessUserExternalID from AccessUser"), sqlCommand.Connection); + _existingUsers = dataSet.Tables[0]; } + return _existingUsers; } + } + + private int _currentlyWritingMappingId = 0; + private long _writtenRowsCount = 0; + public void Write(Dictionary row, Mapping mapping, bool discardDuplicates) + { + Dictionary columnMappings = null; + DataRow dataRow = DataToWrite.Tables[GetTableName(mapping.DestinationTable.Name, mapping)].NewRow(); - internal Dictionary> _existingGroupProductRelations = null; - protected Dictionary> ExistingGroupProductRelations + var mappingColumns = ColumnMappingsByMappingId[mapping.GetId()]; + foreach (ColumnMapping columnMapping in mappingColumns) { - get + if ((columnMapping.SourceColumn != null && row.ContainsKey(columnMapping.SourceColumn.Name)) || columnMapping.HasScriptWithValue) { - if (_existingGroupProductRelations == null) + if (columnMapping.HasScriptWithValue) { - _existingGroupProductRelations = new Dictionary>(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupProductRelationGroupID, GroupProductRelationProductID, GroupProductRelationIsPrimary, GroupProductRelationSorting from EcomGroupProductRelation where GroupProductRelationSorting <> 0"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - string productId = Converter.ToString(row["GroupProductRelationProductId"]); - string groupId = Converter.ToString(row["GroupProductRelationGroupId"]); - bool isPrimary = Converter.ToBoolean(row["GroupProductRelationIsPrimary"]); - int sort = Converter.ToInt32(row["GroupProductRelationSorting"]); - string key = $"{groupId}{_sortingKeySeparator}{productId}"; - _existingGroupProductRelations.Add(key, new Tuple(isPrimary, sort)); - } + dataRow[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue(); + } + else + { + dataRow[columnMapping.DestinationColumn.Name] = columnMapping.ConvertInputToOutputFormat(row[columnMapping.SourceColumn.Name]); } - return _existingGroupProductRelations; } - } - - internal Dictionary _primaryGroupProductRelations = null; - protected Dictionary PrimaryGroupProductRelations - { - get + else { - if (_primaryGroupProductRelations == null) + if (columnMapping.Active) { - _primaryGroupProductRelations = new Dictionary(StringComparer.OrdinalIgnoreCase); - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select GroupProductRelationGroupID, GroupProductRelationProductID, GroupProductRelationSorting from EcomGroupProductRelation where GroupProductRelationIsPrimary = 1"), sqlCommand.Connection); - foreach (DataRow row in dataSet.Tables[0].Rows) - { - var rowId = Converter.ToString(row["GroupProductRelationProductID"]); - if (!_primaryGroupProductRelations.ContainsKey(rowId)) - { - _primaryGroupProductRelations.Add(rowId, row); - } - } + throw new Exception(BaseDestinationWriter.GetRowValueNotFoundMessage(row, columnMapping.SourceColumn.Table.Name, columnMapping.SourceColumn.Name)); } - return _primaryGroupProductRelations; - } } - private DataTable _existingUsers; - private DataTable ExistingUsers + columnMappings = SourceColumnMappings[mapping.GetId()]; + switch (mapping.DestinationTable.Name) { - get - { - if (_existingUsers == null) + case "EcomProductCategoryFieldValue": + WriteCategoyFieldValues(row, columnMappings, dataRow); + break; + case "EcomVariantGroups": + WriteVariantGroups(row, columnMappings, dataRow); + break; + case "EcomVariantsOptions": + WriteVariantOptions(row, columnMappings, dataRow); + break; + case "EcomManufacturers": + WriteManufacturers(row, columnMappings, dataRow); + break; + case "EcomGroups": + WriteGroups(row, columnMappings, dataRow); + break; + case "EcomProducts": + if (!WriteProducts(row, mapping, columnMappings, dataRow)) { - DataSet dataSet = Database.CreateDataSet(CommandBuilder.Create("select AccessUserID, AccessUserUserName, AccessUserCustomerNumber, AccessUserExternalID from AccessUser"), sqlCommand.Connection); - _existingUsers = dataSet.Tables[0]; + return; } - return _existingUsers; - } + break; + case "EcomProductsRelated": + WriteRelatedProducts(row, columnMappings, dataRow); + break; + case "EcomPrices": + WritePrices(row, columnMappings, dataRow); + break; + case "EcomDetails": + WriteDetails(row, columnMappings, dataRow); + break; + case "EcomAssortmentPermissions": + if (!WriteAssortments(row, columnMappings, dataRow)) + { + DataRowsToWrite[GetTableName(mapping.DestinationTable.Name, mapping)].Add(RowAutoId++.ToString(), new List() { dataRow }); + return; + } + break; } - private int _currentlyWritingMappingId = 0; - private long _writtenRowsCount = 0; - public void Write(Dictionary row, Mapping mapping, bool discardDuplicates) + foreach (ColumnMapping columnMapping in mappingColumns) { - Dictionary columnMappings = null; - DataRow dataRow = DataToWrite.Tables[GetTableName(mapping.DestinationTable.Name, mapping)].NewRow(); - - var mappingColumns = ColumnMappingsByMappingId[mapping.GetId()]; - foreach (ColumnMapping columnMapping in mappingColumns) + object rowValue = null; + if (columnMapping.HasScriptWithValue || row.TryGetValue(columnMapping.SourceColumn?.Name, out rowValue)) { - if ((columnMapping.SourceColumn != null && row.ContainsKey(columnMapping.SourceColumn.Name)) || columnMapping.HasScriptWithValue) + object dataToRow = columnMapping.ConvertInputValueToOutputValue(rowValue); + + if (mappingColumns.Any(obj => obj.DestinationColumn.Name == columnMapping.DestinationColumn.Name && obj.GetId() != columnMapping.GetId())) { - if (columnMapping.HasScriptWithValue) - { - dataRow[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue(); - } - else - { - dataRow[columnMapping.DestinationColumn.Name] = columnMapping.ConvertInputToOutputFormat(row[columnMapping.SourceColumn.Name]); - } + dataRow[columnMapping.DestinationColumn.Name] += dataToRow.ToString(); } else { - if (columnMapping.Active) - { - throw new Exception(BaseDestinationWriter.GetRowValueNotFoundMessage(row, columnMapping.SourceColumn.Table.Name, columnMapping.SourceColumn.Name)); - } + dataRow[columnMapping.DestinationColumn.Name] = dataToRow; } } - - columnMappings = SourceColumnMappings[mapping.GetId()]; - switch (mapping.DestinationTable.Name) - { - case "EcomProductCategoryFieldValue": - WriteCategoyFieldValues(row, columnMappings, dataRow); - break; - case "EcomVariantGroups": - WriteVariantGroups(row, columnMappings, dataRow); - break; - case "EcomVariantsOptions": - WriteVariantOptions(row, columnMappings, dataRow); - break; - case "EcomManufacturers": - WriteManufacturers(row, columnMappings, dataRow); - break; - case "EcomGroups": - WriteGroups(row, columnMappings, dataRow); - break; - case "EcomProducts": - if (!WriteProducts(row, mapping, columnMappings, dataRow)) - { - return; - } - break; - case "EcomProductsRelated": - WriteRelatedProducts(row, columnMappings, dataRow); - break; - case "EcomPrices": - WritePrices(row, columnMappings, dataRow); - break; - case "EcomDetails": - WriteDetails(row, columnMappings, dataRow); - break; - case "EcomAssortmentPermissions": - if (!WriteAssortments(row, columnMappings, dataRow)) - { - DataRowsToWrite[GetTableName(mapping.DestinationTable.Name, mapping)].Add(RowAutoId++.ToString(), new List() { dataRow }); - return; - } - break; - } - - foreach (ColumnMapping columnMapping in mappingColumns) + } + if (!discardDuplicates || !duplicateRowsHandler.IsRowDuplicate(mappingColumns.Where(cm => cm.Active), mapping, dataRow, row)) + { + var tableName = GetTableName(mapping.DestinationTable.Name, mapping); + var tableKey = GetTableKey(mapping.DestinationTable.Name); + if (!string.IsNullOrWhiteSpace(tableKey)) { - object rowValue = null; - if (columnMapping.HasScriptWithValue || row.TryGetValue(columnMapping.SourceColumn?.Name, out rowValue)) + var rowId = Converter.ToString(dataRow[tableKey]); + List rows = null; + if (!DataRowsToWrite[tableName].TryGetValue(rowId, out rows)) { - object dataToRow = columnMapping.ConvertInputValueToOutputValue(rowValue); + rows = new List(); - if (mappingColumns.Any(obj => obj.DestinationColumn.Name == columnMapping.DestinationColumn.Name && obj.GetId() != columnMapping.GetId())) - { - dataRow[columnMapping.DestinationColumn.Name] += dataToRow.ToString(); - } - else - { - dataRow[columnMapping.DestinationColumn.Name] = dataToRow; - } + DataRowsToWrite[tableName].Add(rowId, rows); } + rows.Add(dataRow); } - if (!discardDuplicates || !duplicateRowsHandler.IsRowDuplicate(mappingColumns.Where(cm => cm.Active), mapping, dataRow, row)) + else { - var tableName = GetTableName(mapping.DestinationTable.Name, mapping); - var tableKey = GetTableKey(mapping.DestinationTable.Name); - if (!string.IsNullOrWhiteSpace(tableKey)) - { - var rowId = Converter.ToString(dataRow[tableKey]); - List rows = null; - if (!DataRowsToWrite[tableName].TryGetValue(rowId, out rows)) - { - rows = new List(); - - DataRowsToWrite[tableName].Add(rowId, rows); - } - rows.Add(dataRow); - } - else - { - DataRowsToWrite[tableName].Add(RowAutoId++.ToString(), new List() { dataRow }); - } - - if (_currentlyWritingMappingId != mapping.GetId()) - { - _currentlyWritingMappingId = mapping.GetId(); - _writtenRowsCount = 0; - } - if (++_writtenRowsCount % 10000 == 0) - { - logger.Log("Added " + _writtenRowsCount + " rows to temporary table for " + mapping.DestinationTable.Name + "."); - } - - assortmentHandler.ProcessAssortments(dataRow, mapping); + DataRowsToWrite[tableName].Add(RowAutoId++.ToString(), new List() { dataRow }); } - } - private string GetTableKey(string name) - { - switch (name) + if (_currentlyWritingMappingId != mapping.GetId()) + { + _currentlyWritingMappingId = mapping.GetId(); + _writtenRowsCount = 0; + } + if (++_writtenRowsCount % 10000 == 0) { - case "EcomVariantGroups": - return "VariantGroupID"; - case "EcomVariantsOptions": - return "VariantOptionID"; - case "EcomManufacturers": - return "ManufacturerID"; - case "EcomGroups": - return "GroupID"; + logger.Log("Added " + _writtenRowsCount + " rows to temporary table for " + mapping.DestinationTable.Name + "."); + } - case "EcomProductsRelatedGroups": - return "RelatedGroupID"; - case "EcomLanguages": - return "LanguageID"; + assortmentHandler.ProcessAssortments(dataRow, mapping); + } + } - default: - return string.Empty; - } + private string GetTableKey(string name) + { + switch (name) + { + case "EcomVariantGroups": + return "VariantGroupID"; + case "EcomVariantsOptions": + return "VariantOptionID"; + case "EcomManufacturers": + return "ManufacturerID"; + case "EcomGroups": + return "GroupID"; + + case "EcomProductsRelatedGroups": + return "RelatedGroupID"; + case "EcomLanguages": + return "LanguageID"; + + default: + return string.Empty; } + } - private bool WriteProducts(Dictionary row, Mapping mapping, Dictionary columnMappings, DataRow dataRow) - { - //Create ID if missing - DataRow existingProductRow = null; - string productLanguageID = HandleProductLanguageId(row, columnMappings, dataRow); - string productID = HandleProductId(row, mapping, columnMappings, dataRow, ref existingProductRow, productLanguageID); - string productVariantID = HandleVariantId(row, columnMappings, dataRow, existingProductRow, ref productID); + private bool WriteProducts(Dictionary row, Mapping mapping, Dictionary columnMappings, DataRow dataRow) + { + //Create ID if missing + DataRow existingProductRow = null; + string productLanguageID = HandleProductLanguageId(row, columnMappings, dataRow); + string productID = HandleProductId(row, mapping, columnMappings, dataRow, ref existingProductRow, productLanguageID); + string productVariantID = HandleVariantId(row, columnMappings, dataRow, existingProductRow, ref productID); - if (existingProductRow != null) - { - dataRow["ProductVariantCounter"] = existingProductRow["ProductVariantCounter"]; - dataRow["ProductVariantGroupCounter"] = existingProductRow["ProductVariantGroupCounter"]; - dataRow["ProductVariantProdCounter"] = existingProductRow["ProductVariantProdCounter"]; - } + if (existingProductRow != null) + { + dataRow["ProductVariantCounter"] = existingProductRow["ProductVariantCounter"]; + dataRow["ProductVariantGroupCounter"] = existingProductRow["ProductVariantGroupCounter"]; + dataRow["ProductVariantProdCounter"] = existingProductRow["ProductVariantProdCounter"]; + } - //Find groups, create if missing, add relations - HandleProductGroups(row, columnMappings, productID, productLanguageID); + //Find groups, create if missing, add relations + HandleProductGroups(row, columnMappings, productID, productLanguageID); - HandleProductCategoryFields(row, columnMappings, productID, productVariantID, productLanguageID); + HandleProductCategoryFields(row, columnMappings, productID, productVariantID, productLanguageID); - //Find Manufacturer, create if missing, add Manufacturer Reference - HandleProductManufacturers(row, columnMappings, dataRow); + //Find Manufacturer, create if missing, add Manufacturer Reference + HandleProductManufacturers(row, columnMappings, dataRow); - //Find VariantRelations, create if missing, Add Variant Relations - string variantGroupsString = HandleProductVariantGroups(row, columnMappings, productID); + //Find VariantRelations, create if missing, Add Variant Relations + string variantGroupsString = HandleProductVariantGroups(row, columnMappings, productID); - //Find VariantRelations, create if missing, Add Variant Relations - string variantOptionsString = HandleProductVariantOptions(row, columnMappings, productID); + //Find VariantRelations, create if missing, Add Variant Relations + string variantOptionsString = HandleProductVariantOptions(row, columnMappings, productID); - if (string.IsNullOrEmpty(variantGroupsString) && string.IsNullOrEmpty(variantOptionsString) && !string.IsNullOrEmpty(productVariantID)) - { - CountProductVariantGroups(productID, productVariantID); - } + if (string.IsNullOrEmpty(variantGroupsString) && string.IsNullOrEmpty(variantOptionsString) && !string.IsNullOrEmpty(productVariantID)) + { + CountProductVariantGroups(productID, productVariantID); + } - string processedProductKey = string.Format("{0}.{1}.{2}", productID, productVariantID, productLanguageID); - if (_processedProductsKeys.ContainsKey(processedProductKey)) - { - //_logger.Log(string.Format("Skipped product row with [ProductID.VariantID.LanguageID] combination = '{0}' as it is already exists. Source row data: '{1}'.", processedProductKey, BaseProvider.GetFailedSourceRowMessage(row))); - return false; - } - else - { - _processedProductsKeys.Add(processedProductKey, string.Empty); - } - string productKey = string.Format("{0}.{1}", productID, productLanguageID); - int productVariantsCount = 0; - if (ProductVariantsCountDictionary.TryGetValue(productKey, out productVariantsCount)) - { - ProductVariantsCountDictionary[productKey] = productVariantsCount + 1; - } - else - { - ProductVariantsCountDictionary[productKey] = 1; - } - return true; + string processedProductKey = string.Format("{0}.{1}.{2}", productID, productVariantID, productLanguageID); + if (_processedProductsKeys.ContainsKey(processedProductKey)) + { + //_logger.Log(string.Format("Skipped product row with [ProductID.VariantID.LanguageID] combination = '{0}' as it is already exists. Source row data: '{1}'.", processedProductKey, BaseProvider.GetFailedSourceRowMessage(row))); + return false; + } + else + { + _processedProductsKeys.Add(processedProductKey, string.Empty); + } + string productKey = string.Format("{0}.{1}", productID, productLanguageID); + int productVariantsCount = 0; + if (ProductVariantsCountDictionary.TryGetValue(productKey, out productVariantsCount)) + { + ProductVariantsCountDictionary[productKey] = productVariantsCount + 1; + } + else + { + ProductVariantsCountDictionary[productKey] = 1; } + return true; + } - private void HandleProductIdFoundByNumber(Dictionary row, Dictionary columnMappings, DataRow dataRow, ref string productId, ref string productVariantId) + private void HandleProductIdFoundByNumber(Dictionary row, Dictionary columnMappings, DataRow dataRow, ref string productId, ref string productVariantId) + { + if (_useProductIdFoundByNumber) { - if (_useProductIdFoundByNumber) + columnMappings.TryGetValue("ProductNumber", out ColumnMapping column); + string productNumber = GetValue(column, row); + if (!string.IsNullOrEmpty(productNumber) && + ProductNumberVariantIds.TryGetValue(productNumber, out (string ProductId, string VariantId) id)) { - columnMappings.TryGetValue("ProductNumber", out ColumnMapping column); - string productNumber = GetValue(column, row); - if (!string.IsNullOrEmpty(productNumber) && - ProductNumberVariantIds.TryGetValue(productNumber, out (string ProductId, string VariantId) id)) - { - productId = id.ProductId; - productVariantId = id.VariantId; - dataRow["ProductID"] = productId; - dataRow["ProductVariantID"] = productVariantId; - } + productId = id.ProductId; + productVariantId = id.VariantId; + dataRow["ProductID"] = productId; + dataRow["ProductVariantID"] = productVariantId; } } + } - private string HandleProductVariantOptions(Dictionary row, Dictionary columnMappings, string productID) + private string HandleProductVariantOptions(Dictionary row, Dictionary columnMappings, string productID) + { + ColumnMapping column = null; + columnMappings.TryGetValue("VariantOptions", out column); + string variantOptionsString = GetValue(column, row); + if (!string.IsNullOrEmpty(variantOptionsString)) { - ColumnMapping column = null; - columnMappings.TryGetValue("VariantOptions", out column); - string variantOptionsString = GetValue(column, row); - if (!string.IsNullOrEmpty(variantOptionsString)) + var variantOptionIds = SplitOnComma(variantOptionsString); + for (int i = 0; i < variantOptionIds.Length; i++) { - var variantOptionIds = SplitOnComma(variantOptionsString); - for (int i = 0; i < variantOptionIds.Length; i++) - { - string variantOption = variantOptionIds[i]; + string variantOption = variantOptionIds[i]; - string key = string.Format("{0}.{1}", productID.ToLower(), variantOption.ToLower()); - if (!ecomVariantOptionsProductRelationKeys.ContainsKey(key)) + string key = string.Format("{0}.{1}", productID.ToLower(), variantOption.ToLower()); + if (!ecomVariantOptionsProductRelationKeys.ContainsKey(key)) + { + foreach (string option in variantOption.Split('.')) { - foreach (string option in variantOption.Split('.')) + if (!GetVariantOptionList().Contains(option)) { - if (!GetVariantOptionList().Contains(option)) + var filter = new Func(r => (string)r["VariantOptionID"] == option.Replace("'", "''") || (r.Table.Columns.Contains("VariantOptionName") && (string)r["VariantOptionName"] == option.Replace("'", "''"))); + if (FindRow("EcomVariantsOptions", filter) == null) { - var filter = new Func(r => (string)r["VariantOptionID"] == option.Replace("'", "''") || (r.Table.Columns.Contains("VariantOptionName") && (string)r["VariantOptionName"] == option.Replace("'", "''"))); - if (FindRow("EcomVariantsOptions", filter) == null) - { - throw new Exception("Relation betweeen product \"" + productID + "\" and VariantOption \"" + variantOption + "\" can not be created. The VariantOption does not exist."); - } + throw new Exception("Relation betweeen product \"" + productID + "\" and VariantOption \"" + variantOption + "\" can not be created. The VariantOption does not exist."); } } - WriteVariantOptionRelation(productID, variantOption); - ecomVariantOptionsProductRelationKeys.Add(key, null); - HandleVariantsCount(productID, variantOption); } + WriteVariantOptionRelation(productID, variantOption); + ecomVariantOptionsProductRelationKeys.Add(key, null); + HandleVariantsCount(productID, variantOption); } } - - return variantOptionsString; } - private string HandleProductVariantGroups(Dictionary row, Dictionary columnMappings, string productID) + return variantOptionsString; + } + + private string HandleProductVariantGroups(Dictionary row, Dictionary columnMappings, string productID) + { + ColumnMapping column = null; + columnMappings.TryGetValue("VariantGroups", out column); + string variantGroupsString = GetValue(column, row); + if (!string.IsNullOrEmpty(variantGroupsString)) { - ColumnMapping column = null; - columnMappings.TryGetValue("VariantGroups", out column); - string variantGroupsString = GetValue(column, row); - if (!string.IsNullOrEmpty(variantGroupsString)) + var variantGroupId = SplitOnComma(variantGroupsString); + for (int i = 0; i < variantGroupId.Length; i++) { - var variantGroupId = SplitOnComma(variantGroupsString); - for (int i = 0; i < variantGroupId.Length; i++) - { - string variantGroup = variantGroupId[i]; - AddVariantGroupReferenceToProductByVariantGroupName(productID, variantGroup); - } + string variantGroup = variantGroupId[i]; + AddVariantGroupReferenceToProductByVariantGroupName(productID, variantGroup); } - - return variantGroupsString; } - private void HandleProductManufacturers(Dictionary row, Dictionary columnMappings, DataRow dataRow) + return variantGroupsString; + } + + private void HandleProductManufacturers(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping columnMapping = null; + if (columnMappings.TryGetValue("ProductManufacturerID", out columnMapping) && columnMapping.Active && columnMapping.DestinationColumn != null) { - ColumnMapping columnMapping = null; - if (columnMappings.TryGetValue("ProductManufacturerID", out columnMapping) && columnMapping.Active && columnMapping.DestinationColumn != null) + string manufacturer = GetValue(columnMapping, row); + if (!string.IsNullOrEmpty(manufacturer)) { - string manufacturer = GetValue(columnMapping, row); - if (!string.IsNullOrEmpty(manufacturer)) + DataRow manufacturerRow = null; + ProductManufacturers.TryGetValue(manufacturer, out manufacturerRow); + if (manufacturerRow == null) { - DataRow manufacturerRow = null; - ProductManufacturers.TryGetValue(manufacturer, out manufacturerRow); - if (manufacturerRow == null) - { - manufacturerRow = GetExistingManufacturer(row, columnMapping); - } + manufacturerRow = GetExistingManufacturer(row, columnMapping); + } + if (manufacturerRow != null) + { + row[columnMapping.SourceColumn.Name] = manufacturerRow["ManufacturerID"]; + } + else + { + var manufacturerFilter = new Func(r => (string)r["ManufacturerID"] == manufacturer || (r.Table.Columns.Contains("ManufacturerName") && (string)r["ManufacturerName"] == manufacturer)); + manufacturerRow = FindRow("EcomManufacturers", manufacturerFilter); if (manufacturerRow != null) { row[columnMapping.SourceColumn.Name] = manufacturerRow["ManufacturerID"]; } else { - var manufacturerFilter = new Func(r => (string)r["ManufacturerID"] == manufacturer || (r.Table.Columns.Contains("ManufacturerName") && (string)r["ManufacturerName"] == manufacturer)); - manufacturerRow = FindRow("EcomManufacturers", manufacturerFilter); - if (manufacturerRow != null) - { - row[columnMapping.SourceColumn.Name] = manufacturerRow["ManufacturerID"]; - } - else + DataRow newManufacturer = GetDataTableNewRow("EcomManufacturers"); + LastManufacturerId = LastManufacturerId + 1; + newManufacturer["ManufacturerID"] = "ImportedMANU" + LastManufacturerId; + newManufacturer["ManufacturerName"] = manufacturer; + Dictionary> manufacturers = null; + if (!DataRowsToWrite.TryGetValue(newManufacturer.Table.TableName, out manufacturers)) { - DataRow newManufacturer = GetDataTableNewRow("EcomManufacturers"); - LastManufacturerId = LastManufacturerId + 1; - newManufacturer["ManufacturerID"] = "ImportedMANU" + LastManufacturerId; - newManufacturer["ManufacturerName"] = manufacturer; - Dictionary> manufacturers = null; - if (!DataRowsToWrite.TryGetValue(newManufacturer.Table.TableName, out manufacturers)) - { - manufacturers = new Dictionary>(); - DataRowsToWrite.Add("EcomManufacturers", manufacturers); - } - manufacturers.Add("ImportedMANU" + LastManufacturerId, new List() { newManufacturer }); - row[columnMapping.SourceColumn.Name] = newManufacturer["ManufacturerID"]; + manufacturers = new Dictionary>(); + DataRowsToWrite.Add("EcomManufacturers", manufacturers); } + manufacturers.Add("ImportedMANU" + LastManufacturerId, new List() { newManufacturer }); + row[columnMapping.SourceColumn.Name] = newManufacturer["ManufacturerID"]; } - dataRow[columnMapping.DestinationColumn.Name] = row[columnMapping.SourceColumn.Name]; } + dataRow[columnMapping.DestinationColumn.Name] = row[columnMapping.SourceColumn.Name]; } } + } - private void HandleProductGroups(Dictionary row, Dictionary columnMappings, string productID, string productLanguageID) + private void HandleProductGroups(Dictionary row, Dictionary columnMappings, string productID, string productLanguageID) + { + ColumnMapping column = null; + columnMappings.TryGetValue("Groups", out column); + string groups = GetValue(column, row); + string group = string.Empty; + if (!string.IsNullOrEmpty(groups)) { - ColumnMapping column = null; - columnMappings.TryGetValue("Groups", out column); - string groups = GetValue(column, row); - string group = string.Empty; - if (!string.IsNullOrEmpty(groups)) + try { - try - { - ColumnMapping sortingColumn = null; - columnMappings.TryGetValue("GroupSorting", out sortingColumn); - string groupSorting = Converter.ToString(GetValue(sortingColumn, row)); + ColumnMapping sortingColumn = null; + columnMappings.TryGetValue("GroupSorting", out sortingColumn); + string groupSorting = Converter.ToString(GetValue(sortingColumn, row)); - ColumnMapping primaryGroupColumn = null; - columnMappings.TryGetValue("PrimaryGroup", out primaryGroupColumn); - string primaryGroup = Converter.ToString(GetValue(primaryGroupColumn, row)); + ColumnMapping primaryGroupColumn = null; + columnMappings.TryGetValue("PrimaryGroup", out primaryGroupColumn); + string primaryGroup = Converter.ToString(GetValue(primaryGroupColumn, row)); - var groupSortings = SplitOnComma(groupSorting); - var groupIds = SplitOnComma(groups); + var groupSortings = SplitOnComma(groupSorting); + var groupIds = SplitOnComma(groups); - List missingGroups = new List(); - for (int i = 0; i < groupIds.Length; i++) + List missingGroups = new List(); + for (int i = 0; i < groupIds.Length; i++) + { + group = groupIds[i]; + bool referenceAdded = false; + if (groupSortings.Length > i) { - group = groupIds[i]; - bool referenceAdded = false; - if (groupSortings.Length > i) - { - referenceAdded = AddGroupReferenceToProduct(productID, productLanguageID, group, int.Parse(groupSortings[i]), primaryGroup); - } - else - { - referenceAdded = AddGroupReferenceToProduct(productID, productLanguageID, group, null, primaryGroup); - } - if (!referenceAdded && !_createMissingGoups) - { - missingGroups.Add(group); - } + referenceAdded = AddGroupReferenceToProduct(productID, productLanguageID, group, int.Parse(groupSortings[i]), primaryGroup); } - if (missingGroups.Count > 0) + else { - Dictionary cloneRow = new Dictionary(row); - if (!cloneRow.ContainsKey("Groups")) - { - cloneRow.Add("Groups", null); - } - cloneRow["Groups"] = string.Join(",", missingGroups.Distinct()); - _rowsWithMissingGroups.Add(cloneRow); + referenceAdded = AddGroupReferenceToProduct(productID, productLanguageID, group, null, primaryGroup); + } + if (!referenceAdded && !_createMissingGoups) + { + missingGroups.Add(group); } } - catch (Exception ex) + if (missingGroups.Count > 0) { - throw new Exception($"Handle product Groups failed. Group: '{group}'. Reason: {ex.Message}", ex); + Dictionary cloneRow = new Dictionary(row); + if (!cloneRow.ContainsKey("Groups")) + { + cloneRow.Add("Groups", null); + } + cloneRow["Groups"] = string.Join(",", missingGroups.Distinct()); + _rowsWithMissingGroups.Add(cloneRow); } } + catch (Exception ex) + { + throw new Exception($"Handle product Groups failed. Group: '{group}'. Reason: {ex.Message}", ex); + } } + } - private void HandleProductCategoryFields(Dictionary row, Dictionary columnMappings, string productID, string productVariantId, string productLanguageID) + private void HandleProductCategoryFields(Dictionary row, Dictionary columnMappings, string productID, string productVariantId, string productLanguageID) + { + var categoryFieldMappings = new List(); + foreach (var columnMapping in columnMappings) { - var categoryFieldMappings = new List(); - foreach (var columnMapping in columnMappings) + if (columnMapping.Key.StartsWith("ProductCategory|")) { - if (columnMapping.Key.StartsWith("ProductCategory|")) - { - categoryFieldMappings.Add(columnMapping.Value); - } + categoryFieldMappings.Add(columnMapping.Value); } + } - if (categoryFieldMappings.Any()) + if (categoryFieldMappings.Any()) + { + try { - try + foreach (var categoryFieldMapping in categoryFieldMappings) { - foreach (var categoryFieldMapping in categoryFieldMappings) + var categoryId = string.Empty; + var fieldId = string.Empty; + var fieldUniqueId = categoryFieldMapping.DestinationColumn.Name; + var tokens = fieldUniqueId.Split('|'); + + if (tokens.Count() == 3) { - var categoryId = string.Empty; - var fieldId = string.Empty; - var fieldUniqueId = categoryFieldMapping.DestinationColumn.Name; - var tokens = fieldUniqueId.Split('|'); + categoryId = tokens[1]; + fieldId = tokens[2]; - if (tokens.Count() == 3) + string value = GetValue(categoryFieldMapping, row); + if (!_ignoreEmptyCategoryFieldValues || !string.IsNullOrEmpty(value)) { - categoryId = tokens[1]; - fieldId = tokens[2]; - - string value = GetValue(categoryFieldMapping, row); - if (!_ignoreEmptyCategoryFieldValues || !string.IsNullOrEmpty(value)) - { - AddCategoryFieldValueToProduct(productID, productVariantId, productLanguageID, categoryId, fieldId, value); - } + AddCategoryFieldValueToProduct(productID, productVariantId, productLanguageID, categoryId, fieldId, value); } } } - catch (Exception ex) - { - throw new Exception("Write failed. Reason: " + ex.Message, ex); - } } + catch (Exception ex) + { + throw new Exception("Write failed. Reason: " + ex.Message, ex); + } + } + } + + private static string[] SplitOnComma(string inputString) + { + return InternalSplitOnComma(inputString).ToArray(); + } + + private static IEnumerable InternalSplitOnComma(string input) + { + var array = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + for (var i = 0; i < array.Length; i++) + { + var split = array[i]; + yield return split.Trim('"'); } + } - private static string[] SplitOnComma(string inputString) + private string HandleVariantId(Dictionary row, Dictionary columnMappings, DataRow dataRow, DataRow existingProductRow, + ref string productId) + { + string productVariantID; + columnMappings.TryGetValue("ProductVariantID", out ColumnMapping column); + if (column == null || string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) { - return InternalSplitOnComma(inputString).ToArray(); + productVariantID = existingProductRow != null ? existingProductRow["ProductVariantID"].ToString() : ""; + if (column != null) + { + HandleProductIdFoundByNumber(row, columnMappings, dataRow, ref productId, ref productVariantID); + } + dataRow["ProductVariantID"] = productVariantID; + } + else + { + productVariantID = GetValue(column, row); + HandleProductIdFoundByNumber(row, columnMappings, dataRow, ref productId, ref productVariantID); } - private static IEnumerable InternalSplitOnComma(string input) + return productVariantID; + } + + private string HandleProductLanguageId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping column = null; + columnMappings.TryGetValue("ProductLanguageID", out column); + string productLanguageID = _defaultLanguageId; + if (column != null && column.Active && column.ScriptType != ScriptType.Constant && !string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) { - var array = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - for (var i = 0; i < array.Length; i++) + productLanguageID = GetLanguageID((string)row[column.SourceColumn.Name]); + row[column.SourceColumn.Name] = productLanguageID; + } + else + { + dataRow["ProductLanguageID"] = productLanguageID; + if (column != null && column.ScriptType != ScriptType.Constant) { - var split = array[i]; - yield return split.Trim('"'); + row[column.SourceColumn.Name] = productLanguageID; } } - private string HandleVariantId(Dictionary row, Dictionary columnMappings, DataRow dataRow, DataRow existingProductRow, - ref string productId) + return productLanguageID; + } + + private string HandleProductId(Dictionary row, Mapping mapping, Dictionary columnMappings, DataRow dataRow, ref DataRow existingProductRow, string productLanguageId) + { + string productID; + ColumnMapping column = null; + columnMappings.TryGetValue("ProductID", out column); + ColumnMapping productNumberColumn = null; + columnMappings.TryGetValue("ProductNumber", out productNumberColumn); + ColumnMapping productNameColumn = null; + columnMappings.TryGetValue("ProductName", out productNameColumn); + if (column == null) { - string productVariantID; - columnMappings.TryGetValue("ProductVariantID", out ColumnMapping column); - if (column == null || string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) + existingProductRow = GetExistingProduct(row, mapping, productNumberColumn, productNameColumn); + if (existingProductRow == null)//if product is not found by number or name, return new generated ProductID { - productVariantID = existingProductRow != null ? existingProductRow["ProductVariantID"].ToString() : ""; - if (column != null) - { - HandleProductIdFoundByNumber(row, columnMappings, dataRow, ref productId, ref productVariantID); - } - dataRow["ProductVariantID"] = productVariantID; + LastProductId = LastProductId + 1; + productID = "ImportedPROD" + LastProductId; } else { - productVariantID = GetValue(column, row); - HandleProductIdFoundByNumber(row, columnMappings, dataRow, ref productId, ref productVariantID); + productID = existingProductRow["ProductID"].ToString(); } - - return productVariantID; + dataRow["ProductID"] = productID; + row["ProductID"] = productID; } - - private string HandleProductLanguageId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + else if (string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) { - ColumnMapping column = null; - columnMappings.TryGetValue("ProductLanguageID", out column); - string productLanguageID = _defaultLanguageId; - if (column != null && column.Active && column.ScriptType != ScriptType.Constant && !string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) + existingProductRow = GetExistingProduct(row, mapping, productNumberColumn, productNameColumn); + if (existingProductRow == null)//if product is not found by number or name, return new generated ProductID { - productLanguageID = GetLanguageID((string)row[column.SourceColumn.Name]); - row[column.SourceColumn.Name] = productLanguageID; + LastProductId = LastProductId + 1; + productID = "ImportedPROD" + LastProductId; } else { - dataRow["ProductLanguageID"] = productLanguageID; - if (column != null && column.ScriptType != ScriptType.Constant) - { - row[column.SourceColumn.Name] = productLanguageID; - } + productID = existingProductRow["ProductID"].ToString(); } - - return productLanguageID; + row[column.SourceColumn.Name] = productID; } - - private string HandleProductId(Dictionary row, Mapping mapping, Dictionary columnMappings, DataRow dataRow, ref DataRow existingProductRow, string productLanguageId) + else { - string productID; - ColumnMapping column = null; - columnMappings.TryGetValue("ProductID", out column); - ColumnMapping productNumberColumn = null; - columnMappings.TryGetValue("ProductNumber", out productNumberColumn); - ColumnMapping productNameColumn = null; - columnMappings.TryGetValue("ProductName", out productNameColumn); - if (column == null) - { - existingProductRow = GetExistingProduct(row, mapping, productNumberColumn, productNameColumn); - if (existingProductRow == null)//if product is not found by number or name, return new generated ProductID - { - LastProductId = LastProductId + 1; - productID = "ImportedPROD" + LastProductId; - } - else - { - productID = existingProductRow["ProductID"].ToString(); - } - dataRow["ProductID"] = productID; - row["ProductID"] = productID; - } - else if (string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) + productID = (string)row[column.SourceColumn.Name]; + } + if (productNumberColumn != null) + { + string productNumber = row[productNumberColumn.SourceColumn.Name].ToString(); + if (!string.IsNullOrEmpty(productNumber)) { - existingProductRow = GetExistingProduct(row, mapping, productNumberColumn, productNameColumn); - if (existingProductRow == null)//if product is not found by number or name, return new generated ProductID - { - LastProductId = LastProductId + 1; - productID = "ImportedPROD" + LastProductId; - } - else + if (!ImportedProductsByNumber.ContainsKey(productNumber)) { - productID = existingProductRow["ProductID"].ToString(); + ImportedProductsByNumber.Add(productNumber, dataRow); } - row[column.SourceColumn.Name] = productID; - } - else - { - productID = (string)row[column.SourceColumn.Name]; - } - if (productNumberColumn != null) - { - string productNumber = row[productNumberColumn.SourceColumn.Name].ToString(); - if (!string.IsNullOrEmpty(productNumber)) + if (!string.IsNullOrEmpty(productLanguageId)) { - if (!ImportedProductsByNumber.ContainsKey(productNumber)) - { - ImportedProductsByNumber.Add(productNumber, dataRow); - } - if (!string.IsNullOrEmpty(productLanguageId)) + string key = GetImportedProductsByNumberMultipleProductsIdentifier(productNumber, productLanguageId); + if (!ImportedProductsByNumber.ContainsKey(key)) { - string key = GetImportedProductsByNumberMultipleProductsIdentifier(productNumber, productLanguageId); - if (!ImportedProductsByNumber.ContainsKey(key)) - { - ImportedProductsByNumber.Add(key, dataRow); - } + ImportedProductsByNumber.Add(key, dataRow); } } } - return productID; } + return productID; + } - private bool WriteAssortments(Dictionary row, Dictionary columnMappings, DataRow dataRow) + private bool WriteAssortments(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping assortmentIdColumn = null; + if (columnMappings.TryGetValue("AssortmentPermissionAssortmentID", out assortmentIdColumn) && row[assortmentIdColumn.SourceColumn.Name] != DBNull.Value && !string.IsNullOrEmpty(Converter.ToString(row[assortmentIdColumn.SourceColumn.Name]))) { - ColumnMapping assortmentIdColumn = null; - if (columnMappings.TryGetValue("AssortmentPermissionAssortmentID", out assortmentIdColumn) && row[assortmentIdColumn.SourceColumn.Name] != DBNull.Value && !string.IsNullOrEmpty(Converter.ToString(row[assortmentIdColumn.SourceColumn.Name]))) + string assortmentID = (string)row[assortmentIdColumn.SourceColumn.Name]; + List userIDs = new List(); + ColumnMapping assortmentCustomerNumberColumn = null; + if (columnMappings.TryGetValue("AssortmentPermissionCustomerNumber", out assortmentCustomerNumberColumn) && assortmentCustomerNumberColumn.Active && row[assortmentCustomerNumberColumn.SourceColumn.Name] != System.DBNull.Value) { - string assortmentID = (string)row[assortmentIdColumn.SourceColumn.Name]; - List userIDs = new List(); - ColumnMapping assortmentCustomerNumberColumn = null; - if (columnMappings.TryGetValue("AssortmentPermissionCustomerNumber", out assortmentCustomerNumberColumn) && assortmentCustomerNumberColumn.Active && row[assortmentCustomerNumberColumn.SourceColumn.Name] != System.DBNull.Value) + string userNumber = (string)row[assortmentCustomerNumberColumn.SourceColumn.Name]; + if (!string.IsNullOrEmpty(userNumber)) { - string userNumber = (string)row[assortmentCustomerNumberColumn.SourceColumn.Name]; - if (!string.IsNullOrEmpty(userNumber)) - { - userIDs = ExistingUsers.Select("AccessUserCustomerNumber='" + userNumber.Replace("'", "''") + "'").Select(r => r["AccessUserID"].ToString()).ToList(); - } - } - ColumnMapping externalIdmapping = null; - if (columnMappings.TryGetValue("AssortmentPermissionExternalID", out externalIdmapping) && externalIdmapping.Active && row[externalIdmapping.SourceColumn.Name] != DBNull.Value) - { - string externalId = (string)row[externalIdmapping.SourceColumn.Name]; - if (!string.IsNullOrEmpty(externalId)) - { - userIDs.AddRange(ExistingUsers.Select("AccessUserExternalID='" + externalId.Replace("'", "''") + "'").Select(r => r["AccessUserID"].ToString())); - } + userIDs = ExistingUsers.Select("AccessUserCustomerNumber='" + userNumber.Replace("'", "''") + "'").Select(r => r["AccessUserID"].ToString()).ToList(); } - ColumnMapping userIdMapping = null; - if (columnMappings.TryGetValue("AssortmentPermissionAccessUserID", out userIdMapping) && userIdMapping.Active && row[userIdMapping.SourceColumn.Name] != DBNull.Value) + } + ColumnMapping externalIdmapping = null; + if (columnMappings.TryGetValue("AssortmentPermissionExternalID", out externalIdmapping) && externalIdmapping.Active && row[externalIdmapping.SourceColumn.Name] != DBNull.Value) + { + string externalId = (string)row[externalIdmapping.SourceColumn.Name]; + if (!string.IsNullOrEmpty(externalId)) { - string id = (string)row[userIdMapping.SourceColumn.Name]; - if (!string.IsNullOrEmpty(id)) - { - userIDs.AddRange(ExistingUsers.Select("AccessUserID='" + id.Replace("'", "''") + "'").Select(r => r["AccessUserID"].ToString())); - } + userIDs.AddRange(ExistingUsers.Select("AccessUserExternalID='" + externalId.Replace("'", "''") + "'").Select(r => r["AccessUserID"].ToString())); } - foreach (string userID in userIDs.Distinct()) + } + ColumnMapping userIdMapping = null; + if (columnMappings.TryGetValue("AssortmentPermissionAccessUserID", out userIdMapping) && userIdMapping.Active && row[userIdMapping.SourceColumn.Name] != DBNull.Value) + { + string id = (string)row[userIdMapping.SourceColumn.Name]; + if (!string.IsNullOrEmpty(id)) { - dataRow["AssortmentPermissionAssortmentID"] = assortmentID; - dataRow["AssortmentPermissionAccessUserID"] = userID; + userIDs.AddRange(ExistingUsers.Select("AccessUserID='" + id.Replace("'", "''") + "'").Select(r => r["AccessUserID"].ToString())); } - return false; } - return true; + foreach (string userID in userIDs.Distinct()) + { + dataRow["AssortmentPermissionAssortmentID"] = assortmentID; + dataRow["AssortmentPermissionAccessUserID"] = userID; + } + return false; } + return true; + } - private void WriteDetails(Dictionary row, Dictionary columnMappings, DataRow dataRow) + private void WriteDetails(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping detailIdColumn = null; + if (!columnMappings.TryGetValue("DetailID", out detailIdColumn) || string.IsNullOrEmpty(Converter.ToString(row[detailIdColumn.SourceColumn.Name]))) { - ColumnMapping detailIdColumn = null; - if (!columnMappings.TryGetValue("DetailID", out detailIdColumn) || string.IsNullOrEmpty(Converter.ToString(row[detailIdColumn.SourceColumn.Name]))) - { - LastDetailId = LastDetailId + 1; - dataRow["DetailID"] = "ImportedDETAIL" + LastDetailId; - } + LastDetailId = LastDetailId + 1; + dataRow["DetailID"] = "ImportedDETAIL" + LastDetailId; + } + } + + + private void WritePrices(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping priceIdColumn = null; + if (!columnMappings.TryGetValue("PriceID", out priceIdColumn) || string.IsNullOrEmpty(Converter.ToString(row[priceIdColumn.SourceColumn.Name]))) + { + LastPriceId = LastPriceId + 1; + dataRow["PriceID"] = "ImportedPRICE" + LastPriceId; + } + if (!columnMappings.ContainsKey("PriceCurrency")) + { + dataRow["PriceCurrency"] = String.Empty; } + } + private void WriteRelatedProducts(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + string relatedGroupLanguage = _defaultLanguageId; + ColumnMapping productRelatedLanguageIdColumn = null; + if (columnMappings.TryGetValue("ProductRelatedLanguageID", out productRelatedLanguageIdColumn) && !string.IsNullOrEmpty(Converter.ToString(row[productRelatedLanguageIdColumn.SourceColumn.Name]))) + { + relatedGroupLanguage = GetLanguageID(Converter.ToString(row[productRelatedLanguageIdColumn.SourceColumn.Name])); + } - private void WritePrices(Dictionary row, Dictionary columnMappings, DataRow dataRow) + string relatedGroupID = null; + bool relatedGroupIdIsConstant = false; + ColumnMapping productRelatedGroupIdMapping = null; + if (columnMappings.TryGetValue("ProductRelatedGroupID", out productRelatedGroupIdMapping)) { - ColumnMapping priceIdColumn = null; - if (!columnMappings.TryGetValue("PriceID", out priceIdColumn) || string.IsNullOrEmpty(Converter.ToString(row[priceIdColumn.SourceColumn.Name]))) + if (productRelatedGroupIdMapping.ScriptType == ScriptType.Constant) { - LastPriceId = LastPriceId + 1; - dataRow["PriceID"] = "ImportedPRICE" + LastPriceId; + relatedGroupID = productRelatedGroupIdMapping.ScriptValue; + relatedGroupIdIsConstant = true; } - if (!columnMappings.ContainsKey("PriceCurrency")) + else { - dataRow["PriceCurrency"] = String.Empty; + relatedGroupID = Converter.ToString(row[productRelatedGroupIdMapping.SourceColumn.Name]); } } - private void WriteRelatedProducts(Dictionary row, Dictionary columnMappings, DataRow dataRow) + if (!relatedGroupIdIsConstant) { - string relatedGroupLanguage = _defaultLanguageId; - ColumnMapping productRelatedLanguageIdColumn = null; - if (columnMappings.TryGetValue("ProductRelatedLanguageID", out productRelatedLanguageIdColumn) && !string.IsNullOrEmpty(Converter.ToString(row[productRelatedLanguageIdColumn.SourceColumn.Name]))) + if (string.IsNullOrEmpty(relatedGroupID)) { - relatedGroupLanguage = GetLanguageID(Converter.ToString(row[productRelatedLanguageIdColumn.SourceColumn.Name])); + relatedGroupID = GetDefaultGroupID(relatedGroupLanguage); } - - string relatedGroupID = null; - bool relatedGroupIdIsConstant = false; - ColumnMapping productRelatedGroupIdMapping = null; - if (columnMappings.TryGetValue("ProductRelatedGroupID", out productRelatedGroupIdMapping)) + else { - if (productRelatedGroupIdMapping.ScriptType == ScriptType.Constant) + var productsRelRow = FindRow("EcomProductsRelatedGroups", relatedGroupID.Replace("'", "''")); + if (productsRelRow == null) { - relatedGroupID = productRelatedGroupIdMapping.ScriptValue; - relatedGroupIdIsConstant = true; + List productsRelRows = null; + if (ProductsRelatedGroups.TryGetValue(relatedGroupID.Replace("'", "''"), out productsRelRows)) + { + productsRelRow = productsRelRows[0]; + } } - else + if (productsRelRow == null) { - relatedGroupID = Converter.ToString(row[productRelatedGroupIdMapping.SourceColumn.Name]); + var filter = new Func(r => (string)r["RelatedGroupID"] == relatedGroupID || (string)r["RelatedGroupName"] == relatedGroupID); + productsRelRow = FindRow("EcomProductsRelatedGroups", filter); } - } - - if (!relatedGroupIdIsConstant) - { - if (string.IsNullOrEmpty(relatedGroupID)) + if (productsRelRow == null) { - relatedGroupID = GetDefaultGroupID(relatedGroupLanguage); + relatedGroupID = CreateProductRelatedGroup(relatedGroupID, relatedGroupLanguage); } else { - var productsRelRow = FindRow("EcomProductsRelatedGroups", relatedGroupID.Replace("'", "''")); - if (productsRelRow == null) - { - List productsRelRows = null; - if (ProductsRelatedGroups.TryGetValue(relatedGroupID.Replace("'", "''"), out productsRelRows)) - { - productsRelRow = productsRelRows[0]; - } - } - if (productsRelRow == null) - { - var filter = new Func(r => (string)r["RelatedGroupID"] == relatedGroupID || (string)r["RelatedGroupName"] == relatedGroupID); - productsRelRow = FindRow("EcomProductsRelatedGroups", filter); - } - if (productsRelRow == null) - { - relatedGroupID = CreateProductRelatedGroup(relatedGroupID, relatedGroupLanguage); - } - else - { - relatedGroupID = productsRelRow["RelatedGroupID"].ToString(); - } + relatedGroupID = productsRelRow["RelatedGroupID"].ToString(); } } - dataRow["ProductRelatedGroupID"] = relatedGroupID; } + dataRow["ProductRelatedGroupID"] = relatedGroupID; + } - private void WriteGroups(Dictionary row, Dictionary columnMappings, DataRow dataRow) - { - //Set ID if Missing - string groupID = HandleGroupId(row, columnMappings, dataRow); + private void WriteGroups(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + //Set ID if Missing + string groupID = HandleGroupId(row, columnMappings, dataRow); - //find Shops, create if missing, - HandleGroupShop(row, columnMappings, groupID); + //find Shops, create if missing, + HandleGroupShop(row, columnMappings, groupID); - //find Shops, create if missing, - HandleParentGroups(row, columnMappings, groupID); + //find Shops, create if missing, + HandleParentGroups(row, columnMappings, groupID); - string groupLanguageID = _defaultLanguageId; - ColumnMapping groupLanguageColumn = null; - if (columnMappings.TryGetValue("GroupLanguageID", out groupLanguageColumn) && groupLanguageColumn.ScriptType != ScriptType.Constant && !string.IsNullOrEmpty(Converter.ToString(row[groupLanguageColumn.SourceColumn.Name]))) - { - groupLanguageID = GetLanguageID((string)row[groupLanguageColumn.SourceColumn.Name]); - row[groupLanguageColumn.SourceColumn.Name] = groupLanguageID; - } - else - { - dataRow["GroupLanguageID"] = groupLanguageID; - } + string groupLanguageID = _defaultLanguageId; + ColumnMapping groupLanguageColumn = null; + if (columnMappings.TryGetValue("GroupLanguageID", out groupLanguageColumn) && groupLanguageColumn.ScriptType != ScriptType.Constant && !string.IsNullOrEmpty(Converter.ToString(row[groupLanguageColumn.SourceColumn.Name]))) + { + groupLanguageID = GetLanguageID((string)row[groupLanguageColumn.SourceColumn.Name]); + row[groupLanguageColumn.SourceColumn.Name] = groupLanguageID; + } + else + { + dataRow["GroupLanguageID"] = groupLanguageID; } + } - private void HandleParentGroups(Dictionary row, Dictionary columnMappings, string groupID) + private void HandleParentGroups(Dictionary row, Dictionary columnMappings, string groupID) + { + ColumnMapping parentGroupsColumn = null; + columnMappings.TryGetValue("ParentGroups", out parentGroupsColumn); + string parentGroups = GetValue(parentGroupsColumn, row); + if (!string.IsNullOrEmpty(parentGroups)) { - ColumnMapping parentGroupsColumn = null; - columnMappings.TryGetValue("ParentGroups", out parentGroupsColumn); - string parentGroups = GetValue(parentGroupsColumn, row); - if (!string.IsNullOrEmpty(parentGroups)) + var parentGroupIds = SplitOnComma(parentGroups); + //getting GroupRelationsSorting values for parent groups + List parentGroupSortingList = new List(); + if (isParentGroupSortingInEcomGroupsMapping) { - var parentGroupIds = SplitOnComma(parentGroups); - //getting GroupRelationsSorting values for parent groups - List parentGroupSortingList = new List(); - if (isParentGroupSortingInEcomGroupsMapping) + ColumnMapping parentGroupSortingColumn = null; + columnMappings.TryGetValue("ParentGroupsSorting", out parentGroupSortingColumn); + string sortingStr = GetValue(parentGroupSortingColumn, row); + if (!string.IsNullOrEmpty(sortingStr)) { - ColumnMapping parentGroupSortingColumn = null; - columnMappings.TryGetValue("ParentGroupsSorting", out parentGroupSortingColumn); - string sortingStr = GetValue(parentGroupSortingColumn, row); - if (!string.IsNullOrEmpty(sortingStr)) + var sortings = SplitOnComma(sortingStr); + for (int i = 0; i < sortings.Length; i++) { - var sortings = SplitOnComma(sortingStr); - for (int i = 0; i < sortings.Length; i++) - { - parentGroupSortingList.Add(int.Parse(sortings[i])); - } + parentGroupSortingList.Add(int.Parse(sortings[i])); } } - for (int i = 0; i < parentGroupIds.Length; i++) - { - string parentGroupId = parentGroupIds[i]; - int groupRelationsSorting = (parentGroupSortingList.Count > i) ? parentGroupSortingList[i] : 0; - AddParentGroupReference(groupID, parentGroupId, groupRelationsSorting); - } + } + for (int i = 0; i < parentGroupIds.Length; i++) + { + string parentGroupId = parentGroupIds[i]; + int groupRelationsSorting = (parentGroupSortingList.Count > i) ? parentGroupSortingList[i] : 0; + AddParentGroupReference(groupID, parentGroupId, groupRelationsSorting); } } + } - private void HandleGroupShop(Dictionary row, Dictionary columnMappings, string groupID) + private void HandleGroupShop(Dictionary row, Dictionary columnMappings, string groupID) + { + ColumnMapping groupShopsColumn = null; + if (!columnMappings.TryGetValue("Shops", out groupShopsColumn)) + { + AddShopReferenceToGroup(groupID, DefaultShop, 0); + } + else { - ColumnMapping groupShopsColumn = null; - if (!columnMappings.TryGetValue("Shops", out groupShopsColumn)) + bool useShopValueFromConstant = groupShopsColumn.ScriptType == ScriptType.Constant && !string.IsNullOrEmpty(groupShopsColumn.ScriptValue); + + if (!useShopValueFromConstant && string.IsNullOrEmpty(Converter.ToString(row[groupShopsColumn.SourceColumn.Name]))) { AddShopReferenceToGroup(groupID, DefaultShop, 0); } else { - bool useShopValueFromConstant = groupShopsColumn.ScriptType == ScriptType.Constant && !string.IsNullOrEmpty(groupShopsColumn.ScriptValue); + ColumnMapping shopSortingColumn = null; + columnMappings.TryGetValue("ShopSorting", out shopSortingColumn); + string ShopSorting = GetValue(shopSortingColumn, row); + ShopSorting = string.IsNullOrEmpty(ShopSorting) ? "0" : ShopSorting; - if (!useShopValueFromConstant && string.IsNullOrEmpty(Converter.ToString(row[groupShopsColumn.SourceColumn.Name]))) + var sortings = SplitOnComma(ShopSorting); + string shopIdsStr; + if (useShopValueFromConstant) { - AddShopReferenceToGroup(groupID, DefaultShop, 0); + shopIdsStr = groupShopsColumn.ScriptValue; } else { - ColumnMapping shopSortingColumn = null; - columnMappings.TryGetValue("ShopSorting", out shopSortingColumn); - string ShopSorting = GetValue(shopSortingColumn, row); - ShopSorting = string.IsNullOrEmpty(ShopSorting) ? "0" : ShopSorting; - - var sortings = SplitOnComma(ShopSorting); - string shopIdsStr; - if (useShopValueFromConstant) - { - shopIdsStr = groupShopsColumn.ScriptValue; - } - else + shopIdsStr = (string)row[groupShopsColumn.SourceColumn.Name]; + } + var shopIds = SplitOnComma(shopIdsStr); + string shopSorting = null; + for (int i = 0; i < shopIds.Length; i++) + { + if (sortings.Length > i) { - shopIdsStr = (string)row[groupShopsColumn.SourceColumn.Name]; + shopSorting = sortings[i]; } - var shopIds = SplitOnComma(shopIdsStr); - string shopSorting = null; - for (int i = 0; i < shopIds.Length; i++) - { - if (sortings.Length > i) - { - shopSorting = sortings[i]; - } - string shop = shopIds[i]; + string shop = shopIds[i]; - AddShopReferenceToGroupByShopName(groupID, shop, int.Parse(shopSorting)); - } + AddShopReferenceToGroupByShopName(groupID, shop, int.Parse(shopSorting)); } } } + } - private string HandleGroupId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + private string HandleGroupId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + string groupID; + ColumnMapping groupIdColumn = null; + if (!columnMappings.TryGetValue("GroupID", out groupIdColumn)) { - string groupID; - ColumnMapping groupIdColumn = null; - if (!columnMappings.TryGetValue("GroupID", out groupIdColumn)) + LastGroupId = LastGroupId + 1; + dataRow["GroupID"] = "ImportedGROUP" + LastGroupId; + row["GroupID"] = "ImportedGROUP" + LastGroupId; + groupID = "ImportedGROUP" + LastGroupId; + } + else + { + groupID = GetValue(groupIdColumn, row); + if (string.IsNullOrEmpty(groupID)) { LastGroupId = LastGroupId + 1; - dataRow["GroupID"] = "ImportedGROUP" + LastGroupId; row["GroupID"] = "ImportedGROUP" + LastGroupId; groupID = "ImportedGROUP" + LastGroupId; } + } + return groupID; + } + + private void WriteManufacturers(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping manufacturerNameColumn = null; + columnMappings.TryGetValue("ManufacturerName", out manufacturerNameColumn); + ColumnMapping manufacturerColumn = null; + if (!columnMappings.TryGetValue("ManufacturerID", out manufacturerColumn)) + { + DataRow existingManufacturer = GetExistingManufacturer(row, manufacturerNameColumn); + if (existingManufacturer != null) + { + dataRow["ManufacturerID"] = existingManufacturer["ManufacturerID"]; + } else { - groupID = GetValue(groupIdColumn, row); - if (string.IsNullOrEmpty(groupID)) - { - LastGroupId = LastGroupId + 1; - row["GroupID"] = "ImportedGROUP" + LastGroupId; - groupID = "ImportedGROUP" + LastGroupId; - } + LastManufacturerId = LastManufacturerId + 1; + dataRow["ManufacturerID"] = "ImportedMANU" + LastManufacturerId; } - return groupID; } - - private void WriteManufacturers(Dictionary row, Dictionary columnMappings, DataRow dataRow) + else if (string.IsNullOrEmpty(Converter.ToString(row[manufacturerColumn.SourceColumn.Name]))) { - ColumnMapping manufacturerNameColumn = null; - columnMappings.TryGetValue("ManufacturerName", out manufacturerNameColumn); - ColumnMapping manufacturerColumn = null; - if (!columnMappings.TryGetValue("ManufacturerID", out manufacturerColumn)) + DataRow existingManufacturer = GetExistingManufacturer(row, manufacturerNameColumn); + if (existingManufacturer != null) { - DataRow existingManufacturer = GetExistingManufacturer(row, manufacturerNameColumn); - if (existingManufacturer != null) - { - dataRow["ManufacturerID"] = existingManufacturer["ManufacturerID"]; - } - else - { - LastManufacturerId = LastManufacturerId + 1; - dataRow["ManufacturerID"] = "ImportedMANU" + LastManufacturerId; - } + row["ManufacturerID"] = existingManufacturer["ManufacturerID"]; } - else if (string.IsNullOrEmpty(Converter.ToString(row[manufacturerColumn.SourceColumn.Name]))) + else { - DataRow existingManufacturer = GetExistingManufacturer(row, manufacturerNameColumn); - if (existingManufacturer != null) - { - row["ManufacturerID"] = existingManufacturer["ManufacturerID"]; - } - else - { - LastManufacturerId = LastManufacturerId + 1; - row["ManufacturerID"] = "ImportedMANU" + LastManufacturerId; - } + LastManufacturerId = LastManufacturerId + 1; + row["ManufacturerID"] = "ImportedMANU" + LastManufacturerId; } } + } - private void WriteVariantOptions(Dictionary row, Dictionary columnMappings, DataRow dataRow) - { - HandleVariantOptionId(row, columnMappings, dataRow); + private void WriteVariantOptions(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + HandleVariantOptionId(row, columnMappings, dataRow); - HandleVariantOptionLangaugeId(row, columnMappings, dataRow); + HandleVariantOptionLangaugeId(row, columnMappings, dataRow); - ColumnMapping column = null; - columnMappings.TryGetValue("VariantOptionGroupID", out column); - string variantOptionGroupID = Converter.ToString(GetValue(column, row)); - string variantOptionGroupIDEscaped = variantOptionGroupID.Replace("'", "''"); + ColumnMapping column = null; + columnMappings.TryGetValue("VariantOptionGroupID", out column); + string variantOptionGroupID = Converter.ToString(GetValue(column, row)); + string variantOptionGroupIDEscaped = variantOptionGroupID.Replace("'", "''"); - DataRow variantGroupRow = null; - if (VariantGroups.TryGetValue(variantOptionGroupIDEscaped, out variantGroupRow)) + DataRow variantGroupRow = null; + if (VariantGroups.TryGetValue(variantOptionGroupIDEscaped, out variantGroupRow)) + { + dataRow["VariantOptionGroupID"] = variantGroupRow["VariantGroupID"]; + row[column.SourceColumn.Name] = variantGroupRow["VariantGroupID"]; + } + else + { + var filter = new Func(r => (string)r["VariantGroupID"] == variantOptionGroupIDEscaped || (r.Table.Columns.Contains("VariantGroupName") && (string)r["VariantGroupName"] == variantOptionGroupIDEscaped)); + variantGroupRow = FindRow("EcomVariantGroups", filter); + if (variantGroupRow != null) { dataRow["VariantOptionGroupID"] = variantGroupRow["VariantGroupID"]; row[column.SourceColumn.Name] = variantGroupRow["VariantGroupID"]; } else { - var filter = new Func(r => (string)r["VariantGroupID"] == variantOptionGroupIDEscaped || (r.Table.Columns.Contains("VariantGroupName") && (string)r["VariantGroupName"] == variantOptionGroupIDEscaped)); - variantGroupRow = FindRow("EcomVariantGroups", filter); - if (variantGroupRow != null) - { - dataRow["VariantOptionGroupID"] = variantGroupRow["VariantGroupID"]; - row[column.SourceColumn.Name] = variantGroupRow["VariantGroupID"]; - } - else - { - AddNewVariantOptionGroup(row, column); - } + AddNewVariantOptionGroup(row, column); } } + } - private void AddNewVariantOptionGroup(Dictionary row, ColumnMapping column) + private void AddNewVariantOptionGroup(Dictionary row, ColumnMapping column) + { + var newGroup = GetDataTableNewRow("EcomVariantGroups"); + LastVariantGroupId = LastVariantGroupId + 1; + //set groupID on option + newGroup["VariantGroupID"] = "ImportedVARGRP" + LastVariantGroupId; + newGroup["VariantGroupName"] = row[column.SourceColumn.Name]; + newGroup["VariantGroupLanguageID"] = _defaultLanguageId; + if (newGroup.Table.Columns.Contains("VariantGroupFamily")) + { + newGroup["VariantGroupFamily"] = false; + } + DataRowsToWrite[newGroup.Table.TableName].Add("ImportedVARGRP" + LastVariantGroupId, new List() { newGroup }); + row["VariantOptionGroupID"] = "ImportedVARGRP" + LastVariantGroupId; + row[column.SourceColumn.Name] = newGroup["VariantGroupID"]; + } + + private void HandleVariantOptionLangaugeId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + if (!columnMappings.TryGetValue("VariantOptionLanguageID", out ColumnMapping column)) { - var newGroup = GetDataTableNewRow("EcomVariantGroups"); - LastVariantGroupId = LastVariantGroupId + 1; - //set groupID on option - newGroup["VariantGroupID"] = "ImportedVARGRP" + LastVariantGroupId; - newGroup["VariantGroupName"] = row[column.SourceColumn.Name]; - newGroup["VariantGroupLanguageID"] = _defaultLanguageId; - if (newGroup.Table.Columns.Contains("VariantGroupFamily")) - { - newGroup["VariantGroupFamily"] = false; - } - DataRowsToWrite[newGroup.Table.TableName].Add("ImportedVARGRP" + LastVariantGroupId, new List() { newGroup }); - row["VariantOptionGroupID"] = "ImportedVARGRP" + LastVariantGroupId; - row[column.SourceColumn.Name] = newGroup["VariantGroupID"]; + dataRow["VariantOptionLanguageID"] = _defaultLanguageId; } - - private void HandleVariantOptionLangaugeId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + else { - if (!columnMappings.TryGetValue("VariantOptionLanguageID", out ColumnMapping column)) + string language = GetValue(column, row); + if (string.IsNullOrEmpty(language)) { - dataRow["VariantOptionLanguageID"] = _defaultLanguageId; + row["VariantOptionLanguageID"] = _defaultLanguageId; } else { - string language = GetValue(column, row); - if (string.IsNullOrEmpty(language)) - { - row["VariantOptionLanguageID"] = _defaultLanguageId; - } - else - { - row["VariantOptionLanguageID"] = GetLanguageID(language); - } + row["VariantOptionLanguageID"] = GetLanguageID(language); } } + } - private void HandleVariantOptionId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + private void HandleVariantOptionId(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping column = null; + if (!columnMappings.TryGetValue("VariantOptionID", out column)) { - ColumnMapping column = null; - if (!columnMappings.TryGetValue("VariantOptionID", out column)) - { - LastVariantOptionId = LastVariantOptionId + 1; - dataRow["VariantOptionID"] = "ImportedVO" + LastVariantOptionId; - } - else if (string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) - { - LastVariantOptionId = LastVariantOptionId + 1; - row["VariantOptionID"] = "ImportedVO" + LastVariantOptionId; - } + LastVariantOptionId = LastVariantOptionId + 1; + dataRow["VariantOptionID"] = "ImportedVO" + LastVariantOptionId; + } + else if (string.IsNullOrEmpty(Converter.ToString(row[column.SourceColumn.Name]))) + { + LastVariantOptionId = LastVariantOptionId + 1; + row["VariantOptionID"] = "ImportedVO" + LastVariantOptionId; } + } - private void WriteVariantGroups(Dictionary row, Dictionary columnMappings, DataRow dataRow) + private void WriteVariantGroups(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + ColumnMapping variantGroupColumn = null; + if (!columnMappings.TryGetValue("VariantGroupID", out variantGroupColumn)) + { + LastVariantGroupId = LastVariantGroupId + 1; + dataRow["VariantGroupID"] = "ImportedVARGRP" + LastVariantGroupId; + } + else { - ColumnMapping variantGroupColumn = null; - if (!columnMappings.TryGetValue("VariantGroupID", out variantGroupColumn)) + if (string.IsNullOrEmpty(Converter.ToString(row[variantGroupColumn.SourceColumn.Name]))) { LastVariantGroupId = LastVariantGroupId + 1; dataRow["VariantGroupID"] = "ImportedVARGRP" + LastVariantGroupId; + row[variantGroupColumn.SourceColumn.Name] = "ImportedVARGRP" + LastVariantGroupId; } - else - { - if (string.IsNullOrEmpty(Converter.ToString(row[variantGroupColumn.SourceColumn.Name]))) - { - LastVariantGroupId = LastVariantGroupId + 1; - dataRow["VariantGroupID"] = "ImportedVARGRP" + LastVariantGroupId; - row[variantGroupColumn.SourceColumn.Name] = "ImportedVARGRP" + LastVariantGroupId; - } - } + } - string variantGroupLanguageID = _defaultLanguageId; - ColumnMapping variantGroupLanguageColumn = null; - if (columnMappings.TryGetValue("VariantGroupLanguageID", out variantGroupLanguageColumn)) - { - string id = GetValue(variantGroupLanguageColumn, row); - if (!string.IsNullOrEmpty(id)) - { - variantGroupLanguageID = GetLanguageID(id); - } - row[variantGroupLanguageColumn.SourceColumn.Name] = variantGroupLanguageID; - } - else + string variantGroupLanguageID = _defaultLanguageId; + ColumnMapping variantGroupLanguageColumn = null; + if (columnMappings.TryGetValue("VariantGroupLanguageID", out variantGroupLanguageColumn)) + { + string id = GetValue(variantGroupLanguageColumn, row); + if (!string.IsNullOrEmpty(id)) { - dataRow["VariantGroupLanguageID"] = variantGroupLanguageID; + variantGroupLanguageID = GetLanguageID(id); } + row[variantGroupLanguageColumn.SourceColumn.Name] = variantGroupLanguageID; + } + else + { + dataRow["VariantGroupLanguageID"] = variantGroupLanguageID; } + } - private void WriteCategoyFieldValues(Dictionary row, Dictionary columnMappings, DataRow dataRow) + private void WriteCategoyFieldValues(Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + DataRow product = null; + //Fill FieldValueFieldCategoryId by finding exisintg CategoryField by FieldID/SystemName + ColumnMapping fieldIdColumn = null; + ColumnMapping fieldCategoryIdColumn = null; + if (columnMappings.TryGetValue("FieldValueFieldId", out fieldIdColumn) && + (!columnMappings.TryGetValue("FieldValueFieldCategoryId", out fieldCategoryIdColumn) || string.IsNullOrEmpty(row[fieldCategoryIdColumn.SourceColumn.Name].ToString()))) { - DataRow product = null; - //Fill FieldValueFieldCategoryId by finding exisintg CategoryField by FieldID/SystemName - ColumnMapping fieldIdColumn = null; - ColumnMapping fieldCategoryIdColumn = null; - if (columnMappings.TryGetValue("FieldValueFieldId", out fieldIdColumn) && - (!columnMappings.TryGetValue("FieldValueFieldCategoryId", out fieldCategoryIdColumn) || string.IsNullOrEmpty(row[fieldCategoryIdColumn.SourceColumn.Name].ToString()))) + var existingCategoryField = Ecommerce.Services.ProductCategories.GetCategories().SelectMany(c => Ecommerce.Services.ProductCategories.GetFieldsByCategoryId(c.Id)).FirstOrDefault( + field => string.Compare(field.Id, row[fieldIdColumn.SourceColumn.Name].ToString(), true) == 0); + if (existingCategoryField != null) { - var existingCategoryField = Ecommerce.Services.ProductCategories.GetCategories().SelectMany(c => Ecommerce.Services.ProductCategories.GetFieldsByCategoryId(c.Id)).FirstOrDefault( - field => string.Compare(field.Id, row[fieldIdColumn.SourceColumn.Name].ToString(), true) == 0); - if (existingCategoryField != null) + dataRow["FieldValueFieldCategoryId"] = existingCategoryField.Category != null ? existingCategoryField.Category.Id : ""; + if (fieldCategoryIdColumn != null) { - dataRow["FieldValueFieldCategoryId"] = existingCategoryField.Category != null ? existingCategoryField.Category.Id : ""; - if (fieldCategoryIdColumn != null) - { - row[fieldCategoryIdColumn.SourceColumn.Name] = dataRow["FieldValueFieldCategoryId"]; - } + row[fieldCategoryIdColumn.SourceColumn.Name] = dataRow["FieldValueFieldCategoryId"]; } } + } - ColumnMapping fieldProductIdColumn = null; - ColumnMapping fieldProductNumberColumn = null; - ColumnMapping fieldProductLanguageIdColumn = null; - ColumnMapping fieldProductVariantIdColumn = null; - - columnMappings.TryGetValue("FieldValueProductLanguageId", out fieldProductLanguageIdColumn); - columnMappings.TryGetValue("FieldValueProductVariantId", out fieldProductVariantIdColumn); + ColumnMapping fieldProductIdColumn = null; + ColumnMapping fieldProductNumberColumn = null; + ColumnMapping fieldProductLanguageIdColumn = null; + ColumnMapping fieldProductVariantIdColumn = null; - if (!columnMappings.TryGetValue("FieldValueProductId", out fieldProductIdColumn) && columnMappings.TryGetValue("FieldValueProductNumber", out fieldProductNumberColumn)) - { - //find id from productnumber, variantID and Language if available - var productNumber = Converter.ToString(row[fieldProductNumberColumn.SourceColumn.Name]); - string productLanguageId = _defaultLanguageId; - if (fieldProductLanguageIdColumn != null && fieldProductLanguageIdColumn.Active && fieldProductLanguageIdColumn.ScriptType != ScriptType.Constant && - !string.IsNullOrEmpty(Converter.ToString(row[fieldProductLanguageIdColumn.SourceColumn.Name]))) - { - productLanguageId = GetLanguageID((string)row[fieldProductLanguageIdColumn.SourceColumn.Name]); - } - if (!string.IsNullOrEmpty(productLanguageId)) - { - string importedProductsByNumberIdentifier = GetImportedProductsByNumberMultipleProductsIdentifier(productNumber, productLanguageId); - ImportedProductsByNumber.TryGetValue(importedProductsByNumberIdentifier, out product); - } - if (product == null) - { - ImportedProductsByNumber.TryGetValue(productNumber, out product); - } - if (product == null) - { - product = GetExistingProductForFieldValue(row, productNumber, fieldProductVariantIdColumn, fieldProductLanguageIdColumn); - } + columnMappings.TryGetValue("FieldValueProductLanguageId", out fieldProductLanguageIdColumn); + columnMappings.TryGetValue("FieldValueProductVariantId", out fieldProductVariantIdColumn); - if (product != null) - { - dataRow["FieldValueProductId"] = product["ProductId"]; - row["FieldValueProductId"] = dataRow["FieldValueProductId"]; - } - else - { - throw new Exception( - "Attempt to import category field value failed: no productID found. Attempted to import Category field value:" + - dataRow.ToString()); - } - } - string fieldLanguageID = (product != null) ? product["ProductLanguageId"].ToString() : _defaultLanguageId; + if (!columnMappings.TryGetValue("FieldValueProductId", out fieldProductIdColumn) && columnMappings.TryGetValue("FieldValueProductNumber", out fieldProductNumberColumn)) + { + //find id from productnumber, variantID and Language if available + var productNumber = Converter.ToString(row[fieldProductNumberColumn.SourceColumn.Name]); + string productLanguageId = _defaultLanguageId; if (fieldProductLanguageIdColumn != null && fieldProductLanguageIdColumn.Active && fieldProductLanguageIdColumn.ScriptType != ScriptType.Constant && !string.IsNullOrEmpty(Converter.ToString(row[fieldProductLanguageIdColumn.SourceColumn.Name]))) { - fieldLanguageID = GetLanguageID((string)row[fieldProductLanguageIdColumn.SourceColumn.Name]); - row[fieldProductLanguageIdColumn.SourceColumn.Name] = fieldLanguageID; + productLanguageId = GetLanguageID((string)row[fieldProductLanguageIdColumn.SourceColumn.Name]); } - else + if (!string.IsNullOrEmpty(productLanguageId)) { - dataRow["FieldValueProductLanguageId"] = fieldLanguageID; - if (fieldProductLanguageIdColumn != null && fieldProductLanguageIdColumn.ScriptType != ScriptType.Constant) - { - row[fieldProductLanguageIdColumn.SourceColumn.Name] = fieldLanguageID; - } + string importedProductsByNumberIdentifier = GetImportedProductsByNumberMultipleProductsIdentifier(productNumber, productLanguageId); + ImportedProductsByNumber.TryGetValue(importedProductsByNumberIdentifier, out product); + } + if (product == null) + { + ImportedProductsByNumber.TryGetValue(productNumber, out product); + } + if (product == null) + { + product = GetExistingProductForFieldValue(row, productNumber, fieldProductVariantIdColumn, fieldProductLanguageIdColumn); } - if (fieldProductVariantIdColumn == null || string.IsNullOrEmpty(Converter.ToString(row[fieldProductVariantIdColumn.SourceColumn.Name]))) + if (product != null) { - dataRow["FieldValueProductVariantId"] = product != null ? product["ProductVariantID"].ToString() : ""; - row["FieldValueProductVariantId"] = dataRow["FieldValueProductVariantId"]; + dataRow["FieldValueProductId"] = product["ProductId"]; + row["FieldValueProductId"] = dataRow["FieldValueProductId"]; + } + else + { + throw new Exception( + "Attempt to import category field value failed: no productID found. Attempted to import Category field value:" + + dataRow.ToString()); } } - - private DataRow GetExistingProductForFieldValue(Dictionary row, string productNumber, ColumnMapping fieldProductVariantIdColumn, ColumnMapping fieldProductLanguageIdColumn) + string fieldLanguageID = (product != null) ? product["ProductLanguageId"].ToString() : _defaultLanguageId; + if (fieldProductLanguageIdColumn != null && fieldProductLanguageIdColumn.Active && fieldProductLanguageIdColumn.ScriptType != ScriptType.Constant && + !string.IsNullOrEmpty(Converter.ToString(row[fieldProductLanguageIdColumn.SourceColumn.Name]))) { - string selectExpression = "ProductNumber='" + productNumber + "'"; - - if (fieldProductVariantIdColumn != null) + fieldLanguageID = GetLanguageID((string)row[fieldProductLanguageIdColumn.SourceColumn.Name]); + row[fieldProductLanguageIdColumn.SourceColumn.Name] = fieldLanguageID; + } + else + { + dataRow["FieldValueProductLanguageId"] = fieldLanguageID; + if (fieldProductLanguageIdColumn != null && fieldProductLanguageIdColumn.ScriptType != ScriptType.Constant) { - string productVariantId = (string)row[fieldProductVariantIdColumn.SourceColumn.Name]; - selectExpression = selectExpression + " and ProductVariantId='" + productVariantId + "'"; + row[fieldProductLanguageIdColumn.SourceColumn.Name] = fieldLanguageID; } + } - if (fieldProductLanguageIdColumn != null) - { - string productLanguageId = (string)row[fieldProductLanguageIdColumn.SourceColumn.Name]; - selectExpression = selectExpression + " and ProductLanguageId='" + productLanguageId + "'"; - } + if (fieldProductVariantIdColumn == null || string.IsNullOrEmpty(Converter.ToString(row[fieldProductVariantIdColumn.SourceColumn.Name]))) + { + dataRow["FieldValueProductVariantId"] = product != null ? product["ProductVariantID"].ToString() : ""; + row["FieldValueProductVariantId"] = dataRow["FieldValueProductVariantId"]; + } + } - var rows = ExistingProducts.Select(selectExpression); - if (rows.Length > 0) + private DataRow GetExistingProductForFieldValue(Dictionary row, string productNumber, ColumnMapping fieldProductVariantIdColumn, ColumnMapping fieldProductLanguageIdColumn) + { + string selectExpression = "ProductNumber='" + productNumber + "'"; + + if (fieldProductVariantIdColumn != null) + { + string productVariantId = (string)row[fieldProductVariantIdColumn.SourceColumn.Name]; + selectExpression = selectExpression + " and ProductVariantId='" + productVariantId + "'"; + } + + if (fieldProductLanguageIdColumn != null) + { + string productLanguageId = (string)row[fieldProductLanguageIdColumn.SourceColumn.Name]; + selectExpression = selectExpression + " and ProductLanguageId='" + productLanguageId + "'"; + } + + var rows = ExistingProducts.Select(selectExpression); + if (rows.Length > 0) + { + return rows[0]; + } + + return null; + } + + private void AddParentGroupReference(string groupId, string parentGroupId, int groupRelationsSorting) + { + groupId = groupId.Replace("'", "''"); + parentGroupId = parentGroupId.Replace("'", "''"); + var relationSeparator = "_;_"; + if (!DataRowsToWrite["EcomGroupRelations"].ContainsKey($"{groupId}{relationSeparator}{parentGroupId}")) + { + if (groupRelationsSorting == 0)//if no sorting value in the source and it is existing relation keep values from database { - return rows[0]; + GroupRelationSorting.TryGetValue($"{groupId}{_sortingKeySeparator}{parentGroupId}", out groupRelationsSorting); } - - return null; + var newRow = DataToWrite.Tables["EcomGroupRelations"].NewRow(); + newRow["GroupRelationsGroupId"] = groupId; + newRow["GroupRelationsParentId"] = parentGroupId; + newRow["GroupRelationsSorting"] = groupRelationsSorting; + DataRowsToWrite["EcomGroupRelations"].Add($"{groupId}{relationSeparator}{parentGroupId}", new List() { newRow }); } + } - private void AddParentGroupReference(string groupId, string parentGroupId, int groupRelationsSorting) + private string GetLanguageID(string languageId) + { + string result = _defaultLanguageId; + languageId = languageId.Replace("'", "''"); + DataRow languageRow = null; + if (!EcomLanguages.TryGetValue(languageId, out languageRow)) { - groupId = groupId.Replace("'", "''"); - parentGroupId = parentGroupId.Replace("'", "''"); - var relationSeparator = "_;_"; - if (!DataRowsToWrite["EcomGroupRelations"].ContainsKey($"{groupId}{relationSeparator}{parentGroupId}")) + foreach (var row in EcomLanguages.Values) { - if (groupRelationsSorting == 0)//if no sorting value in the source and it is existing relation keep values from database + var languageRowCode = Converter.ToString(row["LanguageCode2"]); + var languageRowName = Converter.ToString(row["LanguageName"]); + if (string.Equals(languageRowCode, languageId, StringComparison.OrdinalIgnoreCase)) + { + languageRow = row; + break; + } + if (string.Equals(languageRowName, languageId, StringComparison.OrdinalIgnoreCase)) { - GroupRelationSorting.TryGetValue($"{groupId}{_sortingKeySeparator}{parentGroupId}", out groupRelationsSorting); + languageRow = row; } - var newRow = DataToWrite.Tables["EcomGroupRelations"].NewRow(); - newRow["GroupRelationsGroupId"] = groupId; - newRow["GroupRelationsParentId"] = parentGroupId; - newRow["GroupRelationsSorting"] = groupRelationsSorting; - DataRowsToWrite["EcomGroupRelations"].Add($"{groupId}{relationSeparator}{parentGroupId}", new List() { newRow }); } } - private string GetLanguageID(string languageId) + if (languageRow == null) { - string result = _defaultLanguageId; - languageId = languageId.Replace("'", "''"); - DataRow languageRow = null; - if (!EcomLanguages.TryGetValue(languageId, out languageRow)) + if (!string.IsNullOrEmpty(languageId)) { - foreach (var row in EcomLanguages.Values) + var row = FindRow("EcomLanguages", languageId); + if (row == null) { - var languageRowCode = Converter.ToString(row["LanguageCode2"]); - var languageRowName = Converter.ToString(row["LanguageName"]); - if (string.Equals(languageRowCode, languageId, StringComparison.OrdinalIgnoreCase)) - { - languageRow = row; - break; - } - if (string.Equals(languageRowName, languageId, StringComparison.OrdinalIgnoreCase)) - { - languageRow = row; - } + var filter = new Func(r => r.Table.Columns.Contains("LanguageName") && (string)r["LanguageName"] == languageId); + row = FindRow("EcomLanguages", filter); } - } + //create new Language + if (row == null) + { + languageRow = GetDataTableNewRow("EcomLanguages"); + LastLanguageId = LastLanguageId + 1; + result = "ImportedLANG" + LastLanguageId; + languageRow["LanguageID"] = result; + languageRow["LanguageName"] = languageId; + languageRow["LanguageNativeName"] = languageId; - if (languageRow == null) - { - if (!string.IsNullOrEmpty(languageId)) + DataRowsToWrite[languageRow.Table.TableName].Add("ImportedLANG" + LastLanguageId, new List() { languageRow }); + } + else { - var row = FindRow("EcomLanguages", languageId); - if (row == null) - { - var filter = new Func(r => r.Table.Columns.Contains("LanguageName") && (string)r["LanguageName"] == languageId); - row = FindRow("EcomLanguages", filter); - } - //create new Language - if (row == null) - { - languageRow = GetDataTableNewRow("EcomLanguages"); - LastLanguageId = LastLanguageId + 1; - result = "ImportedLANG" + LastLanguageId; - languageRow["LanguageID"] = result; - languageRow["LanguageName"] = languageId; - languageRow["LanguageNativeName"] = languageId; - - DataRowsToWrite[languageRow.Table.TableName].Add("ImportedLANG" + LastLanguageId, new List() { languageRow }); - } - else - { - result = (string)row["LanguageID"]; - } + result = (string)row["LanguageID"]; } } - else - { - result = (string)languageRow["LanguageID"]; - } - return result; } - - private string CreateProductRelatedGroup(string relatedGroupID, string relatedGroupLanguage) + else { - DataRow newProductRelatedGroup = DataToWrite.Tables["EcomProductsRelatedGroups"].NewRow(); - LastRelatedGroupID = LastRelatedGroupID + 1; - newProductRelatedGroup["RelatedGroupID"] = "ImportedRELGRP" + LastRelatedGroupID; - newProductRelatedGroup["RelatedGroupName"] = relatedGroupID; - newProductRelatedGroup["RelatedGroupLanguageID"] = relatedGroupLanguage; + result = (string)languageRow["LanguageID"]; + } + return result; + } + + private string CreateProductRelatedGroup(string relatedGroupID, string relatedGroupLanguage) + { + DataRow newProductRelatedGroup = DataToWrite.Tables["EcomProductsRelatedGroups"].NewRow(); + LastRelatedGroupID = LastRelatedGroupID + 1; + newProductRelatedGroup["RelatedGroupID"] = "ImportedRELGRP" + LastRelatedGroupID; + newProductRelatedGroup["RelatedGroupName"] = relatedGroupID; + newProductRelatedGroup["RelatedGroupLanguageID"] = relatedGroupLanguage; - DataRowsToWrite["EcomProductsRelatedGroups"].Add("ImportedRELGRP" + LastRelatedGroupID, new List() { newProductRelatedGroup }); + DataRowsToWrite["EcomProductsRelatedGroups"].Add("ImportedRELGRP" + LastRelatedGroupID, new List() { newProductRelatedGroup }); - return (string)newProductRelatedGroup["RelatedGroupID"]; - } + return (string)newProductRelatedGroup["RelatedGroupID"]; + } - private string GetDefaultGroupID(string relatedGroupLanguage) + private string GetDefaultGroupID(string relatedGroupLanguage) + { + relatedGroupLanguage = relatedGroupLanguage.Replace("'", "''"); + var relationFound = false; + foreach (var productRelGroupRows in ProductsRelatedGroups.Values) { - relatedGroupLanguage = relatedGroupLanguage.Replace("'", "''"); - var relationFound = false; - foreach (var productRelGroupRows in ProductsRelatedGroups.Values) + foreach (var productRelGroupRow in productRelGroupRows) { - foreach (var productRelGroupRow in productRelGroupRows) + var groupName = Converter.ToString(productRelGroupRow["RelatedGroupName"]); + var languageId = Converter.ToString(productRelGroupRow["RelatedGroupLanguageID"]); + if (groupName.Equals("Imported Relations Group") && languageId.Equals(relatedGroupLanguage)) { - var groupName = Converter.ToString(productRelGroupRow["RelatedGroupName"]); - var languageId = Converter.ToString(productRelGroupRow["RelatedGroupLanguageID"]); - if (groupName.Equals("Imported Relations Group") && languageId.Equals(relatedGroupLanguage)) - { - relationFound = true; - break; - } + relationFound = true; + break; } } - if (!relationFound) + } + if (!relationFound) + { + foreach (var productRelGroupRows in DataRowsToWrite["EcomProductsRelatedGroups"].Values) { - foreach (var productRelGroupRows in DataRowsToWrite["EcomProductsRelatedGroups"].Values) + foreach (var productRelGroupRow in productRelGroupRows) { - foreach (var productRelGroupRow in productRelGroupRows) + var groupName = Converter.ToString(productRelGroupRow["RelatedGroupName"]); + var groupLangauge = Converter.ToString(productRelGroupRow["RelatedGroupLanguageID"]); + if (string.Equals(groupName, "Imported Relations Group", StringComparison.OrdinalIgnoreCase) && + string.Equals(groupLangauge, relatedGroupLanguage, StringComparison.OrdinalIgnoreCase)) { - var groupName = Converter.ToString(productRelGroupRow["RelatedGroupName"]); - var groupLangauge = Converter.ToString(productRelGroupRow["RelatedGroupLanguageID"]); - if (string.Equals(groupName, "Imported Relations Group", StringComparison.OrdinalIgnoreCase) && - string.Equals(groupLangauge, relatedGroupLanguage, StringComparison.OrdinalIgnoreCase)) - { - return (string)productRelGroupRow["RelatedGroupID"]; - } + return (string)productRelGroupRow["RelatedGroupID"]; } } } + } - DataRow newProductRelatedGroup = DataToWrite.Tables["EcomProductsRelatedGroups"].NewRow(); - LastRelatedGroupID = LastRelatedGroupID + 1; - newProductRelatedGroup["RelatedGroupID"] = "ImportedRELGRP" + LastRelatedGroupID; - newProductRelatedGroup["RelatedGroupName"] = "Imported Relations Group"; - newProductRelatedGroup["RelatedGroupLanguageID"] = relatedGroupLanguage; + DataRow newProductRelatedGroup = DataToWrite.Tables["EcomProductsRelatedGroups"].NewRow(); + LastRelatedGroupID = LastRelatedGroupID + 1; + newProductRelatedGroup["RelatedGroupID"] = "ImportedRELGRP" + LastRelatedGroupID; + newProductRelatedGroup["RelatedGroupName"] = "Imported Relations Group"; + newProductRelatedGroup["RelatedGroupLanguageID"] = relatedGroupLanguage; - DataRowsToWrite["EcomProductsRelatedGroups"].Add("ImportedRELGRP" + LastRelatedGroupID, new List() { newProductRelatedGroup }); - return (string)newProductRelatedGroup["RelatedGroupID"]; + DataRowsToWrite["EcomProductsRelatedGroups"].Add("ImportedRELGRP" + LastRelatedGroupID, new List() { newProductRelatedGroup }); + return (string)newProductRelatedGroup["RelatedGroupID"]; - } + } - private void AddVariantGroupReferenceToProductByVariantGroupName(string productID, string variantGroupsString) + private void AddVariantGroupReferenceToProductByVariantGroupName(string productID, string variantGroupsString) + { + variantGroupsString = variantGroupsString.Replace("'", "''"); + DataRow variantGroupRow = null; + if (VariantGroups.TryGetValue(variantGroupsString, out variantGroupRow)) { - variantGroupsString = variantGroupsString.Replace("'", "''"); - DataRow variantGroupRow = null; - if (VariantGroups.TryGetValue(variantGroupsString, out variantGroupRow)) + AddVariantGroupReferenceToProduct(productID, (string)variantGroupRow["VariantGroupID"]); + } + else + { + var filter = new Func(r => (string)r["VariantGroupID"] == variantGroupsString || (r.Table.Columns.Contains("VariantGroupName") && (string)r["VariantGroupName"] == variantGroupsString)); + variantGroupRow = FindRow("EcomVariantGroups", filter); + if (variantGroupRow != null) { AddVariantGroupReferenceToProduct(productID, (string)variantGroupRow["VariantGroupID"]); } else { - var filter = new Func(r => (string)r["VariantGroupID"] == variantGroupsString || (r.Table.Columns.Contains("VariantGroupName") && (string)r["VariantGroupName"] == variantGroupsString)); - variantGroupRow = FindRow("EcomVariantGroups", filter); - if (variantGroupRow != null) - { - AddVariantGroupReferenceToProduct(productID, (string)variantGroupRow["VariantGroupID"]); - } - else - { - throw new Exception("Relation betweeen product \"" + productID + "\" and VariantGroup \"" + variantGroupsString + "\" can not be created. The variantgroup does not exist."); - } + throw new Exception("Relation betweeen product \"" + productID + "\" and VariantGroup \"" + variantGroupsString + "\" can not be created. The variantgroup does not exist."); } } + } - private void AddShopReferenceToGroupByShopName(string groupID, string shopName, int shopSorting) + private void AddShopReferenceToGroupByShopName(string groupID, string shopName, int shopSorting) + { + shopName = shopName.Replace("'", "''"); + DataRow shopRow = null; + foreach (var row in EcomShops.Values) { - shopName = shopName.Replace("'", "''"); - DataRow shopRow = null; - foreach (var row in EcomShops.Values) + var name = Converter.ToString(row["shopName"]); + var id = Converter.ToString(row["ShopID"]); + if (id.Equals(shopName) || name.Equals(shopName)) { - var name = Converter.ToString(row["shopName"]); - var id = Converter.ToString(row["ShopID"]); - if (id.Equals(shopName) || name.Equals(shopName)) - { - shopRow = row; - } + shopRow = row; } + } + if (shopRow != null) + { + AddShopReferenceToGroup(groupID, (string)shopRow["ShopID"], shopSorting); + } + else + { + shopRow = FindRow("EcomShops", shopName); if (shopRow != null) { AddShopReferenceToGroup(groupID, (string)shopRow["ShopID"], shopSorting); } else { - shopRow = FindRow("EcomShops", shopName); - if (shopRow != null) - { - AddShopReferenceToGroup(groupID, (string)shopRow["ShopID"], shopSorting); - } - else - { - var newShop = DataToWrite.Tables["EcomShops"].NewRow(); - newShop["ShopID"] = "ImportedSHOP" + LastShopId; - newShop["ShopName"] = shopName; - LastShopId = LastShopId + 1; - DataRowsToWrite["EcomProductsRelatedGroups"].Add("ImportedSHOP" + LastShopId, new List() { newShop }); - AddShopReferenceToGroup(groupID, (string)newShop["ShopID"], shopSorting); - } + var newShop = DataToWrite.Tables["EcomShops"].NewRow(); + newShop["ShopID"] = "ImportedSHOP" + LastShopId; + newShop["ShopName"] = shopName; + LastShopId = LastShopId + 1; + DataRowsToWrite["EcomProductsRelatedGroups"].Add("ImportedSHOP" + LastShopId, new List() { newShop }); + AddShopReferenceToGroup(groupID, (string)newShop["ShopID"], shopSorting); } } - /// - /// Returns true if group reference created, otherwise false - /// - private bool AddGroupReferenceToProduct(string productID, string languageID, string group, int? sorting, string primaryGroup) + } + /// + /// Returns true if group reference created, otherwise false + /// + private bool AddGroupReferenceToProduct(string productID, string languageID, string group, int? sorting, string primaryGroup) + { + bool result = true; + bool isPrimary = (group == primaryGroup) ? true : false; + group = group.Replace("'", "''"); + var filter = new Func(g => (string)g["GroupID"] == group || (string)g["GroupName"] == group); + List groupRows = FindExistingRows(ProductGroups, group, filter); + if (groupRows != null && groupRows.Count > 0) { - bool result = true; - bool isPrimary = (group == primaryGroup) ? true : false; - group = group.Replace("'", "''"); - var filter = new Func(g => (string)g["GroupID"] == group || (string)g["GroupName"] == group); - List groupRows = FindExistingRows(ProductGroups, group, filter); - if (groupRows != null && groupRows.Count > 0) + // Add product to all of the found existing groups + foreach (DataRow row in groupRows) { - // Add product to all of the found existing groups - foreach (DataRow row in groupRows) - { - AddGroupReferenceRowToProduct(productID, (string)row["GroupID"], sorting, isPrimary); - } + AddGroupReferenceRowToProduct(productID, (string)row["GroupID"], sorting, isPrimary); + } + } + else + { + filter = new Func(g => (string)g["GroupID"] == group || (g.Table.Columns.Contains("GroupName") && (string)g["GroupName"] == group)); + DataRow groupRow = FindRow("EcomGroups", filter); + if (groupRow != null) + { + AddGroupReferenceRowToProduct(productID, (string)groupRow["GroupID"], sorting, isPrimary); } else { - filter = new Func(g => (string)g["GroupID"] == group || (g.Table.Columns.Contains("GroupName") && (string)g["GroupName"] == group)); - DataRow groupRow = FindRow("EcomGroups", filter); - if (groupRow != null) + if (_createMissingGoups) { - AddGroupReferenceRowToProduct(productID, (string)groupRow["GroupID"], sorting, isPrimary); - } - else - { - if (_createMissingGoups) - { - LastGroupId = LastGroupId + 1; - var newGroup = GetDataTableNewRow("EcomGroups"); + LastGroupId = LastGroupId + 1; + var newGroup = GetDataTableNewRow("EcomGroups"); - newGroup["GroupID"] = "ImportedGROUP" + LastGroupId; - if (string.IsNullOrEmpty(languageID)) - { - newGroup["GroupLanguageID"] = _defaultLanguageId; - } - else - { - newGroup["GroupLanguageID"] = languageID; - } - newGroup["GroupName"] = group; - DataRowsToWrite[newGroup.Table.TableName].Add("ImportedGROUP" + LastGroupId, new List() { newGroup }); - AddShopReferenceToGroup("ImportedGROUP" + LastGroupId, DefaultShop, 0); - AddGroupReferenceRowToProduct(productID, (string)newGroup["GroupID"], sorting, isPrimary); + newGroup["GroupID"] = "ImportedGROUP" + LastGroupId; + if (string.IsNullOrEmpty(languageID)) + { + newGroup["GroupLanguageID"] = _defaultLanguageId; } else { - result = false; + newGroup["GroupLanguageID"] = languageID; } + newGroup["GroupName"] = group; + DataRowsToWrite[newGroup.Table.TableName].Add("ImportedGROUP" + LastGroupId, new List() { newGroup }); + AddShopReferenceToGroup("ImportedGROUP" + LastGroupId, DefaultShop, 0); + AddGroupReferenceRowToProduct(productID, (string)newGroup["GroupID"], sorting, isPrimary); + } + else + { + result = false; } } - return result; } + return result; + } - private List FindExistingRows(Dictionary> collection, string id, Func filter) + private List FindExistingRows(Dictionary> collection, string id, Func filter) + { + List rows = null; + if (!collection.TryGetValue(id, out rows)) { - List rows = null; - if (!collection.TryGetValue(id, out rows)) - { - return collection.Values.SelectMany(g => g).Where(filter).ToList(); - } - return rows; + return collection.Values.SelectMany(g => g).Where(filter).ToList(); } + return rows; + } - private DataRow FindRow(string tableName, Func filter) + private DataRow FindRow(string tableName, Func filter) + { + Dictionary> collection = null; + if (DataRowsToWrite.TryGetValue(tableName, out collection)) { - Dictionary> collection = null; - if (DataRowsToWrite.TryGetValue(tableName, out collection)) + if (collection != null) { - if (collection != null) - { - return collection.Values.SelectMany(v => v).Where(filter).FirstOrDefault(); - } + return collection.Values.SelectMany(v => v).Where(filter).FirstOrDefault(); } - else + } + else + { + foreach (var key in DataRowsToWrite.Keys) { - foreach (var key in DataRowsToWrite.Keys) + if (key.StartsWith(tableName + "$", StringComparison.InvariantCultureIgnoreCase)) { - if (key.StartsWith(tableName + "$", StringComparison.InvariantCultureIgnoreCase)) + collection = DataRowsToWrite[key]; + if (collection != null) { - collection = DataRowsToWrite[key]; - if (collection != null) + DataRow row = collection.Values.SelectMany(v => v).Where(filter).FirstOrDefault(); + if (row != null) { - DataRow row = collection.Values.SelectMany(v => v).Where(filter).FirstOrDefault(); - if (row != null) - { - return row; - } + return row; } } } } - return null; } + return null; + } - private void AddCategoryFieldValueToProduct(string productID, string productVariantId, string languageID, string categoryId, string fieldId, string value) + private void AddCategoryFieldValueToProduct(string productID, string productVariantId, string languageID, string categoryId, string fieldId, string value) + { + if (ProductCategoryFields.ContainsKey($"{categoryId}|{fieldId}")) { - if (ProductCategoryFields.ContainsKey($"{categoryId}|{fieldId}")) - { - var categoryFieldValue = GetDataTableNewRow("EcomProductCategoryFieldValue"); - categoryFieldValue["FieldValueFieldId"] = fieldId; - categoryFieldValue["FieldValueFieldCategoryId"] = categoryId; - categoryFieldValue["FieldValueProductId"] = productID; - categoryFieldValue["FieldValueProductVariantId"] = productVariantId; - categoryFieldValue["FieldValueProductLanguageId"] = languageID; - categoryFieldValue["FieldValueValue"] = value; + var categoryFieldValue = GetDataTableNewRow("EcomProductCategoryFieldValue"); + categoryFieldValue["FieldValueFieldId"] = fieldId; + categoryFieldValue["FieldValueFieldCategoryId"] = categoryId; + categoryFieldValue["FieldValueProductId"] = productID; + categoryFieldValue["FieldValueProductVariantId"] = productVariantId; + categoryFieldValue["FieldValueProductLanguageId"] = languageID; + categoryFieldValue["FieldValueValue"] = value; - DataRowsToWrite["EcomProductCategoryFieldValue"].Add(RowAutoId++.ToString(), new List() { categoryFieldValue }); - } - else - { - //If you try to import values to a ProductCategoryField, which doesn't exist - } + DataRowsToWrite["EcomProductCategoryFieldValue"].Add(RowAutoId++.ToString(), new List() { categoryFieldValue }); + } + else + { + //If you try to import values to a ProductCategoryField, which doesn't exist } + } - private void AddVariantGroupReferenceToProduct(string productID, string variantGroupID) + private void AddVariantGroupReferenceToProduct(string productID, string variantGroupID) + { + string key = string.Format("{0}.{1}", variantGroupID.ToLower(), productID.ToLower()); + if (!ecomVariantgroupProductrelationKeys.ContainsKey(key)) { - string key = string.Format("{0}.{1}", variantGroupID.ToLower(), productID.ToLower()); - if (!ecomVariantgroupProductrelationKeys.ContainsKey(key)) - { - var variantGroupProductRelation = DataToWrite.Tables["EcomVariantgroupProductrelation"].NewRow(); - variantGroupProductRelation["VariantGroupProductRelationVariantGroupID"] = variantGroupID; - variantGroupProductRelation["VariantGroupProductRelationProductID"] = productID; - _variantGroupProductRelationSortingCounter++; - variantGroupProductRelation["VariantGroupProductRelationSorting"] = _variantGroupProductRelationSortingCounter; - LastVariantGroupProductRelationID = LastVariantGroupProductRelationID + 1; - variantGroupProductRelation["VariantgroupProductRelationID"] = "ImportedVARGRPPRODREL" + LastVariantGroupProductRelationID; + var variantGroupProductRelation = DataToWrite.Tables["EcomVariantgroupProductrelation"].NewRow(); + variantGroupProductRelation["VariantGroupProductRelationVariantGroupID"] = variantGroupID; + variantGroupProductRelation["VariantGroupProductRelationProductID"] = productID; + _variantGroupProductRelationSortingCounter++; + variantGroupProductRelation["VariantGroupProductRelationSorting"] = _variantGroupProductRelationSortingCounter; + LastVariantGroupProductRelationID = LastVariantGroupProductRelationID + 1; + variantGroupProductRelation["VariantgroupProductRelationID"] = "ImportedVARGRPPRODREL" + LastVariantGroupProductRelationID; - DataRowsToWrite["EcomVariantgroupProductrelation"].Add(RowAutoId++.ToString(), new List() { variantGroupProductRelation }); - ecomVariantgroupProductrelationKeys.Add(key, null); - } + DataRowsToWrite["EcomVariantgroupProductrelation"].Add(RowAutoId++.ToString(), new List() { variantGroupProductRelation }); + ecomVariantgroupProductrelationKeys.Add(key, null); } + } - private void HandleVariantsCount(string productID, string variantOptionID) + private void HandleVariantsCount(string productID, string variantOptionID) + { + int productVariantOptionsCount = 0; + if (ProductVariantsCountDictionary.TryGetValue(productID, out productVariantOptionsCount)) { - int productVariantOptionsCount = 0; - if (ProductVariantsCountDictionary.TryGetValue(productID, out productVariantOptionsCount)) - { - ProductVariantsCountDictionary[productID] = productVariantOptionsCount + 1; - } - else - { - ProductVariantsCountDictionary[productID] = 1; - } - CountProductVariantGroups(productID, variantOptionID); + ProductVariantsCountDictionary[productID] = productVariantOptionsCount + 1; } - - private void WriteVariantOptionRelation(string productID, string variantOptionID) + else { - var variantOptionProductRelation = GetDataTableNewRow("EcomVariantOptionsProductRelation"); - variantOptionProductRelation["VariantOptionsProductRelationProductID"] = productID; - variantOptionProductRelation["VariantOptionsProductRelationVariantID"] = variantOptionID; + ProductVariantsCountDictionary[productID] = 1; + } + CountProductVariantGroups(productID, variantOptionID); + } - Dictionary> variantOptionRelations = null; - if (!DataRowsToWrite.TryGetValue(variantOptionProductRelation.Table.TableName, out variantOptionRelations)) - { - variantOptionRelations = new Dictionary>(); - DataRowsToWrite.Add("EcomVariantOptionsProductRelation", variantOptionRelations); - } - variantOptionRelations.Add(RowAutoId++.ToString(), new List() { variantOptionProductRelation }); + private void WriteVariantOptionRelation(string productID, string variantOptionID) + { + var variantOptionProductRelation = GetDataTableNewRow("EcomVariantOptionsProductRelation"); + variantOptionProductRelation["VariantOptionsProductRelationProductID"] = productID; + variantOptionProductRelation["VariantOptionsProductRelationVariantID"] = variantOptionID; + + Dictionary> variantOptionRelations = null; + if (!DataRowsToWrite.TryGetValue(variantOptionProductRelation.Table.TableName, out variantOptionRelations)) + { + variantOptionRelations = new Dictionary>(); + DataRowsToWrite.Add("EcomVariantOptionsProductRelation", variantOptionRelations); } + variantOptionRelations.Add(RowAutoId++.ToString(), new List() { variantOptionProductRelation }); + } - private void AddShopReferenceToGroup(string groupID, string shopID, int shopSorting) + private void AddShopReferenceToGroup(string groupID, string shopID, int shopSorting) + { + var relationAdded = false; + foreach (var relationList in DataRowsToWrite["EcomShopGroupRelation"].Values) { - var relationAdded = false; - foreach (var relationList in DataRowsToWrite["EcomShopGroupRelation"].Values) + foreach (var relationRow in relationList) { - foreach (var relationRow in relationList) - { - var relationGroupId = Converter.ToString(relationRow["ShopGroupGroupID"]); - var relationShopId = Converter.ToString(relationRow["ShopGroupShopID"]); - if (string.Equals(relationGroupId, groupID, StringComparison.OrdinalIgnoreCase) && - string.Equals(relationShopId, shopID, StringComparison.OrdinalIgnoreCase)) - { - relationAdded = true; - break; - } - } - if (relationAdded) + var relationGroupId = Converter.ToString(relationRow["ShopGroupGroupID"]); + var relationShopId = Converter.ToString(relationRow["ShopGroupShopID"]); + if (string.Equals(relationGroupId, groupID, StringComparison.OrdinalIgnoreCase) && + string.Equals(relationShopId, shopID, StringComparison.OrdinalIgnoreCase)) { + relationAdded = true; break; } } - if (!relationAdded) + if (relationAdded) { - if (shopSorting == 0)//if no sorting value for ShopGroup relation in the source and it is existing relation keep values from database - { - ShopGroupRelationSorting.TryGetValue($"{shopID}{_sortingKeySeparator}{groupID}", out shopSorting); - } - var shopGroupRelation = DataToWrite.Tables["EcomShopGroupRelation"].NewRow(); - shopGroupRelation["ShopGroupShopID"] = shopID; - shopGroupRelation["ShopGroupGroupID"] = groupID; - shopGroupRelation["ShopGroupRelationsSorting"] = shopSorting; - - DataRowsToWrite["EcomShopGroupRelation"].Add(RowAutoId++.ToString(), new List() { shopGroupRelation }); + break; + } + } + if (!relationAdded) + { + if (shopSorting == 0)//if no sorting value for ShopGroup relation in the source and it is existing relation keep values from database + { + ShopGroupRelationSorting.TryGetValue($"{shopID}{_sortingKeySeparator}{groupID}", out shopSorting); } + var shopGroupRelation = DataToWrite.Tables["EcomShopGroupRelation"].NewRow(); + shopGroupRelation["ShopGroupShopID"] = shopID; + shopGroupRelation["ShopGroupGroupID"] = groupID; + shopGroupRelation["ShopGroupRelationsSorting"] = shopSorting; + + DataRowsToWrite["EcomShopGroupRelation"].Add(RowAutoId++.ToString(), new List() { shopGroupRelation }); } + } - private void AddGroupReferenceRowToProduct(string productID, string groupID, int? sorting, bool isPrimary) + private void AddGroupReferenceRowToProduct(string productID, string groupID, int? sorting, bool isPrimary) + { + string key = string.Format("{0}.{1}", groupID.ToLower(), productID.ToLower()); + if (!ecomGroupProductRelationKeys.ContainsKey(key)) { - string key = string.Format("{0}.{1}", groupID.ToLower(), productID.ToLower()); - if (!ecomGroupProductRelationKeys.ContainsKey(key)) + bool process = true; + Tuple existingRelationIsPrimaryAndSorting; + if (ExistingGroupProductRelations.TryGetValue($"{groupID}{_sortingKeySeparator}{productID}", out existingRelationIsPrimaryAndSorting) && + existingRelationIsPrimaryAndSorting.Item1 == isPrimary && + (!sorting.HasValue || sorting.Value == existingRelationIsPrimaryAndSorting.Item2)) { - bool process = true; - Tuple existingRelationIsPrimaryAndSorting; - if (ExistingGroupProductRelations.TryGetValue($"{groupID}{_sortingKeySeparator}{productID}", out existingRelationIsPrimaryAndSorting) && - existingRelationIsPrimaryAndSorting.Item1 == isPrimary && - (!sorting.HasValue || sorting.Value == existingRelationIsPrimaryAndSorting.Item2)) - { - process = false; - } - if (!sorting.HasValue) - { - sorting = existingRelationIsPrimaryAndSorting != null ? existingRelationIsPrimaryAndSorting.Item2 : 0; - } - if (process) - { - var groupProductRelation = DataToWrite.Tables["EcomGroupProductRelation"].NewRow(); - groupProductRelation["GroupProductRelationGroupID"] = groupID; - groupProductRelation["GroupProductRelationProductID"] = productID; - groupProductRelation["GroupProductRelationSorting"] = sorting.Value; - groupProductRelation["GroupProductRelationIsPrimary"] = isPrimary; - - DataRowsToWrite["EcomGroupProductRelation"].Add(RowAutoId++.ToString(), new List() { groupProductRelation }); - ecomGroupProductRelationKeys.Add(key, null); - } + process = false; + } + if (!sorting.HasValue) + { + sorting = existingRelationIsPrimaryAndSorting != null ? existingRelationIsPrimaryAndSorting.Item2 : 0; + } + if (process) + { + var groupProductRelation = DataToWrite.Tables["EcomGroupProductRelation"].NewRow(); + groupProductRelation["GroupProductRelationGroupID"] = groupID; + groupProductRelation["GroupProductRelationProductID"] = productID; + groupProductRelation["GroupProductRelationSorting"] = sorting.Value; + groupProductRelation["GroupProductRelationIsPrimary"] = isPrimary; + + DataRowsToWrite["EcomGroupProductRelation"].Add(RowAutoId++.ToString(), new List() { groupProductRelation }); + ecomGroupProductRelationKeys.Add(key, null); + } - if (isPrimary) + if (isPrimary) + { + DataRow existingPrimaryRow = null; + if (PrimaryGroupProductRelations.TryGetValue(productID.Replace("'", "''"), out existingPrimaryRow)) { - DataRow existingPrimaryRow = null; - if (PrimaryGroupProductRelations.TryGetValue(productID.Replace("'", "''"), out existingPrimaryRow)) + string existingPrimaryGroupID = existingPrimaryRow["GroupProductRelationGroupID"].ToString(); + key = string.Format("{0}.{1}", existingPrimaryGroupID.ToLower(), productID.ToLower()); + if (existingPrimaryGroupID.ToLower() != groupID.ToLower() && !ecomGroupProductRelationKeys.ContainsKey(key)) { - string existingPrimaryGroupID = existingPrimaryRow["GroupProductRelationGroupID"].ToString(); - key = string.Format("{0}.{1}", existingPrimaryGroupID.ToLower(), productID.ToLower()); - if (existingPrimaryGroupID.ToLower() != groupID.ToLower() && !ecomGroupProductRelationKeys.ContainsKey(key)) - { - //Update GroupProductRelationIsPrimary to false for the previous existing primary record - var groupProductRelation = DataToWrite.Tables["EcomGroupProductRelation"].NewRow(); - groupProductRelation["GroupProductRelationGroupID"] = existingPrimaryRow["GroupProductRelationGroupID"]; - groupProductRelation["GroupProductRelationProductID"] = existingPrimaryRow["GroupProductRelationProductID"]; - groupProductRelation["GroupProductRelationSorting"] = existingPrimaryRow["GroupProductRelationSorting"]; - groupProductRelation["GroupProductRelationIsPrimary"] = false; - - DataRowsToWrite["EcomGroupProductRelation"].Add(RowAutoId++.ToString(), new List() { groupProductRelation }); - ecomGroupProductRelationKeys.Add(key, null); - } + //Update GroupProductRelationIsPrimary to false for the previous existing primary record + var groupProductRelation = DataToWrite.Tables["EcomGroupProductRelation"].NewRow(); + groupProductRelation["GroupProductRelationGroupID"] = existingPrimaryRow["GroupProductRelationGroupID"]; + groupProductRelation["GroupProductRelationProductID"] = existingPrimaryRow["GroupProductRelationProductID"]; + groupProductRelation["GroupProductRelationSorting"] = existingPrimaryRow["GroupProductRelationSorting"]; + groupProductRelation["GroupProductRelationIsPrimary"] = false; + + DataRowsToWrite["EcomGroupProductRelation"].Add(RowAutoId++.ToString(), new List() { groupProductRelation }); + ecomGroupProductRelationKeys.Add(key, null); } } } } + } - protected internal string _defaultShop; - private bool _removeFromEcomGroups; - private bool _removeFromEcomVariantGroups; - private readonly bool deleteExcess; + protected internal string _defaultShop; + private bool _removeFromEcomGroups; + private bool _removeFromEcomVariantGroups; + private readonly bool deleteExcess; - protected internal string DefaultShop + protected internal string DefaultShop + { + get { - get + if (string.IsNullOrEmpty(_defaultShop)) { - if (string.IsNullOrEmpty(_defaultShop)) - { - sqlCommand.CommandText = "select top(1) ShopID from EcomShops order by ShopDefault DESC, shopID"; - var result = sqlCommand.ExecuteReader(); - result.Read(); - _defaultShop = (string)result["ShopID"]; - result.Close(); - } - return _defaultShop; + sqlCommand.CommandText = "select top(1) ShopID from EcomShops order by ShopDefault DESC, shopID"; + var result = sqlCommand.ExecuteReader(); + result.Read(); + _defaultShop = (string)result["ShopID"]; + result.Close(); } - set { _defaultShop = value; } + return _defaultShop; } + set { _defaultShop = value; } + } - public void ReportProgress(Mapping mapping) + public void ReportProgress(Mapping mapping) + { + if (mapping.DestinationTable != null) { - if (mapping.DestinationTable != null) + var tableName = GetTableName(mapping.DestinationTable.Name, mapping); + if (_writtenRowsCount % 10000 != 0) { - var tableName = GetTableName(mapping.DestinationTable.Name, mapping); - if (_writtenRowsCount % 10000 != 0) - { - logger.Log("Added " + _writtenRowsCount + " rows to temporary table for " + mapping.DestinationTable.Name + "."); - } + logger.Log("Added " + _writtenRowsCount + " rows to temporary table for " + mapping.DestinationTable.Name + "."); } } + } - public void FinishWriting() + public void FinishWriting() + { + foreach (DataTable table in DataToWrite.Tables) { - foreach (DataTable table in DataToWrite.Tables) + Dictionary> tableRows = null; + if (DataRowsToWrite.TryGetValue(table.TableName, out tableRows)) { - Dictionary> tableRows = null; - if (DataRowsToWrite.TryGetValue(table.TableName, out tableRows)) - { - table.Rows.Clear(); - foreach (var rows in tableRows.Values) - { - foreach (var tableRow in rows) - { - table.Rows.Add(tableRow); - } - } - } - using (SqlBulkCopy sqlBulkCopier = new SqlBulkCopy(connection)) + table.Rows.Clear(); + foreach (var rows in tableRows.Values) { - sqlBulkCopier.DestinationTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); - sqlBulkCopier.BulkCopyTimeout = 0; - int skippedFailedRowsCount = 0; - try + foreach (var tableRow in rows) { - sqlBulkCopier.WriteToServer(table); - } - catch - { - string errors = BulkCopyHelper.GetBulkCopyFailures(sqlBulkCopier, table); - if (_skipFailingRows) - { - skippedFailedRowsCount = errors.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length - 1; - skippedFailedRowsCount = skippedFailedRowsCount < 0 ? 0 : skippedFailedRowsCount; - if (skippedFailedRowsCount > 0) - { - logger.Log($"Skipped {skippedFailedRowsCount} failing rows from the temporary {GetTableNameWithoutPrefix(table.TableName)} table"); - } - } - else - { - throw new Exception(errors); - } + table.Rows.Add(tableRow); } - RowsToWriteCount += (table.Rows.Count - skippedFailedRowsCount); } } - } - - public void DeleteExcessFromMainTable(string shop, SqlTransaction transaction, string languageId, bool deleteProductsAndGroupForSpecificLanguage, bool hideDeactivatedProducts) - { - foreach (Mapping mapping in job.Mappings.Where(m => !_addedMappingsForMoveToMainTables.Contains(m))) + using (SqlBulkCopy sqlBulkCopier = new SqlBulkCopy(connection)) { - string tempTablePrefix = "TempTableForBulkImport" + mapping.GetId(); - if (HasRowsToImport(mapping, out tempTablePrefix)) + sqlBulkCopier.DestinationTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); + sqlBulkCopier.BulkCopyTimeout = 0; + int skippedFailedRowsCount = 0; + try { - if ((mapping.DestinationTable.Name == "EcomProducts" || mapping.DestinationTable.Name == "EcomGroups") && deleteProductsAndGroupForSpecificLanguage) - { - sqlCommand.Transaction = transaction; - string extraConditions = GetDeleteFromSpecificLanguageExtraCondition(mapping, tempTablePrefix, languageId); - DynamicwebBulkInsertDestinationWriter.DeleteExcessFromMainTable(mapping, extraConditions, sqlCommand, tempTablePrefix, removeMissingAfterImportDestinationTablesOnly); - } - else if (!(mapping.DestinationTable.Name == "EcomGroups" && !_removeFromEcomGroups) && !(mapping.DestinationTable.Name == "EcomVariantGroups" && !_removeFromEcomVariantGroups)) + sqlBulkCopier.WriteToServer(table); + } + catch + { + string errors = BulkCopyHelper.GetBulkCopyFailures(sqlBulkCopier, table); + if (_skipFailingRows) { - bool? optionValue = mapping.GetOptionValue("RemoveMissingAfterImport"); - bool removeMissingAfterImport = optionValue.HasValue ? optionValue.Value : deleteExcess; - optionValue = mapping.GetOptionValue("DeactivateMissingProducts"); - bool deactivateMissing = optionValue.HasValue ? optionValue.Value : deactivateMissingProducts; - - sqlCommand.Transaction = transaction; - if (mapping.DestinationTable.Name == "EcomProducts" && deactivateMissing) - { - DynamicwebBulkInsertDestinationWriter.DeactivateMissingProductsInMainTable(mapping, sqlCommand, shop, _defaultLanguageId, hideDeactivatedProducts); - } - else if (removeMissingAfterImport || removeMissingAfterImportDestinationTablesOnly) + skippedFailedRowsCount = errors.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length - 1; + skippedFailedRowsCount = skippedFailedRowsCount < 0 ? 0 : skippedFailedRowsCount; + if (skippedFailedRowsCount > 0) { - DynamicwebBulkInsertDestinationWriter.DeleteExcessFromMainTable(mapping, GetExtraConditions(mapping, shop, null), sqlCommand, tempTablePrefix, removeMissingAfterImportDestinationTablesOnly); + logger.Log($"Skipped {skippedFailedRowsCount} failing rows from the temporary {GetTableNameWithoutPrefix(table.TableName)} table"); } } + else + { + throw new Exception(errors); + } } + RowsToWriteCount += (table.Rows.Count - skippedFailedRowsCount); } } + } - public void DeleteExistingFromMainTable(string shop, SqlTransaction transaction, string languageId) - { - sqlCommand.Transaction = transaction; - foreach (Mapping mapping in job.Mappings) - { - string tempTablePrefix = "TempTableForBulkImport" + mapping.GetId(); - if (HasRowsToImport(mapping, out tempTablePrefix)) - { - DynamicwebBulkInsertDestinationWriter.DeleteExistingFromMainTable(mapping, GetExtraConditions(mapping, shop, languageId), sqlCommand, tempTablePrefix); - } - } - } - internal static string GetDeleteFromSpecificLanguageExtraCondition(Mapping mapping, string tempTablePrefix, string languageId) + public void DeleteExcessFromMainTable(string shop, SqlTransaction transaction, string languageId, bool deleteProductsAndGroupForSpecificLanguage, bool hideDeactivatedProducts) + { + foreach (Mapping mapping in job.Mappings.Where(m => !_addedMappingsForMoveToMainTables.Contains(m))) { - string ret = string.Empty; - if (mapping != null && mapping.DestinationTable != null) + string tempTablePrefix = "TempTableForBulkImport" + mapping.GetId(); + if (HasRowsToImport(mapping, out tempTablePrefix)) { - if (mapping.DestinationTable.Name == "EcomProducts") + if ((mapping.DestinationTable.Name == "EcomProducts" || mapping.DestinationTable.Name == "EcomGroups") && deleteProductsAndGroupForSpecificLanguage) { - if (!string.IsNullOrEmpty(languageId)) - { - ret = string.Format(" AND [EcomProducts].[ProductLanguageID] = '{0}' ", languageId); - } - else - { - ret = string.Format(" AND [EcomProducts].[ProductLanguageID] IN (SELECT DISTINCT([ProductLanguageID]) FROM [EcomProducts{0}]) ", tempTablePrefix); - } + sqlCommand.Transaction = transaction; + string extraConditions = GetDeleteFromSpecificLanguageExtraCondition(mapping, tempTablePrefix, languageId); + DeleteExcessFromMainTable(mapping, extraConditions, sqlCommand, tempTablePrefix, removeMissingAfterImportDestinationTablesOnly); } - else if (mapping.DestinationTable.Name == "EcomGroups") + else if (!(mapping.DestinationTable.Name == "EcomGroups" && !_removeFromEcomGroups) && !(mapping.DestinationTable.Name == "EcomVariantGroups" && !_removeFromEcomVariantGroups)) { - if (!string.IsNullOrEmpty(languageId)) + bool? optionValue = mapping.GetOptionValue("RemoveMissingAfterImport"); + bool removeMissingAfterImport = optionValue.HasValue ? optionValue.Value : deleteExcess; + optionValue = mapping.GetOptionValue("DeactivateMissingProducts"); + bool deactivateMissing = optionValue.HasValue ? optionValue.Value : deactivateMissingProducts; + + sqlCommand.Transaction = transaction; + if (mapping.DestinationTable.Name == "EcomProducts" && deactivateMissing) { - ret = string.Format(" AND [EcomGroups].[GroupLanguageID] = '{0}' ", languageId); + DeactivateMissingProductsInMainTable(mapping, sqlCommand, shop, _defaultLanguageId, hideDeactivatedProducts); } - else + else if (removeMissingAfterImport || removeMissingAfterImportDestinationTablesOnly) { - ret = string.Format(" AND [EcomGroups].[GroupLanguageID] IN (SELECT DISTINCT([GroupLanguageID]) FROM [EcomGroups{0}]) ", tempTablePrefix); + DeleteExcessFromMainTable(mapping, GetExtraConditions(mapping, shop, null), sqlCommand, tempTablePrefix, removeMissingAfterImportDestinationTablesOnly); } } } - return ret; } + } - internal static string GetExtraConditions(Mapping mapping, string shop, string languageId) + public void DeleteExistingFromMainTable(string shop, SqlTransaction transaction, string languageId) + { + sqlCommand.Transaction = transaction; + foreach (Mapping mapping in job.Mappings) + { + string tempTablePrefix = "TempTableForBulkImport" + mapping.GetId(); + if (HasRowsToImport(mapping, out tempTablePrefix)) + { + DeleteExistingFromMainTable(mapping, GetExtraConditions(mapping, shop, languageId), sqlCommand, tempTablePrefix); + } + } + } + internal static string GetDeleteFromSpecificLanguageExtraCondition(Mapping mapping, string tempTablePrefix, string languageId) + { + string ret = string.Empty; + if (mapping != null && mapping.DestinationTable != null) { - string extraConditions = ""; if (mapping.DestinationTable.Name == "EcomProducts") { - if (!string.IsNullOrEmpty(shop)) + if (!string.IsNullOrEmpty(languageId)) { - extraConditions = - "and not exists(select * from EcomGroupProductRelation join EcomShopGroupRelation on EcomGroupProductRelation.GroupProductRelationGroupID=EcomShopGroupRelation.ShopGroupGroupID where EcomGroupProductRelation.GroupProductRelationProductID=EcomProducts.ProductID and EcomShopGroupRelation.ShopGroupShopID<>'" + - shop + "')"; + ret = string.Format(" AND [EcomProducts].[ProductLanguageID] = '{0}' ", languageId); } - - if (!string.IsNullOrEmpty(languageId)) + else { - extraConditions = extraConditions + "and (EcomProducts.ProductLanguageID='" + languageId + "')"; + ret = string.Format(" AND [EcomProducts].[ProductLanguageID] IN (SELECT DISTINCT([ProductLanguageID]) FROM [EcomProducts{0}]) ", tempTablePrefix); } } - else if (mapping.DestinationTable.Name == "EcomGroupProductRelation" && !string.IsNullOrEmpty(shop)) - { - extraConditions = - "and not exists(select * from EcomShopGroupRelation where EcomGroupProductRelation.GroupProductRelationGroupID=EcomShopGroupRelation.ShopGroupGroupID and EcomShopGroupRelation.ShopGroupShopID<>'" + - shop + "')"; - } else if (mapping.DestinationTable.Name == "EcomGroups") { - if (!string.IsNullOrEmpty(shop)) - { - extraConditions = - "and not exists(select * from EcomShopGroupRelation where EcomGroups.groupid=EcomShopGroupRelation.ShopGroupGroupID and EcomShopGroupRelation.ShopGroupShopID<>'" + - shop + "')"; - } if (!string.IsNullOrEmpty(languageId)) { - extraConditions = extraConditions + "and (EcomGroups.GroupLanguageID='" + languageId + "')"; + ret = string.Format(" AND [EcomGroups].[GroupLanguageID] = '{0}' ", languageId); } - - } - else if (mapping.DestinationTable.Name == "EcomShopGroupRelation" && !string.IsNullOrEmpty(shop)) - { - extraConditions = " and EcomShopGroupRelation.ShopGroupShopID='" + shop + "'"; - } - else if (mapping.DestinationTable.Name == "EcomDetails") - { - if (!string.IsNullOrEmpty(shop)) + else { - extraConditions = - "and not exists(select * from EcomGroupProductRelation join EcomShopGroupRelation on EcomGroupProductRelation.GroupProductRelationGroupID=EcomShopGroupRelation.ShopGroupGroupID where EcomGroupProductRelation.GroupProductRelationProductID=EcomDetails.DetailProductID and EcomShopGroupRelation.ShopGroupShopID<>'" + - shop + "')"; + ret = string.Format(" AND [EcomGroups].[GroupLanguageID] IN (SELECT DISTINCT([GroupLanguageID]) FROM [EcomGroups{0}]) ", tempTablePrefix); } } - return extraConditions; } + return ret; + } - public void MoveDataToMainTables(string shop, SqlTransaction sqlTransaction, bool updateOnly, bool insertOnly) + public void MoveDataToMainTables(string shop, SqlTransaction sqlTransaction, bool updateOnly, bool insertOnly) + { + bool isGroupsColumnInMapping = false; + List productsMappings = null; + Mapping productsMapping = null; + if (Mappings.TryGetValue("EcomProducts", out productsMappings)) { - bool isGroupsColumnInMapping = false; - List productsMappings = null; - Mapping productsMapping = null; - if (Mappings.TryGetValue("EcomProducts", out productsMappings)) - { - productsMapping = productsMappings[0]; - } - if (productsMapping != null) - { - isGroupsColumnInMapping = productsMappings[0].GetColumnMappings(true).Any(cm => cm != null && cm.DestinationColumn != null && cm.Active && string.Compare(cm.DestinationColumn.Name, "Groups", true) == 0); - } + productsMapping = productsMappings[0]; + } + if (productsMapping != null) + { + isGroupsColumnInMapping = productsMappings[0].GetColumnMappings(true).Any(cm => cm != null && cm.DestinationColumn != null && cm.Active && string.Compare(cm.DestinationColumn.Name, "Groups", true) == 0); + } - //Add mappings that are missing but needs to be there - EcomGroups & EcomVariantGroups. - AddMappingsToJobThatNeedsToBeThereForMoveToMainTables(); - //Remove column mappings that shouldn't be included in move - RemoveColumnMappingsFromJobThatShouldBeSkippedInMoveToMainTables(); + //Add mappings that are missing but needs to be there - EcomGroups & EcomVariantGroups. + AddMappingsToJobThatNeedsToBeThereForMoveToMainTables(); + //Remove column mappings that shouldn't be included in move + RemoveColumnMappingsFromJobThatShouldBeSkippedInMoveToMainTables(); - //Do Move for each mapped table - foreach (Mapping mapping in job.Mappings) + //Do Move for each mapped table + foreach (Mapping mapping in job.Mappings) + { + var mappingId = mapping.GetId(); + if (mapping.Active && ColumnMappingsByMappingId[mappingId].Count > 0) { - var mappingId = mapping.GetId(); - if (mapping.Active && ColumnMappingsByMappingId[mappingId].Count > 0) + string tempTablePrefix = "TempTableForBulkImport" + mappingId; + if (HasRowsToImport(mapping, out tempTablePrefix)) { - string tempTablePrefix = "TempTableForBulkImport" + mappingId; - if (HasRowsToImport(mapping, out tempTablePrefix)) - { - bool? optionValue = mapping.GetOptionValue("UpdateOnlyExistingRecords"); - bool updateOnlyExistingRecords = optionValue.HasValue ? optionValue.Value : updateOnly; + bool? optionValue = mapping.GetOptionValue("UpdateOnlyExistingRecords"); + bool updateOnlyExistingRecords = optionValue.HasValue ? optionValue.Value : updateOnly; - MoveDataToMainTable(mapping, tempTablePrefix, sqlTransaction, updateOnlyExistingRecords, insertOnly); - } - else - { - logger.Log(string.Format("No rows were imported to the table: {0}.", mapping.DestinationTable.Name)); - } + MoveDataToMainTable(mapping, tempTablePrefix, sqlTransaction, updateOnlyExistingRecords, insertOnly); + } + else + { + logger.Log(string.Format("No rows were imported to the table: {0}.", mapping.DestinationTable.Name)); } } + } - RemoveExcessFromRelationsTables(sqlTransaction); + RemoveExcessFromRelationsTables(sqlTransaction); - string groupProductRelationTempTablePrefix = null; - bool removeMissingAfterImport = deleteExcess; - if (productsMapping != null) - { - bool? optionValue = productsMapping.GetOptionValue("RemoveMissingAfterImport"); - removeMissingAfterImport = optionValue.HasValue ? optionValue.Value : deleteExcess; - } + string groupProductRelationTempTablePrefix = null; + bool removeMissingAfterImport = deleteExcess; + if (productsMapping != null) + { + bool? optionValue = productsMapping.GetOptionValue("RemoveMissingAfterImport"); + removeMissingAfterImport = optionValue.HasValue ? optionValue.Value : deleteExcess; + } - if (((removeMissingAfterImport && isGroupsColumnInMapping) || (partialUpdate && !removeMissingAfterImportDestinationTablesOnly)) && HasRowsToImport(productsMapping, out groupProductRelationTempTablePrefix)) - { - DeleteExcessFromGroupProductRelation(shop, sqlTransaction); - } + if (((removeMissingAfterImport && isGroupsColumnInMapping) || (partialUpdate && !removeMissingAfterImportDestinationTablesOnly)) && HasRowsToImport(productsMapping, out groupProductRelationTempTablePrefix)) + { + DeleteExcessFromGroupProductRelation(shop, sqlTransaction); } + } - private void MoveDataToMainTable(Mapping mapping, string tempTablePrefix, SqlTransaction sqlTransaction, bool updateOnly, bool insertOnly) + private void MoveDataToMainTable(Mapping mapping, string tempTablePrefix, SqlTransaction sqlTransaction, bool updateOnly, bool insertOnly) + { + sqlCommand.Transaction = sqlTransaction; + List insertColumns = new List(); + try { - sqlCommand.Transaction = sqlTransaction; - List insertColumns = new List(); - try + string sqlConditions = ""; + string firstKey = ""; + var columnMappings = new ColumnMappingCollection(mapping.GetColumnMappings().Where(m => m.Active).DistinctBy(obj => obj.DestinationColumn.Name)); + foreach (ColumnMapping columnMapping in columnMappings) { - string sqlConditions = ""; - string firstKey = ""; - var columnMappings = new ColumnMappingCollection(mapping.GetColumnMappings().Where(m => m.Active).DistinctBy(obj => obj.DestinationColumn.Name)); - foreach (ColumnMapping columnMapping in columnMappings) - { - if (columnMapping.Active) - { - SqlColumn column = (SqlColumn)columnMapping.DestinationColumn; - if (column.IsKeyColumn(columnMappings)) - { - sqlConditions = sqlConditions + "[" + mapping.DestinationTable.SqlSchema + "].[" + - mapping.DestinationTable.Name + "].[" + columnMapping.DestinationColumn.Name + "]=[" + - mapping.DestinationTable.SqlSchema + "].[" + - mapping.DestinationTable.Name + tempTablePrefix + "].[" + columnMapping.DestinationColumn.Name + "] and "; - if (firstKey == "") - { - firstKey = columnMapping.DestinationColumn.Name; - } - } - } - } - sqlConditions = sqlConditions.Substring(0, sqlConditions.Length - 4); - string insertSelect = ""; - string updateColumns = ""; - foreach (var columnMapping in columnMappings) + if (columnMapping.Active) { - if (columnMapping.Active) + SqlColumn column = (SqlColumn)columnMapping.DestinationColumn; + if (column.IsKeyColumn(columnMappings)) { - if (mapping.DestinationTable.Name.Equals("EcomProducts", StringComparison.OrdinalIgnoreCase) && - (columnMapping.DestinationColumn.Name.Equals("ProductUpdated", StringComparison.OrdinalIgnoreCase) || - columnMapping.DestinationColumn.Name.Equals("ProductCreated", StringComparison.OrdinalIgnoreCase))) - { - continue; - } - insertColumns.Add("[" + columnMapping.DestinationColumn.Name + "]"); - insertSelect = insertSelect + "[dbo].[" + columnMapping.DestinationColumn.Table.Name + tempTablePrefix + "].[" + columnMapping.DestinationColumn.Name + "], "; - if (!((SqlColumn)columnMapping.DestinationColumn).IsIdentity && !((SqlColumn)columnMapping.DestinationColumn).IsKeyColumn(columnMappings) && !columnMapping.ScriptValueForInsert) + sqlConditions = sqlConditions + "[" + mapping.DestinationTable.SqlSchema + "].[" + + mapping.DestinationTable.Name + "].[" + columnMapping.DestinationColumn.Name + "]=[" + + mapping.DestinationTable.SqlSchema + "].[" + + mapping.DestinationTable.Name + tempTablePrefix + "].[" + columnMapping.DestinationColumn.Name + "] and "; + if (firstKey == "") { - updateColumns = updateColumns + "[" + columnMapping.DestinationColumn.Name + "]=[" + mapping.DestinationTable.SqlSchema + "].[" + columnMapping.DestinationColumn.Table.Name + tempTablePrefix + "].[" + columnMapping.DestinationColumn.Name + "], "; + firstKey = columnMapping.DestinationColumn.Name; } } } - string sqlUpdateInsert = ""; - if (!string.IsNullOrEmpty(updateColumns) && !insertOnly) + } + sqlConditions = sqlConditions.Substring(0, sqlConditions.Length - 4); + string insertSelect = ""; + string updateColumns = ""; + foreach (var columnMapping in columnMappings) + { + if (columnMapping.Active) { - if (mapping.DestinationTable.Name.Equals("EcomProducts", StringComparison.OrdinalIgnoreCase)) + if (mapping.DestinationTable.Name.Equals("EcomProducts", StringComparison.OrdinalIgnoreCase) && + (columnMapping.DestinationColumn.Name.Equals("ProductUpdated", StringComparison.OrdinalIgnoreCase) || + columnMapping.DestinationColumn.Name.Equals("ProductCreated", StringComparison.OrdinalIgnoreCase))) { - updateColumns += $"[ProductUpdated] = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}' "; + continue; } - else + insertColumns.Add("[" + columnMapping.DestinationColumn.Name + "]"); + insertSelect = insertSelect + "[dbo].[" + columnMapping.DestinationColumn.Table.Name + tempTablePrefix + "].[" + columnMapping.DestinationColumn.Name + "], "; + if (!((SqlColumn)columnMapping.DestinationColumn).IsIdentity && !((SqlColumn)columnMapping.DestinationColumn).IsKeyColumn(columnMappings) && !columnMapping.ScriptValueForInsert) { - updateColumns = updateColumns.Substring(0, updateColumns.Length - 2); + updateColumns = updateColumns + "[" + columnMapping.DestinationColumn.Name + "]=[" + mapping.DestinationTable.SqlSchema + "].[" + columnMapping.DestinationColumn.Table.Name + tempTablePrefix + "].[" + columnMapping.DestinationColumn.Name + "], "; } - sqlUpdateInsert = sqlUpdateInsert + "update [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "] set " + updateColumns + " from [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + tempTablePrefix + "] where " + sqlConditions + ";"; } - if (!string.IsNullOrEmpty(insertSelect)) + } + string sqlUpdateInsert = ""; + if (!string.IsNullOrEmpty(updateColumns) && !insertOnly) + { + if (mapping.DestinationTable.Name.Equals("EcomProducts", StringComparison.OrdinalIgnoreCase)) + { + updateColumns += $"[ProductUpdated] = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}' "; + } + else { - if (mapping.DestinationTable.Name.Equals("EcomProducts", StringComparison.OrdinalIgnoreCase)) - { - insertColumns.Add("[ProductCreated]"); - insertColumns.Add("[ProductUpdated]"); - insertSelect += $"'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}', "; - insertSelect += $"'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}' "; - } - else + updateColumns = updateColumns.Substring(0, updateColumns.Length - 2); + } + sqlUpdateInsert = sqlUpdateInsert + "update [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "] set " + updateColumns + " from [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + tempTablePrefix + "] where " + sqlConditions + ";"; + } + if (!string.IsNullOrEmpty(insertSelect)) + { + if (mapping.DestinationTable.Name.Equals("EcomProducts", StringComparison.OrdinalIgnoreCase)) + { + insertColumns.Add("[ProductCreated]"); + insertColumns.Add("[ProductUpdated]"); + insertSelect += $"'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}', "; + insertSelect += $"'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)}' "; + } + else + { + insertSelect = insertSelect.Substring(0, insertSelect.Length - 2); + } + if (!updateOnly) + { + if (HasIdentity(columnMappings)) { - insertSelect = insertSelect.Substring(0, insertSelect.Length - 2); + sqlUpdateInsert = sqlUpdateInsert + "set identity_insert [" + mapping.DestinationTable.SqlSchema + "].[" + + mapping.DestinationTable.Name + "] ON;"; } - if (!updateOnly) + if (mapping.DestinationTable.Name != "EcomProducts" || (mapping.DestinationTable.Name == "EcomProducts" && !updateOnlyExistingProducts)) { + sqlUpdateInsert = sqlUpdateInsert + " insert into [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "] (" + string.Join(",", insertColumns) + ") (" + + "select " + insertSelect + " from [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + tempTablePrefix + "] left outer join [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "] on " + sqlConditions + " where [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "].[" + firstKey + "] is null);"; if (HasIdentity(columnMappings)) { sqlUpdateInsert = sqlUpdateInsert + "set identity_insert [" + mapping.DestinationTable.SqlSchema + "].[" + - mapping.DestinationTable.Name + "] ON;"; - } - if (mapping.DestinationTable.Name != "EcomProducts" || (mapping.DestinationTable.Name == "EcomProducts" && !updateOnlyExistingProducts)) - { - sqlUpdateInsert = sqlUpdateInsert + " insert into [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "] (" + string.Join(",", insertColumns) + ") (" + - "select " + insertSelect + " from [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + tempTablePrefix + "] left outer join [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "] on " + sqlConditions + " where [" + mapping.DestinationTable.SqlSchema + "].[" + mapping.DestinationTable.Name + "].[" + firstKey + "] is null);"; - if (HasIdentity(columnMappings)) - { - sqlUpdateInsert = sqlUpdateInsert + "set identity_insert [" + mapping.DestinationTable.SqlSchema + "].[" + - mapping.DestinationTable.Name + "] OFF;"; - } + mapping.DestinationTable.Name + "] OFF;"; } } } - sqlCommand.CommandText = sqlUpdateInsert; - - int timeout = SystemConfiguration.Instance.GetInt32("/GlobalSettings/System/Database/CommandTimeout"); - if (SystemConfiguration.Instance.GetInt32("/Globalsettings/DataIntegration/SQLSourceCommandTimeout") > timeout) - timeout = SystemConfiguration.Instance.GetInt32("/Globalsettings/DataIntegration/SQLSourceCommandTimeout"); - if (timeout < 360) - timeout = 360; - sqlCommand.CommandTimeout = timeout; - sqlCommand.ExecuteNonQuery(); } - catch (Exception ex) - { - throw GetMoveDataToMainTableException(ex, sqlCommand, mapping, tempTablePrefix, sqlTransaction); - } - } + sqlCommand.CommandText = sqlUpdateInsert; - internal static Exception GetMoveDataToMainTableException(Exception ex, SqlCommand sqlCommand, Mapping mapping, string tempTablePrefix, SqlTransaction sqlTransaction) + int timeout = SystemConfiguration.Instance.GetInt32("/GlobalSettings/System/Database/CommandTimeout"); + if (SystemConfiguration.Instance.GetInt32("/Globalsettings/DataIntegration/SQLSourceCommandTimeout") > timeout) + timeout = SystemConfiguration.Instance.GetInt32("/Globalsettings/DataIntegration/SQLSourceCommandTimeout"); + if (timeout < 360) + timeout = 360; + sqlCommand.CommandTimeout = timeout; + sqlCommand.ExecuteNonQuery(); + } + catch (Exception ex) { - string message = string.Format("failed to move data from temporary table [{0}.{1}{2}] to main table [{0}.{3}]. Exception: {4} Sql query: {5}", - mapping.DestinationTable.SqlSchema, - mapping.DestinationTable.Name, - tempTablePrefix, - mapping.DestinationTable.Name, - ex.Message, sqlCommand.CommandText); - if (ex is SqlException) - { - message += GetSqlExceptionMessage(ex as SqlException, sqlCommand, mapping, tempTablePrefix, sqlTransaction); - } - return new Exception(message, ex); + throw GetMoveDataToMainTableException(ex, sqlCommand, mapping, tempTablePrefix, mapping.GetColumnMappings().Where(cm => cm.Active).Select(cm => "[" + cm.DestinationColumn.Name + "]").ToList()); } + } - private static string GetSqlExceptionMessage(SqlException exception, SqlCommand sqlCommand, Mapping mapping, string tempTablePrefix, SqlTransaction sqlTransaction) + /// + /// Source columns are irrelevant, but must be set, so they are set to a random column + /// + private void AddMappingsToJobThatNeedsToBeThereForMoveToMainTables() + { + var schemaTables = job.Destination.GetSchema().GetTables(); + var tableColumnsDictionary = new Dictionary>(); + foreach (var table in schemaTables) { - StringBuilder message = new StringBuilder(); - //If The INSERT statement conflicted with the FOREIGN KEY constraint - if (exception.Number == 547) + var columnsDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + tableColumnsDictionary.Add(table.Name, columnsDictionary); + foreach (var column in table.Columns) { - var columnMappings = mapping.GetColumnMappings(); - List mappings = columnMappings.Where(cm => cm.Active && ((SqlColumn)cm.DestinationColumn).IsKeyColumn(columnMappings)).ToList(); - if (mappings.Count > 0) + if (!columnsDictionary.ContainsKey(column.Name)) { - List constraints = GetTableConstraints(sqlCommand, mapping.DestinationTable.Name, sqlTransaction); - List columns = new List(); - foreach (var columnMapping in mapping.GetColumnMappings().Where(cm => cm.Active)) - { - columns.Add("[" + columnMapping.DestinationColumn.Name + "]"); - } - foreach (FKTableConstraint constraint in constraints) - { - foreach (ColumnMapping cm in mappings) - { - if (cm.DestinationColumn != null && string.Compare(cm.DestinationColumn.Name, constraint.FKColumn, true) == 0) - { - var sql = string.Format("select {0} from [{1}].[{2}] where [{3}] not in (select distinct [{4}] from [{1}].[{5}])", - string.Join(",", columns), - mapping.DestinationTable.SqlSchema, mapping.DestinationTable.Name + tempTablePrefix, - constraint.FKColumn, constraint.PKColumn, constraint.PKTable); - using (var reader = Database.CreateDataReader(CommandBuilder.Create(sql), sqlCommand.Connection, sqlTransaction)) - { - message.Append(System.Environment.NewLine + "Failed rows:" + System.Environment.NewLine); - foreach (string column in columns) - { - message.AppendFormat(" [{0}],", column.Trim(new char[] { '[', ']' })); - } - message.Replace(",", "", message.Length - 1, 1); - message.Append(System.Environment.NewLine); - while (reader.Read()) - { - foreach (string column in columns) - { - message.AppendFormat(" [{0}],", reader[column.Trim(new char[] { '[', ']' })]); - } - message.Replace(",", "", message.Length - 1, 1); - message.Append(System.Environment.NewLine); - } - message.Replace(System.Environment.NewLine, "", message.Length - 2, 2); - } - } - } - } + columnsDictionary.Add(column.Name, column); } } - return message.ToString(); } - - private static List GetTableConstraints(SqlCommand sqlCommand, string fkTableName, SqlTransaction sqlTransaction) + if (HasData("EcomGroups")) { - List ret = new List(); - - var sql = string.Format( - @"SELECT PK_Table = PK.TABLE_NAME,FK_Column = CU.COLUMN_NAME,PK_Column = PT.COLUMN_NAME, Constraint_Name = C.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN ( SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY' ) PT ON PT.TABLE_NAME = PK.TABLE_NAME - WHERE FK.TABLE_NAME = '{0}'", fkTableName); - - using (var reader = Database.CreateDataReader(CommandBuilder.Create(sql), sqlCommand.Connection, sqlTransaction)) + if (!Mappings.ContainsKey("EcomGroups")) { - while (reader.Read()) + Mapping ecomGroupsMapping = job.AddMapping(); + EnsureMapping(ecomGroupsMapping, null, tableColumnsDictionary["EcomGroups"], + new string[] { "GroupID", "GroupLanguageID", "GroupName" }); + job.Mappings.Add(ecomGroupsMapping); + _addedMappingsForMoveToMainTables.Add(ecomGroupsMapping); + _removeFromEcomGroups = false; + ColumnMappingsByMappingId.Add(ecomGroupsMapping.GetId(), ecomGroupsMapping.GetColumnMappings(true)); + } + else + { + foreach (Mapping groupMapping in Mappings["EcomGroups"]) { - FKTableConstraint row = new FKTableConstraint(); - row.PKTable = Converter.ToString(reader["PK_Table"]); - row.PKColumn = Converter.ToString(reader["PK_Column"]); - row.FKColumn = Converter.ToString(reader["FK_Column"]); - ret.Add(row); + EnsureMapping(groupMapping, DestinationColumnMappings["EcomGroups"], tableColumnsDictionary["EcomGroups"], + new string[] { "GroupID", "GroupLanguageID" }); } } - return ret; - } - - private static bool HasIdentity(ColumnMappingCollection mappings) - { - return mappings.Any(cm => ((SqlColumn)cm.DestinationColumn).IsIdentity); } - /// - /// Source columns are irrelevant, but must be set, so they are set to a random column - /// - private void AddMappingsToJobThatNeedsToBeThereForMoveToMainTables() + if (HasData("EcomProductCategoryFieldValue")) { - var schemaTables = job.Destination.GetSchema().GetTables(); - var tableColumnsDictionary = new Dictionary>(); - foreach (var table in schemaTables) + if (!Mappings.ContainsKey("EcomProductCategoryFieldValue")) { - var columnsDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); - tableColumnsDictionary.Add(table.Name, columnsDictionary); - foreach (var column in table.Columns) - { - if (!columnsDictionary.ContainsKey(column.Name)) - { - columnsDictionary.Add(column.Name, column); - } - } + Mapping ecomCategoryFieldValueMapping = job.AddMapping(); + EnsureMapping(ecomCategoryFieldValueMapping, null, tableColumnsDictionary["EcomProductCategoryFieldValue"], + new string[] { "FieldValueFieldId", "FieldValueFieldCategoryId", "FieldValueProductId", "FieldValueProductVariantId", "FieldValueProductLanguageId", "FieldValueValue" }); + job.Mappings.Add(ecomCategoryFieldValueMapping); + _addedMappingsForMoveToMainTables.Add(ecomCategoryFieldValueMapping); + ColumnMappingsByMappingId.Add(ecomCategoryFieldValueMapping.GetId(), ecomCategoryFieldValueMapping.GetColumnMappings(true)); } + } - if (HasData("EcomGroups")) + if (HasData("EcomVariantGroups")) + { + if (!Mappings.ContainsKey("EcomVariantGroups")) { - if (!Mappings.ContainsKey("EcomGroups")) - { - Mapping ecomGroupsMapping = job.AddMapping(); - EnsureMapping(ecomGroupsMapping, null, tableColumnsDictionary["EcomGroups"], - new string[] { "GroupID", "GroupLanguageID", "GroupName" }); - job.Mappings.Add(ecomGroupsMapping); - _addedMappingsForMoveToMainTables.Add(ecomGroupsMapping); - _removeFromEcomGroups = false; - ColumnMappingsByMappingId.Add(ecomGroupsMapping.GetId(), ecomGroupsMapping.GetColumnMappings(true)); - } - else - { - foreach (Mapping groupMapping in Mappings["EcomGroups"]) - { - EnsureMapping(groupMapping, DestinationColumnMappings["EcomGroups"], tableColumnsDictionary["EcomGroups"], - new string[] { "GroupID", "GroupLanguageID" }); - } - } + Mapping ecomVariantsGroupsMapping = job.AddMapping(); + EnsureMapping(ecomVariantsGroupsMapping, null, tableColumnsDictionary["EcomVariantGroups"], + new string[] { "VariantGroupID", "VariantGroupLanguageID", "VariantGroupName" }); + job.Mappings.Add(ecomVariantsGroupsMapping); + _addedMappingsForMoveToMainTables.Add(ecomVariantsGroupsMapping); + _removeFromEcomVariantGroups = false; + ColumnMappingsByMappingId.Add(ecomVariantsGroupsMapping.GetId(), ecomVariantsGroupsMapping.GetColumnMappings(true)); } - - if (HasData("EcomProductCategoryFieldValue")) + else { - if (!Mappings.ContainsKey("EcomProductCategoryFieldValue")) + foreach (var ecomVariantsGroupsMapping in Mappings["EcomVariantGroups"]) { - Mapping ecomCategoryFieldValueMapping = job.AddMapping(); - EnsureMapping(ecomCategoryFieldValueMapping, null, tableColumnsDictionary["EcomProductCategoryFieldValue"], - new string[] { "FieldValueFieldId", "FieldValueFieldCategoryId", "FieldValueProductId", "FieldValueProductVariantId", "FieldValueProductLanguageId", "FieldValueValue" }); - job.Mappings.Add(ecomCategoryFieldValueMapping); - _addedMappingsForMoveToMainTables.Add(ecomCategoryFieldValueMapping); - ColumnMappingsByMappingId.Add(ecomCategoryFieldValueMapping.GetId(), ecomCategoryFieldValueMapping.GetColumnMappings(true)); + EnsureMapping(ecomVariantsGroupsMapping, DestinationColumnMappings["EcomVariantGroups"], tableColumnsDictionary["EcomVariantGroups"], + new string[] { "VariantGroupID", "VariantGroupLanguageID" }); } } + } - if (HasData("EcomVariantGroups")) + if (HasData("EcomVariantsOptions") && job.Mappings.Find(m => m.DestinationTable.Name == "EcomVariantsOptions") != null) + { + foreach (var ecomVariantsOptionsMapping in job.Mappings.FindAll(m => m.DestinationTable.Name == "EcomVariantsOptions")) { - if (!Mappings.ContainsKey("EcomVariantGroups")) - { - Mapping ecomVariantsGroupsMapping = job.AddMapping(); - EnsureMapping(ecomVariantsGroupsMapping, null, tableColumnsDictionary["EcomVariantGroups"], - new string[] { "VariantGroupID", "VariantGroupLanguageID", "VariantGroupName" }); - job.Mappings.Add(ecomVariantsGroupsMapping); - _addedMappingsForMoveToMainTables.Add(ecomVariantsGroupsMapping); - _removeFromEcomVariantGroups = false; - ColumnMappingsByMappingId.Add(ecomVariantsGroupsMapping.GetId(), ecomVariantsGroupsMapping.GetColumnMappings(true)); - } - else - { - foreach (var ecomVariantsGroupsMapping in Mappings["EcomVariantGroups"]) - { - EnsureMapping(ecomVariantsGroupsMapping, DestinationColumnMappings["EcomVariantGroups"], tableColumnsDictionary["EcomVariantGroups"], - new string[] { "VariantGroupID", "VariantGroupLanguageID" }); - } - } + EnsureMapping(ecomVariantsOptionsMapping, DestinationColumnMappings["EcomVariantsOptions"], tableColumnsDictionary["EcomVariantsOptions"], + new string[] { "VariantOptionID", "VariantOptionLanguageID" }); } + } - if (HasData("EcomVariantsOptions") && job.Mappings.Find(m => m.DestinationTable.Name == "EcomVariantsOptions") != null) + List productsMappings = null; + if (HasData("EcomProducts") && Mappings.TryGetValue("EcomProducts", out productsMappings)) + { + foreach (var ecomProductsMapping in productsMappings) { - foreach (var ecomVariantsOptionsMapping in job.Mappings.FindAll(m => m.DestinationTable.Name == "EcomVariantsOptions")) - { - EnsureMapping(ecomVariantsOptionsMapping, DestinationColumnMappings["EcomVariantsOptions"], tableColumnsDictionary["EcomVariantsOptions"], - new string[] { "VariantOptionID", "VariantOptionLanguageID" }); - } + EnsureMapping(ecomProductsMapping, DestinationColumnMappings["EcomProducts"], tableColumnsDictionary["EcomProducts"], + new string[] { "ProductID", "ProductVariantID", "ProductLanguageID" }); + EnsureMapping(ecomProductsMapping, DestinationColumnMappings["EcomProducts"], tableColumnsDictionary["EcomProducts"], + new string[] { "ProductVariantProdCounter", "ProductVariantGroupCounter", "ProductVariantCounter" }); } + } - List productsMappings = null; - if (HasData("EcomProducts") && Mappings.TryGetValue("EcomProducts", out productsMappings)) + if (HasData("EcomManufacturers")) + { + List manufacturersMappings = null; + if (Mappings.TryGetValue("EcomManufacturers", out manufacturersMappings)) { - foreach (var ecomProductsMapping in productsMappings) + foreach (var mapping in manufacturersMappings) { - EnsureMapping(ecomProductsMapping, DestinationColumnMappings["EcomProducts"], tableColumnsDictionary["EcomProducts"], - new string[] { "ProductID", "ProductVariantID", "ProductLanguageID" }); - EnsureMapping(ecomProductsMapping, DestinationColumnMappings["EcomProducts"], tableColumnsDictionary["EcomProducts"], - new string[] { "ProductVariantProdCounter", "ProductVariantGroupCounter", "ProductVariantCounter" }); + EnsureMapping(mapping, DestinationColumnMappings["EcomManufacturers"], tableColumnsDictionary["EcomManufacturers"], + new string[] { "ManufacturerID" }); + EnsureMapping(mapping, DestinationColumnMappings["EcomManufacturers"], tableColumnsDictionary["EcomManufacturers"], + new string[] { "ManufacturerName" }); } } - - if (HasData("EcomManufacturers")) + else { - List manufacturersMappings = null; - if (Mappings.TryGetValue("EcomManufacturers", out manufacturersMappings)) - { - foreach (var mapping in manufacturersMappings) - { - EnsureMapping(mapping, DestinationColumnMappings["EcomManufacturers"], tableColumnsDictionary["EcomManufacturers"], - new string[] { "ManufacturerID" }); - EnsureMapping(mapping, DestinationColumnMappings["EcomManufacturers"], tableColumnsDictionary["EcomManufacturers"], - new string[] { "ManufacturerName" }); - } - } - else - { - Mapping ecomManufacturersMapping = job.AddMapping(); - EnsureMapping(ecomManufacturersMapping, null, tableColumnsDictionary["EcomManufacturers"], new string[] { "ManufacturerID" }); - EnsureMapping(ecomManufacturersMapping, null, tableColumnsDictionary["EcomManufacturers"], new string[] { "ManufacturerName" }); - job.Mappings.Add(ecomManufacturersMapping); - _addedMappingsForMoveToMainTables.Add(ecomManufacturersMapping); - ColumnMappingsByMappingId.Add(ecomManufacturersMapping.GetId(), ecomManufacturersMapping.GetColumnMappings(true)); - } + Mapping ecomManufacturersMapping = job.AddMapping(); + EnsureMapping(ecomManufacturersMapping, null, tableColumnsDictionary["EcomManufacturers"], new string[] { "ManufacturerID" }); + EnsureMapping(ecomManufacturersMapping, null, tableColumnsDictionary["EcomManufacturers"], new string[] { "ManufacturerName" }); + job.Mappings.Add(ecomManufacturersMapping); + _addedMappingsForMoveToMainTables.Add(ecomManufacturersMapping); + ColumnMappingsByMappingId.Add(ecomManufacturersMapping.GetId(), ecomManufacturersMapping.GetColumnMappings(true)); } + } - List productCateforyMappings = null; - if (Mappings.TryGetValue("EcomProductCategoryFieldValue", out productCateforyMappings)) + List productCateforyMappings = null; + if (Mappings.TryGetValue("EcomProductCategoryFieldValue", out productCateforyMappings)) + { + foreach (var mapping in productCateforyMappings) { - foreach (var mapping in productCateforyMappings) - { - EnsureMapping(mapping, DestinationColumnMappings["EcomProductCategoryFieldValue"], tableColumnsDictionary["EcomProductCategoryFieldValue"], - new string[] { "FieldValueFieldCategoryId", "FieldValueProductId", "FieldValueProductLanguageId", "FieldValueProductVariantId" }); - } + EnsureMapping(mapping, DestinationColumnMappings["EcomProductCategoryFieldValue"], tableColumnsDictionary["EcomProductCategoryFieldValue"], + new string[] { "FieldValueFieldCategoryId", "FieldValueProductId", "FieldValueProductLanguageId", "FieldValueProductVariantId" }); } + } - if (HasData("EcomLanguages") && !Mappings.ContainsKey("EcomLanguages")) - { - Mapping ecomLanguagesMapping = job.AddMapping(); + if (HasData("EcomLanguages") && !Mappings.ContainsKey("EcomLanguages")) + { + Mapping ecomLanguagesMapping = job.AddMapping(); - EnsureMapping(ecomLanguagesMapping, null, tableColumnsDictionary["EcomLanguages"], new string[] { "LanguageID" }); - EnsureMapping(ecomLanguagesMapping, null, tableColumnsDictionary["EcomLanguages"], new string[] { "LanguageName", "LanguageNativeName" }); - job.Mappings.Remove(ecomLanguagesMapping); - //Import languages should be first of all - job.Mappings.Insert(0, ecomLanguagesMapping); - _addedMappingsForMoveToMainTables.Add(ecomLanguagesMapping); - ColumnMappingsByMappingId.Add(ecomLanguagesMapping.GetId(), ecomLanguagesMapping.GetColumnMappings(true)); - } + EnsureMapping(ecomLanguagesMapping, null, tableColumnsDictionary["EcomLanguages"], new string[] { "LanguageID" }); + EnsureMapping(ecomLanguagesMapping, null, tableColumnsDictionary["EcomLanguages"], new string[] { "LanguageName", "LanguageNativeName" }); + job.Mappings.Remove(ecomLanguagesMapping); + //Import languages should be first of all + job.Mappings.Insert(0, ecomLanguagesMapping); + _addedMappingsForMoveToMainTables.Add(ecomLanguagesMapping); + ColumnMappingsByMappingId.Add(ecomLanguagesMapping.GetId(), ecomLanguagesMapping.GetColumnMappings(true)); + } - List priceMappings = null; - if (HasData("EcomPrices") && Mappings.TryGetValue("EcomPrices", out priceMappings)) + List priceMappings = null; + if (HasData("EcomPrices") && Mappings.TryGetValue("EcomPrices", out priceMappings)) + { + foreach (Mapping mapping in priceMappings) { - foreach (Mapping mapping in priceMappings) + EnsureMapping(mapping, DestinationColumnMappings["EcomPrices"], tableColumnsDictionary["EcomPrices"], new string[] { "PriceID" }); + Column currencyColumn = DestinationColumnMappings["EcomPrices"]["PriceCurrency"]; + if (currencyColumn != null) { - EnsureMapping(mapping, DestinationColumnMappings["EcomPrices"], tableColumnsDictionary["EcomPrices"], new string[] { "PriceID" }); - Column currencyColumn = DestinationColumnMappings["EcomPrices"]["PriceCurrency"]; - if (currencyColumn != null) + currencyColumn.IsPrimaryKey = false; //change identity to update currency values but not insert the new rows + if (!DestinationColumnMappings["EcomPrices"].ContainsKey("PriceCurrency")) { - currencyColumn.IsPrimaryKey = false; //change identity to update currency values but not insert the new rows - if (!DestinationColumnMappings["EcomPrices"].ContainsKey("PriceCurrency")) - { - mapping.AddMapping(currencyColumn, currencyColumn, false); - } + mapping.AddMapping(currencyColumn, currencyColumn, false); } } } + } - List detailsMappings = null; - if (HasData("EcomDetails") && Mappings.TryGetValue("EcomDetails", out detailsMappings)) + List detailsMappings = null; + if (HasData("EcomDetails") && Mappings.TryGetValue("EcomDetails", out detailsMappings)) + { + foreach (Mapping mapping in detailsMappings) { - foreach (Mapping mapping in detailsMappings) - { - EnsureMapping(mapping, DestinationColumnMappings["EcomDetails"], tableColumnsDictionary["EcomDetails"], new string[] { "DetailID" }); - } + EnsureMapping(mapping, DestinationColumnMappings["EcomDetails"], tableColumnsDictionary["EcomDetails"], new string[] { "DetailID" }); } + } - List assortmentsMappings = null; - if (HasData("EcomAssortmentPermissions") && Mappings.TryGetValue("EcomAssortmentPermissions", out assortmentsMappings)) + List assortmentsMappings = null; + if (HasData("EcomAssortmentPermissions") && Mappings.TryGetValue("EcomAssortmentPermissions", out assortmentsMappings)) + { + foreach (Mapping mapping in assortmentsMappings) { - foreach (Mapping mapping in assortmentsMappings) - { - EnsureMapping(mapping, DestinationColumnMappings["EcomAssortmentPermissions"], tableColumnsDictionary["EcomAssortmentPermissions"], new string[] { "AssortmentPermissionAccessUserID" }); - } + EnsureMapping(mapping, DestinationColumnMappings["EcomAssortmentPermissions"], tableColumnsDictionary["EcomAssortmentPermissions"], new string[] { "AssortmentPermissionAccessUserID" }); } } + } - private static void EnsureMapping(Mapping mapping, Dictionary destinationColumns, Dictionary schemaColumns, string[] keyColumnNames) + private static void EnsureMapping(Mapping mapping, Dictionary destinationColumns, Dictionary schemaColumns, string[] keyColumnNames) + { + foreach (var keyColumn in keyColumnNames) { - foreach (var keyColumn in keyColumnNames) + if (destinationColumns == null || !destinationColumns.ContainsKey(keyColumn)) { - if (destinationColumns == null || !destinationColumns.ContainsKey(keyColumn)) - { - var groupKeyColumn = schemaColumns[keyColumn]; - mapping.AddMapping(groupKeyColumn, groupKeyColumn, false); - } + var groupKeyColumn = schemaColumns[keyColumn]; + mapping.AddMapping(groupKeyColumn, groupKeyColumn, false); } } + } - private void RemoveColumnMappingsFromJobThatShouldBeSkippedInMoveToMainTables() - { - List mappings = null; + private void RemoveColumnMappingsFromJobThatShouldBeSkippedInMoveToMainTables() + { + List mappings = null; - if (Mappings.TryGetValue("EcomProducts", out mappings)) + if (Mappings.TryGetValue("EcomProducts", out mappings)) + { + foreach (Mapping cleanMapping in mappings) { - foreach (Mapping cleanMapping in mappings) + if (cleanMapping != null) { - if (cleanMapping != null) - { - ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); - columnMapping.RemoveAll(cm => cm.DestinationColumn != null && - (new HashSet() { "RelatedProducts", "Groups", "PrimaryGroup", "GroupSorting", "VariantGroups", "VariantOptions" }.Contains(cm.DestinationColumn.Name, StringComparer.OrdinalIgnoreCase) || cm.DestinationColumn.Name.StartsWith("ProductCategory|", StringComparison.OrdinalIgnoreCase))); - } + ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); + columnMapping.RemoveAll(cm => cm.DestinationColumn != null && + (new HashSet() { "RelatedProducts", "Groups", "PrimaryGroup", "GroupSorting", "VariantGroups", "VariantOptions" }.Contains(cm.DestinationColumn.Name, StringComparer.OrdinalIgnoreCase) || cm.DestinationColumn.Name.StartsWith("ProductCategory|", StringComparison.OrdinalIgnoreCase))); } } + } - if (Mappings.TryGetValue("EcomGroups", out mappings)) + if (Mappings.TryGetValue("EcomGroups", out mappings)) + { + foreach (Mapping cleanMapping in mappings) { - foreach (Mapping cleanMapping in mappings) + if (cleanMapping != null) { - if (cleanMapping != null) - { - ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); - columnMapping.RemoveAll(cm => cm.DestinationColumn != null && - new HashSet() { "Shops", "ShopSorting", "ParentGroups", "ParentGroupsSorting", "VariantOptions" }.Contains(cm.DestinationColumn.Name, StringComparer.OrdinalIgnoreCase)); - } + ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); + columnMapping.RemoveAll(cm => cm.DestinationColumn != null && + new HashSet() { "Shops", "ShopSorting", "ParentGroups", "ParentGroupsSorting", "VariantOptions" }.Contains(cm.DestinationColumn.Name, StringComparer.OrdinalIgnoreCase)); } } + } - if (Mappings.TryGetValue("EcomProductsRelated", out mappings)) + if (Mappings.TryGetValue("EcomProductsRelated", out mappings)) + { + foreach (Mapping cleanMapping in mappings) { - foreach (Mapping cleanMapping in mappings) + if (cleanMapping != null) { - if (cleanMapping != null) - { - ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); - columnMapping.RemoveAll(cm => cm.DestinationColumn != null && string.Compare(cm.DestinationColumn.Name, "ProductRelatedLanguageID", true) == 0); - } + ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); + columnMapping.RemoveAll(cm => cm.DestinationColumn != null && string.Compare(cm.DestinationColumn.Name, "ProductRelatedLanguageID", true) == 0); } } + } - if (Mappings.TryGetValue("EcomProductCategoryFieldValue", out mappings)) + if (Mappings.TryGetValue("EcomProductCategoryFieldValue", out mappings)) + { + foreach (Mapping cleanMapping in mappings) { - foreach (Mapping cleanMapping in mappings) + if (cleanMapping != null) { - if (cleanMapping != null) - { - ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); - columnMapping.RemoveAll(cm => cm.DestinationColumn != null && cm.DestinationColumn.Name == "FieldValueProductNumber"); - } + ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); + columnMapping.RemoveAll(cm => cm.DestinationColumn != null && cm.DestinationColumn.Name == "FieldValueProductNumber"); } } + } - if (Mappings.TryGetValue("EcomAssortmentPermissions", out mappings)) + if (Mappings.TryGetValue("EcomAssortmentPermissions", out mappings)) + { + foreach (Mapping cleanMapping in mappings) { - foreach (Mapping cleanMapping in mappings) + if (cleanMapping != null) { - if (cleanMapping != null) - { - ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); - columnMapping.RemoveAll(cm => cm.DestinationColumn != null && - new HashSet() { "AssortmentPermissionCustomerNumber", "AssortmentPermissionExternalID" }.Contains(cm.DestinationColumn.Name, StringComparer.OrdinalIgnoreCase)); - } + ColumnMappingCollection columnMapping = cleanMapping.GetColumnMappings(true); + columnMapping.RemoveAll(cm => cm.DestinationColumn != null && + new HashSet() { "AssortmentPermissionCustomerNumber", "AssortmentPermissionExternalID" }.Contains(cm.DestinationColumn.Name, StringComparer.OrdinalIgnoreCase)); } } } + } - public void UpdateVariantFieldsInProducts() + public void UpdateVariantFieldsInProducts() + { + foreach (DataTable productsDataTable in FindDataTablesStartingWithName("EcomProducts")) { - foreach (DataTable productsDataTable in FindDataTablesStartingWithName("EcomProducts")) + var ecomVariantOptionsProductRelationTables = FindDataTablesStartingWithName("EcomVariantOptionsProductRelation"); + if (DataToWrite.Tables.Contains("EcomVariantgroupProductrelation") && ecomVariantOptionsProductRelationTables.Count() > 0) { - var ecomVariantOptionsProductRelationTables = FindDataTablesStartingWithName("EcomVariantOptionsProductRelation"); - if (DataToWrite.Tables.Contains("EcomVariantgroupProductrelation") && ecomVariantOptionsProductRelationTables.Count() > 0) - { - bool variantProdCounterColExist = productsDataTable.Columns.Contains("ProductVariantID") && - productsDataTable.Columns.Contains("ProductVariantProdCounter"); - bool variantGroupCounterColExist = productsDataTable.Columns.Contains("ProductVariantGroupCounter"); - bool variantCounterColExist = productsDataTable.Columns.Contains("ProductVariantCounter"); + bool variantProdCounterColExist = productsDataTable.Columns.Contains("ProductVariantID") && + productsDataTable.Columns.Contains("ProductVariantProdCounter"); + bool variantGroupCounterColExist = productsDataTable.Columns.Contains("ProductVariantGroupCounter"); + bool variantCounterColExist = productsDataTable.Columns.Contains("ProductVariantCounter"); - if (!variantProdCounterColExist && !variantGroupCounterColExist && !variantCounterColExist) - { - continue; - } + if (!variantProdCounterColExist && !variantGroupCounterColExist && !variantCounterColExist) + { + continue; + } - Dictionary productVariantCounterDict = new Dictionary(); - Dictionary> tableRows = null; - if (DataRowsToWrite.TryGetValue(productsDataTable.TableName, out tableRows)) + Dictionary productVariantCounterDict = new Dictionary(); + Dictionary> tableRows = null; + if (DataRowsToWrite.TryGetValue(productsDataTable.TableName, out tableRows)) + { + foreach (DataRow row in tableRows.Values.SelectMany(c => c)) { - foreach (DataRow row in tableRows.Values.SelectMany(c => c)) + string productId = row["ProductID"].ToString(); + string langId = row["ProductLanguageID"].ToString(); + //Check if it is already existing product row and it has filled variants counter fileds - skip it + if ((row["ProductVariantProdCounter"] == System.DBNull.Value || row["ProductVariantProdCounter"].ToString() == "0") && variantProdCounterColExist) { - string productId = row["ProductID"].ToString(); - string langId = row["ProductLanguageID"].ToString(); - //Check if it is already existing product row and it has filled variants counter fileds - skip it - if ((row["ProductVariantProdCounter"] == System.DBNull.Value || row["ProductVariantProdCounter"].ToString() == "0") && variantProdCounterColExist) + if (string.IsNullOrEmpty(row["ProductVariantID"].ToString())) + { + int variantProdCounter = 0; + ProductVariantsCountDictionary.TryGetValue(string.Format("{0}.{1}", productId, langId), out variantProdCounter); + row["ProductVariantProdCounter"] = variantProdCounter > 0 ? variantProdCounter - 1 : 0; + } + else { - if (string.IsNullOrEmpty(row["ProductVariantID"].ToString())) + if (productVariantCounterDict.ContainsKey(productId + langId)) { - int variantProdCounter = 0; - ProductVariantsCountDictionary.TryGetValue(string.Format("{0}.{1}", productId, langId), out variantProdCounter); - row["ProductVariantProdCounter"] = variantProdCounter > 0 ? variantProdCounter - 1 : 0; + productVariantCounterDict[productId + langId] = productVariantCounterDict[productId + langId] + 1; } else { - if (productVariantCounterDict.ContainsKey(productId + langId)) - { - productVariantCounterDict[productId + langId] = productVariantCounterDict[productId + langId] + 1; - } - else - { - productVariantCounterDict.Add(productId + langId, 0); - } - row["ProductVariantProdCounter"] = productVariantCounterDict[productId + langId]; + productVariantCounterDict.Add(productId + langId, 0); } + row["ProductVariantProdCounter"] = productVariantCounterDict[productId + langId]; } - //Check if it is already existing product row and it has filled variants counter fileds - skip it - if ((row["ProductVariantGroupCounter"] == System.DBNull.Value || row["ProductVariantGroupCounter"].ToString() == "0") && variantGroupCounterColExist) + } + //Check if it is already existing product row and it has filled variants counter fileds - skip it + if ((row["ProductVariantGroupCounter"] == System.DBNull.Value || row["ProductVariantGroupCounter"].ToString() == "0") && variantGroupCounterColExist) + { + int variantGroupsCount = 0; + foreach (var variantGroupRows in DataRowsToWrite["EcomVariantgroupProductrelation"].Values) { - int variantGroupsCount = 0; - foreach (var variantGroupRows in DataRowsToWrite["EcomVariantgroupProductrelation"].Values) + foreach (var variantGroupRow in variantGroupRows) { - foreach (var variantGroupRow in variantGroupRows) + var variantgroupProductRelationProductId = Converter.ToString(variantGroupRow["VariantgroupProductRelationProductID"]); + if (string.Equals(variantgroupProductRelationProductId, productId.Replace("'", "''"), StringComparison.OrdinalIgnoreCase)) { - var variantgroupProductRelationProductId = Converter.ToString(variantGroupRow["VariantgroupProductRelationProductID"]); - if (string.Equals(variantgroupProductRelationProductId, productId.Replace("'", "''"), StringComparison.OrdinalIgnoreCase)) - { - variantGroupsCount++; - } + variantGroupsCount++; } } - if (variantGroupsCount == 0) - { - ProductVariantGroupsCountDictionary.TryGetValue(productId, out variantGroupsCount); - } - row["ProductVariantGroupCounter"] = variantGroupsCount; } - //Check if it is already existing product row and it has filled variants counter fileds - skip it - if ((row["ProductVariantCounter"] == System.DBNull.Value || row["ProductVariantCounter"].ToString() == "0") && variantCounterColExist) + if (variantGroupsCount == 0) { - int variantOptionsCounter = 0; - ProductVariantsCountDictionary.TryGetValue(string.Format("{0}.{1}", productId, langId), out variantOptionsCounter); - row["ProductVariantCounter"] = variantOptionsCounter > 0 ? variantOptionsCounter - 1 : 0; + ProductVariantGroupsCountDictionary.TryGetValue(productId, out variantGroupsCount); } + row["ProductVariantGroupCounter"] = variantGroupsCount; + } + //Check if it is already existing product row and it has filled variants counter fileds - skip it + if ((row["ProductVariantCounter"] == System.DBNull.Value || row["ProductVariantCounter"].ToString() == "0") && variantCounterColExist) + { + int variantOptionsCounter = 0; + ProductVariantsCountDictionary.TryGetValue(string.Format("{0}.{1}", productId, langId), out variantOptionsCounter); + row["ProductVariantCounter"] = variantOptionsCounter > 0 ? variantOptionsCounter - 1 : 0; } } - productVariantCounterDict = null; } + productVariantCounterDict = null; } } + } - public void UpdateFieldsInExistingProductsWithVariantIDs() + public void UpdateFieldsInExistingProductsWithVariantIDs() + { + if (Mappings.ContainsKey("EcomProducts")) { - if (Mappings.ContainsKey("EcomProducts")) + foreach (var mapping in Mappings["EcomProducts"]) { - foreach (var mapping in Mappings["EcomProducts"]) + if (!DestinationColumnMappings["EcomProducts"].ContainsKey("ProductVariantID")) { - if (!DestinationColumnMappings["EcomProducts"].ContainsKey("ProductVariantID")) - { - string keyColumn = "ProductNumber"; + string keyColumn = "ProductNumber"; - if (!DestinationColumnMappings["EcomProducts"].ContainsKey("ProductNumber")) - { - string[] columnsToSkip = new string[] { "ProductID", "ProductVariantID", "ProductLanguageID", - "ProductNumber", "ProductName" }; + if (!DestinationColumnMappings["EcomProducts"].ContainsKey("ProductNumber")) + { + string[] columnsToSkip = new string[] { "ProductID", "ProductVariantID", "ProductLanguageID", + "ProductNumber", "ProductName" }; - var ecomProductsPKColumns = MappingIdEcomProductsPKColumns.Keys.Count > 0 ? - MappingIdEcomProductsPKColumns[MappingIdEcomProductsPKColumns.Keys.First()] : null; - IEnumerable columnsToSearchForProductsToUpdate = ecomProductsPKColumns.Where(c => - !columnsToSkip.Any(cs => string.Equals(c, cs, StringComparison.OrdinalIgnoreCase))); + var ecomProductsPKColumns = MappingIdEcomProductsPKColumns.Keys.Count > 0 ? + MappingIdEcomProductsPKColumns[MappingIdEcomProductsPKColumns.Keys.First()] : null; + IEnumerable columnsToSearchForProductsToUpdate = ecomProductsPKColumns.Where(c => + !columnsToSkip.Any(cs => string.Equals(c, cs, StringComparison.OrdinalIgnoreCase))); - keyColumn = columnsToSearchForProductsToUpdate.FirstOrDefault(); - if (string.IsNullOrEmpty(keyColumn)) - { - keyColumn = "ProductNumber"; - } + keyColumn = columnsToSearchForProductsToUpdate.FirstOrDefault(); + if (string.IsNullOrEmpty(keyColumn)) + { + keyColumn = "ProductNumber"; } - UpdateFieldsInExistingProductsWithVariantIDs(keyColumn, mapping); } + UpdateFieldsInExistingProductsWithVariantIDs(keyColumn, mapping); } } } + } - public void UpdateFieldsInExistingProductsWithVariantIDs(string keyColumn, Mapping mapping) + public void UpdateFieldsInExistingProductsWithVariantIDs(string keyColumn, Mapping mapping) + { + Hashtable existigProductVariantIdsCombination = GetExistingProductVariantsIDsCombinations(keyColumn); + if (existigProductVariantIdsCombination.Keys.Count > 0) { - Hashtable existigProductVariantIdsCombination = GetExistingProductVariantsIDsCombinations(keyColumn); - if (existigProductVariantIdsCombination.Keys.Count > 0) - { - string langId; - string key; - List rowsToAdd = new List(); - DataRow newRow; - var tableName = GetTableName("EcomProducts", mapping); + string langId; + string key; + List rowsToAdd = new List(); + DataRow newRow; + var tableName = GetTableName("EcomProducts", mapping); - if (DataToWrite.Tables[tableName].Columns.Contains(keyColumn) && DataToWrite.Tables[tableName].Columns.Contains("ProductVariantID")) + if (DataToWrite.Tables[tableName].Columns.Contains(keyColumn) && DataToWrite.Tables[tableName].Columns.Contains("ProductVariantID")) + { + foreach (var rows in DataRowsToWrite[tableName].Values) { - foreach (var rows in DataRowsToWrite[tableName].Values) + foreach (var row in rows) { - foreach (var row in rows) + if (!string.IsNullOrEmpty(row[keyColumn].ToString())) { - if (!string.IsNullOrEmpty(row[keyColumn].ToString())) + langId = row["ProductLanguageID"].ToString(); + if (!string.IsNullOrEmpty(langId)) { - langId = row["ProductLanguageID"].ToString(); - if (!string.IsNullOrEmpty(langId)) - { - key = string.Format("{0}.{1}", row[keyColumn].ToString(), langId); - } - else - { - key = row[keyColumn].ToString(); - } - if (existigProductVariantIdsCombination.ContainsKey(key)) + key = string.Format("{0}.{1}", row[keyColumn].ToString(), langId); + } + else + { + key = row[keyColumn].ToString(); + } + if (existigProductVariantIdsCombination.ContainsKey(key)) + { + string rowProductVariantId = (string)row["ProductVariantID"]; + List> variantsInfoList = (List>)existigProductVariantIdsCombination[key]; + foreach (Tuple variantInfo in variantsInfoList) { - string rowProductVariantId = (string)row["ProductVariantID"]; - List> variantsInfoList = (List>)existigProductVariantIdsCombination[key]; - foreach (Tuple variantInfo in variantsInfoList) + if (string.IsNullOrEmpty(rowProductVariantId) || !string.Equals(rowProductVariantId, variantInfo.Item1)) { - if (string.IsNullOrEmpty(rowProductVariantId) || !string.Equals(rowProductVariantId, variantInfo.Item1)) - { - newRow = DataToWrite.Tables[tableName].NewRow(); - newRow.ItemArray = row.ItemArray.Clone() as object[]; - newRow["ProductVariantID"] = variantInfo.Item1; - newRow["ProductVariantCounter"] = variantInfo.Item2; - newRow["ProductVariantGroupCounter"] = variantInfo.Item3; - newRow["ProductVariantProdCounter"] = variantInfo.Item4; - rowsToAdd.Add(newRow); - } + newRow = DataToWrite.Tables[tableName].NewRow(); + newRow.ItemArray = row.ItemArray.Clone() as object[]; + newRow["ProductVariantID"] = variantInfo.Item1; + newRow["ProductVariantCounter"] = variantInfo.Item2; + newRow["ProductVariantGroupCounter"] = variantInfo.Item3; + newRow["ProductVariantProdCounter"] = variantInfo.Item4; + rowsToAdd.Add(newRow); } } } } } } + } - foreach (DataRow dt in rowsToAdd) - { - DataRowsToWrite[tableName].Add(RowAutoId++.ToString(), new List() { dt }); - } - rowsToAdd = null; + foreach (DataRow dt in rowsToAdd) + { + DataRowsToWrite[tableName].Add(RowAutoId++.ToString(), new List() { dt }); } - existigProductVariantIdsCombination = null; + rowsToAdd = null; } + existigProductVariantIdsCombination = null; + } - private Hashtable GetExistingProductVariantsIDsCombinations(string searchingDifferentProductsColumn) - { - Hashtable result = new Hashtable(); - DataRow[] rows = ExistingProducts.Select("ProductVariantID <> ''"); - string key = null; - string languageId = null; + private Hashtable GetExistingProductVariantsIDsCombinations(string searchingDifferentProductsColumn) + { + Hashtable result = new Hashtable(); + DataRow[] rows = ExistingProducts.Select("ProductVariantID <> ''"); + string key = null; + string languageId = null; - foreach (DataRow row in rows) + foreach (DataRow row in rows) + { + if (!string.IsNullOrEmpty(row[searchingDifferentProductsColumn].ToString())) { - if (!string.IsNullOrEmpty(row[searchingDifferentProductsColumn].ToString())) + languageId = row["ProductLanguageID"].ToString(); + if (!string.IsNullOrEmpty(languageId)) { - languageId = row["ProductLanguageID"].ToString(); - if (!string.IsNullOrEmpty(languageId)) - { - key = string.Format("{0}.{1}", row[searchingDifferentProductsColumn].ToString(), languageId); - } - else - { - key = row[searchingDifferentProductsColumn].ToString(); - } + key = string.Format("{0}.{1}", row[searchingDifferentProductsColumn].ToString(), languageId); + } + else + { + key = row[searchingDifferentProductsColumn].ToString(); + } - Tuple variantInfo = new Tuple - ( - row["ProductVariantID"].ToString(), - row["ProductVariantCounter"].ToString(), row["ProductVariantGroupCounter"].ToString(), row["ProductVariantProdCounter"].ToString() - ); - if (result.ContainsKey(key)) - { - List> variantIDsList = (List>)result[key]; - variantIDsList.Add(variantInfo); - } - else - { - result[key] = new List>() { variantInfo }; - } + Tuple variantInfo = new Tuple + ( + row["ProductVariantID"].ToString(), + row["ProductVariantCounter"].ToString(), row["ProductVariantGroupCounter"].ToString(), row["ProductVariantProdCounter"].ToString() + ); + if (result.ContainsKey(key)) + { + List> variantIDsList = (List>)result[key]; + variantIDsList.Add(variantInfo); + } + else + { + result[key] = new List>() { variantInfo }; } } - return result; } + return result; + } - public void UpdateProductRelatedProducts() + public void UpdateProductRelatedProducts() + { + foreach (DataTable dataTable in FindDataTablesStartingWithName("EcomProducts")) { - foreach (DataTable dataTable in FindDataTablesStartingWithName("EcomProducts")) + if (dataTable.Columns.Contains("RelatedProducts") && + dataTable.Columns.Contains("ProductID") && + dataTable.Columns.Contains("ProductLanguageID")) { - if (dataTable.Columns.Contains("RelatedProducts") && - dataTable.Columns.Contains("ProductID") && - dataTable.Columns.Contains("ProductLanguageID")) + var tables = FindDataTablesStartingWithName("EcomProductsRelated"); + if (tables.Count() > 0) { - var tables = FindDataTablesStartingWithName("EcomProductsRelated"); - if (tables.Count() > 0) + //Note the related products will be updated just in the first found EcomProductsRelated table + //this is because if we have several EcomProductsRelated tables in the mapping we can not distinguish what table to use + //so the best choice is to have just one EcomProductsRelated table in the mapping + + DataTable ecomProductsRelatedDataTable = tables.First(); + bool productsRelatedTableIsPresent = false; //ecomProductsRelatedDataTable.Rows.Count > 0; + Dictionary> ecomProductsRelatedDataTableRows = null; + if (DataRowsToWrite.TryGetValue(ecomProductsRelatedDataTable.TableName, out ecomProductsRelatedDataTableRows)) + { + productsRelatedTableIsPresent = ecomProductsRelatedDataTableRows.Values.SelectMany(c => c).Any(); + } + Dictionary> tableRows = null; + if (DataRowsToWrite.TryGetValue(dataTable.TableName, out tableRows)) { - //Note the related products will be updated just in the first found EcomProductsRelated table - //this is because if we have several EcomProductsRelated tables in the mapping we can not distinguish what table to use - //so the best choice is to have just one EcomProductsRelated table in the mapping - - DataTable ecomProductsRelatedDataTable = tables.First(); - bool productsRelatedTableIsPresent = false; //ecomProductsRelatedDataTable.Rows.Count > 0; - Dictionary> ecomProductsRelatedDataTableRows = null; - if (DataRowsToWrite.TryGetValue(ecomProductsRelatedDataTable.TableName, out ecomProductsRelatedDataTableRows)) - { - productsRelatedTableIsPresent = ecomProductsRelatedDataTableRows.Values.SelectMany(c => c).Any(); - } - Dictionary> tableRows = null; - if (DataRowsToWrite.TryGetValue(dataTable.TableName, out tableRows)) + foreach (DataRow row in tableRows.Values.SelectMany(c => c)) { - foreach (DataRow row in tableRows.Values.SelectMany(c => c)) + var relatedProductIdsStr = row["RelatedProducts"].ToString(); + var relatedProductIds = SplitOnComma(relatedProductIdsStr); + for (int i = 0; i < relatedProductIds.Length; i++) { - var relatedProductIdsStr = row["RelatedProducts"].ToString(); - var relatedProductIds = SplitOnComma(relatedProductIdsStr); - for (int i = 0; i < relatedProductIds.Length; i++) + string relatedGroupID = string.Empty; + if (productsRelatedTableIsPresent) { - string relatedGroupID = string.Empty; - if (productsRelatedTableIsPresent) + var filter = new Func(r => (string)r["ProductRelatedProductID"] == row["ProductID"].ToString() && (string)r["ProductRelatedProductRelID"] == relatedProductIds[i]); + DataRow productsRelRow = FindRow(ecomProductsRelatedDataTable.TableName, filter); + //DataRow[] productsRelatedRows = ecomProductsRelatedDataTable.Select("ProductRelatedProductID='" + row["ProductID"].ToString().Replace("'", "''") + "' and ProductRelatedProductRelID='" + relatedProductIds[i].Replace("'", "''") + "'"); + if (productsRelRow != null) { - var filter = new Func(r => (string)r["ProductRelatedProductID"] == row["ProductID"].ToString() && (string)r["ProductRelatedProductRelID"] == relatedProductIds[i]); - DataRow productsRelRow = FindRow(ecomProductsRelatedDataTable.TableName, filter); - //DataRow[] productsRelatedRows = ecomProductsRelatedDataTable.Select("ProductRelatedProductID='" + row["ProductID"].ToString().Replace("'", "''") + "' and ProductRelatedProductRelID='" + relatedProductIds[i].Replace("'", "''") + "'"); - if (productsRelRow != null) - { - relatedGroupID = productsRelRow["ProductRelatedGroupID"].ToString(); - } + relatedGroupID = productsRelRow["ProductRelatedGroupID"].ToString(); } - if (string.IsNullOrEmpty(relatedGroupID)) - { - relatedGroupID = GetDefaultGroupID(row["ProductLanguageID"].ToString()); - } - - AddRelatedProductReferenceToProduct(dataTable, tableRows, ecomProductsRelatedDataTable, row["ProductID"].ToString(), relatedProductIds[i], relatedGroupID); } + if (string.IsNullOrEmpty(relatedGroupID)) + { + relatedGroupID = GetDefaultGroupID(row["ProductLanguageID"].ToString()); + } + + AddRelatedProductReferenceToProduct(dataTable, tableRows, ecomProductsRelatedDataTable, row["ProductID"].ToString(), relatedProductIds[i], relatedGroupID); } } } - else - { - throw new Exception("Can not find any EcomProductsRelated table"); - } + } + else + { + throw new Exception("Can not find any EcomProductsRelated table"); } } } + } - private void AddRelatedProductReferenceToProduct(DataTable ecomProductsDataTable, Dictionary> ecomProductsDataTableRows, DataTable ecomProductsRelatedDataTable, string productID, string relatedProduct, string relatedGroupID) + private void AddRelatedProductReferenceToProduct(DataTable ecomProductsDataTable, Dictionary> ecomProductsDataTableRows, DataTable ecomProductsRelatedDataTable, string productID, string relatedProduct, string relatedGroupID) + { + var filter = new Func(r => (string)r["ProductID"] == relatedProduct); + //find ProductID by relatedProduct string(it can contain ID, Number, Name) + var rows = FindExistingRows(ecomProductsDataTableRows, relatedProduct, filter); + if (rows?.Count == 0 && !useStrictPrimaryKeyMatching) { - var filter = new Func(r => (string)r["ProductID"] == relatedProduct); - //find ProductID by relatedProduct string(it can contain ID, Number, Name) - var rows = FindExistingRows(ecomProductsDataTableRows, relatedProduct, filter); - if (rows?.Count == 0 && !useStrictPrimaryKeyMatching) + if (ecomProductsDataTable.Columns.Contains("ProductNumber")) + { + filter = new Func(r => (string)r["ProductNumber"] == relatedProduct); + rows = FindExistingRows(ecomProductsDataTableRows, string.Empty, filter); + } + if (rows?.Count == 0) { - if (ecomProductsDataTable.Columns.Contains("ProductNumber")) + if (ecomProductsDataTable.Columns.Contains("ProductName")) { - filter = new Func(r => (string)r["ProductNumber"] == relatedProduct); + filter = new Func(r => (string)r["ProductName"] == relatedProduct); rows = FindExistingRows(ecomProductsDataTableRows, string.Empty, filter); } if (rows?.Count == 0) { - if (ecomProductsDataTable.Columns.Contains("ProductName")) + DataRow row = GetExistingProductDataRow(relatedProduct); + if (row != null) { - filter = new Func(r => (string)r["ProductName"] == relatedProduct); - rows = FindExistingRows(ecomProductsDataTableRows, string.Empty, filter); - } - if (rows?.Count == 0) - { - DataRow row = GetExistingProductDataRow(relatedProduct); - if (row != null) - { - rows = new List() { row }; - } + rows = new List() { row }; } } } - if (rows?.Count > 0)//if Product found + } + if (rows?.Count > 0)//if Product found + { + string relatedProductID = rows[0]["ProductID"].ToString(); + + filter = new Func(r => (string)r["ProductRelatedProductID"] == productID && (string)r["ProductRelatedProductRelID"] == relatedProductID && (string)r["ProductRelatedGroupID"] == relatedGroupID); + //string filter = string.Format("ProductRelatedProductID='{0}' and ProductRelatedProductRelID='{1}' and ProductRelatedGroupID='{2}'", productID, relatedProductID, relatedGroupID); + var row = FindRow(ecomProductsRelatedDataTable.TableName, filter); + if (row == null) { - string relatedProductID = rows[0]["ProductID"].ToString(); + var productRelation = ecomProductsRelatedDataTable.NewRow(); + productRelation["ProductRelatedProductID"] = productID; + productRelation["ProductRelatedProductRelID"] = relatedProductID; + productRelation["ProductRelatedGroupID"] = relatedGroupID; - filter = new Func(r => (string)r["ProductRelatedProductID"] == productID && (string)r["ProductRelatedProductRelID"] == relatedProductID && (string)r["ProductRelatedGroupID"] == relatedGroupID); - //string filter = string.Format("ProductRelatedProductID='{0}' and ProductRelatedProductRelID='{1}' and ProductRelatedGroupID='{2}'", productID, relatedProductID, relatedGroupID); - var row = FindRow(ecomProductsRelatedDataTable.TableName, filter); - if (row == null) + Dictionary> productRelations = null; + if (!DataRowsToWrite.TryGetValue(ecomProductsRelatedDataTable.TableName, out productRelations)) { - var productRelation = ecomProductsRelatedDataTable.NewRow(); - productRelation["ProductRelatedProductID"] = productID; - productRelation["ProductRelatedProductRelID"] = relatedProductID; - productRelation["ProductRelatedGroupID"] = relatedGroupID; - - Dictionary> productRelations = null; - if (!DataRowsToWrite.TryGetValue(ecomProductsRelatedDataTable.TableName, out productRelations)) - { - productRelations = new Dictionary>(); - DataRowsToWrite.Add(ecomProductsRelatedDataTable.TableName, productRelations); - } - productRelations.Add(RowAutoId++.ToString(), new List() { productRelation }); + productRelations = new Dictionary>(); + DataRowsToWrite.Add(ecomProductsRelatedDataTable.TableName, productRelations); } + productRelations.Add(RowAutoId++.ToString(), new List() { productRelation }); } } + } - //Returns existing ProductID, if Product is not found returns id like "ImportedPROD"LastProductId - private DataRow GetExistingProduct(Dictionary row, Mapping mapping, ColumnMapping productNumberColumn, ColumnMapping productNameColumn) - { - DataRow result = GetExistingProductByPKColumns(row, mapping); + //Returns existing ProductID, if Product is not found returns id like "ImportedPROD"LastProductId + private DataRow GetExistingProduct(Dictionary row, Mapping mapping, ColumnMapping productNumberColumn, ColumnMapping productNameColumn) + { + DataRow result = GetExistingProductByPKColumns(row, mapping); - if (result == null && !useStrictPrimaryKeyMatching) + if (result == null && !useStrictPrimaryKeyMatching) + { + MappingIdEcomProductsPKColumns.TryGetValue(mapping.GetId(), out var ecomProductsPKColumns); + if (ecomProductsPKColumns == null || !ecomProductsPKColumns.Contains("ProductNumber")) { - MappingIdEcomProductsPKColumns.TryGetValue(mapping.GetId(), out var ecomProductsPKColumns); - if (ecomProductsPKColumns == null || !ecomProductsPKColumns.Contains("ProductNumber")) + //search existing products by ProductNumber + if (productNumberColumn != null && !string.IsNullOrEmpty(productNumberColumn.SourceColumn.Name)) { - //search existing products by ProductNumber - if (productNumberColumn != null && !string.IsNullOrEmpty(productNumberColumn.SourceColumn.Name)) + string productNumber = row[productNumberColumn.SourceColumn.Name].ToString(); + if (!string.IsNullOrEmpty(productNumber)) { - string productNumber = row[productNumberColumn.SourceColumn.Name].ToString(); - if (!string.IsNullOrEmpty(productNumber)) + var rows = ExistingProducts.Select("ProductNumber='" + productNumber.Replace("'", "''") + "'"); + if (rows.Length > 0) { - var rows = ExistingProducts.Select("ProductNumber='" + productNumber.Replace("'", "''") + "'"); - if (rows.Length > 0) - { - result = rows[0]; - } + result = rows[0]; } } } - if (result == null && (ecomProductsPKColumns == null || !ecomProductsPKColumns.Contains("ProductName"))) + } + if (result == null && (ecomProductsPKColumns == null || !ecomProductsPKColumns.Contains("ProductName"))) + { + //search existing products by ProductName + if (productNameColumn != null && !string.IsNullOrEmpty(productNameColumn.SourceColumn.Name)) { - //search existing products by ProductName - if (productNameColumn != null && !string.IsNullOrEmpty(productNameColumn.SourceColumn.Name)) + string productName = row[productNameColumn.SourceColumn.Name].ToString(); + if (!string.IsNullOrEmpty(productName)) { - string productName = row[productNameColumn.SourceColumn.Name].ToString(); - if (!string.IsNullOrEmpty(productName)) + var rows = ExistingProducts.Select("ProductName='" + productName.Replace("'", "''") + "'"); + if (rows.Length > 0) { - var rows = ExistingProducts.Select("ProductName='" + productName.Replace("'", "''") + "'"); - if (rows.Length > 0) - { - result = rows[0]; - } + result = rows[0]; } } } } - - return result; } - private Dictionary> GetEcomProductsPKColumns() + return result; + } + + private Dictionary> GetEcomProductsPKColumns() + { + Dictionary> result = new Dictionary>(); + if (Mappings.TryGetValue("EcomProducts", out List productsMappings)) { - Dictionary> result = new Dictionary>(); - if (Mappings.TryGetValue("EcomProducts", out List productsMappings)) + foreach (var mapping in productsMappings) { - foreach (var mapping in productsMappings) + if (!result.ContainsKey(mapping.GetId())) { - if (!result.ContainsKey(mapping.GetId())) - { - var columnMappings = mapping.GetColumnMappings(); - result.Add(mapping.GetId(), - columnMappings.Where(cm => cm != null && cm.Active && - cm.DestinationColumn != null && !string.IsNullOrEmpty(cm.DestinationColumn.Name) && - cm.DestinationColumn.IsKeyColumn(columnMappings)).Select(cm => cm.DestinationColumn.Name)); - } + var columnMappings = mapping.GetColumnMappings(); + result.Add(mapping.GetId(), + columnMappings.Where(cm => cm != null && cm.Active && + cm.DestinationColumn != null && !string.IsNullOrEmpty(cm.DestinationColumn.Name) && + cm.DestinationColumn.IsKeyColumn(columnMappings)).Select(cm => cm.DestinationColumn.Name)); } } - return result; } + return result; + } - private DataRow GetExistingProductByPKColumns(Dictionary row, Mapping mapping) - { - DataRow result = null; + private DataRow GetExistingProductByPKColumns(Dictionary row, Mapping mapping) + { + DataRow result = null; - MappingIdEcomProductsPKColumns.TryGetValue(mapping.GetId(), out var ecomProductsPKColumns); - if (ecomProductsPKColumns?.Count() > 0) + MappingIdEcomProductsPKColumns.TryGetValue(mapping.GetId(), out var ecomProductsPKColumns); + if (ecomProductsPKColumns?.Count() > 0) + { + string query = string.Empty; + foreach (string column in ecomProductsPKColumns) { - string query = string.Empty; - foreach (string column in ecomProductsPKColumns) - { - var columnMapping = mapping.GetColumnMappings().Find(cm => cm != null && cm.Active && - string.Equals(cm.DestinationColumn?.Name, column, StringComparison.OrdinalIgnoreCase)); + var columnMapping = mapping.GetColumnMappings().Find(cm => cm != null && cm.Active && + string.Equals(cm.DestinationColumn?.Name, column, StringComparison.OrdinalIgnoreCase)); - if (columnMapping != null && !string.IsNullOrEmpty(columnMapping.SourceColumn?.Name)) + if (columnMapping != null && !string.IsNullOrEmpty(columnMapping.SourceColumn?.Name)) + { + string value = GetValue(columnMapping, row); + if (!string.IsNullOrEmpty(value)) { - string value = GetValue(columnMapping, row); - if (!string.IsNullOrEmpty(value)) - { - query += $"{column}='" + value.Replace("'", "''") + "' AND "; - } + query += $"{column}='" + value.Replace("'", "''") + "' AND "; } } + } - if (query.EndsWith(" AND ")) + if (query.EndsWith(" AND ")) + { + var rows = ExistingProducts.Select(query.Substring(0, query.Length - " AND ".Length)); + if (rows.Length > 0) { - var rows = ExistingProducts.Select(query.Substring(0, query.Length - " AND ".Length)); - if (rows.Length > 0) - { - result = rows[0]; - } + result = rows[0]; } } - - return result; } - /// - /// Search for existing product by ProductID, if not found then searches by ProductNumber, and then by ProductName - /// - /// - /// Returns existing DataRow with product information if found, otherwise null - private DataRow GetExistingProductDataRow(string searchString) + return result; + } + + /// + /// Search for existing product by ProductID, if not found then searches by ProductNumber, and then by ProductName + /// + /// + /// Returns existing DataRow with product information if found, otherwise null + private DataRow GetExistingProductDataRow(string searchString) + { + DataRow result = null; + DataRow[] rows = ExistingProducts.Select("ProductID='" + searchString + "'"); + if (rows.Length > 0) { - DataRow result = null; - DataRow[] rows = ExistingProducts.Select("ProductID='" + searchString + "'"); + result = rows[0]; + } + else + { + rows = ExistingProducts.Select("ProductNumber='" + searchString + "'"); if (rows.Length > 0) { result = rows[0]; } else { - rows = ExistingProducts.Select("ProductNumber='" + searchString + "'"); + rows = ExistingProducts.Select("ProductName='" + searchString + "'"); if (rows.Length > 0) { result = rows[0]; } - else - { - rows = ExistingProducts.Select("ProductName='" + searchString + "'"); - if (rows.Length > 0) - { - result = rows[0]; - } - } } - return result; } + return result; + } - //Returns existing ManufacturerID if found Manufacturer by ManufacturerName. Returns null if no manufacturer found. - private DataRow GetExistingManufacturer(Dictionary row, ColumnMapping manufacturerNameColumn) + //Returns existing ManufacturerID if found Manufacturer by ManufacturerName. Returns null if no manufacturer found. + private DataRow GetExistingManufacturer(Dictionary row, ColumnMapping manufacturerNameColumn) + { + DataRow result = null; + if (manufacturerNameColumn != null && !string.IsNullOrEmpty(manufacturerNameColumn.SourceColumn.Name)) { - DataRow result = null; - if (manufacturerNameColumn != null && !string.IsNullOrEmpty(manufacturerNameColumn.SourceColumn.Name)) + string manufacturerName = row[manufacturerNameColumn.SourceColumn.Name].ToString(); + if (!string.IsNullOrEmpty(manufacturerName)) { - string manufacturerName = row[manufacturerNameColumn.SourceColumn.Name].ToString(); - if (!string.IsNullOrEmpty(manufacturerName)) + foreach (var manufactorerRow in ProductManufacturers.Values) { - foreach (var manufactorerRow in ProductManufacturers.Values) + var existingManufacturerName = Converter.ToString(manufactorerRow["ManufacturerName"]); + if (string.Equals(existingManufacturerName, manufacturerName)) { - var existingManufacturerName = Converter.ToString(manufactorerRow["ManufacturerName"]); - if (string.Equals(existingManufacturerName, manufacturerName)) - { - return manufactorerRow; - } + return manufactorerRow; } } } - return result; - } - - internal void Close() - { - //Reset Language cache - if (FindDataTablesStartingWithName("EcomLanguages").Count() > 0) - { - Ecommerce.Services.Languages.ClearCache(); - } - - foreach (DataTable table in DataToWrite.Tables) - { - string tableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); - sqlCommand.CommandText = "if exists (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + tableName + "') AND type in (N'U')) drop table " + tableName; - sqlCommand.ExecuteNonQuery(); - } - - ClearHashTables(); - if (duplicateRowsHandler != null) - { - duplicateRowsHandler.Dispose(); - } - _rowsWithMissingGroups = new List>(); } + return result; + } - private void ClearHashTables() + internal void Close() + { + //Reset Language cache + if (FindDataTablesStartingWithName("EcomLanguages").Count() > 0) { - ecomVariantOptionsProductRelationKeys = null; - ecomGroupProductRelationKeys = null; - ecomVariantgroupProductrelationKeys = null; + Ecommerce.Services.Languages.ClearCache(); } - internal void CleanRelationsTables(SqlTransaction transaction) + foreach (DataTable table in DataToWrite.Tables) { - sqlCommand.Transaction = transaction; - sqlCommand.CommandText = "delete from EcomGroupProductRelation where not exists(select * from EcomProducts where ProductID=GroupProductRelationProductID) or not exists(select * from ecomGroups where GroupID=GroupProductRelationGroupID);"; - sqlCommand.ExecuteNonQuery(); - sqlCommand.Transaction = transaction; - sqlCommand.CommandText = "delete from EcomShopGroupRelation where not exists (select * from EcomGroups where GroupID=ShopGroupGroupID)"; - sqlCommand.ExecuteNonQuery(); - sqlCommand.Transaction = transaction; - sqlCommand.CommandText = "DELETE FROM [EcomAssortmentProductRelations] WHERE ( NOT EXISTS( SELECT [EcomProducts].[ProductAutoID] FROM [EcomProducts] WHERE ( [EcomProducts].[ProductID] = [EcomAssortmentProductRelations].[AssortmentProductRelationProductID] ) AND ( [EcomProducts].[ProductVariantID] = [EcomAssortmentProductRelations].[AssortmentProductRelationProductVariantID] ) ) )"; - sqlCommand.ExecuteNonQuery(); - sqlCommand.Transaction = transaction; - sqlCommand.CommandText = "DELETE FROM [EcomAssortmentItems] WHERE ( [EcomAssortmentItems].[AssortmentItemRelationType] = 'PRODUCT' ) AND ( NOT EXISTS( SELECT * FROM [EcomAssortmentProductRelations] WHERE ( [EcomAssortmentProductRelations].[AssortmentProductRelationAutoID] = [EcomAssortmentItems].[AssortmentItemRelationAutoID] ) ) )"; - sqlCommand.ExecuteNonQuery(); - sqlCommand.Transaction = transaction; - sqlCommand.CommandText = "DELETE FROM [EcomAssortmentGroupRelations] WHERE ( NOT EXISTS( SELECT [EcomGroups].[GroupID] FROM [EcomGroups] WHERE ( [EcomGroups].[GroupID] = [EcomAssortmentGroupRelations].[AssortmentGroupRelationGroupID] ) ) )"; - sqlCommand.ExecuteNonQuery(); - sqlCommand.Transaction = transaction; - sqlCommand.CommandText = "DELETE FROM [EcomAssortmentItems] WHERE ( [EcomAssortmentItems].[AssortmentItemRelationType] = 'GROUP' ) AND ( NOT EXISTS( SELECT * FROM [EcomAssortmentGroupRelations] WHERE ( [EcomAssortmentGroupRelations].[AssortmentGroupRelationAutoID] = [EcomAssortmentItems].[AssortmentItemRelationAutoID] ) ) )"; + string tableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); + sqlCommand.CommandText = "if exists (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + tableName + "') AND type in (N'U')) drop table " + tableName; sqlCommand.ExecuteNonQuery(); } - internal void RebuildAssortments() + ClearHashTables(); + if (duplicateRowsHandler != null) { - assortmentHandler.RebuildAssortments(); + duplicateRowsHandler.Dispose(); } + _rowsWithMissingGroups = new List>(); + } - private void RemoveExcessFromRelationsTables(SqlTransaction sqlTransaction) - { - sqlCommand.Transaction = sqlTransaction; - sqlCommand.CommandText = - "delete t1 from EcomGroupProductRelation t1 INNER JOIN ecomgroupproductrelationTempTableForBulkImport t2 ON t1.GroupProductRelationGroupID = t2.GroupProductRelationGroupID AND t1.GroupProductRelationProductID = t2.GroupProductRelationProductID;" + - "insert into EcomGroupProductRelation (GroupProductRelationGroupID,GroupProductRelationProductID,GroupProductRelationSorting,GroupProductRelationIsPrimary) select GroupProductRelationGroupId, GroupProductRelationProductID,GroupProductRelationSorting,GroupProductRelationIsPrimary from ecomgroupproductrelationTempTableForBulkImport; " + - "delete from EcomShopGroupRelation where ShopGroupGroupID in (select ShopGroupGroupID from EcomShopGroupRelationTempTableForBulkImport); " + - "insert into EcomShopGroupRelation(ShopGroupShopID,ShopGroupGroupID,ShopGroupRelationsSorting) select shopgroupshopid,shopgroupgroupid,ShopGroupRelationsSorting from ecomshopgrouprelationtemptableforbulkimport; " + - "delete from EcomVariantgroupProductRelation where VariantgroupProductRelationProductID in (select VariantgroupProductRelationProductID from EcomVariantgroupProductRelationTempTableForBulkImport); " + - "insert into EcomVariantgroupProductRelation (VariantgroupProductRelationID,VariantgroupProductRelationProductID,VariantgroupProductRelationVariantGroupID,VariantGroupProductRelationSorting)select VariantgroupProductRelationID,VariantgroupProductRelationProductID,VariantgroupProductRelationVariantGroupID,VariantGroupProductRelationSorting from EcomVariantgroupProductRelationTempTableForBulkImport;"; + private void ClearHashTables() + { + ecomVariantOptionsProductRelationKeys = null; + ecomGroupProductRelationKeys = null; + ecomVariantgroupProductrelationKeys = null; + } - foreach (DataTable table in FindDataTablesStartingWithName("EcomVariantOptionsProductRelation")) - { - string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); - if (deleteExcess || removeMissingAfterImportDestinationTablesOnly) - { - sqlCommand.CommandText += $"delete from EcomVariantOptionsProductRelation where VariantOptionsProductRelationProductID in (select VariantOptionsProductRelationProductID from {tempTableName}); "; - } - else - { - sqlCommand.CommandText += $"delete t1 from EcomVariantOptionsProductRelation t1 INNER JOIN {tempTableName} t2 ON t1.VariantOptionsProductRelationProductId = t2.VariantOptionsProductRelationProductId AND t1.VariantOptionsProductRelationVariantId = t2.VariantOptionsProductRelationVariantId;"; - } + internal void CleanRelationsTables(SqlTransaction transaction) + { + sqlCommand.Transaction = transaction; + sqlCommand.CommandText = "delete from EcomGroupProductRelation where not exists(select * from EcomProducts where ProductID=GroupProductRelationProductID) or not exists(select * from ecomGroups where GroupID=GroupProductRelationGroupID);"; + sqlCommand.ExecuteNonQuery(); + sqlCommand.Transaction = transaction; + sqlCommand.CommandText = "delete from EcomShopGroupRelation where not exists (select * from EcomGroups where GroupID=ShopGroupGroupID)"; + sqlCommand.ExecuteNonQuery(); + sqlCommand.Transaction = transaction; + sqlCommand.CommandText = "DELETE FROM [EcomAssortmentProductRelations] WHERE ( NOT EXISTS( SELECT [EcomProducts].[ProductAutoID] FROM [EcomProducts] WHERE ( [EcomProducts].[ProductID] = [EcomAssortmentProductRelations].[AssortmentProductRelationProductID] ) AND ( [EcomProducts].[ProductVariantID] = [EcomAssortmentProductRelations].[AssortmentProductRelationProductVariantID] ) ) )"; + sqlCommand.ExecuteNonQuery(); + sqlCommand.Transaction = transaction; + sqlCommand.CommandText = "DELETE FROM [EcomAssortmentItems] WHERE ( [EcomAssortmentItems].[AssortmentItemRelationType] = 'PRODUCT' ) AND ( NOT EXISTS( SELECT * FROM [EcomAssortmentProductRelations] WHERE ( [EcomAssortmentProductRelations].[AssortmentProductRelationAutoID] = [EcomAssortmentItems].[AssortmentItemRelationAutoID] ) ) )"; + sqlCommand.ExecuteNonQuery(); + sqlCommand.Transaction = transaction; + sqlCommand.CommandText = "DELETE FROM [EcomAssortmentGroupRelations] WHERE ( NOT EXISTS( SELECT [EcomGroups].[GroupID] FROM [EcomGroups] WHERE ( [EcomGroups].[GroupID] = [EcomAssortmentGroupRelations].[AssortmentGroupRelationGroupID] ) ) )"; + sqlCommand.ExecuteNonQuery(); + sqlCommand.Transaction = transaction; + sqlCommand.CommandText = "DELETE FROM [EcomAssortmentItems] WHERE ( [EcomAssortmentItems].[AssortmentItemRelationType] = 'GROUP' ) AND ( NOT EXISTS( SELECT * FROM [EcomAssortmentGroupRelations] WHERE ( [EcomAssortmentGroupRelations].[AssortmentGroupRelationAutoID] = [EcomAssortmentItems].[AssortmentItemRelationAutoID] ) ) )"; + sqlCommand.ExecuteNonQuery(); + } - sqlCommand.CommandText += $"insert into EcomVariantOptionsProductRelation (VariantOptionsProductRelationProductID,VariantOptionsProductRelationVariantID)select VariantOptionsProductRelationProductID,VariantOptionsProductRelationVariantID from {tempTableName};"; - } + internal void RebuildAssortments() + { + assortmentHandler.RebuildAssortments(); + } + + private void RemoveExcessFromRelationsTables(SqlTransaction sqlTransaction) + { + sqlCommand.Transaction = sqlTransaction; + sqlCommand.CommandText = + "delete t1 from EcomGroupProductRelation t1 INNER JOIN ecomgroupproductrelationTempTableForBulkImport t2 ON t1.GroupProductRelationGroupID = t2.GroupProductRelationGroupID AND t1.GroupProductRelationProductID = t2.GroupProductRelationProductID;" + + "insert into EcomGroupProductRelation (GroupProductRelationGroupID,GroupProductRelationProductID,GroupProductRelationSorting,GroupProductRelationIsPrimary) select GroupProductRelationGroupId, GroupProductRelationProductID,GroupProductRelationSorting,GroupProductRelationIsPrimary from ecomgroupproductrelationTempTableForBulkImport; " + + "delete from EcomShopGroupRelation where ShopGroupGroupID in (select ShopGroupGroupID from EcomShopGroupRelationTempTableForBulkImport); " + + "insert into EcomShopGroupRelation(ShopGroupShopID,ShopGroupGroupID,ShopGroupRelationsSorting) select shopgroupshopid,shopgroupgroupid,ShopGroupRelationsSorting from ecomshopgrouprelationtemptableforbulkimport; " + + "delete from EcomVariantgroupProductRelation where VariantgroupProductRelationProductID in (select VariantgroupProductRelationProductID from EcomVariantgroupProductRelationTempTableForBulkImport); " + + "insert into EcomVariantgroupProductRelation (VariantgroupProductRelationID,VariantgroupProductRelationProductID,VariantgroupProductRelationVariantGroupID,VariantGroupProductRelationSorting)select VariantgroupProductRelationID,VariantgroupProductRelationProductID,VariantgroupProductRelationVariantGroupID,VariantGroupProductRelationSorting from EcomVariantgroupProductRelationTempTableForBulkImport;"; - foreach (DataTable table in FindDataTablesStartingWithName("EcomShops")) + foreach (DataTable table in FindDataTablesStartingWithName("EcomVariantOptionsProductRelation")) + { + string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); + if (deleteExcess || removeMissingAfterImportDestinationTablesOnly) { - string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); - sqlCommand.CommandText += $"insert into EcomShops (ShopID,ShopName) select shopid,shopname from {tempTableName}; "; + sqlCommand.CommandText += $"delete from EcomVariantOptionsProductRelation where VariantOptionsProductRelationProductID in (select VariantOptionsProductRelationProductID from {tempTableName}); "; } - - sqlCommand.CommandText += "insert into EcomProductsRelatedGroups (RelatedGroupID,RelatedGroupName,RelatedGroupLanguageID) select RelatedGroupID,RelatedGroupName,RelatedGroupLanguageID from EcomProductsRelatedGroupsTempTableForBulkImport; "; - - foreach (DataTable table in FindDataTablesStartingWithName("EcomProductsRelated")) + else { - string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); - sqlCommand.CommandText += "delete from related from EcomProductsRelated related where ProductRelatedProductID in " + - $"(select ProductRelatedProductID from {tempTableName} inside WHERE related.ProductRelatedProductID = inside.ProductRelatedProductID AND " + - "related.ProductRelatedProductRelID = inside.ProductRelatedProductRelID AND related.ProductRelatedGroupID = inside.ProductRelatedGroupID AND related.ProductRelatedProductRelVariantID = inside.ProductRelatedProductRelVariantID); "; - sqlCommand.CommandText += $"insert into EcomProductsRelated (ProductRelatedProductID,ProductRelatedProductRelID,ProductRelatedGroupID,ProductRelatedProductRelVariantID) select ProductRelatedProductID,ProductRelatedProductRelID,ProductRelatedGroupID,ProductRelatedProductRelVariantID from {tempTableName}; "; + sqlCommand.CommandText += $"delete t1 from EcomVariantOptionsProductRelation t1 INNER JOIN {tempTableName} t2 ON t1.VariantOptionsProductRelationProductId = t2.VariantOptionsProductRelationProductId AND t1.VariantOptionsProductRelationVariantId = t2.VariantOptionsProductRelationVariantId;"; } - sqlCommand.CommandText += "delete from EcomGroupRelations where groupRelationsGroupID in (select groupRelationsGroupID from EcomGroupRelationsTempTableForBulkImport);"; - sqlCommand.CommandText += "insert into EcomGroupRelations (GroupRelationsGroupID,GroupRelationsParentID,GroupRelationsSorting) select GroupRelationsGroupID,GroupRelationsParentID,GroupRelationsSorting from EcomGroupRelationsTempTableForBulkImport;"; + sqlCommand.CommandText += $"insert into EcomVariantOptionsProductRelation (VariantOptionsProductRelationProductID,VariantOptionsProductRelationVariantID)select VariantOptionsProductRelationProductID,VariantOptionsProductRelationVariantID from {tempTableName};"; + } - try - { - sqlCommand.ExecuteNonQuery(); - } - catch (Exception ex) + foreach (DataTable table in FindDataTablesStartingWithName("EcomShops")) + { + string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); + sqlCommand.CommandText += $"insert into EcomShops (ShopID,ShopName) select shopid,shopname from {tempTableName}; "; + } + + sqlCommand.CommandText += "insert into EcomProductsRelatedGroups (RelatedGroupID,RelatedGroupName,RelatedGroupLanguageID) select RelatedGroupID,RelatedGroupName,RelatedGroupLanguageID from EcomProductsRelatedGroupsTempTableForBulkImport; "; + + foreach (DataTable table in FindDataTablesStartingWithName("EcomProductsRelated")) + { + string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); + sqlCommand.CommandText += "delete from related from EcomProductsRelated related where ProductRelatedProductID in " + + $"(select ProductRelatedProductID from {tempTableName} inside WHERE related.ProductRelatedProductID = inside.ProductRelatedProductID AND " + + "related.ProductRelatedProductRelID = inside.ProductRelatedProductRelID AND related.ProductRelatedGroupID = inside.ProductRelatedGroupID AND related.ProductRelatedProductRelVariantID = inside.ProductRelatedProductRelVariantID); "; + sqlCommand.CommandText += $"insert into EcomProductsRelated (ProductRelatedProductID,ProductRelatedProductRelID,ProductRelatedGroupID,ProductRelatedProductRelVariantID) select ProductRelatedProductID,ProductRelatedProductRelID,ProductRelatedGroupID,ProductRelatedProductRelVariantID from {tempTableName}; "; + } + + sqlCommand.CommandText += "delete from EcomGroupRelations where groupRelationsGroupID in (select groupRelationsGroupID from EcomGroupRelationsTempTableForBulkImport);"; + sqlCommand.CommandText += "insert into EcomGroupRelations (GroupRelationsGroupID,GroupRelationsParentID,GroupRelationsSorting) select GroupRelationsGroupID,GroupRelationsParentID,GroupRelationsSorting from EcomGroupRelationsTempTableForBulkImport;"; + + try + { + sqlCommand.ExecuteNonQuery(); + } + catch (Exception ex) + { + string msg = string.Format("Exception: {0} Sql query: {1}", ex.Message, sqlCommand.CommandText); + if (ex.Message.Contains("The conflict occurred in database \"head\", table \"dbo.EcomShops\", column 'ShopID'.")) { - string msg = string.Format("Exception: {0} Sql query: {1}", ex.Message, sqlCommand.CommandText); - if (ex.Message.Contains("The conflict occurred in database \"head\", table \"dbo.EcomShops\", column 'ShopID'.")) - { - throw new Exception("Import attempted with ShopID that does not exist in the database. Check shopID values in input data. " + msg, ex); - } - throw new Exception(msg, ex); + throw new Exception("Import attempted with ShopID that does not exist in the database. Check shopID values in input data. " + msg, ex); } + throw new Exception(msg, ex); } + } - private void DeleteExcessFromGroupProductRelation(string shop, SqlTransaction sqlTransaction) + private void DeleteExcessFromGroupProductRelation(string shop, SqlTransaction sqlTransaction) + { + sqlCommand.Transaction = sqlTransaction; + try { - sqlCommand.Transaction = sqlTransaction; - try + StringBuilder sqlClean = null; + if (partialUpdate) { - StringBuilder sqlClean = null; - if (partialUpdate) + sqlClean = new StringBuilder(); + foreach (DataTable table in FindDataTablesStartingWithName("EcomProducts")) { - sqlClean = new StringBuilder(); - foreach (DataTable table in FindDataTablesStartingWithName("EcomProducts")) - { - string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); - sqlClean.Append($"delete EcomGroupProductRelation from {tempTableName} join ecomgroupproductrelation on {tempTableName}.productid=ecomgroupproductrelation.GroupProductRelationProductID where not exists (select * from [dbo].[EcomGroupProductRelationTempTableForBulkImport] where [dbo].[EcomGroupProductRelation].[GroupProductRelationProductID]=[GroupProductRelationProductID] and [dbo].[EcomGroupProductRelation].[GroupProductRelationGroupID]=[GroupProductRelationGroupID] );"); - } + string tempTableName = GetTableNameWithoutPrefix(table.TableName) + "TempTableForBulkImport" + GetPrefixFromTableName(table.TableName); + sqlClean.Append($"delete EcomGroupProductRelation from {tempTableName} join ecomgroupproductrelation on {tempTableName}.productid=ecomgroupproductrelation.GroupProductRelationProductID where not exists (select * from [dbo].[EcomGroupProductRelationTempTableForBulkImport] where [dbo].[EcomGroupProductRelation].[GroupProductRelationProductID]=[GroupProductRelationProductID] and [dbo].[EcomGroupProductRelation].[GroupProductRelationGroupID]=[GroupProductRelationGroupID] );"); } - else + } + else + { + if (!string.IsNullOrEmpty(shop)) { - if (!string.IsNullOrEmpty(shop)) - { - sqlClean = new StringBuilder("DELETE [EcomGroupProductRelation] FROM [EcomGroupProductRelation] "); - sqlClean.Append("INNER JOIN [EcomShopGroupRelation] ON [GroupProductRelationGroupID] = [ShopGroupGroupID] WHERE NOT EXISTS("); - sqlClean.Append("SELECT GroupProductRelationGroupID, GroupProductRelationProductID FROM [EcomGroupProductRelationTempTableForBulkImport] WHERE "); - sqlClean.Append("[EcomGroupProductRelation].[GroupProductRelationGroupID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationGroupID] and [EcomGroupProductRelation].[GroupProductRelationProductID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationProductID]) "); - sqlClean.Append(string.Format("AND [EcomShopGroupRelation].[ShopGroupShopID] = '{0}'", shop)); - } - else - { - sqlClean = new StringBuilder("DELETE FROM [EcomGroupProductRelation] WHERE NOT EXISTS("); - sqlClean.Append("SELECT GroupProductRelationGroupID, GroupProductRelationProductID FROM [EcomGroupProductRelationTempTableForBulkImport] WHERE "); - sqlClean.Append("[EcomGroupProductRelation].[GroupProductRelationGroupID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationGroupID] and [EcomGroupProductRelation].[GroupProductRelationProductID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationProductID])"); - } + sqlClean = new StringBuilder("DELETE [EcomGroupProductRelation] FROM [EcomGroupProductRelation] "); + sqlClean.Append("INNER JOIN [EcomShopGroupRelation] ON [GroupProductRelationGroupID] = [ShopGroupGroupID] WHERE NOT EXISTS("); + sqlClean.Append("SELECT GroupProductRelationGroupID, GroupProductRelationProductID FROM [EcomGroupProductRelationTempTableForBulkImport] WHERE "); + sqlClean.Append("[EcomGroupProductRelation].[GroupProductRelationGroupID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationGroupID] and [EcomGroupProductRelation].[GroupProductRelationProductID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationProductID]) "); + sqlClean.Append(string.Format("AND [EcomShopGroupRelation].[ShopGroupShopID] = '{0}'", shop)); } - if (sqlClean != null && sqlClean.Length > 0) + else { - sqlCommand.CommandText = sqlClean.ToString(); - sqlCommand.ExecuteNonQuery(); + sqlClean = new StringBuilder("DELETE FROM [EcomGroupProductRelation] WHERE NOT EXISTS("); + sqlClean.Append("SELECT GroupProductRelationGroupID, GroupProductRelationProductID FROM [EcomGroupProductRelationTempTableForBulkImport] WHERE "); + sqlClean.Append("[EcomGroupProductRelation].[GroupProductRelationGroupID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationGroupID] and [EcomGroupProductRelation].[GroupProductRelationProductID] = [EcomGroupProductRelationTempTableForBulkImport].[GroupProductRelationProductID])"); } } - catch (Exception ex) + if (sqlClean != null && sqlClean.Length > 0) { - string msg = "Failed to delete product group relations from EcomGroupProductRelation relation table. "; - msg += "Exception message: " + ex.Message + " Sql query: " + sqlCommand.CommandText; - throw new Exception(msg, ex); + sqlCommand.CommandText = sqlClean.ToString(); + sqlCommand.ExecuteNonQuery(); } } - - private bool HasRowsToImport(Mapping mapping, out string tempTablePrefix) + catch (Exception ex) { - bool result = false; - tempTablePrefix = "TempTableForBulkImport" + mapping.GetId(); + string msg = "Failed to delete product group relations from EcomGroupProductRelation relation table. "; + msg += "Exception message: " + ex.Message + " Sql query: " + sqlCommand.CommandText; + throw new Exception(msg, ex); + } + } + + private bool HasRowsToImport(Mapping mapping, out string tempTablePrefix) + { + bool result = false; + tempTablePrefix = "TempTableForBulkImport" + mapping.GetId(); - if (mapping != null && mapping.DestinationTable != null && mapping.DestinationTable.Name != null && DataToWrite != null && DataToWrite.Tables != null) + if (mapping != null && mapping.DestinationTable != null && mapping.DestinationTable.Name != null && DataToWrite != null && DataToWrite.Tables != null) + { + string destinationTableName = GetTableName(mapping.DestinationTable.Name, mapping); + Dictionary> rows = null; + if (DataRowsToWrite.TryGetValue(destinationTableName, out rows) && rows.Values.Count > 0) { - string destinationTableName = GetTableName(mapping.DestinationTable.Name, mapping); - Dictionary> rows = null; - if (DataRowsToWrite.TryGetValue(destinationTableName, out rows) && rows.Values.Count > 0) - { - result = true; - } - else if (DataRowsToWrite.TryGetValue(mapping.DestinationTable.Name, out rows) && rows.Values.Count > 0) - { - tempTablePrefix = "TempTableForBulkImport"; - result = true; - } + result = true; + } + else if (DataRowsToWrite.TryGetValue(mapping.DestinationTable.Name, out rows) && rows.Values.Count > 0) + { + tempTablePrefix = "TempTableForBulkImport"; + result = true; } - return result; } + return result; + } - private DataRow FindRow(string tableName, string id) - { - DataRow[] ret = new DataRow[0]; + private DataRow FindRow(string tableName, string id) + { + DataRow[] ret = new DataRow[0]; - Dictionary> table = null; - if (!DataRowsToWrite.TryGetValue(tableName, out table)) + Dictionary> table = null; + if (!DataRowsToWrite.TryGetValue(tableName, out table)) + { + foreach (var key in DataRowsToWrite.Keys) { - foreach (var key in DataRowsToWrite.Keys) + if (key.StartsWith(tableName + "$", StringComparison.InvariantCultureIgnoreCase)) { - if (key.StartsWith(tableName + "$", StringComparison.InvariantCultureIgnoreCase)) - { - table = DataRowsToWrite[key]; - } + table = DataRowsToWrite[key]; } } - return table != null && table.ContainsKey(id) ? table[id][0] : null; } + return table != null && table.ContainsKey(id) ? table[id][0] : null; + } - private DataTable FindDataTableByName(string tableName) + private DataTable FindDataTableByName(string tableName) + { + foreach (DataTable table in DataToWrite.Tables) { - foreach (DataTable table in DataToWrite.Tables) + if (string.Compare(table.TableName, tableName, true) == 0) { - if (string.Compare(table.TableName, tableName, true) == 0) - { - return table; - } + return table; } - return null; } + return null; + } - private IEnumerable FindDataTablesStartingWithName(string tableName) + private IEnumerable FindDataTablesStartingWithName(string tableName) + { + List ret = new List(); + foreach (DataTable table in DataToWrite.Tables) { - List ret = new List(); - foreach (DataTable table in DataToWrite.Tables) + if (string.Compare(table.TableName, tableName, true) == 0 || table.TableName.StartsWith(tableName + "$", StringComparison.InvariantCultureIgnoreCase)) { - if (string.Compare(table.TableName, tableName, true) == 0 || table.TableName.StartsWith(tableName + "$", StringComparison.InvariantCultureIgnoreCase)) - { - ret.Add(table); - } + ret.Add(table); } - return ret; } + return ret; + } - private DataRow GetDataTableNewRow(string tableName) + private DataRow GetDataTableNewRow(string tableName) + { + DataRow row = null; + DataTable table = FindDataTableByName(tableName); + if (table == null) { - DataRow row = null; - DataTable table = FindDataTableByName(tableName); - if (table == null) + foreach (DataTable foundTable in FindDataTablesStartingWithName(tableName)) { - foreach (DataTable foundTable in FindDataTablesStartingWithName(tableName)) - { - row = foundTable.NewRow(); - break; - } - if (row == null) - { - //this should never happen - throw new Exception($"Can not find table {tableName} in the DataToWrite.Tables"); - } + row = foundTable.NewRow(); + break; } - else + if (row == null) { - row = table.NewRow(); + //this should never happen + throw new Exception($"Can not find table {tableName} in the DataToWrite.Tables"); } - return row; } + else + { + row = table.NewRow(); + } + return row; + } - private bool HasData(string tableName) + private bool HasData(string tableName) + { + foreach (DataTable table in DataToWrite.Tables) { - foreach (DataTable table in DataToWrite.Tables) + if (table.TableName.StartsWith(tableName) && table.Rows.Count > 0) { - if (table.TableName.StartsWith(tableName) && table.Rows.Count > 0) - { - return true; - } + return true; } - return false; } + return false; + } - private string GetValue(ColumnMapping columnMapping, Dictionary row) + private string GetValue(ColumnMapping columnMapping, Dictionary row) + { + string result = null; + if (columnMapping != null && (columnMapping.HasScriptWithValue || row.ContainsKey(columnMapping.SourceColumn.Name))) { - string result = null; - if (columnMapping != null && (columnMapping.HasScriptWithValue || row.ContainsKey(columnMapping.SourceColumn.Name))) + switch (columnMapping.ScriptType) { - switch (columnMapping.ScriptType) - { - case ScriptType.None: - result = Converter.ToString(row[columnMapping.SourceColumn.Name]); - break; - case ScriptType.Append: - result = Converter.ToString(row[columnMapping.SourceColumn.Name]) + columnMapping.ScriptValue; - break; - case ScriptType.Prepend: - result = columnMapping.ScriptValue + Converter.ToString(row[columnMapping.SourceColumn.Name]); - break; - case ScriptType.Constant: - result = columnMapping.GetScriptValue(); - break; - case ScriptType.NewGuid: - result = columnMapping.GetScriptValue(); - break; - } + case ScriptType.None: + result = Converter.ToString(row[columnMapping.SourceColumn.Name]); + break; + case ScriptType.Append: + result = Converter.ToString(row[columnMapping.SourceColumn.Name]) + columnMapping.ScriptValue; + break; + case ScriptType.Prepend: + result = columnMapping.ScriptValue + Converter.ToString(row[columnMapping.SourceColumn.Name]); + break; + case ScriptType.Constant: + result = columnMapping.GetScriptValue(); + break; + case ScriptType.NewGuid: + result = columnMapping.GetScriptValue(); + break; } - return result; } + return result; + } - private void CountProductVariantGroups(string productID, string variantOptionID) + private void CountProductVariantGroups(string productID, string variantOptionID) + { + int productvariantGroupsCount = 0; + if (ProductVariantGroupsCountDictionary.TryGetValue(productID, out productvariantGroupsCount)) { - int productvariantGroupsCount = 0; - if (ProductVariantGroupsCountDictionary.TryGetValue(productID, out productvariantGroupsCount)) + int currentVariantGroupsCount = variantOptionID.Split(new char[] { '.' }).Length; + if (currentVariantGroupsCount > productvariantGroupsCount) { - int currentVariantGroupsCount = variantOptionID.Split(new char[] { '.' }).Length; - if (currentVariantGroupsCount > productvariantGroupsCount) - { - ProductVariantGroupsCountDictionary[productID] = currentVariantGroupsCount; - } - } - else - { - ProductVariantGroupsCountDictionary[productID] = 1; + ProductVariantGroupsCountDictionary[productID] = currentVariantGroupsCount; } } - internal void UpdateGroupRelations() + else + { + ProductVariantGroupsCountDictionary[productID] = 1; + } + } + internal void UpdateGroupRelations() + { + bool isGroupIdInMapping = job.Mappings.Any(m => m?.DestinationTable?.Name == "EcomGroups" && m.GetColumnMappings().Any(cm => cm.Active && string.Equals(cm?.DestinationColumn.Name, "GroupId", StringComparison.OrdinalIgnoreCase))); + if (!isGroupIdInMapping) { - bool isGroupIdInMapping = job.Mappings.Any(m => m?.DestinationTable?.Name == "EcomGroups" && m.GetColumnMappings().Any(cm => cm.Active && string.Equals(cm?.DestinationColumn.Name, "GroupId", StringComparison.OrdinalIgnoreCase))); - if (!isGroupIdInMapping) + bool isParentGroupsInMapping = job.Mappings.Any(m => m?.DestinationTable.Name == "EcomGroups" && m.GetColumnMappings().Any(cm => cm.Active && string.Equals(cm?.DestinationColumn.Name, "ParentGroups", StringComparison.OrdinalIgnoreCase))); + var rowsToWrite = DataRowsToWrite["EcomGroupRelations"]; + if (isParentGroupsInMapping && rowsToWrite != null && rowsToWrite.Count > 0) { - bool isParentGroupsInMapping = job.Mappings.Any(m => m?.DestinationTable.Name == "EcomGroups" && m.GetColumnMappings().Any(cm => cm.Active && string.Equals(cm?.DestinationColumn.Name, "ParentGroups", StringComparison.OrdinalIgnoreCase))); - var rowsToWrite = DataRowsToWrite["EcomGroupRelations"]; - if (isParentGroupsInMapping && rowsToWrite != null && rowsToWrite.Count > 0) - { - Dictionary searchResults = new Dictionary(); - string groupId, group; + Dictionary searchResults = new Dictionary(); + string groupId, group; - foreach (DataRow row in rowsToWrite.Values.SelectMany(c => c)) + foreach (DataRow row in rowsToWrite.Values.SelectMany(c => c)) + { + group = row["GroupRelationsParentID"].ToString(); + if (!searchResults.TryGetValue(group, out groupId)) { - group = row["GroupRelationsParentID"].ToString(); - if (!searchResults.TryGetValue(group, out groupId)) - { - groupId = FindGroupId(group); - searchResults.Add(group, groupId); - } - if (!string.IsNullOrEmpty(groupId)) - { - row["GroupRelationsParentID"] = groupId; - } + groupId = FindGroupId(group); + searchResults.Add(group, groupId); + } + if (!string.IsNullOrEmpty(groupId)) + { + row["GroupRelationsParentID"] = groupId; } } } } + } - private string FindGroupId(string value) + private string FindGroupId(string value) + { + string groupId = string.Empty; + foreach (string key in DataRowsToWrite.Keys) { - string groupId = string.Empty; - foreach (string key in DataRowsToWrite.Keys) + if (key.StartsWith("EcomGroups")) { - if (key.StartsWith("EcomGroups")) + foreach (List dataRows in DataRowsToWrite[key].Values) { - foreach (List dataRows in DataRowsToWrite[key].Values) + foreach (var dataRow in dataRows) { - foreach (var dataRow in dataRows) + var groupName = Converter.ToString(dataRow["GroupName"]); + if (string.Equals(groupName, value, StringComparison.OrdinalIgnoreCase)) { - var groupName = Converter.ToString(dataRow["GroupName"]); - if (string.Equals(groupName, value, StringComparison.OrdinalIgnoreCase)) - { - return Converter.ToString(dataRow["GroupId"]); - } + return Converter.ToString(dataRow["GroupId"]); } } } } - if (string.IsNullOrEmpty(groupId)) + } + if (string.IsNullOrEmpty(groupId)) + { + foreach (var groupRows in ProductGroups.Values) { - foreach (var groupRows in ProductGroups.Values) + foreach (var groupRow in groupRows) { - foreach (var groupRow in groupRows) + var groupName = Converter.ToString(groupRow["GroupName"]); + if (groupName.Equals(value)) { - var groupName = Converter.ToString(groupRow["GroupName"]); - if (groupName.Equals(value)) - { - return Converter.ToString(groupRow["GroupId"]); - } + return Converter.ToString(groupRow["GroupId"]); } } } - return groupId; } + return groupId; + } - private string GetImportedProductsByNumberMultipleProductsIdentifier(string productNumber, string languageId) - { - return $"{productNumber}.{languageId}"; - } + private string GetImportedProductsByNumberMultipleProductsIdentifier(string productNumber, string languageId) + { + return $"{productNumber}.{languageId}"; + } - internal void FailOnMissingGroups() + internal void FailOnMissingGroups() + { + if (_rowsWithMissingGroups?.Count > 0) { - if (_rowsWithMissingGroups?.Count > 0) - { - throw new Exception(EcomProductsMissingGroupsErrorMessage); - } + throw new Exception(EcomProductsMissingGroupsErrorMessage); } + } - internal void LogFailedRows() + internal void LogFailedRows() + { + logger.Log(EcomProductsMissingGroupsErrorMessage + ":"); + if (_rowsWithMissingGroups?.Count > 0) { - logger.Log(EcomProductsMissingGroupsErrorMessage + ":"); - if (_rowsWithMissingGroups?.Count > 0) + foreach (var row in _rowsWithMissingGroups) { - foreach (var row in _rowsWithMissingGroups) + string rowData = "Failed row:"; + foreach (var kvp in row) { - string rowData = "Failed row:"; - foreach (var kvp in row) - { - rowData += $"\t [{kvp.Key}: \"{kvp.Value}\"],"; - } - rowData = rowData.TrimStart().TrimEnd(','); - logger.Log(rowData); + rowData += $"\t [{kvp.Key}: \"{kvp.Value}\"],"; } + rowData = rowData.TrimStart().TrimEnd(','); + logger.Log(rowData); } } } - - internal class FKTableConstraint - { - public string FKColumn; - public string PKColumn; - public string PKTable; - } } diff --git a/src/EcomProvider.cs b/src/EcomProvider.cs index d7cad67..0d5516b 100644 --- a/src/EcomProvider.cs +++ b/src/EcomProvider.cs @@ -2,796 +2,985 @@ using Dynamicweb.DataIntegration.Integration; using Dynamicweb.DataIntegration.Integration.Interfaces; using Dynamicweb.DataIntegration.ProviderHelpers; +using Dynamicweb.Environment; +using Dynamicweb.Extensibility; using Dynamicweb.Extensibility.AddIns; using Dynamicweb.Extensibility.Editors; +using Dynamicweb.Indexing; +using Dynamicweb.Indexing.Repositories; using Dynamicweb.Logging; +using Microsoft.CodeAnalysis; using System; -using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Globalization; +using System.Linq; using System.Xml; using System.Xml.Linq; -namespace Dynamicweb.DataIntegration.Providers.EcomProvider +namespace Dynamicweb.DataIntegration.Providers.EcomProvider; + +[AddInName("Dynamicweb.DataIntegration.Providers.Provider"), AddInLabel("Ecom Provider"), AddInDescription("Ecom provider"), AddInIgnore(false)] +public class EcomProvider : BaseSqlProvider, ISource, IDestination, IParameterOptions { - [AddInName("Dynamicweb.DataIntegration.Providers.Provider"), AddInLabel("Ecom Provider"), AddInDescription("Ecom provider"), AddInIgnore(false)] - public class EcomProvider : DynamicwebProvider.DynamicwebProvider, ISource, IDestination, IDropDownOptions + private Schema Schema; + private bool IsFirstJobRun = true; + + [AddInParameter("Get groups for variant options by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] + public string GroupsForVariantOptionsBy { - [AddInParameter("Get groups for variant options by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] - public string GroupsForVariantOptionsBy + get { - get - { - return GetGroupNamesForVariantOptions ? "Name" : "ID"; - } - set - { - GetGroupNamesForVariantOptions = value == "Name"; - } + return GetGroupNamesForVariantOptions ? "Name" : "ID"; } - public bool GetGroupNamesForVariantOptions { get; set; } - - [AddInParameter("Get manufacturer for products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] - public string ManufacturerForProductsBy + set { - get - { - return GetManufacturerNamesForProducts ? "Name" : "ID"; - } - set - { - GetManufacturerNamesForProducts = value == "Name"; - } + GetGroupNamesForVariantOptions = value == "Name"; + } + } + public bool GetGroupNamesForVariantOptions { get; set; } + [AddInParameter("Get manufacturer for products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] + public string ManufacturerForProductsBy + { + get + { + return GetManufacturerNamesForProducts ? "Name" : "ID"; } - public bool GetManufacturerNamesForProducts { get; set; } + set + { + GetManufacturerNamesForProducts = value == "Name"; + } + + } + public bool GetManufacturerNamesForProducts { get; set; } - [AddInParameter("Get variant groups for products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] - public string VariantGroupsForProductsBy + [AddInParameter("Get variant groups for products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] + public string VariantGroupsForProductsBy + { + get { - get - { - return GetVariantGroupNamesForProduct ? "Name" : "ID"; - } - set { GetVariantGroupNamesForProduct = value == "Name"; } + return GetVariantGroupNamesForProduct ? "Name" : "ID"; } - public bool GetVariantGroupNamesForProduct { get; set; } - [AddInParameter("Get groups for products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] - public string GroupsForProductsBy + set { GetVariantGroupNamesForProduct = value == "Name"; } + } + public bool GetVariantGroupNamesForProduct { get; set; } + [AddInParameter("Get groups for products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] + public string GroupsForProductsBy + { + get { - get - { - return GetGroupNamesForProduct ? "Name" : "ID"; - } - set { GetGroupNamesForProduct = value == "Name"; } + return GetGroupNamesForProduct ? "Name" : "ID"; } - public bool GetGroupNamesForProduct { get; set; } - [AddInParameter("Get related products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] - public string RelatedProductsBy + set { GetGroupNamesForProduct = value == "Name"; } + } + public bool GetGroupNamesForProduct { get; set; } + [AddInParameter("Get related products by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] + public string RelatedProductsBy + { + get { - get - { - return GetRelatedProductsByName ? "Name" : "ID"; - } - set { GetRelatedProductsByName = value == "Name"; } + return GetRelatedProductsByName ? "Name" : "ID"; } - public bool GetRelatedProductsByName { get; set; } - [AddInParameter("Get related product groups by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] - public string RelatedProductGroupsBy + set { GetRelatedProductsByName = value == "Name"; } + } + public bool GetRelatedProductsByName { get; set; } + [AddInParameter("Get related product groups by:"), AddInParameterEditor(typeof(RadioParameterEditor), ""), AddInParameterGroup("Source")] + public string RelatedProductGroupsBy + { + get { - get - { - return GetRelatedProductGroupsByName ? "Name" : "ID"; - } - set { GetRelatedProductGroupsByName = value == "Name"; } + return GetRelatedProductGroupsByName ? "Name" : "ID"; } + set { GetRelatedProductGroupsByName = value == "Name"; } + } - public bool GetRelatedProductGroupsByName { get; set; } - - [AddInParameter("Default Language"), AddInParameterEditor(typeof(DropDownParameterEditor), "none=true;Tooltip=Set the default language for the imported products"), AddInParameterGroup("Destination"), AddInParameterOrder(10)] - public override string DefaultLanguage + public bool GetRelatedProductGroupsByName { get; set; } + + private string defaultLanguage = null; + [AddInParameter("Default Language"), AddInParameterEditor(typeof(DropDownParameterEditor), "none=true;Tooltip=Set the default language for the imported products"), AddInParameterGroup("Destination"), AddInParameterOrder(10)] + public string DefaultLanguage + { + get { - get - { - return base.DefaultLanguage; - } - set + if (defaultLanguage == null) { - defaultLanguage = value; + SqlCommand sqlCommand = new SqlCommand("select top(1) ecomlanguages.LanguageID from ecomlanguages where ecomlanguages.languageisdefault=1", Connection); + if (Connection.State == ConnectionState.Closed) + Connection.Open(); + var result = sqlCommand.ExecuteReader(); + if (result.Read()) + defaultLanguage = (string)result["LanguageID"]; + result.Close(); } + return defaultLanguage; + } + set + { + defaultLanguage = value; } + } - [AddInParameter("Shop"), AddInParameterEditor(typeof(DropDownParameterEditor), "none=true;Tooltip=Set a shop for the imported products"), AddInParameterGroup("Destination"), AddInParameterOrder(20)] - public override string Shop { get; set; } + [AddInParameter("Shop"), AddInParameterEditor(typeof(DropDownParameterEditor), "none=true;Tooltip=Set a shop for the imported products"), AddInParameterGroup("Destination"), AddInParameterOrder(20)] + public string Shop { get; set; } - [AddInParameter("Deactivate missing products"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When ON missing products are deactivated. When OFF no action is taken. When Delete incoming rows is ON, Deactivate missing products is skipped. The Hide deactivated products option is used only when Deactivate missing products is ON"), AddInParameterGroup("Destination"), AddInParameterOrder(30)] - public override bool DeactivateMissingProducts { get; set; } + [AddInParameter("Insert only new records"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Inserts new records present in the source, but does not update existing records"), AddInParameterGroup("Destination"), AddInParameterOrder(23)] + public bool InsertOnlyNewRecords { get; set; } + [AddInParameter("Update only existing records"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When this option is ON the imported rows are updated but not inserted. When OFF rows are updated and inserted"), AddInParameterGroup("Destination"), AddInParameterOrder(25)] + public bool UpdateOnlyExistingRecords { get; set; } - [AddInParameter("Use strict primary key matching"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=This import affects ONLY records which match the selected primary key. If not checked the provider tries the following: Look at ProductID, Look at ProductNumber, Look at ProductName. If none match, create new record"), AddInParameterGroup("Destination"), AddInParameterOrder(38)] - public bool UseStrictPrimaryKeyMatching { get; set; } + [AddInParameter("Deactivate missing products"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When ON missing products are deactivated. When OFF no action is taken. When Delete incoming rows is ON, Deactivate missing products is skipped. The Hide deactivated products option is used only when Deactivate missing products is ON"), AddInParameterGroup("Destination"), AddInParameterOrder(30)] + public bool DeactivateMissingProducts { get; set; } - [AddInParameter("Update only existing products"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When this option is ON the imported products are updated but not inserted. When OFF products are updated and inserted. If Delete incoming rows is ON, Update only existing products is skipped. If Update only existing records is ON, Update only existing products is skipped"), AddInParameterGroup("Destination"), AddInParameterOrder(41)] - public bool UpdateOnlyExistingProducts { get; set; } + [AddInParameter("Remove missing rows after import in the destination tables only"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Deletes rows not present in the import source - excluding related tabled"), AddInParameterGroup("Destination"), AddInParameterOrder(35)] + public bool RemoveMissingAfterImportDestinationTablesOnly { get; set; } - [AddInParameter("Create missing groups"), AddInParameterEditor(typeof(YesNoParameterEditor), ""), AddInParameterGroup("Destination"), AddInParameterOrder(42)] - public bool CreateMissingGoups { get; set; } + [AddInParameter("Use strict primary key matching"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=This import affects ONLY records which match the selected primary key. If not checked the provider tries the following: Look at ProductID, Look at ProductNumber, Look at ProductName. If none match, create new record"), AddInParameterGroup("Destination"), AddInParameterOrder(38)] + public bool UseStrictPrimaryKeyMatching { get; set; } + [AddInParameter("Remove missing rows after import"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Removes rows from the destination and relation tables. This option takes precedence"), AddInParameterGroup("Destination"), AddInParameterOrder(40)] + public bool RemoveMissingAfterImport { get; set; } - [AddInParameter("Delete incoming rows"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Deletes existing rows present in the import source. When Delete incoming rows is ON, the following options are skipped: Update only existing products, Update only existing records, Deactivate missing products, Remove missing rows after import, and Delete products / groups for languages included in input"), AddInParameterGroup("Destination"), AddInParameterOrder(50)] - public override bool DeleteIncomingItems { get; set; } + [AddInParameter("Update only existing products"), AddInParameterEditor(typeof(YesNoParameterEditor), ""), AddInParameterGroup("Destination"), AddInParameterOrder(41)] + public bool UpdateOnlyExistingProducts { get; set; } - [AddInParameter("Delete products/groups for languages included in input"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Deletes products and groups only from the languages included in the import. When Delete incoming rows is ON, this option is ignored"), AddInParameterGroup("Destination"), AddInParameterOrder(60)] - public override bool DeleteProductsAndGroupForSpecificLanguage { get; set; } + [AddInParameter("Create missing groups"), AddInParameterEditor(typeof(YesNoParameterEditor), ""), AddInParameterGroup("Destination"), AddInParameterOrder(42)] + public bool CreateMissingGoups { get; set; } - [AddInParameter("User key field"), AddInParameterEditor(typeof(TextParameterEditor), ""), AddInParameterGroup("Hidden")] - public new string UserKeyField { get; set; } + [AddInParameter("Delete incoming rows"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Deletes existing rows present in the import source. When Delete incoming rows is ON, the following options are skipped: Update only existing products, Update only existing records, Deactivate missing products, Remove missing rows after import, and Delete products / groups for languages included in input"), AddInParameterGroup("Destination"), AddInParameterOrder(50)] + public bool DeleteIncomingItems { get; set; } - [AddInParameter("Use existing Product Id found by Number in Variant Products"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When checked, the values in Dynamicweb ProductId and ProductVariantID will be used at import to update products in Dynamicweb. A product is only updated if it matches the ProductNumber (in default language) of the imported row and if the Dynamicweb ProductVariantId field is not empty"), AddInParameterGroup("Destination")] - public bool UseProductIdFoundByNumber { get; set; } + [AddInParameter("Delete products/groups for languages included in input"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Deletes products and groups only from the languages included in the import. When Delete incoming rows is ON, this option is ignored"), AddInParameterGroup("Destination"), AddInParameterOrder(60)] + public bool DeleteProductsAndGroupForSpecificLanguage { get; set; } - [AddInParameter("Ignore empty category field values"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When checked, the Ecom provider does not write empty category field values to the database"), AddInParameterGroup("Destination")] - public bool IgnoreEmptyCategoryFieldValues { get; set; } + [AddInParameter("Discard duplicates"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When ON, duplicate rows are skipped"), AddInParameterGroup("Destination")] + public bool DiscardDuplicates { get; set; } - protected override SqlConnection Connection - { - get { return connection ?? (connection = (SqlConnection)Database.CreateConnection()); } - set { connection = value; } - } + [AddInParameter("Hide deactivated products"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When Deactivate missing products is ON, this option hides the deactivated products. If Delete incoming rows is ON, Hide deactivated products is skipped. If Deactivate missing products is OFF, Hide deactivated products is skipped"), AddInParameterGroup("Destination"), AddInParameterOrder(80)] + public bool HideDeactivatedProducts { get; set; } + + [AddInParameter("User key field"), AddInParameterEditor(typeof(TextParameterEditor), ""), AddInParameterGroup("Hidden")] + public string UserKeyField { get; set; } + + [AddInParameter("Repositories index update"), AddInParameterEditor(typeof(DropDownParameterEditor), "multiple=true;none=true;Tooltip=Index update might affect on slower perfomance"), AddInParameterGroup("Destination"), AddInParameterOrder(80)] + public string RepositoriesIndexUpdate { get; set; } + + [AddInParameter("Disable cache clearing"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=This setting disables cache clearing after import\t"), AddInParameterGroup("Destination"), AddInParameterOrder(90)] + public bool DisableCacheClearing { get; set; } + + [AddInParameter("Persist successful rows and skip failing rows"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=Checking this box allows the activity to do partial imports by skipping problematic records and keeping the succesful ones"), AddInParameterGroup("Destination"), AddInParameterOrder(100)] + public bool SkipFailingRows { get; set; } + + [AddInParameter("Use existing Product Id found by Number in Variant Products"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When checked, the values in Dynamicweb ProductId and ProductVariantID will be used at import to update products in Dynamicweb. A product is only updated if it matches the ProductNumber (in default language) of the imported row and if the Dynamicweb ProductVariantId field is not empty"), AddInParameterGroup("Destination")] + public bool UseProductIdFoundByNumber { get; set; } - private EcomDestinationWriter Writer; + [AddInParameter("Ignore empty category field values"), AddInParameterEditor(typeof(YesNoParameterEditor), "Tooltip=When checked, the Ecom provider does not write empty category field values to the database"), AddInParameterGroup("Destination")] + public bool IgnoreEmptyCategoryFieldValues { get; set; } - public EcomProvider(string connectionString) + /// + /// This property is used to remove rows from the EcomGroupProductRelationsTable, but only for the products that are being imported. + /// It can be set in the job settings xml file in the config section + /// + public virtual bool PartialUpdate { get; set; } + + private string SqlConnectionString { get; set; } + + private SqlConnection connection; + protected SqlConnection Connection + { + get { return connection ??= (SqlConnection)Database.CreateConnection(); } + set { connection = value; } + } + + private EcomDestinationWriter Writer; + + public EcomProvider(string connectionString) + { + RemoveMissingAfterImport = false; + RemoveMissingAfterImportDestinationTablesOnly = false; + UseStrictPrimaryKeyMatching = true; + SqlConnectionString = connectionString; + Connection = new SqlConnection(connectionString); + DiscardDuplicates = false; + HideDeactivatedProducts = false; + CreateMissingGoups = true; + } + + public override Schema GetOriginalSourceSchema() + { + return GetSchema(false); + } + + private Schema GetDynamicwebSourceSchema() + { + Schema result = GetSqlSourceSchema(Connection); + //set key for AccessUserTable + if (UserKeyField != null) { - RemoveMissingAfterImport = false; - RemoveMissingAfterImportDestinationTablesOnly = false; - UseStrictPrimaryKeyMatching = true; - SqlConnectionString = connectionString; - Connection = new SqlConnection(connectionString); - DiscardDuplicates = false; - HideDeactivatedProducts = false; - CreateMissingGoups = true; + Column keyColumn = result.GetTables().Find(t => t.Name == "AccessUser").Columns.Find(c => c.Name == UserKeyField); + if (keyColumn != null) + keyColumn.IsPrimaryKey = true; } - public override Schema GetOriginalSourceSchema() + //Set key for other tables that are missing keys in the database + var table = result.GetTables().FirstOrDefault(t => t.Name == "Ecom7Tree"); + if (table != null) + { + table.Columns.Find(c => c.Name == "id").IsPrimaryKey = true; + } + if (result.GetTables().Exists(t => t.Name.Contains("Ipaper"))) + { + UpdateIPaperTables(result); + } + table = result.GetTables().Find(t => t.Name == "Statv2SessionBot"); + if (table != null) + table.Columns.Find(c => c.Name == "Statv2SessionID").IsPrimaryKey = true; + table = result.GetTables().Find(t => t.Name == "Statv2UserAgents"); + if (table != null) + table.Columns.Find(c => c.Name == "Statv2UserAgentsID").IsPrimaryKey = true; + + //For EcomProducts Remove ProductAutoID column from schema + Table ecomProductsTable = result.GetTables().Find(t => t.Name == "EcomProducts"); + if (ecomProductsTable != null) + { + ecomProductsTable.Columns.RemoveAll(c => c.Name == "ProductAutoID"); + } + Table ecomAssortmentPermissionsTable = result.GetTables().Find(t => t.Name == "EcomAssortmentPermissions"); + if (ecomAssortmentPermissionsTable != null) { - return GetSchema(false); + ecomAssortmentPermissionsTable.AddColumn(new SqlColumn(("AssortmentPermissionCustomerNumber"), typeof(string), SqlDbType.NVarChar, ecomAssortmentPermissionsTable, -1, false, false, true)); + ecomAssortmentPermissionsTable.AddColumn(new SqlColumn(("AssortmentPermissionExternalID"), typeof(string), SqlDbType.NVarChar, ecomAssortmentPermissionsTable, -1, false, false, true)); } - /// - /// Gets source or destination schema for Ecom Provider - /// - /// true to get Destination schema, false to get Source schema - /// - public virtual Schema GetSchema(bool getForDestination) + return result; + } + + /// + /// Gets source or destination schema for Ecom Provider + /// + /// true to get Destination schema, false to get Source schema + /// + public Schema GetSchema(bool getForDestination) + { + Schema result = GetDynamicwebSourceSchema(); + List tablestToKeep = new() + { "EcomProducts", "EcomManufacturers", "EcomGroups", "EcomVariantGroups", "EcomVariantsOptions", + "EcomProductsRelated", "EcomProductItems", "EcomStockUnit", "EcomDetails","EcomProductCategoryFieldValue", "EcomLanguages", "EcomPrices", + "EcomAssortmentGroupRelations", "EcomAssortmentPermissions", "EcomAssortmentProductRelations", "EcomAssortments", "EcomAssortmentShopRelations", "EcomVariantOptionsProductRelation"}; + List tablesToRemove = new(); + foreach (Table table in result.GetTables()) { - Schema result = base.GetOriginalSourceSchema(); - List tablestToKeep = new List { "EcomProducts", "EcomManufacturers", "EcomGroups", "EcomVariantGroups", "EcomVariantsOptions", - "EcomProductsRelated", "EcomProductItems", "EcomStockUnit", "EcomDetails","EcomProductCategoryFieldValue", "EcomLanguages", "EcomPrices", - "EcomAssortmentGroupRelations", "EcomAssortmentPermissions", "EcomAssortmentProductRelations", "EcomAssortments", "EcomAssortmentShopRelations", "EcomVariantOptionsProductRelation"}; - List
tablesToRemove = new List
(); - foreach (Table table in result.GetTables()) - { - if (!tablestToKeep.Contains(table.Name)) - tablesToRemove.Add(table); - } - foreach (Table table in tablesToRemove) - { - result.RemoveTable(table); - } - foreach (Table table in result.GetTables()) + if (!tablestToKeep.Contains(table.Name)) + tablesToRemove.Add(table); + } + foreach (Table table in tablesToRemove) + { + result.RemoveTable(table); + } + foreach (Table table in result.GetTables()) + { + switch (table.Name) { - switch (table.Name) - { - case "EcomProducts": - //Add extra fields to EcomProducts - table.AddColumn(new SqlColumn(("Groups"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn(("PrimaryGroup"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn(("GroupSorting"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn(("VariantGroups"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn(("VariantOptions"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn(("RelatedProducts"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - var fields = new List(); - foreach (var category in Ecommerce.Services.ProductCategories.GetCategories()) + case "EcomProducts": + //Add extra fields to EcomProducts + table.AddColumn(new SqlColumn(("Groups"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn(("PrimaryGroup"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn(("GroupSorting"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn(("VariantGroups"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn(("VariantOptions"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn(("RelatedProducts"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + var fields = new List(); + foreach (var category in Ecommerce.Services.ProductCategories.GetCategories()) + { + if (category.CategoryType != Ecommerce.Products.Categories.CategoryType.SystemFields) { - if (category.CategoryType != Ecommerce.Products.Categories.CategoryType.SystemFields) - { - fields.AddRange(Dynamicweb.Ecommerce.Services.ProductCategoryFields.GetFieldsByCategoryId(category.Id)); - } + fields.AddRange(Dynamicweb.Ecommerce.Services.ProductCategoryFields.GetFieldsByCategoryId(category.Id)); } + } - foreach (var field in fields) - { - table.AddColumn(new SqlColumn(($"ProductCategory|{field.Category.Id}|{field.Id}"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - } - break; - - case "EcomGroups": - table.AddColumn(new SqlColumn("Shops", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn("ShopSorting", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn("ParentGroupsSorting", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - table.AddColumn(new SqlColumn("ParentGroups", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - break; - case "EcomProductsRelated": - table.AddColumn(new SqlColumn("ProductRelatedLanguageID", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - break; - case "EcomProductCategoryFieldValue": - table.AddColumn(new SqlColumn("FieldValueProductNumber", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); - break; - } + foreach (var field in fields) + { + table.AddColumn(new SqlColumn(($"ProductCategory|{field.Category.Id}|{field.Id}"), typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + } + break; + + case "EcomGroups": + table.AddColumn(new SqlColumn("Shops", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn("ShopSorting", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn("ParentGroupsSorting", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + table.AddColumn(new SqlColumn("ParentGroups", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + break; + case "EcomProductsRelated": + table.AddColumn(new SqlColumn("ProductRelatedLanguageID", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + break; + case "EcomProductCategoryFieldValue": + table.AddColumn(new SqlColumn("FieldValueProductNumber", typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + break; } - return result; } + return result; + } - public override Schema GetOriginalDestinationSchema() + private void UpdateIPaperTables(Schema schema) + { + Table table = schema.GetTables().Find(t => t.Name == "IpaperCategories"); + if (table != null) { - return GetSchema(true); + table.Columns.Find(c => c.Name == "CategoryID").IsPrimaryKey = true; } - - public override void OverwriteSourceSchemaToOriginal() + table = schema.GetTables().Find(t => t.Name == "IpaperLanguageKeys"); + if (table != null) { - Schema = GetSchema(false); + table.Columns.Find(c => c.Name == "LanguageKeyID").IsPrimaryKey = true; } - - public override void OverwriteDestinationSchemaToOriginal() + table = schema.GetTables().Find(t => t.Name == "IpaperLanguageKeyValues"); + if (table != null) { - Schema = GetSchema(true); + table.Columns.Find(c => c.Name == "LanguageKeyValueID").IsPrimaryKey = true; } - - Schema ISource.GetSchema() + table = schema.GetTables().Find(t => t.Name == "IpaperLanguages"); + if (table != null) { - if (Schema == null) - { - Schema = GetSchema(false); - } - return Schema; + table.Columns.Find(c => c.Name == "LanguageID").IsPrimaryKey = true; } - - Schema IDestination.GetSchema() + table = schema.GetTables().Find(t => t.Name == "IpaperPapers"); + if (table != null) { - if (Schema == null) - { - Schema = GetSchema(true); - } - return Schema; + table.Columns.Find(c => c.Name == "PaperID").IsPrimaryKey = true; + } + table = schema.GetTables().Find(t => t.Name == "IpaperSettingDescriptions"); + if (table != null) + { + table.Columns.Find(c => c.Name == "DescriptionID").IsPrimaryKey = true; + } + table = schema.GetTables().Find(t => t.Name == "IpaperPages"); + if (table != null) + { + table.Columns.Find(c => c.Name == "PageID").IsPrimaryKey = true; + } + table = schema.GetTables().Find(t => t.Name == "IpaperSettingGroups"); + if (table != null) + { + table.Columns.Find(c => c.Name == "GroupID").IsPrimaryKey = true; } + table = schema.GetTables().Find(t => t.Name == "IpaperSettings"); + if (table != null) + { + table.Columns.Find(c => c.Name == "SettingID").IsPrimaryKey = true; + } + table = schema.GetTables().Find(t => t.Name == "IpaperSettingSets"); + if (table != null) + { + table.Columns.Find(c => c.Name == "SetID").IsPrimaryKey = true; + } + table = schema.GetTables().Find(t => t.Name == "IpaperSettingTypes"); + if (table != null) + { + table.Columns.Find(c => c.Name == "TypeID").IsPrimaryKey = true; + } + } + + public override Schema GetOriginalDestinationSchema() + { + return GetSchema(true); + } + + public override void OverwriteSourceSchemaToOriginal() + { + Schema = GetSchema(false); + } + + public override void OverwriteDestinationSchemaToOriginal() + { + Schema = GetSchema(true); + } + + Schema ISource.GetSchema() + { + Schema ??= GetSchema(false); + return Schema; + } + + Schema IDestination.GetSchema() + { + Schema ??= GetSchema(true); + return Schema; + } + + public override Schema GetSchema() + { + Schema ??= GetOriginalSourceSchema(); + return Schema; + } - public override Schema GetSchema() + public EcomProvider(XmlNode xmlNode) + { + RemoveMissingAfterImport = false; + RemoveMissingAfterImportDestinationTablesOnly = false; + UpdateOnlyExistingProducts = false; + DeleteProductsAndGroupForSpecificLanguage = false; + DeleteIncomingItems = false; + DiscardDuplicates = false; + HideDeactivatedProducts = false; + InsertOnlyNewRecords = false; + UseStrictPrimaryKeyMatching = true; + CreateMissingGoups = true; + + foreach (XmlNode node in xmlNode.ChildNodes) { - if (Schema == null) + switch (node.Name) { - Schema = GetOriginalSourceSchema(); + case "SqlConnectionString": + if (node.HasChildNodes) + { + SqlConnectionString = node.FirstChild.Value; + Connection = new SqlConnection(SqlConnectionString); + } + break; + case "Schema": + Schema = new Schema(node); + break; + case "RemoveMissingAfterImport": + if (node.HasChildNodes) + { + RemoveMissingAfterImport = node.FirstChild.Value == "True"; + } + break; + case "RemoveMissingAfterImportDestinationTablesOnly": + if (node.HasChildNodes) + { + RemoveMissingAfterImportDestinationTablesOnly = node.FirstChild.Value == "True"; + } + break; + case "DeactivateMissingProducts": + if (node.HasChildNodes) + { + DeactivateMissingProducts = node.FirstChild.Value == "True"; + } + break; + case "Shop": + if (node.HasChildNodes) + { + Shop = node.FirstChild.Value; + } + break; + case "UserKeyField": + if (node.HasChildNodes) + { + UserKeyField = node.FirstChild.Value; + } + break; + case "GroupsForProductsBy": + if (node.HasChildNodes) + { + GroupsForProductsBy = node.FirstChild.Value; + } + break; + case "GroupsForVariantOptionsBy": + if (node.HasChildNodes) + { + GroupsForVariantOptionsBy = node.FirstChild.Value; + } + break; + case "ManufacturerForProductsBy": + if (node.HasChildNodes) + { + ManufacturerForProductsBy = node.FirstChild.Value; + } + break; + case "RelatedProductGroupsBy": + if (node.HasChildNodes) + { + RelatedProductGroupsBy = node.FirstChild.Value; + } + break; + case "RelatedProductsBy": + if (node.HasChildNodes) + { + RelatedProductsBy = node.FirstChild.Value; + } + break; + case "VariantGroupsForProductsBy": + if (node.HasChildNodes) + { + VariantGroupsForProductsBy = node.FirstChild.Value; + } + break; + case "DefaultLanguage": + if (node.HasChildNodes) + { + DefaultLanguage = node.FirstChild.Value; + } + break; + case "UpdateOnlyExistingProducts": + if (node.HasChildNodes) + { + UpdateOnlyExistingProducts = node.FirstChild.Value == "True"; + } + break; + case "UseStrictPrimaryKeyMatching": + if (node.HasChildNodes) + { + UseStrictPrimaryKeyMatching = node.FirstChild.Value == "True"; + } + break; + case "CreateMissingGoups": + if (node.HasChildNodes) + { + CreateMissingGoups = node.FirstChild.Value == "True"; + } + break; + case "RepositoriesIndexUpdate": + if (node.HasChildNodes) + { + RepositoriesIndexUpdate = node.FirstChild.Value; + } + break; + case "DeleteProductsAndGroupForSpecificLanguage": + if (node.HasChildNodes) + { + DeleteProductsAndGroupForSpecificLanguage = node.FirstChild.Value == "True"; + } + break; + case "UpdateOnlyExistingRecords": + if (node.HasChildNodes) + { + UpdateOnlyExistingRecords = node.FirstChild.Value == "True"; + } + break; + case "DeleteIncomingItems": + if (node.HasChildNodes) + { + DeleteIncomingItems = node.FirstChild.Value == "True"; + } + break; + case "DiscardDuplicates": + if (node.HasChildNodes) + { + DiscardDuplicates = node.FirstChild.Value == "True"; + } + break; + case "HideDeactivatedProducts": + if (node.HasChildNodes) + { + HideDeactivatedProducts = node.FirstChild.Value == "True"; + } + break; + case "InsertOnlyNewRecords": + if (node.HasChildNodes) + { + InsertOnlyNewRecords = node.FirstChild.Value == "True"; + } + break; + case nameof(DisableCacheClearing): + if (node.HasChildNodes) + { + DisableCacheClearing = node.FirstChild.Value == "True"; + } + break; + case "SkipFailingRows": + if (node.HasChildNodes) + { + SkipFailingRows = node.FirstChild.Value == "True"; + } + break; + case nameof(UseProductIdFoundByNumber): + if (node.HasChildNodes) + { + UseProductIdFoundByNumber = node.FirstChild.Value == "True"; + } + break; + case nameof(IgnoreEmptyCategoryFieldValues): + if (node.HasChildNodes) + { + IgnoreEmptyCategoryFieldValues = node.FirstChild.Value == "True"; + } + break; } - return Schema; } + } - public EcomProvider(XmlNode xmlNode) + public override string ValidateDestinationSettings() + { + if (InsertOnlyNewRecords && UpdateOnlyExistingRecords) { - RemoveMissingAfterImport = false; - RemoveMissingAfterImportDestinationTablesOnly = false; - UpdateOnlyExistingProducts = false; - DeleteProductsAndGroupForSpecificLanguage = false; - DeleteIncomingItems = false; - DiscardDuplicates = false; - HideDeactivatedProducts = false; - InsertOnlyNewRecords = false; - UseStrictPrimaryKeyMatching = true; - CreateMissingGoups = true; + return "\"Insert only\" and \"Update only\" options can not be set at the same time"; + } + return ""; + } + public override string ValidateSourceSettings() + { + return null; + } + public new virtual void SaveAsXml(XmlTextWriter xmlTextWriter) + { + xmlTextWriter.WriteElementString("RemoveMissingAfterImport", RemoveMissingAfterImport.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("RemoveMissingAfterImportDestinationTablesOnly", RemoveMissingAfterImportDestinationTablesOnly.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("DeactivateMissingProducts", DeactivateMissingProducts.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("DeleteProductsAndGroupForSpecificLanguage", DeleteProductsAndGroupForSpecificLanguage.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("SqlConnectionString", SqlConnectionString); + xmlTextWriter.WriteElementString("Shop", Shop); + xmlTextWriter.WriteElementString("UserKeyField", UserKeyField); + xmlTextWriter.WriteElementString("GroupsForProductsBy", GroupsForProductsBy); + xmlTextWriter.WriteElementString("GroupsForVariantOptionsBy", GroupsForVariantOptionsBy); + xmlTextWriter.WriteElementString("ManufacturerForProductsBy", ManufacturerForProductsBy); + xmlTextWriter.WriteElementString("RelatedProductGroupsBy", RelatedProductGroupsBy); + xmlTextWriter.WriteElementString("RelatedProductsBy", RelatedProductsBy); + xmlTextWriter.WriteElementString("VariantGroupsForProductsBy", VariantGroupsForProductsBy); + xmlTextWriter.WriteElementString("DefaultLanguage", DefaultLanguage); + xmlTextWriter.WriteElementString("UpdateOnlyExistingProducts", UpdateOnlyExistingProducts.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("UseStrictPrimaryKeyMatching", UseStrictPrimaryKeyMatching.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("RepositoriesIndexUpdate", RepositoriesIndexUpdate); + xmlTextWriter.WriteElementString("UpdateOnlyExistingRecords", UpdateOnlyExistingRecords.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("DeleteIncomingItems", DeleteIncomingItems.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("DiscardDuplicates", DiscardDuplicates.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("HideDeactivatedProducts", HideDeactivatedProducts.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("InsertOnlyNewRecords", InsertOnlyNewRecords.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString("CreateMissingGoups", CreateMissingGoups.ToString(CultureInfo.CurrentCulture)); + xmlTextWriter.WriteElementString(nameof(DisableCacheClearing), DisableCacheClearing.ToString()); + xmlTextWriter.WriteElementString(nameof(UseProductIdFoundByNumber), UseProductIdFoundByNumber.ToString()); + xmlTextWriter.WriteElementString(nameof(IgnoreEmptyCategoryFieldValues), IgnoreEmptyCategoryFieldValues.ToString()); + xmlTextWriter.WriteElementString("SkipFailingRows", SkipFailingRows.ToString(CultureInfo.CurrentCulture)); + GetSchema().SaveAsXml(xmlTextWriter); + } + public override void UpdateSourceSettings(ISource source) + { + EcomProvider newProvider = (EcomProvider)source; + DefaultLanguage = newProvider.DefaultLanguage; + GetGroupNamesForProduct = newProvider.GetGroupNamesForProduct; + GetGroupNamesForVariantOptions = newProvider.GetGroupNamesForVariantOptions; + GetManufacturerNamesForProducts = newProvider.GetManufacturerNamesForProducts; + GetRelatedProductGroupsByName = newProvider.GetRelatedProductGroupsByName; + GetRelatedProductsByName = newProvider.GetRelatedProductsByName; + GetVariantGroupNamesForProduct = newProvider.GetVariantGroupNamesForProduct; + DefaultLanguage = newProvider.DefaultLanguage; + DeactivateMissingProducts = newProvider.DeactivateMissingProducts; + UpdateOnlyExistingProducts = newProvider.UpdateOnlyExistingProducts; + UseStrictPrimaryKeyMatching = newProvider.UseStrictPrimaryKeyMatching; + DeleteProductsAndGroupForSpecificLanguage = newProvider.DeleteProductsAndGroupForSpecificLanguage; + RepositoriesIndexUpdate = newProvider.RepositoriesIndexUpdate; + UpdateOnlyExistingRecords = newProvider.UpdateOnlyExistingRecords; + DeleteIncomingItems = newProvider.DeleteIncomingItems; + DiscardDuplicates = newProvider.DiscardDuplicates; + HideDeactivatedProducts = newProvider.HideDeactivatedProducts; + InsertOnlyNewRecords = newProvider.InsertOnlyNewRecords; + CreateMissingGoups = newProvider.CreateMissingGoups; + DisableCacheClearing = newProvider.DisableCacheClearing; + SkipFailingRows = newProvider.SkipFailingRows; + UseProductIdFoundByNumber = newProvider.UseProductIdFoundByNumber; + IgnoreEmptyCategoryFieldValues = newProvider.IgnoreEmptyCategoryFieldValues; + SqlConnectionString = newProvider.SqlConnectionString; + RemoveMissingAfterImport = newProvider.RemoveMissingAfterImport; + RemoveMissingAfterImportDestinationTablesOnly = newProvider.RemoveMissingAfterImportDestinationTablesOnly; + } + public override void UpdateDestinationSettings(IDestination destination) + { + ISource newProvider = (ISource)destination; + UpdateSourceSettings(newProvider); + } + public override string Serialize() + { + XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty)); + XElement root = new XElement("Parameters"); + document.Add(root); + root.Add(CreateParameterNode(GetType(), "Get groups for variant options by:", GroupsForVariantOptionsBy)); + root.Add(CreateParameterNode(GetType(), "Get manufacturer for products by:", ManufacturerForProductsBy)); + root.Add(CreateParameterNode(GetType(), "Get variant groups for products by:", VariantGroupsForProductsBy)); + root.Add(CreateParameterNode(GetType(), "Get groups for products by:", GroupsForProductsBy)); + root.Add(CreateParameterNode(GetType(), "Get related products by:", RelatedProductsBy)); + root.Add(CreateParameterNode(GetType(), "Get related product groups by:", RelatedProductGroupsBy)); + root.Add(CreateParameterNode(GetType(), "Default Language", DefaultLanguage)); + root.Add(CreateParameterNode(GetType(), "Deactivate missing products", DeactivateMissingProducts.ToString())); + root.Add(CreateParameterNode(GetType(), "Delete products/groups for languages included in input", DeleteProductsAndGroupForSpecificLanguage.ToString())); + root.Add(Shop == null + ? CreateParameterNode(GetType(), "Shop", "") + : CreateParameterNode(GetType(), "Shop", Shop)); + + root.Add(UserKeyField == null + ? CreateParameterNode(GetType(), "User key field", "") + : CreateParameterNode(GetType(), "User key field", UserKeyField)); + root.Add(CreateParameterNode(GetType(), "Remove missing rows after import", RemoveMissingAfterImport.ToString())); + root.Add(CreateParameterNode(GetType(), "Remove missing rows after import in the destination tables only", RemoveMissingAfterImportDestinationTablesOnly.ToString())); + root.Add(CreateParameterNode(GetType(), "Update only existing products", UpdateOnlyExistingProducts.ToString())); + root.Add(CreateParameterNode(GetType(), "Use strict primary key matching", UseStrictPrimaryKeyMatching.ToString())); + root.Add(CreateParameterNode(GetType(), "Repositories index update", RepositoriesIndexUpdate)); + root.Add(CreateParameterNode(GetType(), "Update only existing records", UpdateOnlyExistingRecords.ToString())); + root.Add(CreateParameterNode(GetType(), "Delete incoming rows", DeleteIncomingItems.ToString())); + root.Add(CreateParameterNode(GetType(), "Discard duplicates", DiscardDuplicates.ToString())); + root.Add(CreateParameterNode(GetType(), "Hide deactivated products", HideDeactivatedProducts.ToString())); + root.Add(CreateParameterNode(GetType(), "Insert only new records", InsertOnlyNewRecords.ToString())); + root.Add(CreateParameterNode(GetType(), "Create missing groups", CreateMissingGoups.ToString())); + root.Add(CreateParameterNode(GetType(), "Disable cache clearing", DisableCacheClearing.ToString())); + root.Add(CreateParameterNode(GetType(), "Persist successful rows and skip failing rows", SkipFailingRows.ToString())); + root.Add(CreateParameterNode(GetType(), "Use existing Product Id found by Number in Variant Products", UseProductIdFoundByNumber.ToString())); + root.Add(CreateParameterNode(GetType(), "Ignore empty category field values", IgnoreEmptyCategoryFieldValues.ToString())); + + return document.ToString(); + } - foreach (XmlNode node in xmlNode.ChildNodes) - { - switch (node.Name) - { - case "SqlConnectionString": - if (node.HasChildNodes) - { - SqlConnectionString = node.FirstChild.Value; - Connection = new SqlConnection(SqlConnectionString); - } - break; - case "Schema": - Schema = new Schema(node); - break; - case "RemoveMissingAfterImport": - if (node.HasChildNodes) - { - RemoveMissingAfterImport = node.FirstChild.Value == "True"; - } - break; - case "RemoveMissingAfterImportDestinationTablesOnly": - if (node.HasChildNodes) - { - RemoveMissingAfterImportDestinationTablesOnly = node.FirstChild.Value == "True"; - } - break; - case "DeactivateMissingProducts": - if (node.HasChildNodes) - { - DeactivateMissingProducts = node.FirstChild.Value == "True"; - } - break; - case "Shop": - if (node.HasChildNodes) - { - Shop = node.FirstChild.Value; - } - break; - case "UserKeyField": - if (node.HasChildNodes) - { - UserKeyField = node.FirstChild.Value; - } - break; - case "GroupsForProductsBy": - if (node.HasChildNodes) - { - GroupsForProductsBy = node.FirstChild.Value; - } - break; - case "GroupsForVariantOptionsBy": - if (node.HasChildNodes) - { - GroupsForVariantOptionsBy = node.FirstChild.Value; - } - break; - case "ManufacturerForProductsBy": - if (node.HasChildNodes) - { - ManufacturerForProductsBy = node.FirstChild.Value; - } - break; - case "RelatedProductGroupsBy": - if (node.HasChildNodes) - { - RelatedProductGroupsBy = node.FirstChild.Value; - } - break; - case "RelatedProductsBy": - if (node.HasChildNodes) - { - RelatedProductsBy = node.FirstChild.Value; - } - break; - case "VariantGroupsForProductsBy": - if (node.HasChildNodes) - { - VariantGroupsForProductsBy = node.FirstChild.Value; - } - break; - case "DefaultLanguage": - if (node.HasChildNodes) - { - DefaultLanguage = node.FirstChild.Value; - } - break; - case "UpdateOnlyExistingProducts": - if (node.HasChildNodes) - { - UpdateOnlyExistingProducts = node.FirstChild.Value == "True"; - } - break; - case "UseStrictPrimaryKeyMatching": - if (node.HasChildNodes) - { - UseStrictPrimaryKeyMatching = node.FirstChild.Value == "True"; - } - break; - case "CreateMissingGoups": - if (node.HasChildNodes) - { - CreateMissingGoups = node.FirstChild.Value == "True"; - } - break; - case "RepositoriesIndexUpdate": - if (node.HasChildNodes) - { - RepositoriesIndexUpdate = node.FirstChild.Value; - } - break; - case "DeleteProductsAndGroupForSpecificLanguage": - if (node.HasChildNodes) - { - DeleteProductsAndGroupForSpecificLanguage = node.FirstChild.Value == "True"; - } - break; - case "UpdateOnlyExistingRecords": - if (node.HasChildNodes) - { - UpdateOnlyExistingRecords = node.FirstChild.Value == "True"; - } - break; - case "DeleteIncomingItems": - if (node.HasChildNodes) - { - DeleteIncomingItems = node.FirstChild.Value == "True"; - } - break; - case "DiscardDuplicates": - if (node.HasChildNodes) - { - DiscardDuplicates = node.FirstChild.Value == "True"; - } - break; - case "HideDeactivatedProducts": - if (node.HasChildNodes) - { - HideDeactivatedProducts = node.FirstChild.Value == "True"; - } - break; - case "InsertOnlyNewRecords": - if (node.HasChildNodes) - { - InsertOnlyNewRecords = node.FirstChild.Value == "True"; - } - break; - case nameof(DisableCacheClearing): - if (node.HasChildNodes) - { - DisableCacheClearing = node.FirstChild.Value == "True"; - } - break; - case "SkipFailingRows": - if (node.HasChildNodes) - { - SkipFailingRows = node.FirstChild.Value == "True"; - } - break; - case nameof(UseProductIdFoundByNumber): - if (node.HasChildNodes) - { - UseProductIdFoundByNumber = node.FirstChild.Value == "True"; - } - break; - case nameof(IgnoreEmptyCategoryFieldValues): - if (node.HasChildNodes) - { - IgnoreEmptyCategoryFieldValues = node.FirstChild.Value == "True"; - } - break; - } - } + public EcomProvider() + { + UseStrictPrimaryKeyMatching = true; + CreateMissingGoups = true; + } + public new ISourceReader GetReader(Mapping mapping) + { + return new EcomSourceReader(mapping, Connection, GetGroupNamesForVariantOptions, GetManufacturerNamesForProducts, GetGroupNamesForProduct, GetVariantGroupNamesForProduct, GetRelatedProductsByName, GetRelatedProductGroupsByName); + } + + protected internal static void OrderTablesInJob(Job job) + { + MappingCollection tables = new MappingCollection(); + if (GetMappingsByName(job.Mappings, "EcomLanguages") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomLanguages")); + if (GetMappingsByName(job.Mappings, "EcomGroups") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomGroups")); + if (GetMappingsByName(job.Mappings, "EcomManufacturers") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomManufacturers")); + if (GetMappingsByName(job.Mappings, "EcomVariantGroups") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomVariantGroups")); + if (GetMappingsByName(job.Mappings, "EcomVariantsOptions") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomVariantsOptions")); + if (GetMappingsByName(job.Mappings, "EcomProducts") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomProducts")); + if (GetMappingsByName(job.Mappings, "EcomProductItems") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomProductItems")); + if (GetMappingsByName(job.Mappings, "EcomProductsRelated") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomProductsRelated")); + if (GetMappingsByName(job.Mappings, "EcomStockUnit") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomStockUnit")); + if (GetMappingsByName(job.Mappings, "EcomDetails") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomDetails")); + if (GetMappingsByName(job.Mappings, "EcomProductCategoryFieldValue") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomProductCategoryFieldValue")); + if (GetMappingsByName(job.Mappings, "EcomPrices") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomPrices")); + if (GetMappingsByName(job.Mappings, "EcomAssortments") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortments")); + if (GetMappingsByName(job.Mappings, "EcomAssortmentPermissions") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentPermissions")); + if (GetMappingsByName(job.Mappings, "EcomAssortmentGroupRelations") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentGroupRelations")); + if (GetMappingsByName(job.Mappings, "EcomAssortmentProductRelations") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentProductRelations")); + if (GetMappingsByName(job.Mappings, "EcomAssortmentShopRelations") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentShopRelations")); + if (GetMappingsByName(job.Mappings, "EcomVariantOptionsProductRelation") != null) + tables.AddRange(GetMappingsByName(job.Mappings, "EcomVariantOptionsProductRelation")); + job.Mappings = tables; + } + + internal static IEnumerable GetMappingsByName(MappingCollection collection, string name) + { + return collection.FindAll(map => map.DestinationTable.Name == name); + } + + public override bool RunJob(Job job) + { + ReplaceMappingConditionalsWithValuesFromRequest(job); + if (IsFirstJobRun) + { + OrderTablesInJob(job); } + SqlTransaction sqlTransaction = null; + if (Connection.State.ToString() != "Open") + Connection.Open(); + + Dictionary sourceRow = null; + Exception exception = null; - public override string ValidateDestinationSettings() + try { - if (InsertOnlyNewRecords && UpdateOnlyExistingRecords) - { - return "\"Insert only\" and \"Update only\" options can not be set at the same time"; - } - return ""; - } - public override string ValidateSourceSettings() - { - return null; - } - public new virtual void SaveAsXml(XmlTextWriter xmlTextWriter) - { - xmlTextWriter.WriteElementString("RemoveMissingAfterImport", RemoveMissingAfterImport.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("RemoveMissingAfterImportDestinationTablesOnly", RemoveMissingAfterImportDestinationTablesOnly.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("DeactivateMissingProducts", DeactivateMissingProducts.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("DeleteProductsAndGroupForSpecificLanguage", DeleteProductsAndGroupForSpecificLanguage.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("SqlConnectionString", SqlConnectionString); - xmlTextWriter.WriteElementString("Shop", Shop); - xmlTextWriter.WriteElementString("UserKeyField", UserKeyField); - xmlTextWriter.WriteElementString("GroupsForProductsBy", GroupsForProductsBy); - xmlTextWriter.WriteElementString("GroupsForVariantOptionsBy", GroupsForVariantOptionsBy); - xmlTextWriter.WriteElementString("ManufacturerForProductsBy", ManufacturerForProductsBy); - xmlTextWriter.WriteElementString("RelatedProductGroupsBy", RelatedProductGroupsBy); - xmlTextWriter.WriteElementString("RelatedProductsBy", RelatedProductsBy); - xmlTextWriter.WriteElementString("VariantGroupsForProductsBy", VariantGroupsForProductsBy); - xmlTextWriter.WriteElementString("DefaultLanguage", DefaultLanguage); - xmlTextWriter.WriteElementString("UpdateOnlyExistingProducts", UpdateOnlyExistingProducts.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("UseStrictPrimaryKeyMatching", UseStrictPrimaryKeyMatching.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("RepositoriesIndexUpdate", RepositoriesIndexUpdate); - xmlTextWriter.WriteElementString("UpdateOnlyExistingRecords", UpdateOnlyExistingRecords.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("DeleteIncomingItems", DeleteIncomingItems.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("DiscardDuplicates", DiscardDuplicates.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("HideDeactivatedProducts", HideDeactivatedProducts.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("InsertOnlyNewRecords", InsertOnlyNewRecords.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString("CreateMissingGoups", CreateMissingGoups.ToString(CultureInfo.CurrentCulture)); - xmlTextWriter.WriteElementString(nameof(DisableCacheClearing), DisableCacheClearing.ToString()); - xmlTextWriter.WriteElementString(nameof(UseProductIdFoundByNumber), UseProductIdFoundByNumber.ToString()); - xmlTextWriter.WriteElementString(nameof(IgnoreEmptyCategoryFieldValues), IgnoreEmptyCategoryFieldValues.ToString()); - xmlTextWriter.WriteElementString("SkipFailingRows", SkipFailingRows.ToString(CultureInfo.CurrentCulture)); - GetSchema().SaveAsXml(xmlTextWriter); - } - public override void UpdateSourceSettings(ISource source) - { - EcomProvider newProvider = (EcomProvider)source; - DefaultLanguage = newProvider.DefaultLanguage; - GetGroupNamesForProduct = newProvider.GetGroupNamesForProduct; - GetGroupNamesForVariantOptions = newProvider.GetGroupNamesForVariantOptions; - GetManufacturerNamesForProducts = newProvider.GetManufacturerNamesForProducts; - GetRelatedProductGroupsByName = newProvider.GetRelatedProductGroupsByName; - GetRelatedProductsByName = newProvider.GetRelatedProductsByName; - GetVariantGroupNamesForProduct = newProvider.GetVariantGroupNamesForProduct; - DefaultLanguage = newProvider.DefaultLanguage; - DeactivateMissingProducts = newProvider.DeactivateMissingProducts; - UpdateOnlyExistingProducts = newProvider.UpdateOnlyExistingProducts; - UseStrictPrimaryKeyMatching = newProvider.UseStrictPrimaryKeyMatching; - DeleteProductsAndGroupForSpecificLanguage = newProvider.DeleteProductsAndGroupForSpecificLanguage; - RepositoriesIndexUpdate = newProvider.RepositoriesIndexUpdate; - UpdateOnlyExistingRecords = newProvider.UpdateOnlyExistingRecords; - DeleteIncomingItems = newProvider.DeleteIncomingItems; - DiscardDuplicates = newProvider.DiscardDuplicates; - HideDeactivatedProducts = newProvider.HideDeactivatedProducts; - InsertOnlyNewRecords = newProvider.InsertOnlyNewRecords; - CreateMissingGoups = newProvider.CreateMissingGoups; - DisableCacheClearing = newProvider.DisableCacheClearing; - SkipFailingRows = newProvider.SkipFailingRows; - UseProductIdFoundByNumber = newProvider.UseProductIdFoundByNumber; - IgnoreEmptyCategoryFieldValues = newProvider.IgnoreEmptyCategoryFieldValues; - base.UpdateSourceSettings(source); - } - public override void UpdateDestinationSettings(IDestination destination) - { - ISource newProvider = (ISource)destination; - UpdateSourceSettings(newProvider); - base.UpdateDestinationSettings(destination); - } - public override string Serialize() - { - XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty)); - XElement root = new XElement("Parameters"); - document.Add(root); - root.Add(CreateParameterNode(GetType(), "Get groups for variant options by:", GroupsForVariantOptionsBy)); - root.Add(CreateParameterNode(GetType(), "Get manufacturer for products by:", ManufacturerForProductsBy)); - root.Add(CreateParameterNode(GetType(), "Get variant groups for products by:", VariantGroupsForProductsBy)); - root.Add(CreateParameterNode(GetType(), "Get groups for products by:", GroupsForProductsBy)); - root.Add(CreateParameterNode(GetType(), "Get related products by:", RelatedProductsBy)); - root.Add(CreateParameterNode(GetType(), "Get related product groups by:", RelatedProductGroupsBy)); - root.Add(CreateParameterNode(GetType(), "Default Language", DefaultLanguage)); - root.Add(CreateParameterNode(GetType(), "Deactivate missing products", DeactivateMissingProducts.ToString())); - root.Add(CreateParameterNode(GetType(), "Delete products/groups for languages included in input", DeleteProductsAndGroupForSpecificLanguage.ToString())); - root.Add(Shop == null - ? CreateParameterNode(GetType(), "Shop", "") - : CreateParameterNode(GetType(), "Shop", Shop)); - - root.Add(UserKeyField == null - ? CreateParameterNode(GetType(), "User key field", "") - : CreateParameterNode(GetType(), "User key field", UserKeyField)); - root.Add(CreateParameterNode(GetType(), "Remove missing rows after import", RemoveMissingAfterImport.ToString())); - root.Add(CreateParameterNode(GetType(), "Remove missing rows after import in the destination tables only", RemoveMissingAfterImportDestinationTablesOnly.ToString())); - root.Add(CreateParameterNode(GetType(), "Update only existing products", UpdateOnlyExistingProducts.ToString())); - root.Add(CreateParameterNode(GetType(), "Use strict primary key matching", UseStrictPrimaryKeyMatching.ToString())); - root.Add(CreateParameterNode(GetType(), "Repositories index update", RepositoriesIndexUpdate)); - root.Add(CreateParameterNode(GetType(), "Update only existing records", UpdateOnlyExistingRecords.ToString())); - root.Add(CreateParameterNode(GetType(), "Delete incoming rows", DeleteIncomingItems.ToString())); - root.Add(CreateParameterNode(GetType(), "Discard duplicates", DiscardDuplicates.ToString())); - root.Add(CreateParameterNode(GetType(), "Hide deactivated products", HideDeactivatedProducts.ToString())); - root.Add(CreateParameterNode(GetType(), "Insert only new records", InsertOnlyNewRecords.ToString())); - root.Add(CreateParameterNode(GetType(), "Create missing groups", CreateMissingGoups.ToString())); - root.Add(CreateParameterNode(GetType(), "Disable cache clearing", DisableCacheClearing.ToString())); - root.Add(CreateParameterNode(GetType(), "Persist successful rows and skip failing rows", SkipFailingRows.ToString())); - root.Add(CreateParameterNode(GetType(), "Use existing Product Id found by Number in Variant Products", UseProductIdFoundByNumber.ToString())); - root.Add(CreateParameterNode(GetType(), "Ignore empty category field values", IgnoreEmptyCategoryFieldValues.ToString())); - - return document.ToString(); - } - - public EcomProvider() - { - UseStrictPrimaryKeyMatching = true; - CreateMissingGoups = true; - } - public new ISourceReader GetReader(Mapping mapping) - { - return new EcomSourceReader(mapping, Connection, GetGroupNamesForVariantOptions, GetManufacturerNamesForProducts, GetGroupNamesForProduct, GetVariantGroupNamesForProduct, GetRelatedProductsByName, GetRelatedProductGroupsByName); - } - - protected internal static void OrderTablesInJob(Job job) - { - MappingCollection tables = new MappingCollection(); - if (GetMappingsByName(job.Mappings, "EcomLanguages") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomLanguages")); - if (GetMappingsByName(job.Mappings, "EcomGroups") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomGroups")); - if (GetMappingsByName(job.Mappings, "EcomManufacturers") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomManufacturers")); - if (GetMappingsByName(job.Mappings, "EcomVariantGroups") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomVariantGroups")); - if (GetMappingsByName(job.Mappings, "EcomVariantsOptions") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomVariantsOptions")); - if (GetMappingsByName(job.Mappings, "EcomProducts") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomProducts")); - if (GetMappingsByName(job.Mappings, "EcomProductItems") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomProductItems")); - if (GetMappingsByName(job.Mappings, "EcomProductsRelated") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomProductsRelated")); - if (GetMappingsByName(job.Mappings, "EcomStockUnit") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomStockUnit")); - if (GetMappingsByName(job.Mappings, "EcomDetails") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomDetails")); - if (GetMappingsByName(job.Mappings, "EcomProductCategoryFieldValue") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomProductCategoryFieldValue")); - if (GetMappingsByName(job.Mappings, "EcomPrices") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomPrices")); - if (GetMappingsByName(job.Mappings, "EcomAssortments") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortments")); - if (GetMappingsByName(job.Mappings, "EcomAssortmentPermissions") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentPermissions")); - if (GetMappingsByName(job.Mappings, "EcomAssortmentGroupRelations") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentGroupRelations")); - if (GetMappingsByName(job.Mappings, "EcomAssortmentProductRelations") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentProductRelations")); - if (GetMappingsByName(job.Mappings, "EcomAssortmentShopRelations") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomAssortmentShopRelations")); - if (GetMappingsByName(job.Mappings, "EcomVariantOptionsProductRelation") != null) - tables.AddRange(GetMappingsByName(job.Mappings, "EcomVariantOptionsProductRelation")); - job.Mappings = tables; - } - - internal static IEnumerable GetMappingsByName(MappingCollection collection, string name) - { - return collection.FindAll(map => map.DestinationTable.Name == name); - } - - public override bool RunJob(Job job) - { - ReplaceMappingConditionalsWithValuesFromRequest(job); if (IsFirstJobRun) { - OrderTablesInJob(job); + Writer = new EcomDestinationWriter(job, Connection, DeactivateMissingProducts, null, RemoveMissingAfterImport, Logger, + UpdateOnlyExistingProducts, DefaultLanguage, DiscardDuplicates, PartialUpdate, RemoveMissingAfterImportDestinationTablesOnly, UseStrictPrimaryKeyMatching, + CreateMissingGoups, SkipFailingRows, UseProductIdFoundByNumber, IgnoreEmptyCategoryFieldValues); + if (!string.IsNullOrEmpty(Shop)) + Writer.DefaultShop = Shop; } - SqlTransaction sqlTransaction = null; - if (Connection.State.ToString() != "Open") - Connection.Open(); - - Dictionary sourceRow = null; - bool isReadFromSourceFinished = IsReadingSourceXmlFinished(job); - Exception exception = null; - - try + else { - if (IsFirstJobRun) + if (Writer == null) { - Writer = new EcomDestinationWriter(job, Connection, DeactivateMissingProducts, null, RemoveMissingAfterImport, Logger, - UpdateOnlyExistingProducts, DefaultLanguage, DiscardDuplicates, PartialUpdate, RemoveMissingAfterImportDestinationTablesOnly, UseStrictPrimaryKeyMatching, - CreateMissingGoups, SkipFailingRows, UseProductIdFoundByNumber, IgnoreEmptyCategoryFieldValues); - if (!string.IsNullOrEmpty(Shop)) - Writer.DefaultShop = Shop; - } - else - { - if (Writer == null) - { - throw new Exception($"Can not find EcomDestinationWriter."); - } + throw new Exception($"Can not find Ecom"); } + } - foreach (Mapping mapping in job.Mappings) + foreach (Mapping mapping in job.Mappings) + { + if (mapping.Active && mapping.GetColumnMappings().Count > 0) { - if (mapping.Active && mapping.GetColumnMappings().Count > 0) + Logger.Log("Starting import to temporary table for " + mapping.DestinationTable.Name + "."); + using (var reader = job.Source.GetReader(mapping)) { - Logger.Log("Starting import to temporary table for " + mapping.DestinationTable.Name + "."); - using (var reader = job.Source.GetReader(mapping)) - { - bool? optionValue = mapping.GetOptionValue("DiscardDuplicates"); - bool discardDuplicates = optionValue.HasValue ? optionValue.Value : DiscardDuplicates; - - while (!reader.IsDone()) - { - sourceRow = reader.GetNext(); - ProcessInputRow(mapping, sourceRow); - Writer.Write(sourceRow, mapping, discardDuplicates); - } - Writer.ReportProgress(mapping); - } - if (mapping.DestinationTable.Name == "EcomProducts" && !CreateMissingGoups) + bool? optionValue = mapping.GetOptionValue("DiscardDuplicates"); + bool discardDuplicates = optionValue.HasValue ? optionValue.Value : DiscardDuplicates; + + while (!reader.IsDone()) { - Writer.FailOnMissingGroups(); + sourceRow = reader.GetNext(); + ProcessInputRow(mapping, sourceRow); + Writer.Write(sourceRow, mapping, discardDuplicates); } - Logger.Log("Finished import to temporary table for " + mapping.DestinationTable.Name + "."); - } - } - sourceRow = null; - if (isReadFromSourceFinished) - { - Logger.Log("Starting update products information."); - Writer.UpdateProductRelatedProducts(); - Writer.UpdateVariantFieldsInProducts(); - Writer.UpdateFieldsInExistingProductsWithVariantIDs(); - Writer.UpdateGroupRelations(); - Logger.Log("Update products information finished."); - Writer.FinishWriting(); - sqlTransaction = Connection.BeginTransaction(); - if (DeleteIncomingItems) - { - Writer.DeleteExistingFromMainTable(Shop, sqlTransaction, defaultLanguage); - } - else - { - Writer.MoveDataToMainTables(Shop, sqlTransaction, UpdateOnlyExistingRecords, InsertOnlyNewRecords); - Writer.DeleteExcessFromMainTable(Shop, sqlTransaction, defaultLanguage, DeleteProductsAndGroupForSpecificLanguage, HideDeactivatedProducts); + Writer.ReportProgress(mapping); } - Writer.CleanRelationsTables(sqlTransaction); - sqlTransaction.Commit(); - if (!DisableCacheClearing) + if (mapping.DestinationTable.Name == "EcomProducts" && !CreateMissingGoups) { - Ecommerce.Common.Application.KillAll(); - Ecommerce.Services.Variants.ClearCache(); - Writer.RebuildAssortments(); - } - UpdateProductIndex(job); - if (!DisableCacheClearing) - { - Ecommerce.Services.Discounts.ClearCache(); + Writer.FailOnMissingGroups(); } + Logger.Log("Finished import to temporary table for " + mapping.DestinationTable.Name + "."); } } - catch (Exception ex) + sourceRow = null; + + Logger.Log("Starting update products information."); + Writer.UpdateProductRelatedProducts(); + Writer.UpdateVariantFieldsInProducts(); + Writer.UpdateFieldsInExistingProductsWithVariantIDs(); + Writer.UpdateGroupRelations(); + Logger.Log("Update products information finished."); + Writer.FinishWriting(); + sqlTransaction = Connection.BeginTransaction(); + if (DeleteIncomingItems) { - exception = ex; - string msg = ex.Message; - - LogManager.System.GetLogger(LogCategory.Application, "Dataintegration").Error($"{GetType().Name} error: {ex.Message} Stack: {ex.StackTrace}", ex); + Writer.DeleteExistingFromMainTable(Shop, sqlTransaction, defaultLanguage); + } + else + { + Writer.MoveDataToMainTables(Shop, sqlTransaction, UpdateOnlyExistingRecords, InsertOnlyNewRecords); + Writer.DeleteExcessFromMainTable(Shop, sqlTransaction, defaultLanguage, DeleteProductsAndGroupForSpecificLanguage, HideDeactivatedProducts); + } + Writer.CleanRelationsTables(sqlTransaction); + sqlTransaction.Commit(); + if (!DisableCacheClearing) + { + Ecommerce.Common.Application.KillAll(); + Ecommerce.Services.Variants.ClearCache(); + Writer.RebuildAssortments(); + } + UpdateProductIndex(); + if (!DisableCacheClearing) + { + Ecommerce.Services.Discounts.ClearCache(); + } + } + catch (Exception ex) + { + exception = ex; + string msg = ex.Message; - if (ex.Message.Contains("Subquery returned more than 1 value")) - msg += System.Environment.NewLine + "When using Ecom Provider and no ProductID is given, product is next recognized on ProductNumber. This error usually indicates duplicates on column ProductNumber."; + LogManager.System.GetLogger(LogCategory.Application, "Dataintegration").Error($"{GetType().Name} error: {ex.Message} Stack: {ex.StackTrace}", ex); - if (ex.Message.Contains("Bulk copy failures")) - { - Logger.Log("Job Failed with the following message:"); - BulkCopyHelper.LogFailedRows(Logger, msg); - } - else if (ex.Message.Contains(EcomDestinationWriter.EcomProductsMissingGroupsErrorMessage) && Writer != null) - { - Logger.Log("Job Failed with the following message:"); - Writer.LogFailedRows(); - } - else - { - if (sourceRow != null) - msg += GetFailedSourceRowMessage(sourceRow); - Logger.Log("Job Failed with the following message: " + msg); - } + if (ex.Message.Contains("Subquery returned more than 1 value")) + msg += System.Environment.NewLine + "When using Ecom Provider and no ProductID is given, product is next recognized on ProductNumber. This error usually indicates duplicates on column ProductNumber."; - if (sqlTransaction != null) - sqlTransaction.Rollback(); - return false; + if (ex.Message.Contains("Bulk copy failures")) + { + Logger.Log("Job Failed with the following message:"); + BulkCopyHelper.LogFailedRows(Logger, msg); } - finally + else if (ex.Message.Contains(EcomDestinationWriter.EcomProductsMissingGroupsErrorMessage) && Writer != null) { - if (exception != null || isReadFromSourceFinished) - { - if (Writer != null) - Writer.Close(); - } - sourceRow = null; + Logger.Log("Job Failed with the following message:"); + Writer.LogFailedRows(); } - if (IsFirstJobRun) + else { - IsFirstJobRun = false; + if (sourceRow != null) + msg += GetFailedSourceRowMessage(sourceRow); + Logger.Log("Job Failed with the following message: " + msg); } - return true; - } - public new Hashtable GetOptions(string name) + if (sqlTransaction != null) + sqlTransaction.Rollback(); + return false; + } + finally { - var options = new Hashtable(); - switch (name) + if (exception != null) { - case "Default Language": - options = base.GetOptions(name); - break; - case "Shop": - options = base.GetOptions(name); - break; - case "Product index update": - options = base.GetOptions(name); - break; - case "Repositories index update": - options = GetRepositoriesIndexes(); - break; - default: - options.Add("Name", "Name"); - options.Add("ID", "ID"); - break; + Writer?.Close(); } - return options; } + if (IsFirstJobRun) + { + IsFirstJobRun = false; + } + return true; + } + + protected void UpdateProductIndex() + { + if (string.IsNullOrEmpty(RepositoriesIndexUpdate)) + return; + + UpdateIndexes(RepositoriesIndexUpdate.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()); + } + + public override void Close() + { + Connection.Close(); + } + + public IEnumerable GetParameterOptions(string parameterName) + { + return parameterName switch + { + "Default Language" => GetDefaultLanguageOptions(), + "Shop" => GetShopOptions(), + "Product index update" => new List() + { + new("Full", "Full"), + new("Partial", "Partial") + }, + "Repositories index update" => GetRepositoryIndexOptions(), + _ => new List() + { + new("Name", "Name"), + new("ID", "ID") + }, + }; + } + + private IEnumerable GetShopOptions() + { + var options = new List(); + var sqlCommand = GetOpenConnection(); + var languagesDataAdapter = new SqlDataAdapter("SELECT ShopID, ShopName FROM EcomShops", sqlCommand.Connection); + _ = new SqlCommandBuilder(languagesDataAdapter); + var dataSet = new DataSet(); + languagesDataAdapter.Fill(dataSet); + foreach (DataRow row in dataSet.Tables[0].Rows) + { + options.Add(new(row["ShopName"].ToString(), row["shopID"])); + } + return options; + } + + private IEnumerable GetDefaultLanguageOptions() + { + var options = new List(); + var sqlCommand = GetOpenConnection(); + var languagesDataAdapter = new SqlDataAdapter("SELECT LanguageID, LanguageCode2, LanguageName FROM EcomLanguages", sqlCommand.Connection); + _ = new SqlCommandBuilder(languagesDataAdapter); + var languageDataSet = new DataSet(); + languagesDataAdapter.Fill(languageDataSet); + foreach (DataRow row in languageDataSet.Tables[0].Rows) + { + options.Add(new(row["LanguageName"].ToString(), row["LanguageID"])); + } + return options; + } + + private SqlCommand GetOpenConnection() + { + SqlCommand sqlCommand = new() { Connection = Connection }; + if (Connection.State == ConnectionState.Closed) + Connection.Open(); + return sqlCommand; } } diff --git a/src/EcomSourceReader.cs b/src/EcomSourceReader.cs index beeb304..4bba5eb 100644 --- a/src/EcomSourceReader.cs +++ b/src/EcomSourceReader.cs @@ -6,209 +6,191 @@ using System.Data.SqlClient; using System.Linq; -namespace Dynamicweb.DataIntegration.Providers.EcomProvider +namespace Dynamicweb.DataIntegration.Providers.EcomProvider; + +class EcomSourceReader : BaseSqlReader, ISourceReader { - class EcomSourceReader : ISourceReader - { - protected SqlCommand _command; - protected SqlDataReader _reader; - protected Mapping _mapping; + private Dictionary Columns = new Dictionary(); + private Dictionary CategoriesColumns = new Dictionary(); - private Dictionary Columns = new Dictionary(); - private Dictionary CategoriesColumns = new Dictionary(); + protected new string GetDistinctColumnsFromMapping() + { + return GetDistinctColumnsFromMapping(new HashSet { }); + } - private void LoadReaderFromDatabase() + protected string GetDistinctColumnsFromMapping(HashSet columnsToSkip) + { + var result = string.Empty; + foreach (var mapping in Columns.Keys) { - try - { - ColumnMappingCollection columnmappings = _mapping.GetColumnMappings(); - if (columnmappings.Count == 0) - return; - string columns = GetColumns(); - string fromTables = GetFromTables(); - string sql = "select * from (select " + columns + " from " + fromTables + ") as result"; - - List parameters = new List(); - string conditionalsSql = SqlProvider.MappingExtensions.GetConditionalsSql(out parameters, _mapping.Conditionals, false, false); - if (conditionalsSql != "") - { - conditionalsSql = conditionalsSql.Substring(0, conditionalsSql.Length - 4); - sql = sql + " where " + conditionalsSql; - foreach (SqlParameter p in parameters) - _command.Parameters.Add(p); - } - _command.CommandText = sql; - _reader = _command.ExecuteReader(); - } - catch (Exception ex) + if (columnsToSkip.Count < 1 || !columnsToSkip.Contains(mapping)) { - throw new Exception("Failed to open sqlSourceReader. Reason: " + ex.Message, ex); + result += "[" + mapping + "], "; } } + return result; + } + protected new string GetColumnsFromMappingConditions() + { + return GetColumnsFromMappingConditions(new HashSet()); + } - protected string GetDistinctColumnsFromMapping() - { - return GetDistinctColumnsFromMapping(new HashSet { }); - } - - protected string GetDistinctColumnsFromMapping(HashSet columnsToSkip) + protected string GetColumnsFromMappingConditions(HashSet columnsToSkip) + { + string ret = string.Empty; + if (mapping.Conditionals.Count > 0) { - var result = string.Empty; - foreach (var mapping in Columns.Keys) + foreach (MappingConditional mc in mapping.Conditionals.Where(mc => mc != null && mc.SourceColumn != null).GroupBy(g => new { g.SourceColumn.Name }).Select(g => g.First())) { - if (columnsToSkip.Count < 1 || !columnsToSkip.Contains(mapping)) + if (!columnsToSkip.Contains(mc.SourceColumn.Name.ToLower()) && !Columns.ContainsKey(mc.SourceColumn.Name.ToLower())) { - result += "[" + mapping + "], "; + ret += "[" + mc.SourceColumn.Name + "], "; } } - return result; } + return ret; + } - protected string GetColumnsFromMappingConditions() + protected bool IsColumnUsedInMappingConditions(string columnName) + { + return mapping.Conditionals.Any(mc => string.Compare(mc.SourceColumn?.Name, columnName, true) == 0); + } + + /// + /// base implementation, + /// + /// + public virtual new Dictionary GetNext() + { + var rowValues = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var column in Columns.Keys) { - return GetColumnsFromMappingConditions(new HashSet()); + rowValues[column] = _reader[column]; } + return rowValues; + } - protected string GetColumnsFromMappingConditions(HashSet columnsToSkip) + public EcomSourceReader(Mapping mapping, SqlConnection connection, bool getGroupNamesForVariantOptions, bool getManufacturerNamesForProducts, bool getGroupNamesForProduct, bool getVariantGroupNamesForProduct, bool getRelatedProductsByName, bool getRelatedProductGroupsByName) : base(mapping, connection) + { + this.getGroupNamesForVariantOptions = getGroupNamesForVariantOptions; + this.getRelatedProductGroupsByName = getRelatedProductGroupsByName; + this.getRelatedProductsByName = getRelatedProductsByName; + this.getVariantGroupNamesForProduct = getVariantGroupNamesForProduct; + this.getGroupNamesForProduct = getGroupNamesForProduct; + this.getManufacturerNamesForProducts = getManufacturerNamesForProducts; + DoInitialization(mapping, connection); + } + + private readonly bool getGroupNamesForVariantOptions; + private readonly bool getManufacturerNamesForProducts; + private readonly bool getGroupNamesForProduct; + private readonly bool getVariantGroupNamesForProduct; + private readonly bool getRelatedProductsByName; + private readonly bool getRelatedProductGroupsByName; + + protected void DoInitialization(Mapping mapping, SqlConnection connection) + { + foreach (var columnMapping in mapping.GetColumnMappings()) { - string ret = string.Empty; - if (_mapping.Conditionals.Count > 0) + if (columnMapping.SourceColumn != null) { - foreach (MappingConditional mc in _mapping.Conditionals.Where(mc => mc != null && mc.SourceColumn != null).GroupBy(g => new { g.SourceColumn.Name }).Select(g => g.First())) + if (!Columns.ContainsKey(columnMapping.SourceColumn.Name.ToLower())) + { + Columns.Add(columnMapping.SourceColumn.Name.ToLower(), columnMapping); + } + if (columnMapping.SourceColumn.Name.StartsWith("ProductCategory|")) { - if (!columnsToSkip.Contains(mc.SourceColumn.Name.ToLower()) && !Columns.ContainsKey(mc.SourceColumn.Name.ToLower())) + if (!CategoriesColumns.ContainsKey(columnMapping.SourceColumn.Name.ToLower())) { - ret += "[" + mc.SourceColumn.Name + "], "; + CategoriesColumns.Add(columnMapping.SourceColumn.Name.ToLower(), columnMapping); } } } - return ret; } + this.mapping = mapping; + _command = new SqlCommand { Connection = connection }; - protected bool IsColumnUsedInMappingConditions(string columnName) - { - return _mapping.Conditionals.Any(mc => string.Compare(mc.SourceColumn?.Name, columnName, true) == 0); - } + int _commandtimeout = Dynamicweb.Configuration.SystemConfiguration.Instance.Contains("/Globalsettings/Settings/DataIntegration/SQLSourceCommandTimeout") ? + Converter.ToInt32(Dynamicweb.Configuration.SystemConfiguration.Instance.GetValue("/Globalsettings/Settings/DataIntegration/SQLSourceCommandTimeout")) : + Converter.ToInt32(Dynamicweb.Configuration.SystemConfiguration.Instance.GetValue("/Globalsettings/DataIntegration/SQLSourceCommandTimeout")); + if (_commandtimeout > 0) + _command.CommandTimeout = _commandtimeout; - public virtual bool IsDone() - { - if (_reader.Read()) - return false; - _reader.Close(); - return true; - } + if (connection.State.ToString() != "Open") + connection.Open(); + LoadReaderFromDatabase(); + } - /// - /// base implementation, - /// - /// - public virtual Dictionary GetNext() + private void LoadReaderFromDatabase() + { + try { - var rowValues = new Dictionary(StringComparer.OrdinalIgnoreCase); - foreach (var column in Columns.Keys) + ColumnMappingCollection columnmappings = mapping.GetColumnMappings(); + if (columnmappings.Count == 0) + return; + string columns = GetColumns(); + string fromTables = GetFromTables(); + string sql = "select * from (select " + columns + " from " + fromTables + ") as result"; + + List parameters = new List(); + string conditionalsSql = MappingExtensions.GetConditionalsSql(out parameters, mapping.Conditionals, false, false); + if (conditionalsSql != "") { - rowValues[column] = _reader[column]; + conditionalsSql = conditionalsSql.Substring(0, conditionalsSql.Length - 4); + sql = sql + " where " + conditionalsSql; + foreach (SqlParameter p in parameters) + _command.Parameters.Add(p); } - return rowValues; - } - - public void Dispose() - { - _reader.Close(); + _command.CommandText = sql; + _reader = _command.ExecuteReader(); } - - public EcomSourceReader(Mapping mapping, SqlConnection connection, bool getGroupNamesForVariantOptions, bool getManufacturerNamesForProducts, bool getGroupNamesForProduct, bool getVariantGroupNamesForProduct, bool getRelatedProductsByName, bool getRelatedProductGroupsByName) + catch (Exception ex) { - this.getGroupNamesForVariantOptions = getGroupNamesForVariantOptions; - this.getRelatedProductGroupsByName = getRelatedProductGroupsByName; - this.getRelatedProductsByName = getRelatedProductsByName; - this.getVariantGroupNamesForProduct = getVariantGroupNamesForProduct; - this.getGroupNamesForProduct = getGroupNamesForProduct; - this.getManufacturerNamesForProducts = getManufacturerNamesForProducts; - DoInitialization(mapping, connection); + throw new Exception("Failed to open sqlSourceReader. Reason: " + ex.Message, ex); } + } - private readonly bool getGroupNamesForVariantOptions; - private readonly bool getManufacturerNamesForProducts; - private readonly bool getGroupNamesForProduct; - private readonly bool getVariantGroupNamesForProduct; - private readonly bool getRelatedProductsByName; - private readonly bool getRelatedProductGroupsByName; + protected new string GetFromTables() + { + string result = "[" + mapping.SourceTable.SqlSchema + "].[" + mapping.SourceTable.Name + + "] as outer" + mapping.SourceTable.Name; - protected void DoInitialization(Mapping mapping, SqlConnection connection) + switch (mapping.SourceTable.Name) { - foreach (var columnMapping in mapping.GetColumnMappings()) - { - if (columnMapping.SourceColumn != null) + case "EcomGroups": + if (Columns.ContainsKey("GroupLanguageID".ToLower()) || IsColumnUsedInMappingConditions("GroupLanguageID")) { - if (!Columns.ContainsKey(columnMapping.SourceColumn.Name.ToLower())) - { - Columns.Add(columnMapping.SourceColumn.Name.ToLower(), columnMapping); - } - if (columnMapping.SourceColumn.Name.StartsWith("ProductCategory|")) - { - if (!CategoriesColumns.ContainsKey(columnMapping.SourceColumn.Name.ToLower())) - { - CategoriesColumns.Add(columnMapping.SourceColumn.Name.ToLower(), columnMapping); - } - } + result = result + " join EcomLanguages on GroupLanguageID=LanguageID"; } - } - this._mapping = mapping; - _command = new SqlCommand { Connection = connection }; - - int _commandtimeout = Dynamicweb.Configuration.SystemConfiguration.Instance.Contains("/Globalsettings/Settings/DataIntegration/SQLSourceCommandTimeout") ? - Converter.ToInt32(Dynamicweb.Configuration.SystemConfiguration.Instance.GetValue("/Globalsettings/Settings/DataIntegration/SQLSourceCommandTimeout")) : - Converter.ToInt32(Dynamicweb.Configuration.SystemConfiguration.Instance.GetValue("/Globalsettings/DataIntegration/SQLSourceCommandTimeout")); - if (_commandtimeout > 0) - _command.CommandTimeout = _commandtimeout; - - if (connection.State.ToString() != "Open") - connection.Open(); - LoadReaderFromDatabase(); - } - - protected string GetFromTables() - { - string result = "[" + _mapping.SourceTable.SqlSchema + "].[" + _mapping.SourceTable.Name + - "] as outer" + _mapping.SourceTable.Name; - - switch (_mapping.SourceTable.Name) - { - case "EcomGroups": - if (Columns.ContainsKey("GroupLanguageID".ToLower()) || IsColumnUsedInMappingConditions("GroupLanguageID")) - { - result = result + " join EcomLanguages on GroupLanguageID=LanguageID"; - } - break; - case "EcomVariantGroups": - if (Columns.ContainsKey("VariantGroupLanguageID".ToLower()) || IsColumnUsedInMappingConditions("VariantGroupLanguageID")) - { - result = result + " join EcomLanguages on VariantGroupLanguageID=LanguageID"; - } - break; - case "EcomVariantsOptions": - result = result + " join EcomLanguages on VariantOptionLanguageID=LanguageID"; - if (getGroupNamesForVariantOptions && Columns.ContainsKey("VariantOptionGroupID".ToLower())) - { + break; + case "EcomVariantGroups": + if (Columns.ContainsKey("VariantGroupLanguageID".ToLower()) || IsColumnUsedInMappingConditions("VariantGroupLanguageID")) + { + result = result + " join EcomLanguages on VariantGroupLanguageID=LanguageID"; + } + break; + case "EcomVariantsOptions": + result = result + " join EcomLanguages on VariantOptionLanguageID=LanguageID"; + if (getGroupNamesForVariantOptions && Columns.ContainsKey("VariantOptionGroupID".ToLower())) + { - result = result + " left join EcomVariantGroups on VariantOptionGroupID=VariantGroupID and VariantOptionLanguageID=VariantGroupLanguageID"; - } - break; - case "EcomProducts": - if (Columns.ContainsKey("ProductLanguageID".ToLower()) || IsColumnUsedInMappingConditions("ProductLanguageID")) - { - result = result + " join EcomLanguages on ProductLanguageID=LanguageID"; - } - if (getManufacturerNamesForProducts && Columns.ContainsKey("ProductManufacturerID".ToLower())) - { - result = result + " left join EcomManufacturers on productManufacturerID=ManufacturerID"; - } + result = result + " left join EcomVariantGroups on VariantOptionGroupID=VariantGroupID and VariantOptionLanguageID=VariantGroupLanguageID"; + } + break; + case "EcomProducts": + if (Columns.ContainsKey("ProductLanguageID".ToLower()) || IsColumnUsedInMappingConditions("ProductLanguageID")) + { + result = result + " join EcomLanguages on ProductLanguageID=LanguageID"; + } + if (getManufacturerNamesForProducts && Columns.ContainsKey("ProductManufacturerID".ToLower())) + { + result = result + " left join EcomManufacturers on productManufacturerID=ManufacturerID"; + } - if (CategoriesColumns.Keys.Any()) - { - result = result + string.Format(@" left join + if (CategoriesColumns.Keys.Any()) + { + result = result + string.Format(@" left join (SELECT FieldValueProductId, FieldValueProductLanguageId, FieldValueProductVariantId, {0} FROM ( @@ -221,202 +203,218 @@ FROM [EcomProductCategoryFieldValue] ON ProductId = FieldValueProductId AND ProductLanguageId = FieldValueProductLanguageId AND ProductVariantId = FieldValueProductVariantId ", string.Join(",", CategoriesColumns.Keys.Select(x => $"[{x}]"))); - } + } - break; - case "EcomProductsRelated": - if (getRelatedProductsByName) - result = result + - " join EcomProducts as source on ProductRelatedProductID=source.productID join EcomProducts as destination on ProductRelatedProductRelID=destination.productID"; - result = result + " join EcomProductsRelatedGroups on ProductRelatedGroupID=RelatedGroupID join EcomLanguages on LanguageID = RelatedGroupLanguageId"; - break; - case "EcomProductCategoryFieldValue": + break; + case "EcomProductsRelated": + if (getRelatedProductsByName) result = result + - " join EcomProducts on FieldValueProductId=productID and FieldValueProductVariantId=productVariantID and FieldValueProductLanguageId=productLanguageID "; - break; - case "EcomAssortmentPermissions": - if (Columns.ContainsKey("AssortmentPermissionAccessUserID".ToLower()) || - Columns.ContainsKey("AssortmentPermissionCustomerNumber".ToLower()) || - Columns.ContainsKey("AssortmentPermissionExternalID".ToLower())) - { - result = result + " join AccessUser on AssortmentPermissionAccessUserID=AccessUserID"; - } - break; - default: - result = "[" + _mapping.SourceTable.SqlSchema + "].[" + _mapping.SourceTable.Name + "]"; - if (_mapping.SourceTable != null && _mapping.SourceTable.Name == "EcomAssortmentPermissions" && - (_mapping.GetColumnMappings().Find(cm => cm.SourceColumn != null && cm.SourceColumn.Name.ToLower() == "AssortmentPermissionAccessUserID".ToLower()) != null || - _mapping.GetColumnMappings().Find(cm => cm.SourceColumn != null && cm.SourceColumn.Name.ToLower() == "AssortmentPermissionCustomerNumber".ToLower()) != null || - _mapping.GetColumnMappings().Find(cm => cm.SourceColumn != null && cm.SourceColumn.Name.ToLower() == "AssortmentPermissionExternalID".ToLower()) != null)) - { - result = "[" + _mapping.SourceTable.SqlSchema + "].[" + _mapping.SourceTable.Name + "] as outer" + _mapping.SourceTable.Name; - result = result + " join AccessUser on AssortmentPermissionAccessUserID=AccessUserID"; - } - break; - } - return result; + " join EcomProducts as source on ProductRelatedProductID=source.productID join EcomProducts as destination on ProductRelatedProductRelID=destination.productID"; + result = result + " join EcomProductsRelatedGroups on ProductRelatedGroupID=RelatedGroupID join EcomLanguages on LanguageID = RelatedGroupLanguageId"; + break; + case "EcomProductCategoryFieldValue": + result = result + + " join EcomProducts on FieldValueProductId=productID and FieldValueProductVariantId=productVariantID and FieldValueProductLanguageId=productLanguageID "; + break; + case "EcomAssortmentPermissions": + if (Columns.ContainsKey("AssortmentPermissionAccessUserID".ToLower()) || + Columns.ContainsKey("AssortmentPermissionCustomerNumber".ToLower()) || + Columns.ContainsKey("AssortmentPermissionExternalID".ToLower())) + { + result = result + " join AccessUser on AssortmentPermissionAccessUserID=AccessUserID"; + } + break; + default: + result = "[" + mapping.SourceTable.SqlSchema + "].[" + mapping.SourceTable.Name + "]"; + if (mapping.SourceTable != null && mapping.SourceTable.Name == "EcomAssortmentPermissions" && + (mapping.GetColumnMappings().Find(cm => cm.SourceColumn != null && cm.SourceColumn.Name.ToLower() == "AssortmentPermissionAccessUserID".ToLower()) != null || + mapping.GetColumnMappings().Find(cm => cm.SourceColumn != null && cm.SourceColumn.Name.ToLower() == "AssortmentPermissionCustomerNumber".ToLower()) != null || + mapping.GetColumnMappings().Find(cm => cm.SourceColumn != null && cm.SourceColumn.Name.ToLower() == "AssortmentPermissionExternalID".ToLower()) != null)) + { + result = "[" + mapping.SourceTable.SqlSchema + "].[" + mapping.SourceTable.Name + "] as outer" + mapping.SourceTable.Name; + result = result + " join AccessUser on AssortmentPermissionAccessUserID=AccessUserID"; + } + break; } + return result; + } - protected string GetColumns() + protected new string GetColumns() + { + string result = ""; + switch (mapping.SourceTable.Name) { - string result = ""; - switch (_mapping.SourceTable.Name) - { - case "EcomGroups": - var ecomGroupfieldsToSkip = new HashSet { "shops", "grouplanguageid", "parentgroups", "shopsorting", "parentgroupssorting" }; - result = GetDistinctColumnsFromMapping(ecomGroupfieldsToSkip); - if (Columns.ContainsKey("shops") || IsColumnUsedInMappingConditions("Shops")) - { - result = result + "STUFF((SELECT ',\"'+ ShopGroupShopID +'\"' FROM ecomgroups JOIN EcomShopGroupRelation on groupid=ShopGroupGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as Shops, "; - } - if (Columns.ContainsKey("shopsorting") || IsColumnUsedInMappingConditions("ShopSorting")) - { - result = result + "STUFF((SELECT ',\"'+ convert(nvarchar,ShopGroupRelationsSorting) +'\"' FROM ecomgroups JOIN EcomShopGroupRelation on groupid=ShopGroupGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as ShopSorting, "; - } - if ((Columns.ContainsKey("grouplanguageid")) || IsColumnUsedInMappingConditions("GroupLanguageID")) - { - result = result + "[LanguageID] as GroupLanguageID, "; - } - if (Columns.ContainsKey("parentgroups") || IsColumnUsedInMappingConditions("ParentGroups")) - { - result = result + "STUFF((SELECT ',\"'+ GroupRelationsParentID+'\"' FROM ecomgroups JOIN EcomGroupRelations on groupid=GroupRelationsGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as ParentGroups, "; - } - if (Columns.ContainsKey("parentgroupssorting") || IsColumnUsedInMappingConditions("ParentGroupsSorting")) - { - result = result + "STUFF((SELECT ',\"'+ convert(nvarchar,GroupRelationsSorting)+'\"' FROM ecomgroups JOIN EcomGroupRelations on groupid=GroupRelationsGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as ParentGroupsSorting, "; - } - result += GetColumnsFromMappingConditions(ecomGroupfieldsToSkip); - break; - case "EcomVariantGroups": - var ecomVariantGroupfieldsToSkip = new HashSet { "variantgrouplanguageid" }; - result = GetDistinctColumnsFromMapping(ecomVariantGroupfieldsToSkip); + case "EcomGroups": + var ecomGroupfieldsToSkip = new HashSet { "shops", "grouplanguageid", "parentgroups", "shopsorting", "parentgroupssorting" }; + result = GetDistinctColumnsFromMapping(ecomGroupfieldsToSkip); + if (Columns.ContainsKey("shops") || IsColumnUsedInMappingConditions("Shops")) + { + result = result + "STUFF((SELECT ',\"'+ ShopGroupShopID +'\"' FROM ecomgroups JOIN EcomShopGroupRelation on groupid=ShopGroupGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as Shops, "; + } + if (Columns.ContainsKey("shopsorting") || IsColumnUsedInMappingConditions("ShopSorting")) + { + result = result + "STUFF((SELECT ',\"'+ convert(nvarchar,ShopGroupRelationsSorting) +'\"' FROM ecomgroups JOIN EcomShopGroupRelation on groupid=ShopGroupGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as ShopSorting, "; + } + if ((Columns.ContainsKey("grouplanguageid")) || IsColumnUsedInMappingConditions("GroupLanguageID")) + { + result = result + "[LanguageID] as GroupLanguageID, "; + } + if (Columns.ContainsKey("parentgroups") || IsColumnUsedInMappingConditions("ParentGroups")) + { + result = result + "STUFF((SELECT ',\"'+ GroupRelationsParentID+'\"' FROM ecomgroups JOIN EcomGroupRelations on groupid=GroupRelationsGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as ParentGroups, "; + } + if (Columns.ContainsKey("parentgroupssorting") || IsColumnUsedInMappingConditions("ParentGroupsSorting")) + { + result = result + "STUFF((SELECT ',\"'+ convert(nvarchar,GroupRelationsSorting)+'\"' FROM ecomgroups JOIN EcomGroupRelations on groupid=GroupRelationsGroupID WHERE GroupID=outerEcomGroups.GroupID FOR XML PATH('')),1,1,'') as ParentGroupsSorting, "; + } + result += GetColumnsFromMappingConditions(ecomGroupfieldsToSkip); + break; + case "EcomVariantGroups": + var ecomVariantGroupfieldsToSkip = new HashSet { "variantgrouplanguageid" }; + result = GetDistinctColumnsFromMapping(ecomVariantGroupfieldsToSkip); - if ((Columns.ContainsKey("VariantGroupLanguageID".ToLower())) || - IsColumnUsedInMappingConditions("VariantGroupLanguageID")) - { - result = result + "[LanguageID] as VariantGroupLanguageID, "; - } - result += GetColumnsFromMappingConditions(ecomVariantGroupfieldsToSkip); - break; - case "EcomVariantsOptions": - var ecomVariantsOptionsfieldsToSkip = new HashSet { "variantoptionlanguageid", "variantoptiongroupid" }; - result = GetDistinctColumnsFromMapping(ecomVariantsOptionsfieldsToSkip); - if ((Columns.ContainsKey("VariantOptionLanguageID".ToLower())) || - IsColumnUsedInMappingConditions("VariantOptionLanguageID")) - { - result = result + "[LanguageID] as VariantOptionLanguageID, "; - } - if ((Columns.ContainsKey("VariantOptionGroupID".ToLower())) || - IsColumnUsedInMappingConditions("VariantOptionGroupID")) - { - if (getGroupNamesForVariantOptions) - result = result + "[VariantGroupName] as VariantOptionGroupID, "; - else - { - result = result + "[VariantOptionGroupID], "; - } - } - result += GetColumnsFromMappingConditions(ecomVariantsOptionsfieldsToSkip); - break; - case "EcomProducts": - var ecomProductfieldsToSkip = new HashSet { "groups", "groupsorting", "primarygroup", "variantgroups", "productlanguageid", "productmanufacturerid", "variantoptions", "relatedproducts" }; - result = GetDistinctColumnsFromMapping(ecomProductfieldsToSkip); - if (Columns.ContainsKey("Groups".ToLower()) || IsColumnUsedInMappingConditions("Groups")) - { - if (getGroupNamesForProduct) - result = result + " STUFF((SELECT ',\"'+ groupname +'\"' FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as Groups, "; - else - result = result + "STUFF((SELECT ',\"'+ GroupID +'\"' FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as Groups, "; - } - if (Columns.ContainsKey("GroupSorting".ToLower()) || IsColumnUsedInMappingConditions("GroupSorting")) + if ((Columns.ContainsKey("VariantGroupLanguageID".ToLower())) || + IsColumnUsedInMappingConditions("VariantGroupLanguageID")) + { + result = result + "[LanguageID] as VariantGroupLanguageID, "; + } + result += GetColumnsFromMappingConditions(ecomVariantGroupfieldsToSkip); + break; + case "EcomVariantsOptions": + var ecomVariantsOptionsfieldsToSkip = new HashSet { "variantoptionlanguageid", "variantoptiongroupid" }; + result = GetDistinctColumnsFromMapping(ecomVariantsOptionsfieldsToSkip); + if ((Columns.ContainsKey("VariantOptionLanguageID".ToLower())) || + IsColumnUsedInMappingConditions("VariantOptionLanguageID")) + { + result = result + "[LanguageID] as VariantOptionLanguageID, "; + } + if ((Columns.ContainsKey("VariantOptionGroupID".ToLower())) || + IsColumnUsedInMappingConditions("VariantOptionGroupID")) + { + if (getGroupNamesForVariantOptions) + result = result + "[VariantGroupName] as VariantOptionGroupID, "; + else { - result = result + "STUFF((SELECT ',\"'+ convert(nvarchar,GroupProductRelationSorting)+'\"' FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as GroupSorting, "; + result = result + "[VariantOptionGroupID], "; } - if (Columns.ContainsKey("PrimaryGroup".ToLower()) || IsColumnUsedInMappingConditions("PrimaryGroup")) + } + result += GetColumnsFromMappingConditions(ecomVariantsOptionsfieldsToSkip); + break; + case "EcomProducts": + var ecomProductfieldsToSkip = new HashSet { "groups", "groupsorting", "primarygroup", "variantgroups", "productlanguageid", "productmanufacturerid", "variantoptions", "relatedproducts" }; + result = GetDistinctColumnsFromMapping(ecomProductfieldsToSkip); + if (Columns.ContainsKey("Groups".ToLower()) || IsColumnUsedInMappingConditions("Groups")) + { + if (getGroupNamesForProduct) + result = result + " STUFF((SELECT ',\"'+ groupname +'\"' FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as Groups, "; + else + result = result + "STUFF((SELECT ',\"'+ GroupID +'\"' FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as Groups, "; + } + if (Columns.ContainsKey("GroupSorting".ToLower()) || IsColumnUsedInMappingConditions("GroupSorting")) + { + result = result + "STUFF((SELECT ',\"'+ convert(nvarchar,GroupProductRelationSorting)+'\"' FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as GroupSorting, "; + } + if (Columns.ContainsKey("PrimaryGroup".ToLower()) || IsColumnUsedInMappingConditions("PrimaryGroup")) + { + result = result + "(SELECT TOP(1) GroupID FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID and GroupProductRelationIsPrimary=1) as PrimaryGroup, "; + } + if (Columns.ContainsKey("VariantGroups".ToLower()) || IsColumnUsedInMappingConditions("VariantGroups")) + { + if (getVariantGroupNamesForProduct) { - result = result + "(SELECT TOP(1) GroupID FROM EcomProducts JOIN EcomGroupProductRelation on ProductID=GroupProductRelationProductID JOIN ecomGroups on GroupID=GroupProductRelationGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and GroupLanguageID=ProductLanguageID and GroupProductRelationIsPrimary=1) as PrimaryGroup, "; + result = result + "STUFF((SELECT ',\"'+ VariantGroupName +'\"' FROM EcomProducts JOIN EcomVariantgroupProductRelation on ProductID=VariantgroupProductRelationProductID JOIN EcomVariantGroups on VariantGroupID=VariantgroupProductRelationVariantGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and variantGroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as VariantGroups, "; } - if (Columns.ContainsKey("VariantGroups".ToLower()) || IsColumnUsedInMappingConditions("VariantGroups")) + else { - if (getVariantGroupNamesForProduct) - { - result = result + "STUFF((SELECT ',\"'+ VariantGroupName +'\"' FROM EcomProducts JOIN EcomVariantgroupProductRelation on ProductID=VariantgroupProductRelationProductID JOIN EcomVariantGroups on VariantGroupID=VariantgroupProductRelationVariantGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and variantGroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as VariantGroups, "; - } - else - { - result = result + "STUFF((SELECT ',\"'+ VariantGroupid +'\"' FROM EcomProducts JOIN EcomVariantgroupProductRelation on ProductID=VariantgroupProductRelationProductID JOIN EcomVariantGroups on VariantGroupID=VariantgroupProductRelationVariantGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and variantGroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as VariantGroups, "; - } + result = result + "STUFF((SELECT ',\"'+ VariantGroupid +'\"' FROM EcomProducts JOIN EcomVariantgroupProductRelation on ProductID=VariantgroupProductRelationProductID JOIN EcomVariantGroups on VariantGroupID=VariantgroupProductRelationVariantGroupID WHERE ProductID=outerEcomProducts.ProductID and ProductLanguageID=outerEcomProducts.ProductLanguageID and ProductVariantID=outerEcomProducts.ProductVariantID and variantGroupLanguageID=ProductLanguageID FOR XML PATH('')),1,1,'') as VariantGroups, "; } - if ((Columns.ContainsKey("ProductLanguageID".ToLower())) || IsColumnUsedInMappingConditions("ProductLanguageID")) - { - result = result + "[LanguageID] as ProductLanguageID, "; + } + if ((Columns.ContainsKey("ProductLanguageID".ToLower())) || IsColumnUsedInMappingConditions("ProductLanguageID")) + { + result = result + "[LanguageID] as ProductLanguageID, "; - } - if ((Columns.ContainsKey("ProductManufacturerID".ToLower())) || IsColumnUsedInMappingConditions("ProductManufacturerID")) - { - if (getManufacturerNamesForProducts) - { - result = result + "isnull([ManufacturerName],'') as ProductManufacturerId, "; - } - else - { - result = result + "[ProductManufacturerId], "; - } - } - if (Columns.ContainsKey("VariantOptions".ToLower()) || IsColumnUsedInMappingConditions("VariantOptions")) - { - result = result + "STUFF((SELECT ',\"'+ DistinctVariants.VariantOptionsProductRelationVariantID +'\"' FROM "; - result += "(SELECT DISTINCT VariantOptionsProductRelationVariantID FROM EcomProducts JOIN EcomVariantOptionsProductRelation on ProductID=VariantOptionsProductRelationProductID WHERE ProductID=outerEcomProducts.ProductID) DistinctVariants FOR XML PATH('')),1,1,'') as VariantOptions, "; - } - if (Columns.ContainsKey("RelatedProducts".ToLower()) || IsColumnUsedInMappingConditions("RelatedProducts")) - { - if (getRelatedProductsByName) - { - result = result + "STUFF((SELECT ',\"'+ ProductName+'\"' FROM EcomProducts JOIN EcomProductsRelated on ProductID=ProductRelatedProductRelID WHERE productrelatedProductID=outerEcomProducts.ProductID FOR XML PATH('')),1,1,'') as RelatedProducts, "; - } - else - { - result = result + "STUFF((SELECT ',\"'+ ProductID+'\"' FROM EcomProducts JOIN EcomProductsRelated on ProductID=ProductRelatedProductRelID WHERE productrelatedProductID=outerEcomProducts.ProductID FOR XML PATH('')),1,1,'') as RelatedProducts, "; - } - } - result += GetColumnsFromMappingConditions(ecomProductfieldsToSkip); - break; - case "EcomProductsRelated": - result = GetDistinctColumnsFromMapping(new HashSet { "productrelatedproductid", "productrelatedproductrelid", "productrelatedgroupid", "productrelatedlanguageid" }); - if (getRelatedProductsByName) + } + if ((Columns.ContainsKey("ProductManufacturerID".ToLower())) || IsColumnUsedInMappingConditions("ProductManufacturerID")) + { + if (getManufacturerNamesForProducts) { - result = result + "source.ProductName as ProductRelatedProductID, destination.productName as ProductRelatedProductRelID, "; + result = result + "isnull([ManufacturerName],'') as ProductManufacturerId, "; } else { - result = result + "ProductRelatedProductID, ProductRelatedProductRelID, "; + result = result + "[ProductManufacturerId], "; } - if (getRelatedProductGroupsByName) + } + if (Columns.ContainsKey("VariantOptions".ToLower()) || IsColumnUsedInMappingConditions("VariantOptions")) + { + result = result + "STUFF((SELECT ',\"'+ DistinctVariants.VariantOptionsProductRelationVariantID +'\"' FROM "; + result += "(SELECT DISTINCT VariantOptionsProductRelationVariantID FROM EcomProducts JOIN EcomVariantOptionsProductRelation on ProductID=VariantOptionsProductRelationProductID WHERE ProductID=outerEcomProducts.ProductID) DistinctVariants FOR XML PATH('')),1,1,'') as VariantOptions, "; + } + if (Columns.ContainsKey("RelatedProducts".ToLower()) || IsColumnUsedInMappingConditions("RelatedProducts")) + { + if (getRelatedProductsByName) { - result = result + "RelatedGroupName as ProductrelatedGroupID, "; + result = result + "STUFF((SELECT ',\"'+ ProductName+'\"' FROM EcomProducts JOIN EcomProductsRelated on ProductID=ProductRelatedProductRelID WHERE productrelatedProductID=outerEcomProducts.ProductID FOR XML PATH('')),1,1,'') as RelatedProducts, "; } else { - result = result + "ProductrelatedGroupID, "; + result = result + "STUFF((SELECT ',\"'+ ProductID+'\"' FROM EcomProducts JOIN EcomProductsRelated on ProductID=ProductRelatedProductRelID WHERE productrelatedProductID=outerEcomProducts.ProductID FOR XML PATH('')),1,1,'') as RelatedProducts, "; } - if ((Columns.ContainsKey("ProductRelatedLanguageID".ToLower())) || IsColumnUsedInMappingConditions("ProductRelatedLanguageID")) - { - result = result + "LanguageID as ProductRelatedLanguageID, "; - } - result += GetColumnsFromMappingConditions(new HashSet { "productrelatedlanguageid" }); - break; - case "EcomProductCategoryFieldValue": - var ecomProductCategoryFieldValuefieldsToSkip = new HashSet { "fieldvalueproductnumber" }; - result = GetDistinctColumnsFromMapping(ecomProductCategoryFieldValuefieldsToSkip); - if (Columns.ContainsKey("FieldValueProductNumber".ToLower()) || IsColumnUsedInMappingConditions("FieldValueProductNumber")) - { - result = result + "[ProductNumber] as FieldValueProductNumber, "; - } - result += GetColumnsFromMappingConditions(ecomProductCategoryFieldValuefieldsToSkip); - break; - case "EcomAssortmentPermissions": + } + result += GetColumnsFromMappingConditions(ecomProductfieldsToSkip); + break; + case "EcomProductsRelated": + result = GetDistinctColumnsFromMapping(new HashSet { "productrelatedproductid", "productrelatedproductrelid", "productrelatedgroupid", "productrelatedlanguageid" }); + if (getRelatedProductsByName) + { + result = result + "source.ProductName as ProductRelatedProductID, destination.productName as ProductRelatedProductRelID, "; + } + else + { + result = result + "ProductRelatedProductID, ProductRelatedProductRelID, "; + } + if (getRelatedProductGroupsByName) + { + result = result + "RelatedGroupName as ProductrelatedGroupID, "; + } + else + { + result = result + "ProductrelatedGroupID, "; + } + if ((Columns.ContainsKey("ProductRelatedLanguageID".ToLower())) || IsColumnUsedInMappingConditions("ProductRelatedLanguageID")) + { + result = result + "LanguageID as ProductRelatedLanguageID, "; + } + result += GetColumnsFromMappingConditions(new HashSet { "productrelatedlanguageid" }); + break; + case "EcomProductCategoryFieldValue": + var ecomProductCategoryFieldValuefieldsToSkip = new HashSet { "fieldvalueproductnumber" }; + result = GetDistinctColumnsFromMapping(ecomProductCategoryFieldValuefieldsToSkip); + if (Columns.ContainsKey("FieldValueProductNumber".ToLower()) || IsColumnUsedInMappingConditions("FieldValueProductNumber")) + { + result = result + "[ProductNumber] as FieldValueProductNumber, "; + } + result += GetColumnsFromMappingConditions(ecomProductCategoryFieldValuefieldsToSkip); + break; + case "EcomAssortmentPermissions": + result = GetDistinctColumnsFromMapping(new HashSet { "assortmentpermissioncustomernumber", "assortmentpermissionexternalid" }); + if (Columns.ContainsKey("AssortmentPermissionCustomerNumber".ToLower()) || IsColumnUsedInMappingConditions("AssortmentPermissionCustomerNumber")) + { + result = result + "(SELECT AccessUserCustomerNumber FROM AccessUser JOIN EcomAssortmentPermissions on AssortmentPermissionAccessUserID=AccessUserID WHERE AccessUserID=outerEcomAssortmentPermissions.AssortmentPermissionAccessUserID) as AssortmentPermissionCustomerNumber, "; + } + if (Columns.ContainsKey("AssortmentPermissionExternalID".ToLower())) + { + result = result + "(SELECT AccessUserExternalID FROM AccessUser JOIN EcomAssortmentPermissions on AssortmentPermissionAccessUserID=AccessUserID WHERE AccessUserID=outerEcomAssortmentPermissions.AssortmentPermissionAccessUserID) as AssortmentPermissionExternalID, "; + } + result += GetColumnsFromMappingConditions(new HashSet { "assortmentpermissioncustomernumber", "assortmentpermissionexternalid" }); + break; + default: + result = GetDistinctColumnsFromMapping(); + + if (mapping.SourceTable != null && mapping.SourceTable.Name == "EcomAssortmentPermissions") + { result = GetDistinctColumnsFromMapping(new HashSet { "assortmentpermissioncustomernumber", "assortmentpermissionexternalid" }); - if (Columns.ContainsKey("AssortmentPermissionCustomerNumber".ToLower()) || IsColumnUsedInMappingConditions("AssortmentPermissionCustomerNumber")) + if (Columns.ContainsKey("AssortmentPermissionCustomerNumber".ToLower())) { result = result + "(SELECT AccessUserCustomerNumber FROM AccessUser JOIN EcomAssortmentPermissions on AssortmentPermissionAccessUserID=AccessUserID WHERE AccessUserID=outerEcomAssortmentPermissions.AssortmentPermissionAccessUserID) as AssortmentPermissionCustomerNumber, "; } @@ -424,28 +422,11 @@ protected string GetColumns() { result = result + "(SELECT AccessUserExternalID FROM AccessUser JOIN EcomAssortmentPermissions on AssortmentPermissionAccessUserID=AccessUserID WHERE AccessUserID=outerEcomAssortmentPermissions.AssortmentPermissionAccessUserID) as AssortmentPermissionExternalID, "; } - result += GetColumnsFromMappingConditions(new HashSet { "assortmentpermissioncustomernumber", "assortmentpermissionexternalid" }); - break; - default: - result = GetDistinctColumnsFromMapping(); - - if (_mapping.SourceTable != null && _mapping.SourceTable.Name == "EcomAssortmentPermissions") - { - result = GetDistinctColumnsFromMapping(new HashSet { "assortmentpermissioncustomernumber", "assortmentpermissionexternalid" }); - if (Columns.ContainsKey("AssortmentPermissionCustomerNumber".ToLower())) - { - result = result + "(SELECT AccessUserCustomerNumber FROM AccessUser JOIN EcomAssortmentPermissions on AssortmentPermissionAccessUserID=AccessUserID WHERE AccessUserID=outerEcomAssortmentPermissions.AssortmentPermissionAccessUserID) as AssortmentPermissionCustomerNumber, "; - } - if (Columns.ContainsKey("AssortmentPermissionExternalID".ToLower())) - { - result = result + "(SELECT AccessUserExternalID FROM AccessUser JOIN EcomAssortmentPermissions on AssortmentPermissionAccessUserID=AccessUserID WHERE AccessUserID=outerEcomAssortmentPermissions.AssortmentPermissionAccessUserID) as AssortmentPermissionExternalID, "; - } - } - result += GetColumnsFromMappingConditions(); - break; - } - result = result.Substring(0, result.Length - 2); - return result; + } + result += GetColumnsFromMappingConditions(); + break; } + result = result.Substring(0, result.Length - 2); + return result; } }