From 83044f828409d8fba77bbb418cf36f3769d43a50 Mon Sep 17 00:00:00 2001 From: Thomas Nind Date: Tue, 16 Mar 2021 10:00:13 +0000 Subject: [PATCH 1/4] Made FoDicomAnonymiser checks create UID mapping server --- .../Integration/FoDicomAnonymiserTests.cs | 22 +++++++++ .../FoDicomBased/FoDicomAnonymiser.cs | 48 ++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs b/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs index 29b4a971..73629630 100644 --- a/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs +++ b/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs @@ -238,7 +238,28 @@ 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(); + + var ex = Assert.Throws(()=>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() { @@ -262,5 +283,6 @@ private IExtractDatasetCommand MockExtractionCommand() return cmd; } + } } diff --git a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs index 0e61bd36..2e9cd742 100644 --- a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs +++ b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs @@ -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 { @@ -185,7 +186,52 @@ public void PreInitialize(IExtractCommand value, IDataLoadEventListener listener public void Check(ICheckNotifier notifier) { - + 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; + } + } + + + + } } } From 011ef97c96df6ebabe11291a0f6cecdbed84398e Mon Sep 17 00:00:00 2001 From: Thomas Nind Date: Tue, 16 Mar 2021 10:13:46 +0000 Subject: [PATCH 2/4] Fixed not set UIDMappingServer property in test --- Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs b/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs index 73629630..b4ecc3d1 100644 --- a/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs +++ b/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs @@ -249,7 +249,8 @@ public void TestUIDTableExists() eds.SetProperties(db); var anon = new FoDicomAnonymiser(); - + anon.UIDMappingServer = eds; + var ex = Assert.Throws(()=>anon.Check(new ThrowImmediatelyCheckNotifier() { ThrowOnWarning = true })); StringAssert.AreEqualIgnoringCase("UIDMappingServer is not set up yet", ex.Message); From b13003498ae18743d429643c8eab52a56341a4d4 Mon Sep 17 00:00:00 2001 From: Thomas Nind Date: Tue, 16 Mar 2021 13:16:28 +0000 Subject: [PATCH 3/4] Added lock to prevent parallel checks attempting to create database at same time --- .../FoDicomBased/FoDicomAnonymiser.cs | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs index 2e9cd742..80f1f07b 100644 --- a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs +++ b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs @@ -184,54 +184,55 @@ public void PreInitialize(IExtractCommand value, IDataLoadEventListener listener _extractCommand = value as IExtractDatasetCommand; } + private static object CreateServersOneAtATime = new object(); + public void Check(ICheckNotifier notifier) { - if(UIDMappingServer == null) + lock(CreateServersOneAtATime) { - throw new Exception($"{nameof(UIDMappingServer)} not set, set it existing UID mapping server or to an empty database to create a new one"); - } + 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(); + var patcher = new SMIDatabasePatcher(); - if(!UIDMappingServer.WasCreatedBy(patcher)) - { - if (string.IsNullOrWhiteSpace(UIDMappingServer.CreatedByAssembly)) + if (!UIDMappingServer.WasCreatedBy(patcher)) { - bool create = notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} is not set up yet", CheckResult.Warning, null, "Attempt to create UID mapping schema")); - - if(create) + if (string.IsNullOrWhiteSpace(UIDMappingServer.CreatedByAssembly)) { - var db = UIDMappingServer.Discover(ReusableLibraryCode.DataAccess.DataAccessContext.DataExport); + bool create = notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} is not set up yet", CheckResult.Warning, null, "Attempt to create UID mapping schema")); - if (!db.Exists()) + if (create) { - notifier.OnCheckPerformed(new CheckEventArgs($"About to create {db}", CheckResult.Success)); - db.Create(); - } + var db = UIDMappingServer.Discover(ReusableLibraryCode.DataAccess.DataAccessContext.DataExport); - notifier.OnCheckPerformed(new CheckEventArgs($"Creating UID Mapping schema in {db}", CheckResult.Success)); + if (!db.Exists()) + { + notifier.OnCheckPerformed(new CheckEventArgs($"About to create {db}", CheckResult.Success)); + db.Create(); + } - var scripter = new MasterDatabaseScriptExecutor(db); - scripter.CreateAndPatchDatabase(patcher, new AcceptAllCheckNotifier()); + notifier.OnCheckPerformed(new CheckEventArgs($"Creating UID Mapping schema in {db}", CheckResult.Success)); - UIDMappingServer.CreatedByAssembly = patcher.Name; - UIDMappingServer.SaveToDatabase(); + 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; } } - 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; - } } - - - - } } } From 134127bab3622bf2adbd3f1f6edd90254f792e4a Mon Sep 17 00:00:00 2001 From: Thomas Nind Date: Tue, 16 Mar 2021 13:17:27 +0000 Subject: [PATCH 4/4] Added changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5238d7b5..01134020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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