-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDelimitedFlatFileAttacher.cs
172 lines (146 loc) · 6.23 KB
/
DelimitedFlatFileAttacher.cs
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
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Data;
using System.Globalization;
using System.IO;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.DataFlowPipeline;
using Rdmp.Core.DataFlowPipeline.Requirements;
using Rdmp.Core.DataLoad.Engine.Job;
using Rdmp.Core.DataLoad.Modules.DataFlowSources;
using Rdmp.Core.DataLoad.Modules.Exceptions;
using Rdmp.Core.ReusableLibraryCode.Progress;
namespace Rdmp.Core.DataLoad.Modules.Attachers;
/// <summary>
/// See AnySeparatorFileAttacher
/// </summary>
public abstract class DelimitedFlatFileAttacher : FlatFileAttacher
{
protected readonly DelimitedFlatFileDataFlowSource Source;
[DemandsInitialization(DelimitedFlatFileDataFlowSource.ForceHeaders_DemandDescription)]
public string ForceHeaders
{
get => Source.ForceHeaders;
set => Source.ForceHeaders = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.IgnoreQuotes_DemandDescription)]
public bool IgnoreQuotes
{
get => Source.IgnoreQuotes;
set => Source.IgnoreQuotes = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.IgnoreBlankLines_DemandDescription)]
public bool IgnoreBlankLines
{
get => Source.IgnoreBlankLines;
set => Source.IgnoreBlankLines = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.ForceHeadersReplacesFirstLineInFile_Description)]
public bool ForceHeadersReplacesFirstLineInFile
{
get => Source.ForceHeadersReplacesFirstLineInFile;
set => Source.ForceHeadersReplacesFirstLineInFile = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.IgnoreColumns_Description)]
public string IgnoreColumns
{
get => Source.IgnoreColumns;
set => Source.IgnoreColumns = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.BadDataHandlingStrategy_DemandDescription,
DefaultValue = BadDataHandlingStrategy.ThrowException)]
public BadDataHandlingStrategy BadDataHandlingStrategy
{
get => Source.BadDataHandlingStrategy;
set => Source.BadDataHandlingStrategy = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.IgnoreBadReads_DemandDescription)]
public bool IgnoreBadReads
{
get => Source.IgnoreBadReads;
set => Source.IgnoreBadReads = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.ThrowOnEmptyFiles_DemandDescription, DefaultValue = true)]
public bool ThrowOnEmptyFiles
{
get => Source.ThrowOnEmptyFiles;
set => Source.ThrowOnEmptyFiles = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.AttemptToResolveNewLinesInRecords_DemandDescription,
DefaultValue = false)]
public bool AttemptToResolveNewLinesInRecords
{
get => Source.AttemptToResolveNewLinesInRecords;
set => Source.AttemptToResolveNewLinesInRecords = value;
}
[DemandsInitialization(DelimitedFlatFileDataFlowSource.MaximumErrorsToReport_DemandDescription, DefaultValue = 100)]
public int MaximumErrorsToReport
{
get => Source.MaximumErrorsToReport;
set => Source.MaximumErrorsToReport = value;
}
[DemandsInitialization(ExcelDataFlowSource.AddFilenameColumnNamed_DemandDescription)]
public string AddFilenameColumnNamed { get; set; }
[DemandsInitialization(Culture_DemandDescription)]
public override CultureInfo Culture
{
get => Source.Culture;
set => Source.Culture = value;
}
[DemandsInitialization(ExplicitDateTimeFormat_DemandDescription)]
public override string ExplicitDateTimeFormat
{
get => Source.ExplicitDateTimeFormat;
set => Source.ExplicitDateTimeFormat = value;
}
protected DelimitedFlatFileAttacher(char separator)
{
Source = new DelimitedFlatFileDataFlowSource
{
Separator = separator.ToString(),
StronglyTypeInput = false,
StronglyTypeInputBatchSize = 0
};
}
private IDataLoadEventListener _listener;
private FileInfo _currentFile;
protected override int IterativelyBatchLoadDataIntoDataTable(DataTable dt, int maxBatchSize,
GracefulCancellationToken cancellationToken)
{
Source.MaxBatchSize = maxBatchSize;
Source.SetDataTable(dt);
Source.GetChunk(_listener, cancellationToken);
//if we are adding a column to the data read which contains the file path
if (!string.IsNullOrWhiteSpace(AddFilenameColumnNamed))
{
if (!dt.Columns.Contains(AddFilenameColumnNamed))
throw new FlatFileLoadException(
$"AddFilenameColumnNamed is set to '{AddFilenameColumnNamed}' but the column did not exist in RAW");
foreach (DataRow row in dt.Rows)
if (row[AddFilenameColumnNamed] == DBNull.Value)
row[AddFilenameColumnNamed] = _currentFile.FullName;
}
return dt.Rows.Count;
}
protected override void OpenFile(FileInfo fileToLoad, IDataLoadEventListener listener,
GracefulCancellationToken cancellationToken)
{
Source.StronglyTypeInput = false;
Source.StronglyTypeInputBatchSize = 0;
_listener = listener;
Source.PreInitialize(new FlatFileToLoad(fileToLoad), listener);
_currentFile = fileToLoad;
}
protected override void ConfirmFlatFileHeadersAgainstDataTable(DataTable loadTarget, IDataLoadJob job)
{
//automatically handled by SetDataTable
}
protected override void CloseFile()
{
Source.Dispose(_listener, null);
}
}