Index: Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs =================================================================== diff -u -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) +++ Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -34,8 +34,6 @@ /// public abstract class SqLiteDatabaseReaderBase : IDisposable { - private readonly string fullFilePath; - /// /// Creates a new instance of which will use the /// as its source. @@ -64,20 +62,14 @@ throw new CriticalFileReadException(message); } - fullFilePath = databaseFilePath; + Path = databaseFilePath; OpenConnection(databaseFilePath); } /// /// Gets the path to the file. /// - public string Path - { - get - { - return fullFilePath; - } - } + public string Path { get; private set; } /// /// Closes and disposes the existing . Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicDatabaseHelper.cs =================================================================== diff -u -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicDatabaseHelper.cs (.../HydraulicDatabaseHelper.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicDatabaseHelper.cs (.../HydraulicDatabaseHelper.cs) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -1,6 +1,8 @@ -using System.IO; +using System; +using System.IO; using Core.Common.IO.Exceptions; using log4net; +using Ringtoets.HydraRing.Data; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; @@ -32,5 +34,48 @@ } return null; } + + /// + /// Returns the version from the database pointed at by the . + /// + /// The location of the database. + /// The version from the database as a . + /// Thrown when no connection with the hydraulic + /// boundary database could be created. + private static string GetVersion(string filePath) + { + using (var db = new HydraulicBoundarySqLiteDatabaseReader(filePath)) + { + return db.GetVersion(); + } + } + + /// + /// Checks whether the version of a matches the version + /// of a database at the given . + /// + /// The database to compare the version of. + /// The path to the database to compare the version of. + /// true if is of the given , + /// false otherwise. + /// Thrown when no connection with the hydraulic + /// boundary database could be created using . + /// Thrown when: + /// + /// is null + /// is null + /// + public static bool HaveEqualVersion(HydraulicBoundaryDatabase database, string pathToDatabase) + { + if (database == null) + { + throw new ArgumentNullException("database"); + } + if (pathToDatabase == null) + { + throw new ArgumentNullException("pathToDatabase"); + } + return database.Version == GetVersion(pathToDatabase); + } } } \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicDatabaseHelperTest.cs =================================================================== diff -u -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicDatabaseHelperTest.cs (.../HydraulicDatabaseHelperTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicDatabaseHelperTest.cs (.../HydraulicDatabaseHelperTest.cs) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -1,6 +1,9 @@ -using System.IO; +using System; +using System.IO; +using Core.Common.IO.Exceptions; using Core.Common.TestUtil; using NUnit.Framework; +using Ringtoets.HydraRing.Data; namespace Ringtoets.HydraRing.IO.Test { @@ -75,6 +78,78 @@ // Assert StringAssert.StartsWith(string.Format("Fout bij het lezen van bestand '{0}':", validFilePath), result); } - + + [Test] + public void HaveEqualVersion_InvalidFile_ThrowsCriticalFileReadException() + { + // Setup + var invalidPath = Path.Combine(testDataPath, "complete.sqlite"); + invalidPath = invalidPath.Replace('c', Path.GetInvalidPathChars()[0]); + + // Call + TestDelegate test = () => HydraulicDatabaseHelper.HaveEqualVersion(new HydraulicBoundaryDatabase(), invalidPath); + + // Assert + Assert.Throws(test); + } + + [Test] + public void HaveEqualVersion_PathNotSet_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => HydraulicDatabaseHelper.HaveEqualVersion(new HydraulicBoundaryDatabase(), null); + + // Assert + var parameter = Assert.Throws(test).ParamName; + Assert.AreEqual("pathToDatabase", parameter); + } + + [Test] + public void HaveEqualVersion_DatabaseNotSet_ThrowsArgumentNullException() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + TestDelegate test = () => HydraulicDatabaseHelper.HaveEqualVersion(null, validFilePath); + + // Assert + var parameter = Assert.Throws(test).ParamName; + Assert.AreEqual("database", parameter); + } + + [Test] + public void HaveEqualVersion_ValidFileEqualVersion_ReturnsTrue() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + var database = new HydraulicBoundaryDatabase() + { + Version = "Dutch coast South19-11-2015 12:00" + }; + + // Call + bool isEqual = HydraulicDatabaseHelper.HaveEqualVersion(database, validFilePath); + + // Assert + Assert.IsTrue(isEqual); + } + + [Test] + public void HaveEqualVersion_ValidFileDifferentVersion_ReturnsFalse() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + var database = new HydraulicBoundaryDatabase() + { + Version = "Dutch coast South19-11-2015 12:01" + }; + + // Call + bool isEqual = HydraulicDatabaseHelper.HaveEqualVersion(database, validFilePath); + + // Assert + Assert.IsFalse(isEqual); + } } } \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj =================================================================== diff -u -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -68,6 +68,10 @@ + + {3bbfd65b-b277-4e50-ae6d-bd24c3434609} + Core.Common.Base + {E344867E-9AC9-44C8-88A5-8185681679A9} Core.Common.IO Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs =================================================================== diff -u -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -22,7 +22,6 @@ using System; using System.IO; using Core.Common.IO.Exceptions; -using Core.Common.Utils; using Core.Common.Utils.Builders; using log4net; using Ringtoets.Common.Data.AssessmentSection; @@ -56,8 +55,8 @@ private void ValidateAndConnectTo(string filePath) { hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(filePath); - string hlcdFilePath = Path.Combine(Path.GetDirectoryName(filePath), "hlcd.sqlite"); + string hlcdFilePath = Path.Combine(Path.GetDirectoryName(filePath), "hlcd.sqlite"); try { hydraulicLocationConfigurationDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath); @@ -70,18 +69,6 @@ } /// - /// Gets the version of the database. - /// - /// The path to the database to obtain the version for. - /// The database version. - /// Thrown when the version could not be obtained from the database. - public string GetHydraulicBoundaryDatabaseVersion(string filePath) - { - ValidateAndConnectTo(filePath); - return hydraulicBoundaryDatabaseReader.GetVersion(); - } - - /// /// Creates a new instance of , based upon the data read from /// the hydraulic boundary database file, and saved into . /// @@ -99,18 +86,32 @@ { ValidateAndConnectTo(filePath); - var importResult = GetHydraulicBoundaryDatabase(); - - if (importResult == null) + var hydraulicBoundaryDatabase = targetItem.HydraulicBoundaryDatabase; + if (!IsImportRequired(hydraulicBoundaryDatabase)) { - return false; + hydraulicBoundaryDatabase.FilePath = filePath; } + else + { + var importResult = GetHydraulicBoundaryDatabase(); - AddImportedDataToModel(targetItem, importResult); - log.Info(Resources.HydraulicBoundaryDatabaseImporter_Import_All_hydraulic_locations_read); + if (importResult == null) + { + return false; + } + + AddImportedDataToModel(targetItem, importResult); + log.Info(Resources.HydraulicBoundaryDatabaseImporter_Import_All_hydraulic_locations_read); + } + return true; } + private bool IsImportRequired(HydraulicBoundaryDatabase hydraulicBoundaryDatabase) + { + return hydraulicBoundaryDatabase == null || hydraulicBoundaryDatabaseReader.GetVersion() != hydraulicBoundaryDatabase.Version; + } + public void Dispose() { if (hydraulicBoundaryDatabaseReader != null) Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs =================================================================== diff -u -r1e6d07e8cbd1c29a16b5addddb7205e717543729 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs (.../RingtoetsGuiPlugin.cs) (revision 1e6d07e8cbd1c29a16b5addddb7205e717543729) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs (.../RingtoetsGuiPlugin.cs) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -615,7 +615,7 @@ { try { - ValidateAndImportSelectedFile(dialog.FileName, assessmentSection); + ImportHydraulicBoundaryDatabase(dialog.FileName, assessmentSection); } catch (CriticalFileReadException exception) { @@ -626,37 +626,34 @@ } /// - /// Attempts to update the based on the . + /// Attempts to update the with a + /// imported from the . /// - /// The file to use to import a from. - /// The to which the imported will be assigned. - /// A new which is connected to the . - /// Thrown when the importer could not be created for the selected file. - private static void ValidateAndImportSelectedFile(string selectedFile, IAssessmentSection assessmentSection) + /// The file to use to import a from. + /// The to which the imported + /// will be assigned. + /// Thrown when importing from the + /// failed. + private static void ImportHydraulicBoundaryDatabase(string databaseFile, IAssessmentSection assessmentSection) { - using (var hydraulicBoundaryLocationsImporter = new HydraulicBoundaryDatabaseImporter()) - { - if (assessmentSection.HydraulicBoundaryDatabase != null) - { - var newVersion = hydraulicBoundaryLocationsImporter.GetHydraulicBoundaryDatabaseVersion(selectedFile); + var hydraulicBoundaryDatabase = assessmentSection.HydraulicBoundaryDatabase; - var currentVersion = assessmentSection.HydraulicBoundaryDatabase.Version; - var currentFilePath = assessmentSection.HydraulicBoundaryDatabase.FilePath; + var isHydraulicBoundaryDatabaseSet = hydraulicBoundaryDatabase != null; + var isClearConfirmationRequired = isHydraulicBoundaryDatabaseSet && !HydraulicDatabaseHelper.HaveEqualVersion(hydraulicBoundaryDatabase, databaseFile); + var isClearConfirmationGiven = isClearConfirmationRequired && IsClearCalculationConfirmationGiven(); - if (currentVersion != newVersion && HasClearCalculationConfirmation()) + if (!isHydraulicBoundaryDatabaseSet || !isClearConfirmationRequired || isClearConfirmationGiven) + { + var hydraulicBoundaryLocationsImporter = new HydraulicBoundaryDatabaseImporter(); + if (hydraulicBoundaryLocationsImporter.Import(assessmentSection, databaseFile)) + { + if (isClearConfirmationRequired) { ClearCalculations(assessmentSection); - ImportSelectedFile(assessmentSection, selectedFile, hydraulicBoundaryLocationsImporter); } - else if (currentFilePath != selectedFile) - { - SetBoundaryDatabaseData(assessmentSection, selectedFile); - } + assessmentSection.NotifyObservers(); + log.InfoFormat(RingtoetsFormsResources.RingtoetsGuiPlugin_SetBoundaryDatabaseFilePath_Database_on_path_0_linked, assessmentSection.HydraulicBoundaryDatabase.FilePath); } - else - { - ImportSelectedFile(assessmentSection, selectedFile, hydraulicBoundaryLocationsImporter); - } } } @@ -684,7 +681,7 @@ } } - private static bool HasClearCalculationConfirmation() + private static bool IsClearCalculationConfirmationGiven() { var confirmation = MessageBox.Show( RingtoetsFormsResources.Delete_Calculations_Text, @@ -708,25 +705,6 @@ log.Info(RingtoetsFormsResources.Calculations_Deleted); } - private static void ImportSelectedFile(IAssessmentSection assessmentSection, string hydraulicDatabasePath, HydraulicBoundaryDatabaseImporter hydraulicBoundaryLocationsImporter) - { - if (hydraulicBoundaryLocationsImporter.Import(assessmentSection, hydraulicDatabasePath)) - { - SetBoundaryDatabaseData(assessmentSection); - } - } - - private static void SetBoundaryDatabaseData(IAssessmentSection assessmentSection, string selectedFile = null) - { - if (!String.IsNullOrEmpty(selectedFile)) - { - assessmentSection.HydraulicBoundaryDatabase.FilePath = selectedFile; - } - - assessmentSection.NotifyObservers(); - log.InfoFormat(RingtoetsFormsResources.RingtoetsGuiPlugin_SetBoundaryDatabaseFilePath_Database_on_path_0_linked, assessmentSection.HydraulicBoundaryDatabase.FilePath); - } - #endregion } } \ No newline at end of file Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/HydraulicBoundaryDatabaseImporterTest.cs =================================================================== diff -u -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 -r8476d48f3aa0d36c866433c29be238aa52abcfe4 --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/HydraulicBoundaryDatabaseImporterTest.cs (.../HydraulicBoundaryDatabaseImporterTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/HydraulicBoundaryDatabaseImporterTest.cs (.../HydraulicBoundaryDatabaseImporterTest.cs) (revision 8476d48f3aa0d36c866433c29be238aa52abcfe4) @@ -65,19 +65,6 @@ } [Test] - public void GetHydraulicBoundaryDatabaseVersion_ValidFile_GetDatabaseVersion() - { - // Setup - string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); - - // Call - string version = importer.GetHydraulicBoundaryDatabaseVersion(validFilePath); - - // Assert - Assert.IsNotNullOrEmpty(version); - } - - [Test] public void Import_ExistingFile_DoesNotThrowException() { // Setup