Skip to content

Commit

Permalink
Merge pull request #25 from dynamicweb/mss/16537
Browse files Browse the repository at this point in the history
Split the provider into Source and Destination. fixed the Destination…
  • Loading branch information
frederik5480 authored Jun 3, 2024
2 parents f5484c3 + efacc8e commit 7cb48e7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.15</Version>
<Version>10.0.16</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Title>Excel Provider</Title>
<Description>Excel Provider</Description>
Expand Down
73 changes: 32 additions & 41 deletions src/ExcelDestinationWriter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Dynamicweb.Core;
using Dynamicweb.DataIntegration.Integration;
using Dynamicweb.DataIntegration.Integration.Interfaces;
using Dynamicweb.DataIntegration.ProviderHelpers;
using Dynamicweb.Logging;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -60,6 +62,10 @@ public virtual MappingCollection Mappings

public virtual void Write(Dictionary<string, object> row)
{
//need to change "var cultureInfo = CultureInfo.CurrentCulture" into "columnMapping.Mapping.Job.Culture" inside
// foreach (ColumnMapping columnMapping in _currentColumnMappings.Where(cm => cm.Active)), once DW10 is released
var cultureInfo = CultureInfo.CurrentCulture;

if (tableForExcel == null)
{
tableForExcel = GetTableForExcel();
Expand All @@ -69,62 +75,47 @@ public virtual void Write(Dictionary<string, object> row)

foreach (ColumnMapping columnMapping in _currentColumnMappings.Where(cm => cm.Active))
{
if (columnMapping.ScriptType != ScriptType.None)
if (columnMapping.HasScriptWithValue)
{
string evaluatedValue = null;
switch (columnMapping.ScriptType)
if (columnMapping.SourceColumn.Type == typeof(DateTime))
{
case ScriptType.Append:
evaluatedValue = GetValue(columnMapping, row) + columnMapping.ScriptValue;
break;
case ScriptType.Prepend:
evaluatedValue = columnMapping.ScriptValue + GetValue(columnMapping, row);
break;
case ScriptType.Constant:
evaluatedValue = columnMapping.GetScriptValue();
break;
case ScriptType.NewGuid:
evaluatedValue = columnMapping.GetScriptValue();
break;
DateTime theDate = DateTime.Parse(columnMapping.GetScriptValue(), CultureInfo.InvariantCulture);
r[columnMapping.DestinationColumn.Name] = theDate.ToString("dd-MM-yyyy HH:mm:ss:fff", cultureInfo);
}
else if (columnMapping.SourceColumn.Type == typeof(decimal) ||
columnMapping.SourceColumn.Type == typeof(double) ||
columnMapping.SourceColumn.Type == typeof(float))
{
r[columnMapping.DestinationColumn.Name] = ValueFormatter.GetFormattedValue(columnMapping.GetScriptValue(), cultureInfo, columnMapping.ScriptType, columnMapping.ScriptValue);
}
else
{
r[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue();
}

r[columnMapping.DestinationColumn.Name] = evaluatedValue;
}
else
else if (row[columnMapping.SourceColumn.Name] == DBNull.Value)
{
r[columnMapping.DestinationColumn.Name] = "NULL";
}
else if (columnMapping.SourceColumn.Type == typeof(DateTime))
{
if (row[columnMapping.SourceColumn.Name] == DBNull.Value)
if (DateTime.TryParse(columnMapping.ConvertInputValueToOutputValue(row[columnMapping.SourceColumn.Name])?.ToString(), out var theDateTime))
{
r[columnMapping.DestinationColumn.Name] = "NULL";
r[columnMapping.DestinationColumn.Name] = theDateTime.ToString("dd-MM-yyyy HH:mm:ss:fff", cultureInfo);
}
else
{
string evaluatedValue = GetValue(columnMapping, row);
if (!string.IsNullOrEmpty(evaluatedValue))
{
r[columnMapping.DestinationColumn.Name] = evaluatedValue;
}
r[columnMapping.DestinationColumn.Name] = DateTime.MinValue.ToString("dd-MM-yyyy HH:mm:ss:fff", CultureInfo.InvariantCulture);
}
}
else
{
r[columnMapping.DestinationColumn.Name] = string.Format(cultureInfo, "{0}", columnMapping.ConvertInputValueToOutputValue(row[columnMapping.SourceColumn.Name]));
}
}

tableForExcel.Rows.Add(r);
}

private string GetValue(ColumnMapping columnMapping, Dictionary<string, object> row)
{
if (columnMapping.SourceColumn.Type == typeof(string) || columnMapping.SourceColumn.Type == typeof(int) || columnMapping.SourceColumn.Type == typeof(double)
|| columnMapping.SourceColumn.Type == typeof(float) || columnMapping.SourceColumn.Type == typeof(decimal) || columnMapping.SourceColumn.Type == typeof(bool)
|| columnMapping.SourceColumn.Type == typeof(long))
{
return row[columnMapping.SourceColumn.Name].ToString();
}
else if (columnMapping.SourceColumn.Type == typeof(DateTime))
{
return ((DateTime)row[columnMapping.SourceColumn.Name]).ToString("dd-MM-yyyy HH:mm:ss:fff");
}
return null;
}

private DataTable GetTableForExcel()
{
var table = new DataTable(currentMapping.DestinationTable.Name);
Expand Down
47 changes: 25 additions & 22 deletions src/ExcelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public string DestinationFile

public override Schema GetOriginalDestinationSchema()
{
return GetSchema();
return schema = new Schema();
}

public override bool SchemaIsEditable => true;
Expand Down Expand Up @@ -96,7 +96,7 @@ public override Schema GetOriginalSourceSchema()
return result;
}

private string workingDirectory;
private string workingDirectory = SystemInformation.MapPath("/Files/");
public override string WorkingDirectory
{
get
Expand Down Expand Up @@ -128,7 +128,6 @@ public override void UpdateSourceSettings(ISource source)
{
ExcelProvider newProvider = (ExcelProvider)source;
SourceFile = newProvider.SourceFile;
DestinationFolder = newProvider.DestinationFolder;
}

public override string Serialize()
Expand All @@ -145,13 +144,17 @@ public override string Serialize()
return document.ToString();
}

public new virtual void SaveAsXml(XmlTextWriter xmlTextWriter)
void ISource.SaveAsXml(XmlTextWriter xmlTextWriter)
{
xmlTextWriter.WriteElementString("SourcePath", SourceFile);
(this as ISource).GetSchema().SaveAsXml(xmlTextWriter);
}

void IDestination.SaveAsXml(XmlTextWriter xmlTextWriter)
{
xmlTextWriter.WriteElementString("DestinationFile", DestinationFile);
xmlTextWriter.WriteElementString("DestinationFolder", DestinationFolder);
xmlTextWriter.WriteElementString("WorkingDirectory", WorkingDirectory);
GetSchema().SaveAsXml(xmlTextWriter);
(this as IDestination).GetSchema().SaveAsXml(xmlTextWriter);
}

public new ISourceReader GetReader(Mapping mapping)
Expand Down Expand Up @@ -183,8 +186,9 @@ public override void Close()

public override void UpdateDestinationSettings(IDestination destination)
{
ISource newProvider = (ISource)destination;
UpdateSourceSettings(newProvider);
ExcelProvider newProvider = (ExcelProvider)destination;
newProvider.DestinationFile = DestinationFile;
newProvider.DestinationFolder = DestinationFolder;
}

public override bool RunJob(Job job)
Expand Down Expand Up @@ -261,12 +265,12 @@ private void GetSchemaForTableFromFile(Schema schema, Dictionary<string, ExcelRe
columnCount = dt.Columns.Count;
}
foreach (System.Data.DataColumn c in dt.Columns)
{
{
Column column = new Column(c.ColumnName, c.DataType, excelTable);
if(!string.IsNullOrEmpty(c.Caption) && !string.Equals(c.Caption, c.ColumnName, StringComparison.OrdinalIgnoreCase))
if (!string.IsNullOrEmpty(c.Caption) && !string.Equals(c.Caption, c.ColumnName, StringComparison.OrdinalIgnoreCase))
{
column.NameWithWhitespaceStripped = c.Caption;
}
}
excelTable.AddColumn(column);
}

Expand All @@ -282,12 +286,15 @@ private void GetSchemaForTableFromFile(Schema schema, Dictionary<string, ExcelRe
}
}

public override Schema GetSchema()
Schema IDestination.GetSchema()
{
if (schema == null)
{
schema = GetOriginalSourceSchema();
}
schema ??= new Schema();
return schema;
}

Schema ISource.GetSchema()
{
schema ??= GetOriginalSourceSchema();
return schema;
}

Expand Down Expand Up @@ -326,16 +333,11 @@ public ExcelProvider(XmlNode xmlNode)
DestinationFolder = node.FirstChild.Value;
}
break;
case "WorkingDirectory":
if (node.HasChildNodes)
{
WorkingDirectory = node.FirstChild.Value;
}
break;

}
}
}

internal ExcelProvider(Dictionary<string, ExcelReader> excelReaders, Schema schema, ExcelDestinationWriter writer)
{
this.schema = schema;
Expand All @@ -354,6 +356,7 @@ public override void OverwriteSourceSchemaToOriginal()

public override void OverwriteDestinationSchemaToOriginal()
{
schema = new Schema();
}

public override string ValidateDestinationSettings()
Expand Down

0 comments on commit 7cb48e7

Please sign in to comment.