-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathImportFilesDataProvider.cs
86 lines (73 loc) · 4.21 KB
/
ImportFilesDataProvider.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
// 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.IO;
using FAnsi.Discovery;
using Rdmp.Core.Curation;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.DataFlowPipeline;
using Rdmp.Core.DataLoad.Engine.DataProvider;
using Rdmp.Core.DataLoad.Engine.Job;
using Rdmp.Core.ReusableLibraryCode.Checks;
using Rdmp.Core.ReusableLibraryCode.Progress;
namespace Rdmp.Core.DataLoad.Modules.DataProvider;
/// <summary>
/// Data load component that copies files into the ForLoading directory from the remote directory (that match the file pattern e.g. *.csv). A good use case
/// for this is if you want to expose a network location as a share for data provders to send you files to but want the DLE to take a copy of the files at
/// runtime for the purposes of loading.
///
/// <para>Optionally deletes files from the fetch location if the data load is successful</para>
/// </summary>
public class ImportFilesDataProvider : IPluginDataProvider
{
private FileInfo[] _files;
[DemandsInitialization("The path you want to copy files from", Mandatory = true)]
public string DirectoryPath { get; set; }
[DemandsInitialization("The file pattern to match on the DirectoryPath", Mandatory = true)]
public string FilePattern { get; set; }
[DemandsInitialization(
"If true then at the end of a successful data load the files that were originally matched and copied to forLoading will be deleted from the remote DirectoryPath. Note that only the files copied will be deleted, any new files that appear during the load will not be deleted")]
public bool DeleteFilesOnsuccessfulLoad { get; set; }
public void Check(ICheckNotifier notifier)
{
if (string.IsNullOrWhiteSpace(DirectoryPath))
notifier.OnCheckPerformed(new CheckEventArgs(
"No DirectoryPath has been specified, this should be set to the remote folder you want to copy files out of",
CheckResult.Fail));
if (string.IsNullOrWhiteSpace(FilePattern))
notifier.OnCheckPerformed(new CheckEventArgs(
"No FilePattern has been specified, this should be a pattern that matches files in the remote folder you want to copy files out of e.g. *.*",
CheckResult.Fail));
notifier.OnCheckPerformed(new DirectoryInfo(DirectoryPath).Exists
? new CheckEventArgs($"Path {DirectoryPath} was found", CheckResult.Success)
: new CheckEventArgs($"Path {DirectoryPath} was not found", CheckResult.Fail));
}
public void Initialize(ILoadDirectory directory, DiscoveredDatabase dbInfo)
{
}
public ExitCodeType Fetch(IDataLoadJob job, GracefulCancellationToken cancellationToken)
{
_files = new DirectoryInfo(DirectoryPath).GetFiles(FilePattern);
foreach (var f in _files)
{
var to = Path.Combine(job.LoadDirectory.ForLoading.FullName, f.Name);
job.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information,
$"Copying file {f.FullName} to directory {to}"));
f.CopyTo(to, true);
}
return ExitCodeType.Success;
}
public void LoadCompletedSoDispose(ExitCodeType exitCode, IDataLoadEventListener postLoadEventsListener)
{
if (exitCode == ExitCodeType.Success)
if (DeleteFilesOnsuccessfulLoad)
foreach (var f in _files)
{
postLoadEventsListener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information,
$"About to delete {f.FullName}"));
f.Delete();
}
}
}