-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExporter.cs
More file actions
432 lines (388 loc) · 15.7 KB
/
Copy pathExporter.cs
File metadata and controls
432 lines (388 loc) · 15.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//-----------------------------------------------------------------------
// <copyright file="Exporter.cs" company="Sergio Inzunza">
// Sergio Inzunza and Contributors
// </copyright>
//-----------------------------------------------------------------------
// This file is part of Linq2Csv and is dual licensed under MS-PL and Apache 2.0.
// https://github.com/inxunxa/Linq2Csv
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Linq2Csv.DataAnnotations;
namespace Linq2Csv
{
/// <summary>
/// Class to Export a Linq Entity to a File
/// </summary>
public class Exporter : IDisposable
{
// class needed fields
private StreamWriter _writer;
private List<string> _csvHeader = new List<string>();
private List<string> _binaryHeader = new List<string>();
private List<List<object>> _csvRows = new List<List<object>>();
private List<List<string>> _binaryRows = new List<List<string>>();
private int _rowsPerObject;
private bool _csvHeaderWriten;
private bool _binaryHeaderWriten;
/// <summary>
/// The string that will be used to separete the columns
/// </summary>
public string Separator { get; set; } = ",";
/// <summary>
/// How enumerable properties will be converted.
/// <value>True</value> will create column(s) for the elements in the Enumerable property
/// <value>False</value> will create a new row for each element in the Enumerable property
/// </summary>
public bool TreatEnumerablesAsColumns { get; set; } = true;
/// <summary>
/// Map an object (without anotations) to Rows
/// </summary>
/// <param name="entity">The entity to export</param>
internal void AutoMap(object entity)
{
if (entity == null) return;
Type objType = entity.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(entity, null);
var elems = propValue as IList;
if (elems != null)
{
for (int i = 0; i < elems.Count; i++)
{
// element in elements will have the same GUID
if (i > 0) NextLine(true);
AutoMap(elems[i]);
}
}
else
{
if (PrimitiveTypes.IsPrimitive(property.PropertyType))
{
// Bottom node reached, consider value
AddRecord(property.DeclaringType?.Name + "." + property.Name, propValue);
}
else
{
// Brach node found, iterate through childs
AutoMap(propValue);
}
}
}
}
/// <summary>
/// Maps and object to rows using the DataAnnotations
/// </summary>
/// <param name="entity">The objecto to be mapped</param>
/// <param name="enumerableColumnId">The string to append to repeated columns (by enumerables) applicable only when <c>TreatEnumerablesAsColumns</c> is True</param>
internal void Map(Object entity, string enumerableColumnId = "")
{
if (entity == null || entity.GetType().GetCustomAttribute<NonExportable>() != null) return;
Type objType = entity.GetType();
// Sort properties by the order property of the custon annotation, send nulls (the one that aren't decorated with the attribute) at bottom
var properties = objType.GetProperties().Where(x => x.GetCustomAttribute<NonExportable>() == null).OrderBy(x => x.GetCustomAttribute<Exportable>() == null).ThenBy(x => x.GetCustomAttribute<Exportable>()?.Order);
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(entity, null);
var elems = propValue as IList;
if (elems != null)
{
for (int i = 0; i < elems.Count; i++)
{
if (TreatEnumerablesAsColumns)
{
Map(elems[i], i.ToString());
}
else
{
if (i > 0) NextLine(true); // add new line only after the first element
Map(elems[i]);
}
}
}
else
{
if (PrimitiveTypes.IsPrimitive(property.PropertyType))
{
var attribute = property.GetCustomAttribute<Exportable>();
var name = attribute?.Name ?? property.DeclaringType?.Name + "." + property.Name;
name += enumerableColumnId;
var globalPosition = attribute?.GlobalOrder ?? -1;
// Bottom node reached, consider value
AddRecord(name, propValue, globalPosition);
}
else
{
// Brach node found, iterate through childs
Map(propValue, enumerableColumnId);
}
}
}
}
private void BinarizeRows(int firstColumnsToSkip)
{
foreach (var csvRow in _csvRows)
{
NextBinaryLine();
for (int j = 0; j < csvRow.Count; j++)
{
AddBinaryRecord(_csvHeader[j], csvRow[j], !(j < firstColumnsToSkip));
}
}
}
/// <summary>
/// Generates a Csv file with using the DataAnnotation as a guide
/// </summary>
/// <param name="entity">The object or List (IList implementation) of objects to writte</param>
/// <param name="filePath">The full file name to save the resulting csv file</param>
public void GenerateCsv(Object entity, string filePath)
{
Separator = ",";
_writer = new StreamWriter(filePath);
// verify if the object is a collection
var objs = entity as IList;
if (objs != null)
{
foreach (var obj in objs)
{
Map(obj);
// flush each object to same a little memory
FlushObject();
}
}
else
{
Map(entity);
}
Flush();
}
/// <summary>
/// Generates a binary dataset file with using the DataAnnotation as a guide. See:
/// <seealso cref="http://github.com/inxunxa/Linq2Csv"/> for more details
/// </summary>
/// <param name="entity">The object or List (IList implementation) of objects to writte</param>
/// <param name="filePath">The full file name to save the resulting csv file</param>
/// <param name="firstColumsToSkip">How many colums should be skip (of binarization)</param>
public void GenerateBinaryFormat(Object entity, string filePath, int firstColumsToSkip = 0)
{
Separator = ",";
// verify if the object is a collection
var objs = entity as IList;
if (objs != null)
{
foreach (var obj in objs)
{
Map(obj);
// unlike GenerateCsv, here we can't flush each object as we need them in memory
// just reset _rowsPerObject, and create a new line (in that order)
_rowsPerObject = 0;
NextLine();
}
}
else
{
Map(entity);
}
BinarizeRows(firstColumsToSkip);
_writer = new StreamWriter(filePath);
FlushBinary();
}
/// <summary>
/// Generate a CSV files whit the data of the object
/// </summary>
/// <param name="entity">The object to export</param>
/// <param name="filePath">Full file name where for the Csv File</param>
public void GenerateCsvAutoMap(Object entity, string filePath)
{
Separator = ",";
_writer = new StreamWriter(filePath);
// verify if the object is a collection
// if so, iterate trough elements
var objs = entity as IList;
if (objs != null)
{
foreach (var obj in objs)
{
AutoMap(obj);
FlushObject();
}
}
else
{
AutoMap(entity);
}
Flush();
}
/// <summary>
/// Write the Entity to the File
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
public void WriteEntity<T>(T entity)
{
if (entity == null) return;
AutoMap(entity);
}
/// <summary>
/// Add as a record to the records matrix.
/// </summary>
/// <param name="columnName">The corresponding column for the record</param>
/// <param name="value">The value of the record</param>
/// <param name="globalPosition">The position of the column respect the overall file</param>
private void AddRecord(string columnName, object value, int globalPosition = -1)
{
if (!_csvRows.Any()) NextLine();
// get the index of the corresponding column in the header.
int index = _csvHeader.IndexOf(columnName);
if (index < 0)
{
if (globalPosition >= 0) // put the column in specific index.
{
if (globalPosition >= _csvHeader.Count) // empty cell are needed to create the globalPosition index.
{
int addRange = globalPosition - _csvHeader.Count; // how many empty cells
for (int i = 0; i < addRange; i++)
{
_csvHeader.Add(string.Empty);
_csvRows.ForEach(x => x.Add(string.Empty));
}
}
_csvHeader.Insert(globalPosition, columnName);
_csvRows.ForEach(x => x.Insert(globalPosition, string.Empty));
index = globalPosition;
}
else // put the column in the first index available (or create one).
{
// TODO: Get first empty string (if any) or add it to the end
_csvHeader.Add(columnName);
_csvRows.ForEach(x => x.Add(string.Empty));
index = _csvHeader.Count - 1;
}
}
// put the value in the current row
_csvRows.Last()[index] = value;
// check if the above cell of the same object are empty
// if so, copy the same value on all of them
for (int i = 1; i < _rowsPerObject; i++) // the i start at 1, as the 0 position is forced in (_csvRows.Last()[index] = value;)
{
int row = (_csvRows.Count - 1) - i; // last row (_csvRows.Count - 1) minus i
while (_csvRows[row].Count <= index) // create the index if needed
_csvRows[row].Add("");
if(_csvRows[row][index] is string && string.IsNullOrEmpty(_csvRows[row][index].ToString())) _csvRows[row][index] = value;
}
}
private void AddBinaryRecord(string columnName, object value, bool isBinarized = true)
{
string strVal = value?.ToString() ?? "na";
if (isBinarized)
{
columnName += ":" + strVal;
strVal = "1";
}
int index = _binaryHeader.IndexOf(columnName);
if (index < 0)
{
// new header
_binaryHeader.Add(columnName);
_binaryRows.ForEach(x => x.Add("0"));
index = _binaryHeader.Count - 1;
}
_binaryRows.Last()[index] = strVal;
}
private void NextLine(bool copyPrevious = false)
{
/* When creating a csv, if one objects has a property (list) with two elements (for example)
then two rows should result in the csv, coping all the base information,
the only diference should be the elements of the array.
*/
if (copyPrevious)
{
if (_csvRows.Count > 0 && _csvRows.Last().Count > 0)
{
// copy pnly if above line isn't empty
_csvRows.Add(new List<object>(_csvRows.Last().ToArray()));
_rowsPerObject++;
}
}
else
{
_csvRows.Add(new List<object>(Enumerable.Repeat(string.Empty, _csvHeader.Count)));
_rowsPerObject++;
}
}
private void NextBinaryLine()
{
// add a new row to the list filled with ceros
_binaryRows.Add(Enumerable.Repeat("0", _binaryHeader.Count).ToList());
}
private void WriteHeader()
{
if (!_csvHeaderWriten)
{
_writer.WriteLine(string.Join(Separator, _csvHeader.ToArray()));
_csvHeaderWriten = true;
}
}
private void WriteBinaryHeader()
{
if (!_binaryHeaderWriten)
{
_writer.WriteLine(string.Join(Separator, _binaryHeader.ToArray()));
_binaryHeaderWriten = true;
}
}
private void FlushObject()
{
WriteHeader();
foreach (List<object> row in _csvRows)
{
_writer.WriteLine(string.Join(",", row.ToArray()));
}
_csvRows.Clear();
_rowsPerObject = 0;
}
/// <summary>
/// Flush (writes) the data to the file. And close the stream
/// </summary>
public void Flush()
{
WriteHeader();
foreach (var row in _csvRows)
{
_writer.WriteLine(string.Join(",", row.ToArray()));
}
_writer.Close();
_writer.Dispose();
_csvHeader.Clear();
_csvRows.Clear();
_csvHeaderWriten = false;
}
private void FlushBinary()
{
WriteBinaryHeader();
foreach (var row in _binaryRows)
{
_writer.WriteLine(string.Join(",", row.ToArray()));
}
_writer.Close();
_writer.Dispose();
_csvHeader.Clear();
_binaryHeader.Clear();
_csvRows.Clear();
_binaryRows.Clear();
_binaryHeaderWriten = false;
}
/// <summary>
/// Dispose the unused object
/// </summary>
public void Dispose()
{
_writer?.Dispose();
}
}
}