-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathExtractableDataSet.cs
More file actions
199 lines (166 loc) · 6.45 KB
/
Copy pathExtractableDataSet.cs
File metadata and controls
199 lines (166 loc) · 6.45 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
// 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.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.MapsDirectlyToDatabaseTable;
using Rdmp.Core.MapsDirectlyToDatabaseTable.Injection;
using Rdmp.Core.Repositories;
namespace Rdmp.Core.DataExport.Data;
/// <inheritdoc cref="IExtractableDataSet"/>
public class ExtractableDataSet : DatabaseEntity, IExtractableDataSet, IInjectKnown<ICatalogue>
{
#region Database Properties
private int _catalogue_ID;
private bool _disableExtraction;
private List<IProject> _projects;
/// <inheritdoc/>
public int Catalogue_ID
{
get => _catalogue_ID;
set
{
ClearAllInjections();
SetField(ref _catalogue_ID, value);
}
}
/// <inheritdoc/>
public bool DisableExtraction
{
get => _disableExtraction;
set => SetField(ref _disableExtraction, value);
}
/// <inheritdoc/>
[NoMappingToDatabase]
public List<IProject> Projects
{
get
{
if (_projects != null) return _projects;
var ids = Repository.GetAllObjectsWhere<ExtractableDataSetProject>("ExtractableDataSet_ID", this.ID).Select(edsp => edsp.Project_ID);
Projects = Repository.GetAllObjectsInIDList<Project>(ids).Cast<IProject>().ToList();
return _projects;
}
set => SetField(ref _projects, value);
}
#endregion
#region Relationships
/// <summary>
/// Returns all <see cref="IExtractionConfiguration"/> in which this dataset is one of the extracted datasets
/// </summary>
[NoMappingToDatabase]
public IExtractionConfiguration[] ExtractionConfigurations
{
get
{
return
Repository.GetAllObjectsWithParent<SelectedDataSets>(this)
.Select(sds => sds.ExtractionConfiguration)
.ToArray();
}
}
/// <inheritdoc/>
[NoMappingToDatabase]
public ICatalogue Catalogue => _catalogue.Value;
#endregion
public ExtractableDataSet()
{
ClearAllInjections();
}
/// <summary>
/// Defines that the given Catalogue is extractable to researchers as a data set, this is stored in the DataExport database
/// </summary>
/// <returns></returns>
public ExtractableDataSet(IDataExportRepository repository, ICatalogue catalogue, bool disableExtraction = false)
{
Repository = repository;
Repository.InsertAndHydrate(this, new Dictionary<string, object>
{
{ "DisableExtraction", disableExtraction },
{ "Catalogue_ID", catalogue.ID }
});
ClearAllInjections();
InjectKnown(catalogue);
}
internal ExtractableDataSet(IDataExportRepository repository, DbDataReader r)
: base(repository, r)
{
Catalogue_ID = Convert.ToInt32(r["Catalogue_ID"]);
DisableExtraction = (bool)r["DisableExtraction"];
ClearAllInjections();
}
/// <inheritdoc/>
[NoMappingToDatabase]
public bool IsCatalogueDeprecated => Catalogue == null || Catalogue.IsDeprecated;
/// <summary>
/// Returns the <see cref="ICatalogue"/> behind this dataset's Name or a string describing the object state if the Catalogue is unreachable.
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (Catalogue == null)
return $"DELETED CATALOGUE {Catalogue_ID}";
string name = Catalogue.ExtractionName != null? Catalogue.ExtractionName:Catalogue.Name;
//only bother refreshing Catalogue details if we will be able to get a legit catalogue name
return Catalogue.IsDeprecated ? $"DEPRECATED CATALOGUE {name}" : name;
}
#region Stuff for updating our internal database records
/// <summary>
/// Deletes the dataset, this will make the <see cref="ICatalogue"/> non extractable. This operation fails if
/// the dataset is part of any <see cref="ExtractionConfigurations"/>.
/// </summary>
public override void DeleteInDatabase()
{
try
{
Repository.DeleteFromDatabase(this);
}
catch (Exception e)
{
if (e.Message.Contains("FK_SelectedDataSets_ExtractableDataSet"))
throw new Exception(
$"Cannot delete {this} because it is in use by the following configurations :{Environment.NewLine}{string.Join(Environment.NewLine, ExtractionConfigurations.Select(c => $"{c.Name}({c.Project})"))}",
e);
throw;
}
}
#endregion
/// <summary>
/// Returns an object indicating whether the dataset is project specific or not
/// </summary>
/// <returns></returns>
public CatalogueExtractabilityStatus GetCatalogueExtractabilityStatus() => new(true, Projects.Any());
private Lazy<ICatalogue> _catalogue;
/// <inheritdoc/>
public void InjectKnown(ICatalogue instance)
{
if (instance.ID != Catalogue_ID)
throw new ArgumentOutOfRangeException(nameof(instance),
$"You told us our Catalogue was '{instance}' but its ID didn't match so that is NOT our Catalogue");
_catalogue = new Lazy<ICatalogue>(instance);
}
/// <inheritdoc/>
public void ClearAllInjections()
{
_catalogue = new Lazy<ICatalogue>(FetchCatalogue);
}
private ICatalogue FetchCatalogue()
{
try
{
var cata = ((IDataExportRepository)Repository).CatalogueRepository.GetObjectByID<Catalogue>(Catalogue_ID);
cata.InjectKnown(GetCatalogueExtractabilityStatus());
return cata;
}
catch (KeyNotFoundException)
{
//Catalogue has been deleted!
return null;
}
}
}