From ca60bf9473f394617ac996bc09ef4722317816b3 Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort Date: Tue, 26 Nov 2024 14:13:50 +0100 Subject: [PATCH 1/6] Added virtual fields OrderDeliveryAddressExternalId, OrderDeliveryAddressExternalId, OrderDeliveryAddressLocationCode, OrderDeliveryAddressShipmentMethodCode, OrderDeliveryAddressShippingAgentCode and OrderDeliveryAddressShippingAgentServiceCode to store and handle DeliveryAddresses for orders, only OrderDeliveryAddressExternalId for import. Added logic for merging conditionals from EcomOrders to EcomOrderLines so when we only have one table mapping to EcomOrders it will merge else the conditionals will be skipped. Added left join on EcomOrders so we now fetch AccessUserAddress based on the OrderDeliveryAddressId. Updated PackageReference to 10.11 (not released yet) as the extra fields/virtuel-fields will be released on that version. --- ...Integration.Providers.OrderProvider.csproj | 6 +- src/OrderProvider.cs | 112 +++++- src/OrderSourceReader.cs | 361 +++++++++--------- 3 files changed, 293 insertions(+), 186 deletions(-) diff --git a/src/Dynamicweb.DataIntegration.Providers.OrderProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.OrderProvider.csproj index 9e8d796..9e1ceb2 100644 --- a/src/Dynamicweb.DataIntegration.Providers.OrderProvider.csproj +++ b/src/Dynamicweb.DataIntegration.Providers.OrderProvider.csproj @@ -1,6 +1,6 @@  - 10.8.3 + 10.11.0 1.0.0.0 Order Provider Order Provider @@ -23,8 +23,8 @@ snupkg - - + + diff --git a/src/OrderProvider.cs b/src/OrderProvider.cs index 69c544f..c78a069 100644 --- a/src/OrderProvider.cs +++ b/src/OrderProvider.cs @@ -24,11 +24,20 @@ public class OrderProvider : BaseSqlProvider, IParameterOptions, ISource, IDesti private const string OrderCustomerAccessUserExternalId = "OrderCustomerAccessUserExternalId"; private const string OrderLineCalculatedDiscountPercentage = "OrderLineCalculatedDiscountPercentage"; private const string OrderIntegrationOrderId = "OrderIntegrationOrderId"; + private const string OrderDeliveryAddressExternalId = "OrderDeliveryAddressExternalId"; + private const string OrderDeliveryAddressLocationCode = "OrderDeliveryAddressLocationCode"; + private const string OrderDeliveryAddressShipmentMethodCode = "OrderDeliveryAddressShipmentMethodCode"; + private const string OrderDeliveryAddressShippingAgentCode = "OrderDeliveryAddressShippingAgentCode"; + private const string OrderDeliveryAddressShippingAgentServiceCode = "OrderDeliveryAddressShippingAgentServiceCode"; + private Job job = null; private Schema Schema { get; set; } private string SqlConnectionString { get; set; } + private string SourceColumnNameForDestinationOrderIntegrationOrderId = string.Empty; private string SourceColumnNameForDestinationOrderCustomerAccessUserId = string.Empty; + private string SourceColumnNameForDestinationOrderDeliveryAddressId = string.Empty; + private ColumnMapping OrderShippingMethodCodeMapping = null; private ColumnMapping OrderPaymentMethodCodeMapping = null; @@ -95,11 +104,16 @@ public override Schema GetOriginalSourceSchema() var orderLinesTable = result.GetTables().FirstOrDefault(obj => string.Equals(obj.Name, "EcomOrderLines", StringComparison.OrdinalIgnoreCase)); var ordersTable = result.GetTables().FirstOrDefault(obj => string.Equals(obj.Name, "EcomOrders", StringComparison.OrdinalIgnoreCase)); - if (orderLinesTable != null && ordersTable != null) + + if (ordersTable == null) + return result; + + if (orderLinesTable != null) { foreach (var column in ordersTable.Columns) { - if (!column.Name.Equals(OrderCustomerAccessUserExternalId, StringComparison.OrdinalIgnoreCase)) + if (!column.Name.Equals(OrderCustomerAccessUserExternalId, StringComparison.OrdinalIgnoreCase) + && !column.Name.Equals(OrderDeliveryAddressExternalId, StringComparison.OrdinalIgnoreCase)) { orderLinesTable.AddColumn(new SqlColumn(column.Name, typeof(string), SqlDbType.NVarChar, orderLinesTable, -1, false, false, true)); } @@ -107,6 +121,11 @@ public override Schema GetOriginalSourceSchema() orderLinesTable.AddColumn(new SqlColumn(OrderLineCalculatedDiscountPercentage, typeof(double), SqlDbType.NVarChar, orderLinesTable, -1, false, false, true)); } + ordersTable.AddColumn(new SqlColumn(OrderDeliveryAddressLocationCode, typeof(string), SqlDbType.NVarChar, ordersTable, -1, false, false, true)); + ordersTable.AddColumn(new SqlColumn(OrderDeliveryAddressShipmentMethodCode, typeof(string), SqlDbType.NVarChar, ordersTable, -1, false, false, true)); + ordersTable.AddColumn(new SqlColumn(OrderDeliveryAddressShippingAgentCode, typeof(string), SqlDbType.NVarChar, ordersTable, -1, false, false, true)); + ordersTable.AddColumn(new SqlColumn(OrderDeliveryAddressShippingAgentServiceCode, typeof(string), SqlDbType.NVarChar, ordersTable, -1, false, false, true)); + return result; } @@ -131,8 +150,16 @@ public override Schema GetOriginalDestinationSchema() private Schema GetSqlSchemas() { - List tablestToKeep = new() { "EcomOrders", "EcomOrderLines", "EcomOrderLineFields", "EcomOrderLineFieldGroupRelation" }; - return GetSqlSourceSchema(Connection, tablestToKeep); + List tablestToKeep = ["EcomOrders", "EcomOrderLines", "EcomOrderLineFields", "EcomOrderLineFieldGroupRelation"]; + var result = GetSqlSourceSchema(Connection, tablestToKeep); + foreach (Table table in result.GetTables()) + { + if (table.Name == "EcomOrders") + { + table.AddColumn(new SqlColumn(OrderDeliveryAddressExternalId, typeof(string), SqlDbType.NVarChar, table, -1, false, false, true)); + } + } + return result; } public override void OverwriteSourceSchemaToOriginal() @@ -506,6 +533,23 @@ private void HandleMappingsForOrders(Mapping mapping) } } } + if (columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0) != null) + { + var OrderDeliveryAddressIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "OrderDeliveryAddressId", true) == 0); + if (OrderDeliveryAddressIdMapping == null) + { + var randomColumn = mapping.SourceTable.Columns.First(); + SourceColumnNameForDestinationOrderDeliveryAddressId = randomColumn.Name; + mapping.AddMapping(randomColumn, mapping.DestinationTable.Columns.Find(c => string.Compare(c.Name, "OrderDeliveryAddressId", true) == 0), true); + } + else + { + if (OrderDeliveryAddressIdMapping.SourceColumn != null) + { + SourceColumnNameForDestinationOrderDeliveryAddressId = OrderDeliveryAddressIdMapping.SourceColumn.Name; + } + } + } var ordersTable = mapping.DestinationTable; if (columnMappings.FirstOrDefault(cm => cm.Active && string.Equals(cm.DestinationColumn.Name, "OrderId", StringComparison.OrdinalIgnoreCase)) is not null) { @@ -531,7 +575,7 @@ private void HandleMappingsForOrders(Mapping mapping) } } - private void EnsureMapping(ColumnMappingCollection columnMappings, Mapping mapping, Column destinationColumn) + private static void EnsureMapping(ColumnMappingCollection columnMappings, Mapping mapping, Column destinationColumn) { if (destinationColumn is null || mapping is null || columnMappings is null) return; @@ -551,7 +595,9 @@ private static void RemoveColumnMappingsThatShouldBeSkippedInMoveToMainTables(Ma switch (mapping.DestinationTable.Name) { case "EcomOrders": - columnMapping.RemoveAll(cm => cm.DestinationColumn != null && string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); + columnMapping.RemoveAll(cm => cm.DestinationColumn != null + && (string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0 + || string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0)); break; case "EcomOrderLines": columnMapping.RemoveAll(cm => cm.DestinationColumn != null && string.Compare(cm.DestinationColumn.Name, OrderIntegrationOrderId, true) == 0); @@ -561,6 +607,36 @@ private static void RemoveColumnMappingsThatShouldBeSkippedInMoveToMainTables(Ma } } + private Dictionary _existingAddresses = null; + /// + /// Collection of , key value pairs + /// + private Dictionary ExistingAddresses + { + get + { + if (_existingAddresses == null) + { + _existingAddresses = []; + SqlDataAdapter usersDataAdapter = new("select AccessUserAddressExternalId, AccessUserAddressId from AccessUserAddress where AccessUserAddressExternalId is not null and AccessUserAddressExternalId <> ''", Connection); + new SqlCommandBuilder(usersDataAdapter); + DataSet dataSet = new(); + usersDataAdapter.Fill(dataSet); + DataTable dataTable = dataSet.Tables[0]; + if (dataTable != null) + { + string key; + foreach (DataRow row in dataTable.Rows) + { + key = row["AccessUserAddressExternalId"].ToString(); + _existingAddresses.TryAdd(key, row["AccessUserAddressId"].ToString()); + } + } + } + return _existingAddresses; + } + } + private Dictionary _existingUsers = null; /// /// Collection of , key value pairs @@ -693,6 +769,30 @@ private void ProcessOrderRow(Mapping mapping, ColumnMappingCollection columnMapp row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId; } } + if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderDeliveryAddressId)) + { + object addressId = DBNull.Value; + var OrderDeliveryAddressExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0); + if (OrderDeliveryAddressExternalIdMapping != null && OrderDeliveryAddressExternalIdMapping.SourceColumn != null) + { + if (row.ContainsKey(OrderDeliveryAddressExternalIdMapping.SourceColumn.Name)) + { + string externalID = Convert.ToString(row[OrderDeliveryAddressExternalIdMapping.SourceColumn.Name]); + if (!string.IsNullOrEmpty(externalID) && ExistingAddresses.ContainsKey(externalID)) + { + addressId = ExistingAddresses[externalID]; + } + } + } + if (!row.ContainsKey(SourceColumnNameForDestinationOrderDeliveryAddressId)) + { + row.Add(SourceColumnNameForDestinationOrderDeliveryAddressId, addressId); + } + else + { + row[SourceColumnNameForDestinationOrderDeliveryAddressId] = addressId; + } + } if (OrderShippingMethodCodeMapping is not null) { ProcessShipping(mapping, columnMappings, row); diff --git a/src/OrderSourceReader.cs b/src/OrderSourceReader.cs index 94d974c..95d2687 100644 --- a/src/OrderSourceReader.cs +++ b/src/OrderSourceReader.cs @@ -5,235 +5,242 @@ using System.Data.SqlClient; using System.Linq; -namespace Dynamicweb.DataIntegration.Providers.OrderProvider -{ - internal class OrderSourceReader : BaseSqlReader - { - private static MappingConditionalCollection _ordersConditions = null; - private static List _ordersToExport = null; - private ColumnMappingCollection _columnMappings = null; - - public OrderSourceReader(Mapping mapping, SqlConnection connection, bool exportNotExportedOrders, bool exportOnlyOrdersWithoutExtID, bool doNotExportCarts) : base(mapping, connection) - { - _columnMappings = mapping.GetColumnMappings(); - _command = new SqlCommand { Connection = connection }; - if (connection.State.ToString() != "Open") - connection.Open(); - //save order conditions for OrderLines filtering - if (mapping.SourceTable.Name == "EcomOrders") - { - _ordersToExport = new List(); - _ordersConditions = mapping.Conditionals; - } - //add order conditions for correct OrderLines filtering - if (mapping.SourceTable.Name == "EcomOrderLines" && _ordersConditions != null && _ordersConditions.Count > 0) - mapping.Conditionals.AddRange(_ordersConditions); +namespace Dynamicweb.DataIntegration.Providers.OrderProvider; - string whereSql = string.Empty; +internal class OrderSourceReader : BaseSqlReader +{ + private static MappingConditionalCollection _ordersConditions = null; + private static List _ordersToExport = null; + private ColumnMappingCollection _columnMappings = null; - //make where statement only for EcomOrders or EcomOrderLines tables - if (mapping.SourceTable.Name == "EcomOrders" || mapping.SourceTable.Name == "EcomOrderLines") - { - whereSql = GetWhereSql(exportNotExportedOrders, exportOnlyOrdersWithoutExtID, doNotExportCarts); - } + public OrderSourceReader(Mapping mapping, SqlConnection connection, bool exportNotExportedOrders, bool exportOnlyOrdersWithoutExtID, bool doNotExportCarts) : base(mapping, connection) + { + _columnMappings = mapping.GetColumnMappings(); + _command = new SqlCommand { Connection = connection }; + if (connection.State.ToString() != "Open") + connection.Open(); - LoadReader(whereSql); + //save order conditions for OrderLines filtering + if (mapping.SourceTable.Name == "EcomOrders") + { + _ordersToExport ??= []; + _ordersConditions = mapping.Conditionals; } - public new void Write(Dictionary row) + //add order conditions for correct OrderLines filtering but ignore when there is multiple mappings from EcomOrders + var ignoreOrderConditionals = mapping.Job.Mappings.Count(obj => obj.SourceTable != null && obj.SourceTable.Name.Equals("EcomOrders", StringComparison.OrdinalIgnoreCase)) > 1; + if (!ignoreOrderConditionals) { - base.Write(row); - // Need to clear cache for the order when doing response-mappings - Ecommerce.Services.Orders.ClearCache(Core.Converter.ToString(_reader["OrderId"])); + if (mapping.SourceTable.Name == "EcomOrderLines" && _ordersConditions != null && _ordersConditions.Count > 0) + mapping.Conditionals.AddRange(_ordersConditions); } + string whereSql = string.Empty; - private void LoadReader(string whereSql) + //make where statement only for EcomOrders or EcomOrderLines tables + if (mapping.SourceTable.Name == "EcomOrders" || mapping.SourceTable.Name == "EcomOrderLines") { - try - { - if (_columnMappings.Count == 0) - return; + whereSql = GetWhereSql(exportNotExportedOrders, exportOnlyOrdersWithoutExtID, doNotExportCarts); + } - string sql = $"SELECT * FROM (SELECT {GetColumns()} FROM {GetFromTables()}) AS innerTable "; + LoadReader(whereSql); + } - if (!string.IsNullOrEmpty(whereSql)) - sql = sql + " where " + whereSql; + public new void Write(Dictionary row) + { + base.Write(row); + // Need to clear cache for the order when doing response-mappings + Ecommerce.Services.Orders.ClearCache(Core.Converter.ToString(_reader["OrderId"])); + } - _command.CommandText = sql; - _reader = _command.ExecuteReader(); - } - catch (SqlException) - { + private void LoadReader(string whereSql) + { + try + { + if (_columnMappings.Count == 0) + return; - throw; - } - catch (Exception ex) - { - throw new Exception("Failed to open sqlSourceReader. Reason: " + ex.Message, ex); - } + string sql = $"SELECT * FROM (SELECT {GetColumns()} FROM {GetFromTables()}) AS innerTable "; + + if (!string.IsNullOrEmpty(whereSql)) + sql = sql + " where " + whereSql; + + _command.CommandText = sql; + _reader = _command.ExecuteReader(); } - protected override string GetColumns() + catch (SqlException) { - string columns = GetDistinctColumnsFromMapping(new string[] { "OrderCustomerAccessUserExternalId", "OrderLineCalculatedDiscountPercentage" }); - columns = columns.Substring(0, columns.Length - 2); - switch (mapping.SourceTable.Name) - { - case "EcomOrders": - columns = columns + ", [AccessUserExternalId] as [OrderCustomerAccessUserExternalId]"; - if (!columns.Split(',').Any(c => c.Trim(new char[] { ' ', '[', ']' }).Equals("OrderId", StringComparison.OrdinalIgnoreCase))) - { - columns += ", [OrderId]"; - } - break; - case "EcomOrderLines": - columns = columns + ", (-1 * OrderLineTotalDiscountWithVAT) / OrderLinePriceWithVat * 100 as [OrderLineCalculatedDiscountPercentage]"; - break; - - } - if (mapping.Conditionals.Any()) - { - columns += $", {GetColumnsFromMappingConditions()}"; - columns = columns[..^2]; - } + throw; + } + catch (Exception ex) + { + throw new Exception("Failed to open sqlSourceReader. Reason: " + ex.Message, ex); + } + } + protected override string GetColumns() + { + string columns = GetDistinctColumnsFromMapping(["OrderCustomerAccessUserExternalId", "OrderDeliveryAddressExternalId", "OrderDeliveryAddressLocationCode", + "OrderDeliveryAddressShipmentMethodCode", "OrderDeliveryAddressShippingAgentCode", "OrderDeliveryAddressShippingAgentServiceCode", "OrderLineCalculatedDiscountPercentage"]); + columns = columns.Substring(0, columns.Length - 2); + switch (mapping.SourceTable.Name) + { + case "EcomOrders": + columns = columns + ", [AccessUserExternalId] as [OrderCustomerAccessUserExternalId], [AccessUserAddressExternalId] as [OrderDeliveryAddressExternalId]" + + ", [AccessUserAddressLocationCode] as [OrderDeliveryAddressLocationCode], [AccessUserAddressShipmentMethodCode] as [OrderDeliveryAddressShipmentMethodCode]" + + ", [AccessUserAddressShippingAgentCode] as [OrderDeliveryAddressShippingAgentCode], [AccessUserAddressShippingAgentServiceCode] as [OrderDeliveryAddressShippingAgentServiceCode]"; + if (!columns.Split(',').Any(c => c.Trim([' ', '[', ']']).Equals("OrderId", StringComparison.OrdinalIgnoreCase))) + { + columns += ", [OrderId]"; + } + break; + case "EcomOrderLines": + columns = columns + ", (-1 * OrderLineTotalDiscountWithVAT) / NULLIF(OrderLinePriceWithVat, 0) * 100 as [OrderLineCalculatedDiscountPercentage]"; + break; - return columns; } - private string GetWhereSql(bool exportNotExportedOrders, bool exportOnlyOrdersWithoutExtID, bool doNotExportCarts) + if (mapping.Conditionals.Any()) { - List parameters = new List(); - string conditionalsSql = MappingExtensions.GetConditionalsSql(out parameters, mapping.Conditionals, false, false); - if (conditionalsSql != "") - { - conditionalsSql = conditionalsSql.Substring(0, conditionalsSql.Length - 4); - foreach (SqlParameter p in parameters) - _command.Parameters.Add(p); - } + columns += $", {GetColumnsFromMappingConditions()}"; + columns = columns[..^2]; + } - if (exportNotExportedOrders) - { - conditionalsSql = (string.IsNullOrEmpty(conditionalsSql) ? "([OrderIsExported] = 0 OR [OrderIsExported] IS NULL)" : - conditionalsSql + " AND ([OrderIsExported] = 0 OR [OrderIsExported] IS NULL)"); - } + return columns; + } - if (exportOnlyOrdersWithoutExtID) - { - conditionalsSql = (string.IsNullOrEmpty(conditionalsSql) ? "([OrderIntegrationOrderID] = '' OR [OrderIntegrationOrderID] IS NULL)" : - conditionalsSql + " AND ([OrderIntegrationOrderID] = '' OR [OrderIntegrationOrderID] IS NULL)"); - } - if (doNotExportCarts) - { - conditionalsSql = (string.IsNullOrEmpty(conditionalsSql) ? "[OrderCart] != 1 AND [OrderComplete] = 1" : - conditionalsSql + " AND [OrderCart] != 1 AND [OrderComplete] = 1"); - } + private string GetWhereSql(bool exportNotExportedOrders, bool exportOnlyOrdersWithoutExtID, bool doNotExportCarts) + { + List parameters = new List(); + string conditionalsSql = MappingExtensions.GetConditionalsSql(out parameters, mapping.Conditionals, false, false); + if (conditionalsSql != "") + { + conditionalsSql = conditionalsSql.Substring(0, conditionalsSql.Length - 4); + foreach (SqlParameter p in parameters) + _command.Parameters.Add(p); + } - return conditionalsSql; + if (exportNotExportedOrders) + { + conditionalsSql = (string.IsNullOrEmpty(conditionalsSql) ? "([OrderIsExported] = 0 OR [OrderIsExported] IS NULL)" : + conditionalsSql + " AND ([OrderIsExported] = 0 OR [OrderIsExported] IS NULL)"); } - protected override string GetFromTables() + if (exportOnlyOrdersWithoutExtID) + { + conditionalsSql = (string.IsNullOrEmpty(conditionalsSql) ? "([OrderIntegrationOrderID] = '' OR [OrderIntegrationOrderID] IS NULL)" : + conditionalsSql + " AND ([OrderIntegrationOrderID] = '' OR [OrderIntegrationOrderID] IS NULL)"); + } + if (doNotExportCarts) { - string result = "[" + mapping.SourceTable.SqlSchema + "].[" + mapping.SourceTable.Name + "] "; + conditionalsSql = (string.IsNullOrEmpty(conditionalsSql) ? "[OrderCart] != 1 AND [OrderComplete] = 1" : + conditionalsSql + " AND [OrderCart] != 1 AND [OrderComplete] = 1"); + } - switch (mapping.SourceTable.Name) - { - case "EcomOrderLines": - result = result + " inner join EcomOrders on EcomOrderLines.OrderLineOrderID = EcomOrders.OrderID"; - break; - case "EcomOrders": - result = "[dbo].[EcomOrders] left join dbo.AccessUser on OrderCustomerAccessUserID = AccessUserID"; - break; - default: - break; - } - return result; + return conditionalsSql; + } + + protected override string GetFromTables() + { + string result = "[" + mapping.SourceTable.SqlSchema + "].[" + mapping.SourceTable.Name + "] "; + + switch (mapping.SourceTable.Name) + { + case "EcomOrderLines": + result = result + " inner join EcomOrders on EcomOrderLines.OrderLineOrderID = EcomOrders.OrderID"; + break; + case "EcomOrders": + result = "[dbo].[EcomOrders] left join dbo.AccessUser on OrderCustomerAccessUserID = AccessUserID left join dbo.AccessUserAddress on OrderDeliveryAddressId = AccessUserAddressId"; + break; + default: + break; } + return result; + } - public override Dictionary GetNext() + public override Dictionary GetNext() + { + Dictionary row = _columnMappings.Where(columnMapping => columnMapping.SourceColumn != null).GroupBy(cm => cm.SourceColumn.Name, (key, group) => group.First()).ToDictionary(columnMapping => columnMapping.SourceColumn.Name, columnMapping => _reader[columnMapping.SourceColumn.Name]); + if (mapping.SourceTable.Name == "EcomOrders") { - Dictionary row = _columnMappings.Where(columnMapping => columnMapping.SourceColumn != null).GroupBy(cm => cm.SourceColumn.Name, (key, group) => group.First()).ToDictionary(columnMapping => columnMapping.SourceColumn.Name, columnMapping => _reader[columnMapping.SourceColumn.Name]); - if (mapping.SourceTable.Name == "EcomOrders") + string orderId = Core.Converter.ToString(_reader["OrderId"]); + if (!string.IsNullOrEmpty(orderId)) { - string orderId = Core.Converter.ToString(_reader["OrderId"]); - if (!string.IsNullOrEmpty(orderId)) + if (!_ordersToExport.Contains(orderId)) { - if (!_ordersToExport.Contains(orderId)) - { - _ordersToExport.Add(orderId); - } - if (!row.ContainsKey("OrderId")) - { - row.Add("OrderId", orderId); - } + _ordersToExport.Add(orderId); + } + if (!row.ContainsKey("OrderId")) + { + row.Add("OrderId", orderId); } } - return row; } + return row; + } - public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlConnection connection) + public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlConnection connection) + { + if (_ordersToExport != null && _ordersToExport.Count > 0) { - if (_ordersToExport != null && _ordersToExport.Count > 0) + //Execute script to update IsExported and OrderStateID columns in Orders Table + SqlCommand command = new SqlCommand { Connection = connection }; + try { - //Execute script to update IsExported and OrderStateID columns in Orders Table - SqlCommand command = new SqlCommand { Connection = connection }; - try - { - command.Transaction = connection.BeginTransaction("OrderProviderTransaction"); + command.Transaction = connection.BeginTransaction("OrderProviderTransaction"); - if (connection.State.ToString() != "Open") - connection.Open(); + if (connection.State.ToString() != "Open") + connection.Open(); - string sql = "UPDATE EcomOrders SET OrderIsExported = 1"; + string sql = "UPDATE EcomOrders SET OrderIsExported = 1"; - if (!string.IsNullOrEmpty(orderStateIDAfterExport)) - { - sql = sql + string.Format(", OrderStateID = '{0}'", orderStateIDAfterExport); - } - if (_ordersToExport.Count > 100) + if (!string.IsNullOrEmpty(orderStateIDAfterExport)) + { + sql = sql + string.Format(", OrderStateID = '{0}'", orderStateIDAfterExport); + } + if (_ordersToExport.Count > 100) + { + var taken = 0; + int step = 100; + while (taken < _ordersToExport.Count) { - var taken = 0; - int step = 100; - while (taken < _ordersToExport.Count) + var idsCollection = _ordersToExport.Skip(taken).Take(step); + string ids = string.Join("','", idsCollection); + if (!string.IsNullOrEmpty(ids)) { - var idsCollection = _ordersToExport.Skip(taken).Take(step); - string ids = string.Join("','", idsCollection); - if (!string.IsNullOrEmpty(ids)) - { - command.CommandText = sql + string.Format(" WHERE [OrderID] IN ('{0}')", ids); - command.ExecuteNonQuery(); - ClearOrderCache(idsCollection); - } - taken = taken + step; + command.CommandText = sql + string.Format(" WHERE [OrderID] IN ('{0}')", ids); + command.ExecuteNonQuery(); + ClearOrderCache(idsCollection); } + taken = taken + step; } - else - { - command.CommandText = sql + string.Format(" WHERE [OrderID] IN ('{0}')", string.Join("','", _ordersToExport)); - command.ExecuteNonQuery(); - ClearOrderCache(_ordersToExport); - } - command.Transaction.Commit(); - } - catch (Exception ex) - { - command.Transaction.Rollback(); - throw new Exception(string.Format("A rollback is made as exception is made with message: {0} Sql query: {1}", ex.Message, command.CommandText), ex); } - finally + else { - _ordersConditions = null; - _ordersToExport = null; + command.CommandText = sql + string.Format(" WHERE [OrderID] IN ('{0}')", string.Join("','", _ordersToExport)); + command.ExecuteNonQuery(); + ClearOrderCache(_ordersToExport); } + command.Transaction.Commit(); + } + catch (Exception ex) + { + command.Transaction.Rollback(); + throw new Exception(string.Format("A rollback is made as exception is made with message: {0} Sql query: {1}", ex.Message, command.CommandText), ex); + } + finally + { + _ordersConditions = null; + _ordersToExport = null; } } + } - private static void ClearOrderCache(IEnumerable orderIds) + private static void ClearOrderCache(IEnumerable orderIds) + { + OrderService os = new(); + foreach (string id in orderIds) { - OrderService os = new(); - foreach (string id in orderIds) - { - os.RemoveOrderCache(id); - } + os.RemoveOrderCache(id); } } } From 37cbba83b4aa2d5935e40ad2362b1fe2dd9ed712 Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort Date: Thu, 28 Nov 2024 08:46:50 +0100 Subject: [PATCH 2/6] lowercase local properties and extracted code to their own functions --- src/OrderProvider.cs | 82 +++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/src/OrderProvider.cs b/src/OrderProvider.cs index 0a7a60d..bbeaa76 100644 --- a/src/OrderProvider.cs +++ b/src/OrderProvider.cs @@ -498,8 +498,8 @@ private void HandleMappingsForOrders(Mapping mapping) var columnMappings = mapping.GetColumnMappings(); if (columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0) != null) { - var OrderCustomerAccessUserIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "OrderCustomerAccessUserId", true) == 0); - if (OrderCustomerAccessUserIdMapping == null) + var orderCustomerAccessUserIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "OrderCustomerAccessUserId", true) == 0); + if (orderCustomerAccessUserIdMapping == null) { var randomColumn = mapping.SourceTable.Columns.First(); SourceColumnNameForDestinationOrderCustomerAccessUserId = randomColumn.Name; @@ -507,16 +507,16 @@ private void HandleMappingsForOrders(Mapping mapping) } else { - if (OrderCustomerAccessUserIdMapping.SourceColumn != null) + if (orderCustomerAccessUserIdMapping.SourceColumn != null) { - SourceColumnNameForDestinationOrderCustomerAccessUserId = OrderCustomerAccessUserIdMapping.SourceColumn.Name; + SourceColumnNameForDestinationOrderCustomerAccessUserId = orderCustomerAccessUserIdMapping.SourceColumn.Name; } } } if (columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0) != null) { - var OrderDeliveryAddressIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "OrderDeliveryAddressId", true) == 0); - if (OrderDeliveryAddressIdMapping == null) + var orderDeliveryAddressIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "OrderDeliveryAddressId", true) == 0); + if (orderDeliveryAddressIdMapping == null) { var randomColumn = mapping.SourceTable.Columns.First(); SourceColumnNameForDestinationOrderDeliveryAddressId = randomColumn.Name; @@ -524,9 +524,9 @@ private void HandleMappingsForOrders(Mapping mapping) } else { - if (OrderDeliveryAddressIdMapping.SourceColumn != null) + if (orderDeliveryAddressIdMapping.SourceColumn != null) { - SourceColumnNameForDestinationOrderDeliveryAddressId = OrderDeliveryAddressIdMapping.SourceColumn.Name; + SourceColumnNameForDestinationOrderDeliveryAddressId = orderDeliveryAddressIdMapping.SourceColumn.Name; } } } @@ -725,30 +725,20 @@ private void ProcessOrderLineRow(ColumnMappingCollection columnMappings, Diction private void ProcessOrderRow(Mapping mapping, ColumnMappingCollection columnMappings, Dictionary row) { - if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderCustomerAccessUserId)) + ProcessOrderCustomerAccessUser(columnMappings, row); + ProcessOrderDeliveryAddress(columnMappings, row); + if (OrderShippingMethodCodeMapping is not null) { - object accessUserId = DBNull.Value; - var OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); - if (OrderCustomerAccessUserExternalIdMapping != null && OrderCustomerAccessUserExternalIdMapping.SourceColumn != null) - { - if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name)) - { - string externalID = Convert.ToString(row[OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name]); - if (!string.IsNullOrEmpty(externalID) && ExistingUsers.ContainsKey(externalID)) - { - accessUserId = ExistingUsers[externalID]; - } - } - } - if (!row.ContainsKey(SourceColumnNameForDestinationOrderCustomerAccessUserId)) - { - row.Add(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId); - } - else - { - row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId; - } + ProcessShipping(mapping, columnMappings, row); } + if (OrderPaymentMethodCodeMapping is not null) + { + ProcessPayment(mapping, columnMappings, row); + } + } + + private void ProcessOrderDeliveryAddress(ColumnMappingCollection columnMappings, Dictionary row) + { if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderDeliveryAddressId)) { object addressId = DBNull.Value; @@ -773,13 +763,33 @@ private void ProcessOrderRow(Mapping mapping, ColumnMappingCollection columnMapp row[SourceColumnNameForDestinationOrderDeliveryAddressId] = addressId; } } - if (OrderShippingMethodCodeMapping is not null) - { - ProcessShipping(mapping, columnMappings, row); - } - if (OrderPaymentMethodCodeMapping is not null) + } + + private void ProcessOrderCustomerAccessUser(ColumnMappingCollection columnMappings, Dictionary row) + { + if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderCustomerAccessUserId)) { - ProcessPayment(mapping, columnMappings, row); + object accessUserId = DBNull.Value; + var OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); + if (OrderCustomerAccessUserExternalIdMapping != null && OrderCustomerAccessUserExternalIdMapping.SourceColumn != null) + { + if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name)) + { + string externalID = Convert.ToString(row[OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name]); + if (!string.IsNullOrEmpty(externalID) && ExistingUsers.ContainsKey(externalID)) + { + accessUserId = ExistingUsers[externalID]; + } + } + } + if (!row.ContainsKey(SourceColumnNameForDestinationOrderCustomerAccessUserId)) + { + row.Add(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId); + } + else + { + row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId; + } } } From a1afe12d71c03c9cf16f397104186b6f9acb5d5d Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort Date: Thu, 28 Nov 2024 09:07:41 +0100 Subject: [PATCH 3/6] Visual studio suggestions --- src/OrderDestinationWriter.cs | 19 +++++++++--------- src/OrderProvider.cs | 37 ++++++++++++----------------------- src/OrderSourceReader.cs | 14 ++++++------- 3 files changed, 28 insertions(+), 42 deletions(-) diff --git a/src/OrderDestinationWriter.cs b/src/OrderDestinationWriter.cs index 01f3096..e216a5f 100644 --- a/src/OrderDestinationWriter.cs +++ b/src/OrderDestinationWriter.cs @@ -38,9 +38,11 @@ public OrderDestinationWriter(Mapping mapping, SqlConnection connection, ILogger SkipFailingRows = skipFailingRows; DiscardDuplicates = discardDuplicates; TempTablePrefix = $"TempTableForBulkImport{mapping.GetId()}"; - SqlBulkCopier = new SqlBulkCopy(connection); - SqlBulkCopier.DestinationTableName = mapping.DestinationTable.Name + TempTablePrefix; - SqlBulkCopier.BulkCopyTimeout = 0; + SqlBulkCopier = new SqlBulkCopy(connection) + { + DestinationTableName = mapping.DestinationTable.Name + TempTablePrefix, + BulkCopyTimeout = 0 + }; Initialize(); if (connection.State != ConnectionState.Open) connection.Open(); @@ -48,7 +50,7 @@ public OrderDestinationWriter(Mapping mapping, SqlConnection connection, ILogger public new void Initialize() { - List destColumns = new(); + List destColumns = []; var columnMappings = Mapping.GetColumnMappings(); foreach (ColumnMapping columnMapping in columnMappings.DistinctBy(obj => obj.DestinationColumn.Name)) { @@ -112,9 +114,9 @@ internal int MoveDataToMainTable(SqlTransaction sqlTransaction, bool updateOnlyE // if 10k write table to db, empty table if (TableToWrite.Rows.Count >= 1000) { - RowsToWriteCount = RowsToWriteCount + TableToWrite.Rows.Count; + RowsToWriteCount += TableToWrite.Rows.Count; SkippedFailedRowsCount = SqlBulkCopierWriteToServer(SqlBulkCopier, TableToWrite, SkipFailingRows, Mapping, Logger); - RowsToWriteCount = RowsToWriteCount - SkippedFailedRowsCount; + RowsToWriteCount -= SkippedFailedRowsCount; TableToWrite.Clear(); if (RowsToWriteCount >= LastLogRowsCount + 10000) { @@ -131,9 +133,6 @@ internal int MoveDataToMainTable(SqlTransaction sqlTransaction, bool updateOnlyE SqlCommand.CommandText = "if exists (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + text + "') AND type in (N'U')) drop table " + text; SqlCommand.ExecuteNonQuery(); ((IDisposable)SqlBulkCopier).Dispose(); - if (duplicateRowsHandler != null) - { - duplicateRowsHandler.Dispose(); - } + duplicateRowsHandler?.Dispose(); } } diff --git a/src/OrderProvider.cs b/src/OrderProvider.cs index bbeaa76..ed51b80 100644 --- a/src/OrderProvider.cs +++ b/src/OrderProvider.cs @@ -65,7 +65,7 @@ public class OrderProvider : BaseSqlProvider, IParameterOptions, ISource, IDesti private SqlConnection connection; private SqlConnection Connection { - get { return connection ?? (connection = (SqlConnection)Database.CreateConnection()); } + get { return connection ??= (SqlConnection)Database.CreateConnection(); } set { connection = value; } } @@ -318,8 +318,8 @@ public override void UpdateDestinationSettings(IDestination destination) public override string Serialize() { - XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty)); - XElement root = new XElement("Parameters"); + XDocument document = new(new XDeclaration("1.0", "utf-8", string.Empty)); + XElement root = new("Parameters"); root.Add(CreateParameterNode(GetType(), "Export not yet exported Orders", ExportNotExportedOrders.ToString(CultureInfo.CurrentCulture))); root.Add(CreateParameterNode(GetType(), "Only export orders without externalID", ExportOnlyOrdersWithoutExtID.ToString(CultureInfo.CurrentCulture))); root.Add(CreateParameterNode(GetType(), "Export completed orders only", DoNotExportCarts.ToString(CultureInfo.CurrentCulture))); @@ -419,8 +419,7 @@ public override bool RunJob(Job job) msg += GetFailedSourceRowMessage(sourceRow); Logger.Log("Import job failed: " + msg); - if (sqlTransaction != null) - sqlTransaction.Rollback(); + sqlTransaction?.Rollback(); TotalRowsAffected = 0; @@ -708,15 +707,11 @@ private void ProcessOrderLineRow(ColumnMappingCollection columnMappings, Diction string integrationOrderId = Convert.ToString(value); if (!string.IsNullOrEmpty(integrationOrderId)) { - orderId = ExistingOrdersWithOrderIntegrationOrderId.ContainsKey(integrationOrderId) ? ExistingOrdersWithOrderIntegrationOrderId[integrationOrderId] : integrationOrderId; + orderId = ExistingOrdersWithOrderIntegrationOrderId.TryGetValue(integrationOrderId, out string existingIntegrationOrderId) ? existingIntegrationOrderId : integrationOrderId; } } } - if (!row.ContainsKey(SourceColumnNameForDestinationOrderIntegrationOrderId)) - { - row.Add(SourceColumnNameForDestinationOrderIntegrationOrderId, orderId); - } - else + if (!row.TryAdd(SourceColumnNameForDestinationOrderIntegrationOrderId, orderId)) { row[SourceColumnNameForDestinationOrderIntegrationOrderId] = orderId; } @@ -748,17 +743,13 @@ private void ProcessOrderDeliveryAddress(ColumnMappingCollection columnMappings, if (row.ContainsKey(OrderDeliveryAddressExternalIdMapping.SourceColumn.Name)) { string externalID = Convert.ToString(row[OrderDeliveryAddressExternalIdMapping.SourceColumn.Name]); - if (!string.IsNullOrEmpty(externalID) && ExistingAddresses.ContainsKey(externalID)) + if (!string.IsNullOrEmpty(externalID) && ExistingAddresses.TryGetValue(externalID, out string value)) { - addressId = ExistingAddresses[externalID]; + addressId = value; } } } - if (!row.ContainsKey(SourceColumnNameForDestinationOrderDeliveryAddressId)) - { - row.Add(SourceColumnNameForDestinationOrderDeliveryAddressId, addressId); - } - else + if (!row.TryAdd(SourceColumnNameForDestinationOrderDeliveryAddressId, addressId)) { row[SourceColumnNameForDestinationOrderDeliveryAddressId] = addressId; } @@ -776,17 +767,13 @@ private void ProcessOrderCustomerAccessUser(ColumnMappingCollection columnMappin if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name)) { string externalID = Convert.ToString(row[OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name]); - if (!string.IsNullOrEmpty(externalID) && ExistingUsers.ContainsKey(externalID)) + if (!string.IsNullOrEmpty(externalID) && ExistingUsers.TryGetValue(externalID, out string value)) { - accessUserId = ExistingUsers[externalID]; + accessUserId = value; } } } - if (!row.ContainsKey(SourceColumnNameForDestinationOrderCustomerAccessUserId)) - { - row.Add(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId); - } - else + if (!row.TryAdd(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId)) { row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId; } diff --git a/src/OrderSourceReader.cs b/src/OrderSourceReader.cs index 9dda9b0..64cafb2 100644 --- a/src/OrderSourceReader.cs +++ b/src/OrderSourceReader.cs @@ -82,7 +82,7 @@ protected override string GetColumns() { string columns = GetDistinctColumnsFromMapping(["OrderCustomerAccessUserExternalId", "OrderDeliveryAddressExternalId", "OrderDeliveryAddressLocationCode", "OrderDeliveryAddressShipmentMethodCode", "OrderDeliveryAddressShippingAgentCode", "OrderDeliveryAddressShippingAgentServiceCode", "OrderLineCalculatedDiscountPercentage"]); - columns = columns.Substring(0, columns.Length - 2); + columns = columns[..^2]; switch (mapping.SourceTable.Name) { case "EcomOrders": @@ -95,7 +95,7 @@ protected override string GetColumns() } break; case "EcomOrderLines": - columns = columns + ", (-1 * OrderLineTotalDiscountWithVAT) / NULLIF(OrderLinePriceWithVat, 0) * 100 as [OrderLineCalculatedDiscountPercentage]"; + columns += ", (-1 * OrderLineTotalDiscountWithVAT) / NULLIF(OrderLinePriceWithVat, 0) * 100 as [OrderLineCalculatedDiscountPercentage]"; break; } @@ -111,11 +111,11 @@ protected override string GetColumns() private string GetWhereSql(bool exportNotExportedOrders, bool exportOnlyOrdersWithoutExtID, bool doNotExportCarts) { - List parameters = new List(); + List parameters = []; string conditionalsSql = MappingExtensions.GetConditionalsSql(out parameters, mapping.Conditionals, false, false); if (conditionalsSql != "") { - conditionalsSql = conditionalsSql.Substring(0, conditionalsSql.Length - 4); + conditionalsSql = conditionalsSql[..^4]; foreach (SqlParameter p in parameters) _command.Parameters.Add(p); } @@ -147,7 +147,7 @@ protected override string GetFromTables() switch (mapping.SourceTable.Name) { case "EcomOrderLines": - result = result + " inner join EcomOrders on EcomOrderLines.OrderLineOrderID = EcomOrders.OrderID"; + result += " inner join EcomOrders on EcomOrderLines.OrderLineOrderID = EcomOrders.OrderID"; break; case "EcomOrders": result = "[dbo].[EcomOrders] left join dbo.AccessUser on OrderCustomerAccessUserID = AccessUserID left join dbo.AccessUserAddress on OrderDeliveryAddressId = AccessUserAddressId"; @@ -196,7 +196,7 @@ public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlC if (!string.IsNullOrEmpty(orderStateIDAfterExport)) { - sql = sql + string.Format(", OrderStateID = '{0}'", orderStateIDAfterExport); + sql += string.Format(", OrderStateID = '{0}'", orderStateIDAfterExport); } if (_ordersToExport.Count > 100) { @@ -212,7 +212,7 @@ public static void UpdateExportedOrdersInDb(string orderStateIDAfterExport, SqlC command.ExecuteNonQuery(); ClearOrderCache(idsCollection); } - taken = taken + step; + taken += step; } } else From 74e628fa860daf4a3f975591d88cf4c1b998deae Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort Date: Thu, 28 Nov 2024 09:19:00 +0100 Subject: [PATCH 4/6] mode two .Find() out, so it is only check once for eachtable mapping. --- src/OrderProvider.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/OrderProvider.cs b/src/OrderProvider.cs index ed51b80..7563a25 100644 --- a/src/OrderProvider.cs +++ b/src/OrderProvider.cs @@ -40,6 +40,8 @@ public class OrderProvider : BaseSqlProvider, IParameterOptions, ISource, IDesti private ColumnMapping OrderShippingMethodCodeMapping = null; private ColumnMapping OrderPaymentMethodCodeMapping = null; + private ColumnMapping OrderDeliveryAddressExternalIdMapping = null; + private ColumnMapping OrderCustomerAccessUserExternalIdMapping = null; [AddInParameter("Export not yet exported Orders"), AddInParameterEditor(typeof(YesNoParameterEditor), ""), AddInParameterGroup("Source")] public virtual bool ExportNotExportedOrders { get; set; } @@ -378,6 +380,7 @@ public override bool RunJob(Job job) using (var reader = job.Source.GetReader(mapping)) { var columnMappings = new ColumnMappingCollection(MappingExtensions.ReplaceKeyColumnsWithAutoIdIfExists(mapping)); + FindColumnMappings(columnMappings); var writer = new OrderDestinationWriter(mapping, Connection, Logger, SkipFailingRows, DiscardDuplicates); while (!reader.IsDone()) { @@ -438,6 +441,12 @@ public override bool RunJob(Job job) return true; } + private void FindColumnMappings(ColumnMappingCollection columnMappings) + { + OrderDeliveryAddressExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0); + OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); + } + IEnumerable IParameterOptions.GetParameterOptions(string parameterName) { var result = new List(); @@ -736,8 +745,7 @@ private void ProcessOrderDeliveryAddress(ColumnMappingCollection columnMappings, { if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderDeliveryAddressId)) { - object addressId = DBNull.Value; - var OrderDeliveryAddressExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0); + object addressId = DBNull.Value; if (OrderDeliveryAddressExternalIdMapping != null && OrderDeliveryAddressExternalIdMapping.SourceColumn != null) { if (row.ContainsKey(OrderDeliveryAddressExternalIdMapping.SourceColumn.Name)) @@ -760,8 +768,7 @@ private void ProcessOrderCustomerAccessUser(ColumnMappingCollection columnMappin { if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderCustomerAccessUserId)) { - object accessUserId = DBNull.Value; - var OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); + object accessUserId = DBNull.Value; if (OrderCustomerAccessUserExternalIdMapping != null && OrderCustomerAccessUserExternalIdMapping.SourceColumn != null) { if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name)) From 60af6410cf539276382d4bb9b42db40484e259d8 Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort Date: Thu, 28 Nov 2024 09:21:40 +0100 Subject: [PATCH 5/6] code-review feedback, using IsNull in sql query --- src/OrderProvider.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OrderProvider.cs b/src/OrderProvider.cs index 7563a25..e99e4ff 100644 --- a/src/OrderProvider.cs +++ b/src/OrderProvider.cs @@ -606,7 +606,7 @@ private Dictionary ExistingAddresses if (_existingAddresses == null) { _existingAddresses = []; - SqlDataAdapter usersDataAdapter = new("select AccessUserAddressExternalId, AccessUserAddressId from AccessUserAddress where AccessUserAddressExternalId is not null and AccessUserAddressExternalId <> ''", Connection); + SqlDataAdapter usersDataAdapter = new("select AccessUserAddressExternalId, AccessUserAddressId from AccessUserAddress where IsNull(AccessUserAddressExternalId, '') <> ''", Connection); new SqlCommandBuilder(usersDataAdapter); DataSet dataSet = new(); usersDataAdapter.Fill(dataSet); @@ -636,7 +636,7 @@ private Dictionary ExistingUsers if (_existingUsers == null) { _existingUsers = []; - SqlDataAdapter usersDataAdapter = new("select AccessUserExternalID, AccessUserID from AccessUser where AccessUserExternalID is not null and AccessUserExternalID <> ''", Connection); + SqlDataAdapter usersDataAdapter = new("select AccessUserExternalID, AccessUserID from AccessUser where IsNull(AccessUserExternalID, '') <> ''", Connection); new SqlCommandBuilder(usersDataAdapter); DataSet dataSet = new(); usersDataAdapter.Fill(dataSet); From 3e555870e1df4eed964b6ac3f760ca70a65c8ad9 Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort Date: Thu, 28 Nov 2024 11:07:30 +0100 Subject: [PATCH 6/6] added switch/case in new function FindColumnMappings --- src/OrderProvider.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/OrderProvider.cs b/src/OrderProvider.cs index e99e4ff..c1246fa 100644 --- a/src/OrderProvider.cs +++ b/src/OrderProvider.cs @@ -380,7 +380,7 @@ public override bool RunJob(Job job) using (var reader = job.Source.GetReader(mapping)) { var columnMappings = new ColumnMappingCollection(MappingExtensions.ReplaceKeyColumnsWithAutoIdIfExists(mapping)); - FindColumnMappings(columnMappings); + FindColumnMappings(mapping, columnMappings); var writer = new OrderDestinationWriter(mapping, Connection, Logger, SkipFailingRows, DiscardDuplicates); while (!reader.IsDone()) { @@ -441,10 +441,17 @@ public override bool RunJob(Job job) return true; } - private void FindColumnMappings(ColumnMappingCollection columnMappings) + private void FindColumnMappings(Mapping mapping, ColumnMappingCollection columnMappings) { - OrderDeliveryAddressExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0); - OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); + switch (mapping.DestinationTable.Name) + { + case "EcomOrders": + OrderDeliveryAddressExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderDeliveryAddressExternalId, true) == 0); + OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0); + break; + default: + break; + } } IEnumerable IParameterOptions.GetParameterOptions(string parameterName)