Skip to content

Commit a558b35

Browse files
committed
Added ImportDataSegmenter.GetColumnIndexes() method
1 parent 697fac4 commit a558b35

4 files changed

Lines changed: 87 additions & 12 deletions

File tree

src/Libraries/SmartStore.Services/DataExchange/Import/ImportDataSegmenter.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public class ImportDataSegmenter<T> where T : BaseEntity
1212
{
1313
private const int BATCHSIZE = 100;
1414

15-
private IDataTable _table;
15+
private readonly IDataTable _table;
1616
private ImportRow<T>[] _currentBatch;
17-
private IPageable _pageable;
17+
private readonly IPageable _pageable;
1818
private bool _bof;
1919
private CultureInfo _culture;
2020
private ColumnMap _columnMap;
2121

22+
private readonly IDictionary<string, string[]> _columnIndexes = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
23+
2224
public ImportDataSegmenter(IDataTable table)
2325
{
2426
Guard.ArgumentNotNull(() => table);
@@ -95,6 +97,42 @@ public bool HasDataColumn(string name, string index)
9597
return _table.HasColumn(_columnMap.GetMappedName(name, index));
9698
}
9799

100+
/// <summary>
101+
/// Returns an array of exisiting index names for a column
102+
/// </summary>
103+
/// <param name="name">The name of the columns without index qualification</param>
104+
/// <returns>An array of index names</returns>
105+
/// <remarks>
106+
/// If following columns exist in source: Attr[Color], Attr[Size]
107+
/// This method returns: <code>string[] { "Color", "Size" }</code>
108+
/// </remarks>
109+
public string[] GetColumnIndexes(string name)
110+
{
111+
string[] indexes;
112+
113+
if (!_columnIndexes.TryGetValue(name, out indexes))
114+
{
115+
var startsWith = name + "[";
116+
117+
var columns1 = _columnMap.Mappings
118+
.Where(x => x.Key.StartsWith(startsWith, StringComparison.OrdinalIgnoreCase))
119+
.Select(x => x.Key);
120+
121+
var columns2 = _table.Columns
122+
.Where(x => x.Name.StartsWith(startsWith, StringComparison.OrdinalIgnoreCase))
123+
.Select(x => x.Name);
124+
125+
indexes = columns1.Concat(columns2)
126+
.Distinct(StringComparer.OrdinalIgnoreCase)
127+
.Select(x => x.Substring(x.IndexOf("[", StringComparison.OrdinalIgnoreCase) + 1).TrimEnd(']'))
128+
.ToArray();
129+
130+
_columnIndexes[name] = indexes;
131+
}
132+
133+
return indexes;
134+
}
135+
98136
public void Reset()
99137
{
100138
if (_pageable.PageIndex != 0 && _currentBatch != null)

src/Libraries/SmartStore.Services/DataExchange/Import/ImportRow.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ImportRow<T> where T : BaseEntity
1414
private bool _initialized = false;
1515
private T _entity;
1616
private string _entityDisplayName;
17-
private int _position;
17+
private readonly int _position;
1818
private bool _isNew;
1919
private ImportRowInfo _rowInfo;
2020

@@ -130,15 +130,9 @@ public bool SetProperty<TProp>(
130130
}
131131
else
132132
{
133-
if (value.ToString().ToUpper().Equals("NULL"))
134-
{
135-
// prop is set "explicitly" to null.
136-
converted = default(TProp);
137-
}
138-
else
139-
{
140-
converted = value.Convert<TProp>(_segmenter.Culture);
141-
}
133+
converted = value.ToString().ToUpper().Equals("NULL")
134+
? default(TProp)
135+
: value.Convert<TProp>(_segmenter.Culture);
142136
}
143137

144138
fastProp.SetValue(target, converted);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using SmartStore.Core.Domain.Catalog;
8+
using SmartStore.Services.DataExchange.Import;
9+
using SmartStore.Tests;
10+
11+
namespace SmartStore.Services.Tests.DataExchange
12+
{
13+
[TestFixture]
14+
public class ImportDataSegmenterTests
15+
{
16+
[Test]
17+
public void CanResolveColumnIndexNames()
18+
{
19+
var columns = new List<IDataColumn>
20+
{
21+
new LightweightDataColumn("Attr[Color]", typeof(string)),
22+
new LightweightDataColumn("Attr[Size]", typeof(string)),
23+
new LightweightDataColumn("material", typeof(string)),
24+
new LightweightDataColumn("Name[en]", typeof(string)),
25+
new LightweightDataColumn("Name[de]", typeof(string)),
26+
new LightweightDataColumn("Name[fr]", typeof(string)),
27+
new LightweightDataColumn("name_it", typeof(string)),
28+
};
29+
30+
var table = new LightweightDataTable(columns, new List<object[]>());
31+
var segmenter = new ImportDataSegmenter<Product>(table);
32+
segmenter.ColumnMap.AddMapping("Attr[Material]", "material");
33+
segmenter.ColumnMap.AddMapping("Name[it]", "name_it");
34+
35+
var attrs = segmenter.GetColumnIndexes("Attr");
36+
attrs.ShouldSequenceEqual(new string[] {"Material", "Color", "Size"});
37+
38+
var langs = segmenter.GetColumnIndexes("Name");
39+
langs.ShouldSequenceEqual(new string[] { "it", "en", "de", "fr" });
40+
}
41+
}
42+
}

src/Tests/SmartStore.Services.Tests/SmartStore.Services.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<Compile Include="Customers\CustomerRegistrationServiceTests.cs" />
107107
<Compile Include="DataExchange\CsvWriterTests.cs" />
108108
<Compile Include="DataExchange\DataReaderTests.cs" />
109+
<Compile Include="DataExchange\ImportDataSegmenterTests.cs" />
109110
<Compile Include="DataExchange\SyncMappingServiceTests.cs" />
110111
<Compile Include="Directory\CurrencyServiceTests.cs" />
111112
<Compile Include="Directory\TestExchangeRateProvider.cs" />

0 commit comments

Comments
 (0)