Skip to content

Commit 033fb55

Browse files
committed
Some code cleanup and refactoring for import framework
1 parent 4b62bcb commit 033fb55

23 files changed

Lines changed: 129 additions & 477 deletions

File tree

src/Libraries/SmartStore.Core/SmartStore.Core.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@
163163
<Compile Include="ComponentModel\TypeConversion\TypeConverterAdapter.cs" />
164164
<Compile Include="ComponentModel\TypeConversion\TypeConverterBase.cs" />
165165
<Compile Include="ComponentModel\TypeConversion\TypeConverterFactory.cs" />
166-
<Compile Include="Data\DataTable\IDataTable.cs" />
167-
<Compile Include="Data\DataTable\LightweightDataTable.cs" />
168166
<Compile Include="Data\IDbContextExtensions.cs" />
169167
<Compile Include="Data\ITransaction.cs" />
170168
<Compile Include="Domain\Catalog\PriceDisplayType.cs" />
@@ -225,10 +223,6 @@
225223
<Compile Include="Data\Hooks\PreInsertHook.cs" />
226224
<Compile Include="Data\Hooks\PreUpdateHook.cs" />
227225
<Compile Include="Data\IDbContext.cs" />
228-
<Compile Include="Data\Impex\ImportModeFlags.cs" />
229-
<Compile Include="Data\Impex\ImportMessage.cs" />
230-
<Compile Include="Data\Impex\ImportProgressInfo.cs" />
231-
<Compile Include="Data\Impex\ImportResult.cs" />
232226
<Compile Include="Data\RepositoryExtensions.cs" />
233227
<Compile Include="Data\SqlFileTokenizer.cs" />
234228
<Compile Include="Domain\Catalog\ProductBundleData.cs" />

