// Copyright (C) Stichting Deltares 2017. 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.Globalization;
using System.IO;
using Core.Common.Base.IO;
using Core.Common.Utils;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext;
using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext;
using Ringtoets.HydraRing.IO.Properties;
namespace Ringtoets.Common.IO.HydraRing
{
///
/// This class defines helper methods for obtaining meta data from hyraulic boundary databases.
///
public static class HydraulicDatabaseHelper
{
private const string hydraRingConfigurationDatabaseExtension = "config.sqlite";
private const string hlcdFileName = "HLCD.sqlite";
///
/// Attempts to connect to the as if it is a Hydraulic Boundary Locations
/// database with a Hydraulic Location Configurations database next to it.
///
/// The path of the Hydraulic Boundary Locations database file.
/// A describing the problem when trying to connect to the
/// or null if a connection could be correctly made.
public static string ValidatePathForCalculation(string filePath)
{
try
{
IOUtils.ValidateFilePath(filePath);
}
catch (ArgumentException e)
{
return e.Message;
}
string directoryName;
try
{
directoryName = Path.GetDirectoryName(filePath);
}
catch (PathTooLongException)
{
return string.Format(CultureInfo.CurrentCulture, Resources.HydraulicDatabaseHelper_ValidatePathForCalculation_Invalid_path_0_,
filePath);
}
string settingsDatabaseFileName = GetHydraulicBoundarySettingsDatabase(filePath);
try
{
using (var db = new HydraulicBoundarySqLiteDatabaseReader(filePath))
{
db.GetVersion();
}
string hlcdFilePath = Path.Combine(directoryName, hlcdFileName);
using (new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath)) {}
using (new DesignTablesSettingsProvider(settingsDatabaseFileName)) {}
using (new TimeIntegrationSettingsProvider(settingsDatabaseFileName)) {}
using (new NumericsSettingsProvider(settingsDatabaseFileName)) {}
}
catch (CriticalFileReadException e)
{
return e.Message;
}
return null;
}
///
/// 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 equals the version of the database at
/// , 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(nameof(database));
}
if (pathToDatabase == null)
{
throw new ArgumentNullException(nameof(pathToDatabase));
}
return database.Version == GetVersion(pathToDatabase);
}
public static string GetHydraulicBoundarySettingsDatabase(string hydraulicBoundaryDatabaseFilePath)
{
return Path.ChangeExtension(hydraulicBoundaryDatabaseFilePath, hydraRingConfigurationDatabaseExtension);
}
///
/// 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();
}
}
}
}