Skip to content

Commit

Permalink
Merge pull request #125 from HicServices/feature/checks-build-uid-map
Browse files Browse the repository at this point in the history
Feature/checks build uid map
  • Loading branch information
tznind authored Mar 16, 2021
2 parents 5879684 + 134127b commit 528431c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
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;
}
}
}
}
}
}

0 comments on commit 528431c

Please sign in to comment.