src/Libraries/SmartStore.Core/Data/DataTable/IDataTable.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/DataTable/IDataTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace SmartStore.Core.Data
4+
namespace SmartStore.Services.DataExchange.Import
55
{
66
public interface IDataColumn
77
{

src/Libraries/SmartStore.Core/Data/DataTable/LightweightDataTable.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/DataTable/LightweightDataTable.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
using System.Data.Common;
66
using System.Dynamic;
77
using System.Globalization;
8+
using System.IO;
89
using System.Linq;
910
using System.Text;
1011
using System.Threading.Tasks;
1112
using System.Web;
13+
using SmartStore.Services.DataExchange.Csv;
14+
using SmartStore.Services.DataExchange.Excel;
1215

13-
namespace SmartStore.Core.Data
16+
namespace SmartStore.Services.DataExchange.Import
1417
{
1518
public class LightweightDataTable : IDataTable
1619
{
@@ -74,6 +77,70 @@ public IList<IDataRow> Rows
7477
}
7578
}
7679

80+
public static IDataTable FromPostedFile(
81+
HttpPostedFileBase file,
82+
int skip = 0,
83+
int take = int.MaxValue)
84+
{
85+
return FromPostedFile(file, new CsvConfiguration(), skip, take);
86+
}
87+
88+
public static IDataTable FromPostedFile(
89+
HttpPostedFileBase file,
90+
CsvConfiguration configuration,
91+
int skip = 0,
92+
int take = int.MaxValue)
93+
{
94+
Guard.ArgumentNotNull(() => file);
95+
Guard.ArgumentNotNull(() => configuration);
96+
97+
if (file.ContentLength == 0)
98+
{
99+
throw Error.Argument("file", "The posted file '{0}' does not contain any data.".FormatInvariant(file.FileName)); // TODO Loc
100+
}
101+
102+
IDataReader dataReader = null;
103+
104+
try
105+
{
106+
var fileExt = System.IO.Path.GetExtension(file.FileName).ToLowerInvariant();
107+
108+
switch (fileExt)
109+
{
110+
case ".xlsx":
111+
dataReader = new ExcelDataReader(file.InputStream, true); // TODO: let the user specify if excel file has headers
112+
break;
113+
default:
114+
dataReader = new CsvDataReader(new StreamReader(file.InputStream), configuration);
115+
break;
116+
}
117+
118+
var table = LightweightDataTable.FromDataReader(dataReader);
119+
120+
if (table.Columns.Count == 0 || table.Rows.Count == 0)
121+
{
122+
throw Error.InvalidOperation("file", "The posted file '{0}' does not contain any columns or data rows.".FormatInvariant(file.FileName)); // TODO Loc
123+
}
124+
125+
return table;
126+
}
127+
catch
128+
{
129+
throw;
130+
}
131+
finally
132+
{
133+
if (dataReader != null)
134+
{
135+
if (!dataReader.IsClosed)
136+
{
137+
dataReader.Dispose();
138+
}
139+
dataReader = null;
140+
}
141+
}
142+
}
143+
77144
public static IDataTable FromDataReader(
78145
IDataReader reader,
79146
int skip = 0,

src/Libraries/SmartStore.Services/ExportImport/IImportManager.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/IImportManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.IO;
32
using System.Threading;
4-
using SmartStore.Core.Data;
53

6-
namespace SmartStore.Services.ExportImport
4+
namespace SmartStore.Services.DataExchange.Import
75
{
86
/// <summary>
97
/// Import manager interface

src/Libraries/SmartStore.Services/ExportImport/ImportDataSegmenter.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/ImportDataSegmenter.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Globalization;
45
using System.IO;
56
using System.Linq;
6-
using System.Linq.Expressions;
7-
using System.Reflection;
8-
using System.Text;
9-
using OfficeOpenXml;
107
using SmartStore.Core;
11-
using SmartStore.Core.Data;
12-
using SmartStore.Utilities.Reflection;
138

14-
namespace SmartStore.Services.ExportImport
9+
namespace SmartStore.Services.DataExchange.Import
1510
{
1611
public class ImportDataSegmenter<T> where T : BaseEntity
1712
{
1813
private const int BATCHSIZE = 100;
1914

2015
private IDataTable _table;
21-
private IList<ImportRow<T>> _currentBatch;
16+
private ImportRow<T>[] _currentBatch;
2217
private IPageable _pageable;
2318
private bool _bof;
2419

@@ -30,6 +25,13 @@ public ImportDataSegmenter(IDataTable table)
3025

3126
_bof = true;
3227
_pageable = new PagedList(0, BATCHSIZE, table.Rows.Count);
28+
Culture = CultureInfo.InvariantCulture;
29+
}
30+
31+
public CultureInfo Culture
32+
{
33+
get;
34+
set;
3335
}
3436

3537
public int TotalRows
@@ -66,7 +68,6 @@ public void Reset()
6668
{
6769
if (_pageable.PageIndex != 0 && _currentBatch != null)
6870
{
69-
_currentBatch.Clear();
7071
_currentBatch = null;
7172
}
7273
_bof = true;
@@ -77,7 +78,6 @@ public bool ReadNextBatch()
7778
{
7879
if (_currentBatch != null)
7980
{
80-
_currentBatch.Clear();
8181
_currentBatch = null;
8282
}
8383

@@ -103,19 +103,21 @@ public ImportRow<T>[] CurrentBatch
103103
{
104104
if (_currentBatch == null)
105105
{
106-
_currentBatch = new List<ImportRow<T>>();
106+
_currentBatch = new ImportRow<T>[BATCHSIZE];
107107

108-
int start = _pageable.FirstItemIndex;
109-
int end = _pageable.LastItemIndex;
108+
int start = _pageable.FirstItemIndex - 1;
109+
int end = _pageable.LastItemIndex - 1;
110110

111111
// Determine values per row
112+
int i = 0;
112113
for (int r = start; r <= end; r++)
113114
{
114-
_currentBatch.Add(new ImportRow<T>(_table.Rows[r], r));
115+
_currentBatch[i] = new ImportRow<T>(this, _table.Rows[r], r);
116+
i++;
115117
}
116118
}
117119

118-
return _currentBatch.ToArray();
120+
return _currentBatch;
119121
}
120122
}
121123
}

src/Libraries/SmartStore.Services/ExportImport/ImportManager.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/ImportManager.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
using SmartStore.Services.Stores;
2020
using SmartStore.Core.Domain.Stores;
2121
using SmartStore.Core;
22+
using System.Globalization;
2223

23-
namespace SmartStore.Services.ExportImport
24+
namespace SmartStore.Services.DataExchange.Import
2425
{
2526
/// <summary>
2627
/// Import manager
@@ -751,22 +752,10 @@ private void ProcessProductPictures(ImportRow<Product>[] batch, ImportResult res
751752
_eventPublisher.EntityInserted(lastInserted);
752753
}
753754

754-
//private DateTime? OADateToUtcDate(object value)
755-
//{
756-
// double oaDate;
757-
// if (CommonHelper.TryConvert<double>(value, out oaDate) && oaDate != 0)
758-
// {
759-
// return DateTime.FromOADate(Convert.ToDouble(oaDate));
760-
// }
761-
762-
// return null;
763-
//}
764-
765-
766-
private int? ZeroToNull(object value)
755+
private int? ZeroToNull(object value, CultureInfo culture)
767756
{
768757
int result;
769-
if (CommonHelper.TryConvert<int>(value, out result) && result > 0)
758+
if (CommonHelper.TryConvert<int>(value, culture, out result) && result > 0)
770759
{
771760
return result;
772761
}

src/Libraries/SmartStore.Core/Data/Impex/ImportMessage.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/ImportMessage.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42

5-
namespace SmartStore.Core.Data
3+
namespace SmartStore.Services.DataExchange.Import
64
{
75

86
public class ImportMessage

src/Libraries/SmartStore.Core/Data/Impex/ImportModeFlags.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/ImportModeFlags.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace SmartStore.Core.Data
3+
namespace SmartStore.Services.DataExchange.Import
44
{
55

66
[Flags]

src/Libraries/SmartStore.Core/Data/Impex/ImportProgressInfo.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/ImportProgressInfo.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

7-
namespace SmartStore.Core.Data
3+
namespace SmartStore.Services.DataExchange.Import
84
{
95

106
public class ImportProgressInfo

src/Libraries/SmartStore.Core/Data/Impex/ImportResult.cs renamed to src/Libraries/SmartStore.Services/DataExchange/Import/ImportResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54

6-
namespace SmartStore.Core.Data
5+
namespace SmartStore.Services.DataExchange.Import
76
{
87

98
public class ImportResult

0 commit comments

Comments
 (0)