Skip to content

Commit

Permalink
Merge pull request #11 from dynamicweb/frn/15477-cleanup-dependencies
Browse files Browse the repository at this point in the history
Cleanup and moving funcitionality to bases
  • Loading branch information
frederik5480 authored Oct 17, 2023
2 parents 7f26f1b + 165ba0f commit 1662caa
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 468 deletions.
10 changes: 4 additions & 6 deletions src/Dynamicweb.DataIntegration.Providers.OrderProvider.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.2</Version>
<Version>10.0.3</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Description>Order Provider</Description>
</PropertyGroup>
Expand All @@ -22,10 +22,8 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.0.0" />
<PackageReference Include="Dynamicweb.DataIntegration.Providers.DynamicwebProvider" Version="10.0.1" />
<PackageReference Include="Dynamicweb.DataIntegration.Providers.SqlProvider" Version="10.0.0" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.0.0" />
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.0.14" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.0.9" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>
</Project>
74 changes: 74 additions & 0 deletions src/OrderDestinationWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Dynamicweb.DataIntegration.Integration;
using Dynamicweb.DataIntegration.ProviderHelpers;
using Dynamicweb.Logging;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Dynamicweb.DataIntegration.Providers.OrderProvider;

internal class OrderDestinationWriter : BaseSqlWriter
{
private new Mapping Mapping { get; }
private ILogger Logger { get; }
private SqlBulkCopy SqlBulkCopier { get; }
private int SkippedFailedRowsCount { get; set; }
private DataTable TableToWrite { get; set; }
private DataSet DataToWrite { get; } = new DataSet();
private string TempTablePrefix { get; }
private bool SkipFailingRows { get; }
internal SqlCommand SqlCommand { get; }
internal int RowsToWriteCount { get; set; }

public OrderDestinationWriter(Mapping mapping, SqlConnection connection, ILogger logger, bool skipFailingRows)
{
Mapping = mapping;
SqlCommand = connection.CreateCommand();
SqlCommand.CommandTimeout = 1200;
Logger = logger;
SkipFailingRows = skipFailingRows;
TempTablePrefix = $"TempTableForBulkImport{mapping.GetId()}";
SqlBulkCopier = new SqlBulkCopy(connection);
SqlBulkCopier.DestinationTableName = mapping.DestinationTable.Name + TempTablePrefix;
SqlBulkCopier.BulkCopyTimeout = 0;
Initialize();
if (connection.State != ConnectionState.Open)
connection.Open();
}

protected new virtual void Initialize()
{
List<SqlColumn> destColumns = new();
var columnMappings = Mapping.GetColumnMappings();
foreach (ColumnMapping columnMapping in columnMappings.DistinctBy(obj => obj.DestinationColumn.Name))
{
destColumns.Add((SqlColumn)columnMapping.DestinationColumn);
}
if (Mapping.DestinationTable != null && Mapping.DestinationTable.Name == "EcomAssortmentPermissions")
{
if (columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, "AssortmentPermissionAccessUserID", true) == 0) == null)
destColumns.Add(new SqlColumn("AssortmentPermissionAccessUserID", typeof(string), SqlDbType.Int, null, -1, false, true, false));
}
SQLTable.CreateTempTable(SqlCommand, Mapping.DestinationTable.SqlSchema, Mapping.DestinationTable.Name, TempTablePrefix, destColumns, Logger);

TableToWrite = DataToWrite.Tables.Add(Mapping.DestinationTable.Name + TempTablePrefix);
foreach (SqlColumn column in destColumns)
{
TableToWrite.Columns.Add(column.Name, column.Type);
}
}

internal void FinishWriting()
{
SkippedFailedRowsCount = SqlBulkCopierWriteToServer(SqlBulkCopier, TableToWrite, SkipFailingRows, Mapping, Logger);
if (TableToWrite.Rows.Count != 0)
{
RowsToWriteCount = RowsToWriteCount + TableToWrite.Rows.Count - SkippedFailedRowsCount;
Logger.Log("Added " + RowsToWriteCount + " rows to temporary table for " + Mapping.DestinationTable.Name + ".");
}
}

internal void MoveDataToMainTable(SqlTransaction sqlTransaction, bool updateOnlyExistingRecords, bool insertOnlyNewRecords) =>
MoveDataToMainTable(Mapping, SqlCommand, sqlTransaction, TempTablePrefix, updateOnlyExistingRecords, insertOnlyNewRecords);
}
Loading

0 comments on commit 1662caa

Please sign in to comment.