Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/checks build uid map #125

Merged
merged 4 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]


## Added

- FoDicomAnonymiser checks now support automatic database creation/patching for UID mapping server

## [2.2.4] 2021-03-08

### Changed
Expand Down
23 changes: 23 additions & 0 deletions Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,29 @@ public void TestPutDicomFilesInExtractionDirectories(Type putterType)

}


[Test]
public void TestUIDTableExists()
{
var db = GetCleanedServer(DatabaseType.MicrosoftSQLServer);

// set it to an empty database
var eds = new ExternalDatabaseServer(CatalogueRepository,"UID server",null);
eds.SetProperties(db);

var anon = new FoDicomAnonymiser();
anon.UIDMappingServer = eds;

var ex = Assert.Throws<Exception>(()=>anon.Check(new ThrowImmediatelyCheckNotifier() { ThrowOnWarning = true }));

StringAssert.AreEqualIgnoringCase("UIDMappingServer is not set up yet", ex.Message);

anon.Check(new AcceptAllCheckNotifier());

// no warnings after it has been created
Assert.DoesNotThrow(() => anon.Check(new ThrowImmediatelyCheckNotifier() { ThrowOnWarning = true }));

}

private IExtractDatasetCommand MockExtractionCommand()
{
Expand All @@ -262,5 +284,6 @@ private IExtractDatasetCommand MockExtractionCommand()

return cmd;
}

}
}
49 changes: 48 additions & 1 deletion Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Rdmp.Core.DataFlowPipeline.Requirements;
using Rdmp.Core.DataFlowPipeline;
using Rdmp.Core.Repositories.Construction;
using MapsDirectlyToDatabaseTable.Versioning;

namespace Rdmp.Dicom.Extraction.FoDicomBased
{
Expand Down Expand Up @@ -183,9 +184,55 @@ public void PreInitialize(IExtractCommand value, IDataLoadEventListener listener
_extractCommand = value as IExtractDatasetCommand;
}

private static object CreateServersOneAtATime = new object();

public void Check(ICheckNotifier notifier)
{

lock(CreateServersOneAtATime)
{
if (UIDMappingServer == null)
{
throw new Exception($"{nameof(UIDMappingServer)} not set, set it existing UID mapping server or to an empty database to create a new one");
}

var patcher = new SMIDatabasePatcher();

if (!UIDMappingServer.WasCreatedBy(patcher))
{
if (string.IsNullOrWhiteSpace(UIDMappingServer.CreatedByAssembly))
{
bool create = notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} is not set up yet", CheckResult.Warning, null, "Attempt to create UID mapping schema"));

if (create)
{
var db = UIDMappingServer.Discover(ReusableLibraryCode.DataAccess.DataAccessContext.DataExport);

if (!db.Exists())
{
notifier.OnCheckPerformed(new CheckEventArgs($"About to create {db}", CheckResult.Success));
db.Create();
}

notifier.OnCheckPerformed(new CheckEventArgs($"Creating UID Mapping schema in {db}", CheckResult.Success));

var scripter = new MasterDatabaseScriptExecutor(db);
scripter.CreateAndPatchDatabase(patcher, new AcceptAllCheckNotifier());

UIDMappingServer.CreatedByAssembly = patcher.Name;
UIDMappingServer.SaveToDatabase();
}
else
{
return;
}
}
else
{
notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} '{UIDMappingServer}' was created by '{UIDMappingServer.CreatedByAssembly}' not a UID patcher. Try creating a new server reference to a blank database", CheckResult.Fail));
return;
}
}
}
}
}
}