// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.IO; using Application.Ringtoets.Migration.Core; using Application.Ringtoets.Migration.Properties; using Core.Common.Base.Storage; using Core.Common.Gui; using Core.Common.Utils; using log4net; using Migration.Scripts.Data.Exceptions; using Ringtoets.Common.Utils; using MigrationCoreStorageResources = Migration.Core.Storage.Properties.Resources; namespace Application.Ringtoets.Migration { /// /// A GUI implementation to migrate a Ringtoets database file to a newer version. /// public class RingtoetsProjectMigrator : IMigrateProject { private static readonly string currentDatabaseVersion = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); private readonly ILog log = LogManager.GetLogger(typeof(RingtoetsProjectMigrator)); private readonly RingtoetsSqLiteDatabaseFileMigrator fileMigrator; private readonly IInquiryHelper inquiryHelper; private readonly FileFilterGenerator fileFilter; /// /// Instantiates a . /// /// Object responsible for inquiring the data. public RingtoetsProjectMigrator(IInquiryHelper inquiryHelper) { if (inquiryHelper == null) { throw new ArgumentNullException(nameof(inquiryHelper)); } this.inquiryHelper = inquiryHelper; fileMigrator = new RingtoetsSqLiteDatabaseFileMigrator(); fileFilter = new FileFilterGenerator(Resources.RingtoetsProject_FileExtension, Resources.RingtoetsProject_TypeDescription); } public MigrationNeeded ShouldMigrate(string filePath) { if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } ValidateProjectPath(filePath); var versionedFile = new RingtoetsVersionedFile(filePath); string version = versionedFile.GetVersion(); if (version.Equals(currentDatabaseVersion)) { return MigrationNeeded.No; } if (!fileMigrator.IsVersionSupported(version)) { string errorMessage = string.Format(MigrationCoreStorageResources.Migrate_From_Version_0_To_Version_1_Not_Supported, version, currentDatabaseVersion); log.Error(errorMessage); return MigrationNeeded.Aborted; } string query = string.Format(Resources.RingtoetsProjectMigrator_Migrate_Outdated_project_file_update_to_current_version_0_inquire, currentDatabaseVersion); if (inquiryHelper.InquireContinuation(query)) { return MigrationNeeded.Yes; } else { GenerateMigrationCancelledLogMessage(filePath); return MigrationNeeded.Aborted; } } public string Migrate(string filePath) { if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } ValidateProjectPath(filePath); string suggestedFileName = GetSuggestedFileName(filePath); string targetLocation = inquiryHelper.GetTargetFileLocation(fileFilter, suggestedFileName); if (!string.IsNullOrEmpty(targetLocation)) { return MigrateToTargetLocation(filePath, targetLocation); } GenerateMigrationCancelledLogMessage(filePath); return null; } private string MigrateToTargetLocation(string sourceFilePath, string targetLocation) { try { var versionedFile = new RingtoetsVersionedFile(sourceFilePath); fileMigrator.Migrate(versionedFile, currentDatabaseVersion, targetLocation); string message = string.Format(Resources.RingtoetsProjectMigrator_MigrateToTargetLocation_Outdated_projectfile_0_succesfully_updated_to_target_filepath_1_version_2_, sourceFilePath, targetLocation, currentDatabaseVersion); log.Info(message); return targetLocation; } catch (CriticalMigrationException e) { string errorMessage = string.Format(Resources.RingtoetsProjectMigrator_MigrateToTargetLocation_Updating_outdated_projectfile_0_failed_with_exception_1_, sourceFilePath, e.Message); log.Error(errorMessage, e); return null; } } private static string GetSuggestedFileName(string sourceFilePath) { string fileName = Path.GetFileNameWithoutExtension(sourceFilePath); string versionSuffix = currentDatabaseVersion.Replace(".", "-"); string suggestedFileName = $"{fileName}_{versionSuffix}"; return suggestedFileName; } private void GenerateMigrationCancelledLogMessage(string sourceFilePath) { string warningMessage = string.Format(Resources.RingtoetsProjectMigrator_GenerateMigrationCancelledLogMessage_Updating_projectfile_0_was_cancelled, sourceFilePath); log.Warn(warningMessage); } private static void ValidateProjectPath(string sourceFilePath) { if (!IOUtils.IsValidFilePath(sourceFilePath)) { throw new ArgumentException(Resources.RingtoetsProjectMigrator_ValidateProjectPath_Source_filepath_must_be_a_valid_path, nameof(sourceFilePath)); } } } }