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 diff --git a/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs b/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs index 29b4a971..b4ecc3d1 100644 --- a/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs +++ b/Rdmp.Dicom.Tests/Integration/FoDicomAnonymiserTests.cs @@ -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(()=>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 +284,6 @@ private IExtractDatasetCommand MockExtractionCommand() return cmd; } + } } diff --git a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs index 0e61bd36..80f1f07b 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 { @@ -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; + } + } + } } } }