// 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.Linq; using Application.Ringtoets.Migration; using Application.Ringtoets.MigrationConsole.Properties; using Migration.Console; using Migration.Scripts.Data.Exceptions; using MigrationConsoleResources = Migration.Console.Properties.Resources; namespace Application.Ringtoets.MigrationConsole { /// /// Console application that can migrate a Ringtoets database file to a newer version. /// public static class RingtoetsMigrationTool { private const string commandMigrate = "--migrate"; private const string commandVersionSupported = "--supported"; private const string commandHelp = "--help"; /// /// Main Ringtoets migration application. /// /// Arguments /// Accepted commands: /// --help Shows help menu, /// --supported Returns if the database file is supported, /// --migrate Migrates the database file to a newer version. /// public static void Main(string[] args) { try { ExecuteCommand(args); } catch (Exception exception) { DisplayException(exception); DisplayAllCommands(); if (exception is CriticalMigrationException || exception is NotSupportedException) { Exit(ErrorCode.ErrorBadCommand); return; } Exit(ErrorCode.ErrorInvalidCommandLine); return; } Exit(ErrorCode.ErrorSuccess); } private static void Exit(ErrorCode errorCode) { EnvironmentControl.Instance.Exit((int) errorCode); } private static void DisplayException(Exception exception) { ConsoleHelper.WriteErrorLine(exception.Message); if (exception.InnerException != null) { ConsoleHelper.WriteErrorLine(MigrationConsoleResources.Message_Inner_Exception_0, exception.InnerException.Message); } ConsoleHelper.WriteErrorLine(""); } private static void ExecuteCommand(string[] args) { string command = args.FirstOrDefault() ?? commandHelp; switch (command) { case commandMigrate: MigrateCommand(args); break; case commandVersionSupported: IsVersionSupportedCommand(args); break; case commandHelp: DisplayAllCommands(); break; default: InvalidCommand(command); break; } } private static void DisplayAllCommands() { Console.WriteLine(commandHelp + "\t" + Resources.CommandHelp_Command_0_Detailed, commandHelp); ShowMigrateCommand(); ShowSupportedCommand(); } #region Invalid Command private static void InvalidCommand(string command) { throw new NotSupportedException(string.Format(Resources.CommandInvalid_Command_0_Is_not_valid, command)); } #endregion #region Version Supported Command private static void IsVersionSupportedCommand(string[] args) { if (args.Length != 2) { throw new InvalidOperationException(string.Format(MigrationConsoleResources.Command_0_Incorrect_number_of_parameters, commandVersionSupported)); } string version = args[1]; var migrator = new RingtoetsSqLiteDatabaseFileMigrator(); bool isSupported = migrator.IsVersionSupported(version); Console.WriteLine(isSupported ? "Version '{0}' is supported." : "Version '{0}' is not supported.", version); } private static void ShowSupportedCommand() { Console.WriteLine(Resources.CommandSupported_Command_0_Brief + "\t" + Resources.CommandSupported_Detailed, commandVersionSupported); } #endregion #region Migrate Command private static void MigrateCommand(string[] args) { if (args.Length != 4) { throw new InvalidOperationException(string.Format(MigrationConsoleResources.Command_0_Incorrect_number_of_parameters, commandMigrate)); } var migrator = new RingtoetsSqLiteDatabaseFileMigrator(); string filepath = args[1]; string toVersion = args[2]; string toFilepath = args[3]; var sourceFile = new RingtoetsVersionedFile(filepath); migrator.Migrate(sourceFile, toVersion, toFilepath); Console.WriteLine(Resources.CommandMigrate_Successful_Migration_From_Location_0_To_Version_1_At_Location_2, filepath, toVersion, toFilepath); } private static void ShowMigrateCommand() { Console.WriteLine(Resources.CommandMigrate_Command_0_Brief + "\t" + Resources.CommandMigrate_Detailed, commandMigrate); } #endregion } }