-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvDataReader.cs
More file actions
249 lines (215 loc) · 8.96 KB
/
CsvDataReader.cs
File metadata and controls
249 lines (215 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CsvHelper;
using CsvHelper.Configuration;
using VirtoCommerce.ImportModule.Core.Models;
using VirtoCommerce.ImportModule.Core.Services;
using VirtoCommerce.Platform.Core.Settings;
namespace VirtoCommerce.ImportModule.CsvHelper
{
public class CsvDataReader<TCsvImportable, TCsvClassMap> : IImportDataReader where TCsvClassMap : ClassMap
{
private readonly int _pageSize;
private readonly bool _needReadRaw;
private bool _disposed;
protected CsvConfiguration CsvConfiguration { get; set; }
protected readonly Stream Stream;
protected readonly Stream CountStream;
protected readonly CsvReader CsvReader;
protected string HeaderRaw;
protected int? TotalCount;
public bool HasMoreResults { get; private set; } = true;
public CsvDataReader(Stream stream, ImportContext context, bool needReadRaw = false)
{
CsvConfiguration = GetConfiguration(context);
Stream = stream;
CsvReader = new CsvReader(new StreamReader(Stream), CsvConfiguration);
CsvReader.Context.RegisterClassMap<TCsvClassMap>();
_pageSize = Convert.ToInt32(context.ImportProfile.Settings.FirstOrDefault(x => x.Name == CsvSettings.PageSize.Name)?.Value ?? 50);
_needReadRaw = needReadRaw;
}
public CsvDataReader(Stream stream, ImportContext context, CsvConfiguration csvConfiguration, bool needReadRaw = false)
{
CsvConfiguration = MergeWithDefaultConfig(csvConfiguration, context);
Stream = stream;
CsvReader = new CsvReader(new StreamReader(Stream), CsvConfiguration);
CsvReader.Context.RegisterClassMap<TCsvClassMap>();
_pageSize = Convert.ToInt32(context.ImportProfile.Settings.FirstOrDefault(x => x.Name == CsvSettings.PageSize.Name)?.Value ?? 50);
_needReadRaw = needReadRaw;
}
public CsvDataReader(Stream stream, Stream countStream, ImportContext context, bool needReadRaw = false)
: this(stream, context, needReadRaw)
{
CountStream = countStream;
}
public CsvDataReader(Stream stream, Stream countStream, ImportContext context, CsvConfiguration csvConfiguration, bool needReadRaw = false)
: this(stream, context, csvConfiguration, needReadRaw)
{
CountStream = countStream;
}
public virtual async Task<int> GetTotalCountAsync(ImportContext context)
{
if (TotalCount.HasValue)
{
return TotalCount.Value;
}
Stream stream;
bool leaveOpen;
if (Stream.CanSeek)
{
stream = Stream;
leaveOpen = true;
}
else
{
stream = CountStream ?? throw new InvalidOperationException("Count stream is not provided.");
leaveOpen = false;
}
var streamPosition = 0L;
if (stream.CanSeek)
{
streamPosition = stream.Position;
stream.Seek(0, SeekOrigin.Begin);
}
using var csvReader = new CsvReader(new StreamReader(stream), CsvConfiguration, leaveOpen);
await csvReader.ReadAsync();
csvReader.ReadHeader();
HeaderRaw = string.Join(csvReader.Configuration.Delimiter, csvReader.HeaderRecord);
TotalCount = 0;
while (await csvReader.ReadAsync())
{
TotalCount++;
}
if (stream.CanSeek)
{
stream.Seek(streamPosition, SeekOrigin.Begin);
}
return TotalCount.Value;
}
public virtual async Task<object[]> ReadNextPageAsync(ImportContext context)
{
var result = new List<object>();
for (var i = 0; i < _pageSize && HasMoreResults; i++)
{
try
{
HasMoreResults = await CsvReader.ReadAsync();
if (HasMoreResults)
{
var record = CsvReader.GetRecord<TCsvImportable>();
if (!_needReadRaw)
{
result.Add(record);
}
else
{
var rawRecord = CsvReader.Parser.RawRecord.TrimEnd('\r', '\n');
var row = CsvReader.Parser.Row;
result.Add(new CsvImportRecord<TCsvImportable>
{
Row = row,
RawHeader = HeaderRaw,
RawRecord = rawRecord,
Record = record,
});
}
}
}
catch (Exception ex)
{
context.ErrorCallback?.Invoke(new ErrorInfo
{
ErrorMessage = ex.Message,
});
}
}
return result.ToArray();
}
protected virtual CsvConfiguration GetConfiguration(ImportContext context)
{
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = context.ImportProfile.Settings.GetValue<string>(CsvSettings.Delimiter),
PrepareHeaderForMatch = args =>
{
var result = args.Header.ToLower();
return result;
},
Mode = CsvMode.RFC4180,
BadDataFound = null,
//TODO: Temporary disable since it cause false positive errors on CsvMapping when access to csv cell by name in custom mapping converters args.Row["Name"]
//BadDataFound = args =>
//{
// var errorInfo = new ErrorInfo
// {
// ErrorMessage = $"Bad entry found at field {args.Field}",
// ErrorCode = "BadData",
// ErrorLine = args.Context.Parser.Row,
// RawHeader = string.Join(args.Context.Parser.Delimiter, args.Context.Reader.HeaderRecord),
// RawData = args.Context.Parser.RawRecord,
// };
// context.ErrorCallback(errorInfo);
//}
};
if (context.ErrorCallback != null)
{
csvConfiguration.ReadingExceptionOccurred = args =>
{
var errorInfo = new ErrorInfo
{
ErrorMessage = args.Exception.ToString(),
ErrorCode = "CsvHelperException"
};
context.ErrorCallback(errorInfo);
return false;
};
csvConfiguration.MissingFieldFound = args =>
{
var errorInfo = new ErrorInfo
{
ErrorMessage = $"Headers with names {string.Join(", ", args.HeaderNames)} not found",
ErrorCode = "MissingFieldFound",
ErrorLine = args.Context.Parser.Row,
RawHeader = string.Join(args.Context.Parser.Delimiter, args.Context.Reader.HeaderRecord),
RawData = args.Context.Parser.RawRecord,
};
context.ErrorCallback(errorInfo);
};
}
return csvConfiguration;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
CsvReader.Dispose();
Stream?.Dispose();
CountStream?.Dispose();
}
_disposed = true;
}
private CsvConfiguration MergeWithDefaultConfig(CsvConfiguration csvConfiguration, ImportContext context)
{
var defaultCsvConfiguration = GetConfiguration(context);
csvConfiguration.Delimiter ??= defaultCsvConfiguration.Delimiter;
csvConfiguration.PrepareHeaderForMatch ??= defaultCsvConfiguration.PrepareHeaderForMatch;
csvConfiguration.BadDataFound ??= defaultCsvConfiguration.BadDataFound;
csvConfiguration.ReadingExceptionOccurred ??= defaultCsvConfiguration.ReadingExceptionOccurred;
csvConfiguration.MissingFieldFound ??= defaultCsvConfiguration.MissingFieldFound;
return csvConfiguration;
}
}
}