Index: Core/Common/src/Core.Common.Utils/FileUtils.cs =================================================================== diff -u -r5ce78402e22d198b69cc19c796cf74bfcfc702b9 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Core/Common/src/Core.Common.Utils/FileUtils.cs (.../FileUtils.cs) (revision 5ce78402e22d198b69cc19c796cf74bfcfc702b9) +++ Core/Common/src/Core.Common.Utils/FileUtils.cs (.../FileUtils.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -64,53 +64,5 @@ throw new ArgumentException(message); } } - - /// - /// Compares with . - /// - /// Path to the original file. - /// Path to the file to compare to. - /// True if the files are structural equal, false otherwise. - /// Thrown when: - /// is invalid; - /// is invalid; - /// Failed to read file ; - /// Failed to read file ; - /// - public static bool CompareFiles(string pathA, string pathB) - { - ValidateFilePath(pathA); - ValidateFilePath(pathB); - - try - { - var fileA = new FileInfo(pathA); - var fileB = new FileInfo(pathB); - if (fileA.Length != fileB.Length) - { - return false; - } - - using (var md5 = MD5.Create()) - { - byte[] hashA; - byte[] hashB; - using (var stream = File.OpenRead(pathA)) - { - hashA = md5.ComputeHash(stream); - } - - using (var stream = File.OpenRead(pathB)) - { - hashB = md5.ComputeHash(stream); - } - return StructuralComparisons.StructuralEqualityComparer.Equals(hashA, hashB); - } - } - catch (SystemException exception) - { - throw new ArgumentException(Resources.Error_General_IO_ErrorMessage, exception); - } - } } } \ No newline at end of file Index: Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs =================================================================== diff -u -r38916623f1906041a5309d5241f7672020d69b35 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs (.../FileUtilsTest.cs) (revision 38916623f1906041a5309d5241f7672020d69b35) +++ Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs (.../FileUtilsTest.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -68,84 +68,5 @@ var expectedMessage = String.Format("Fout bij het lezen van bestand '{0}': Bestandspad mag niet naar een map verwijzen.", folderPath); Assert.AreEqual(expectedMessage, exception.Message); } - - [Test] - [TestCase(null, "FileToCompare_Original.txt")] - [TestCase("", "FileToCompare_Original.txt")] - [TestCase(" ", "FileToCompare_Original.txt")] - [TestCase("FileToCompare_Original.txt", null)] - [TestCase("FileToCompare_Original.txt", "")] - [TestCase("FileToCompare_Original.txt", " ")] - public void CompareFiles_InvalidPaths_ThrowsArgumentxception(string pathA, string pathB) - { - // Setup - var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': Bestandspad mag niet leeg of ongedefinieerd zijn.", - ("FileToCompare_Original.txt" == pathB) ? pathA : pathB); - - // Call - TestDelegate test = () => FileUtils.CompareFiles(pathA, pathB); - - // Assert - ArgumentException exception = Assert.Throws(test); - Assert.AreEqual(expectedMessage, exception.Message); - } - - [Test] - [TestCase("DoesNotExist", "FileToCompare_Original.txt")] - [TestCase("FileToCompare_Original.txt", "DoesNotExist")] - public void CompareFiles_NonExistingPaths_ThrowsArgumentException(string pathA, string pathB) - { - // Setup - const string expectedMessage = "Er is een onverwachte fout opgetreden tijdens het inlezen van het bestand."; - - // Call - TestDelegate test = () => FileUtils.CompareFiles(pathA, pathB); - - // Assert - ArgumentException exception = Assert.Throws(test); - Assert.AreEqual(expectedMessage, exception.Message); - } - - [Test] - public void CompareFiles_SameFiles_ReturnsTrue() - { - // Setup - var pathA = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "FileToCompare_Original.txt"); - var pathB = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "FileToCompare_Equal.txt"); - - // Call - bool areEqual = FileUtils.CompareFiles(pathA, pathB); - - // Assert - Assert.IsTrue(areEqual); - } - - [Test] - public void CompareFiles_EqualFiles_ReturnsTrue() - { - // Setup - var pathA = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "FileToCompare_Original.txt"); - var pathB = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "FileToCompare_Equal.txt"); - - // Call - bool areEqual = FileUtils.CompareFiles(pathA, pathB); - - // Assert - Assert.IsTrue(areEqual); - } - - [Test] - public void CompareFiles_DifferentFiles_ReturnsFalse() - { - // Setup - var pathA = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "FileToCompare_Original.txt"); - var pathB = TestHelper.GetTestDataPath(TestDataPath.Core.Common.Utils, "FileToCompare_Different.txt"); - - // Call - bool areEqual = FileUtils.CompareFiles(pathA, pathB); - - // Assert - Assert.IsFalse(areEqual); - } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseColumns.cs =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseColumns.cs (.../HydraulicBoundaryDatabaseColumns.cs) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseColumns.cs (.../HydraulicBoundaryDatabaseColumns.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -2,6 +2,7 @@ { internal static class HydraulicBoundaryDatabaseColumns { + internal const string Version = "Version"; internal const string LocationCount = "LocationCount"; internal const string LocationName = "LocationName"; internal const string LocationId = "LocationId"; Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseReader.cs =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseReader.cs (.../HydraulicBoundaryDatabaseReader.cs) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicBoundaryDatabaseReader.cs (.../HydraulicBoundaryDatabaseReader.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -36,7 +36,13 @@ /// public int Count { get; private set; } + /// + /// Gets the version from the database. + /// + public string Version { get; private set; } + + /// /// Gets the value true if profiles can be read using the . /// false otherwise. /// @@ -122,6 +128,7 @@ /// private void ReadLocations() { + var versionQuery = string.Format("SELECT (NameRegion || CreationDate) as {0} FROM General LIMIT 0,1;", HydraulicBoundaryDatabaseColumns.Version); var countQuery = string.Format("SELECT count(*) as {0} FROM HRDLocations WHERE LocationTypeId > 1 ;", HydraulicBoundaryDatabaseColumns.LocationCount); var locationsQuery = string.Format( @@ -131,7 +138,7 @@ HydraulicBoundaryDatabaseColumns.LocationX, HydraulicBoundaryDatabaseColumns.LocationY); - CreateDataReader(string.Join(" ", countQuery, locationsQuery), new SQLiteParameter + CreateDataReader(string.Join(" ", versionQuery, countQuery, locationsQuery), new SQLiteParameter { DbType = DbType.String }); @@ -149,6 +156,7 @@ try { dataReader = query.ExecuteReader(); + GetVersion(); GetCount(); } catch (SQLiteException exception) @@ -160,6 +168,15 @@ } } + private void GetVersion() + { + if (dataReader.Read()) + { + Version = Read(HydraulicBoundaryDatabaseColumns.Version); + } + dataReader.NextResult(); + } + private void GetCount() { dataReader.Read(); Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/HydraulicBoundaryLocationsImporter.cs =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/HydraulicBoundaryLocationsImporter.cs (.../HydraulicBoundaryLocationsImporter.cs) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/HydraulicBoundaryLocationsImporter.cs (.../HydraulicBoundaryLocationsImporter.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -41,6 +41,11 @@ private bool shouldCancel; /// + /// Gets the version of the used Hydraulic Boundary Database. + /// + public string Version { get; private set; } + + /// /// Gets the name of the . /// public string Name @@ -102,11 +107,30 @@ public ProgressChangedDelegate ProgressChanged { get; set; } /// + /// Validates the file at and sets the version. + /// + /// The paht to the file. + public void ValidateFile(string filePath) + { + try + { + using (var hydraulicBoundaryDatabaseReader = new HydraulicBoundaryDatabaseReader(filePath)) + { + Version = hydraulicBoundaryDatabaseReader.Version; + } + } + catch (CriticalFileReadException e) + { + HandleException(e); + } + } + + /// /// This method imports the data to an item from a file at the given location. /// /// The item to perform the import on. /// The path of the file to import the data from. - /// true if the import was successful. false otherwise. + /// True if the import was successful. False otherwise. public bool Import(object targetItem, string filePath) { var importResult = ReadHydraulicBoundaryLocations(filePath); Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/Ringtoets.HydraRing.Plugin.csproj =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/Ringtoets.HydraRing.Plugin.csproj (.../Ringtoets.HydraRing.Plugin.csproj) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/Ringtoets.HydraRing.Plugin.csproj (.../Ringtoets.HydraRing.Plugin.csproj) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -99,6 +99,7 @@ PublicResXFileCodeGenerator Resources.Designer.cs + Designer Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Plugin.Test/HydraulicBoundaryLocationsImporterTest.cs =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Plugin.Test/HydraulicBoundaryLocationsImporterTest.cs (.../HydraulicBoundaryLocationsImporterTest.cs) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Plugin.Test/HydraulicBoundaryLocationsImporterTest.cs (.../HydraulicBoundaryLocationsImporterTest.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -68,11 +68,47 @@ Assert.AreEqual(typeof(HydraulicBoundaryLocation), importer.SupportedItemType); Assert.AreEqual(expectedFileFilter, importer.FileFilter); Assert.IsNull(importer.ProgressChanged); + Assert.IsNull(importer.Version); } [Test] [TestCase("/")] [TestCase("nonexisting.sqlit")] + public void ValidateFile_NonExistingFileOrInvalidFile_LogError(string filename) + { + // Setup + string filePath = Path.Combine(testDataPath, filename); + var importer = new HydraulicBoundaryLocationsImporter(); + + // Call + Action call = () => importer.ValidateFile(filePath); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] messageArray = messages.ToArray(); + var ExpectedMessage = string.Format(RingtoetsHydraRingPluginResources.HydraulicBoundaryLocationsImporter_CriticalErrorMessage_0_File_Skipped, String.Empty); + StringAssert.EndsWith(ExpectedMessage, messageArray[0]); + }); + } + + [Test] + public void ValidateFile_ValidFile_GetDatabaseVersion() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + var importer = new HydraulicBoundaryLocationsImporter(); + + // Call + importer.ValidateFile(validFilePath); + + // Assert + Assert.IsNotNullOrEmpty(importer.Version); + } + + [Test] + [TestCase("/")] + [TestCase("nonexisting.sqlit")] public void Import_FromNonExistingFileOrInvalidFile_LogError(string filename) { // Setup Index: Ringtoets/Integration/src/Ringtoets.Integration.Data/HydraulicBoundary/HydraulicBoundaryDatabase.cs =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/Integration/src/Ringtoets.Integration.Data/HydraulicBoundary/HydraulicBoundaryDatabase.cs (.../HydraulicBoundaryDatabase.cs) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/Integration/src/Ringtoets.Integration.Data/HydraulicBoundary/HydraulicBoundaryDatabase.cs (.../HydraulicBoundaryDatabase.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -33,6 +33,7 @@ } public string FilePath { get; set; } + public string Version { get; set; } /// /// Gets the hydraulic boundary locations. Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs =================================================================== diff -u -r98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187 -r95df0a957637a4722ea6bf24ac371259cec3d3ad --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs (.../RingtoetsGuiPlugin.cs) (revision 98fc85d8cecf9edae9fe7c1f2f47b60ecda5e187) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs (.../RingtoetsGuiPlugin.cs) (revision 95df0a957637a4722ea6bf24ac371259cec3d3ad) @@ -30,8 +30,6 @@ using Core.Common.Gui.ContextMenu; using Core.Common.Gui.Forms; using Core.Common.Gui.Plugin; -using Core.Common.IO.Exceptions; -using Core.Common.Utils; using log4net; using Ringtoets.Common.Data; using Ringtoets.Common.Forms.PresentationObjects; @@ -128,11 +126,11 @@ { Text = context => "Referentielijn", Image = context => RingtoetsFormsResources.ReferenceLineIcon, - ForeColor = context => context.WrappedData == null ? - Color.FromKnownColor(KnownColor.GrayText) : - Color.FromKnownColor(KnownColor.ControlText), - ContextMenuStrip = (nodeData, parentData, treeViewControl) => - Gui.Get(nodeData, treeViewControl).AddImportItem().Build() + ForeColor = context => context.WrappedData == null ? + Color.FromKnownColor(KnownColor.GrayText) : + Color.FromKnownColor(KnownColor.ControlText), + ContextMenuStrip = (nodeData, parentData, treeViewControl) => + Gui.Get(nodeData, treeViewControl).AddImportItem().Build() }; yield return new TreeNodeInfo @@ -377,11 +375,7 @@ var connectionItem = new StrictContextMenuItem( RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Connect, RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Connect_ToolTip, - RingtoetsCommonFormsResources.DatabaseIcon, (sender, args) => - { - SelectDatabaseFile(nodeData); - ImportDatabaseFileLovations(nodeData); - }); + RingtoetsCommonFormsResources.DatabaseIcon, (sender, args) => { SelectDatabaseFile(nodeData); }); var toetsPeilItem = new StrictContextMenuItem( RingtoetsCommonFormsResources.Toetspeil_Calculate, @@ -415,43 +409,35 @@ { if (dialog.ShowDialog(Gui.MainWindow) == DialogResult.OK) { - ValidateSelectedFile(nodeData, dialog.FileName); + ValidateAndImportSelectedFile(nodeData, dialog.FileName); } } } - private void ImportDatabaseFileLovations(HydraulicBoundaryDatabaseContext nodeData) + private static void ValidateAndImportSelectedFile(HydraulicBoundaryDatabaseContext nodeData, string selectedFile) { var hydraulicBoundaryLocationsImporter = new HydraulicBoundaryLocationsImporter(); - hydraulicBoundaryLocationsImporter.Import(nodeData.BoundaryDatabase.Locations, nodeData.BoundaryDatabase.FilePath); - } - private static void ValidateSelectedFile(HydraulicBoundaryDatabaseContext nodeData, string selectedFile) - { - try - { - if (!string.IsNullOrEmpty(nodeData.BoundaryDatabase.FilePath)) - { - // Compare - bool isEqual = FileUtils.CompareFiles(nodeData.BoundaryDatabase.FilePath, selectedFile); + hydraulicBoundaryLocationsImporter.ValidateFile(selectedFile); - if (!isEqual) - { - // show dialog - ShowCleanDialog(nodeData, selectedFile); - return; - } - } + var currentVersion = nodeData.BoundaryDatabase.Version; + var newVersion = hydraulicBoundaryLocationsImporter.Version; - SetBoundaryDatabaseFilePath(nodeData, selectedFile); - } - catch (Exception e) + // Compare + if (!string.IsNullOrEmpty(currentVersion) && currentVersion != newVersion) { - throw new CriticalFileReadException(string.Format(UtilsResources.Error_General_IO_ErrorMessage_0_, selectedFile), e); + // Show dialog + ShowCleanDialog(nodeData, hydraulicBoundaryLocationsImporter, selectedFile, newVersion); + return; } + + ImportSelectedFile(nodeData, hydraulicBoundaryLocationsImporter, selectedFile, newVersion); } - private static void ShowCleanDialog(HydraulicBoundaryDatabaseContext nodeData, string filePath) + private static void ShowCleanDialog(HydraulicBoundaryDatabaseContext nodeData, + HydraulicBoundaryLocationsImporter hydraulicBoundaryLocationsImporter, + string filePath, + string version) { var confirmation = MessageBox.Show( RingtoetsCommonFormsResources.Delete_ToetsPeil_Calculations_Text, @@ -462,7 +448,7 @@ { ClearCalculations(nodeData.BaseNode); - SetBoundaryDatabaseFilePath(nodeData, filePath); + ImportSelectedFile(nodeData, hydraulicBoundaryLocationsImporter, filePath, version); } } @@ -477,9 +463,19 @@ } } - private static void SetBoundaryDatabaseFilePath(HydraulicBoundaryDatabaseContext nodeData, string selectedFile) + private static void ImportSelectedFile(HydraulicBoundaryDatabaseContext nodeData, + HydraulicBoundaryLocationsImporter hydraulicBoundaryLocationsImporter, + string selectedFile, + string newVersion) { + hydraulicBoundaryLocationsImporter.Import(nodeData.BoundaryDatabase.Locations, selectedFile); + SetBoundaryDatabaseData(nodeData, selectedFile, newVersion); + } + + private static void SetBoundaryDatabaseData(HydraulicBoundaryDatabaseContext nodeData, string selectedFile, string version) + { nodeData.BoundaryDatabase.FilePath = selectedFile; + nodeData.BoundaryDatabase.Version = version; nodeData.NotifyObservers(); log.InfoFormat(RingtoetsCommonFormsResources.RingtoetsGuiPlugin_SetBoundaryDatabaseFilePath_Database_on_path__0__linked, selectedFile); }