using System;
using System.IO;
using Core.Common.IO.Exceptions;
using Ringtoets.HydraRing.Data;
using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext;
using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext;
namespace Ringtoets.HydraRing.IO
{
///
/// This class defines helper methods for obtaining meta data from hyraulic boundary databases.
///
public static class HydraulicDatabaseHelper
{
///
/// 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
{
using (var db = new HydraulicBoundarySqLiteDatabaseReader(filePath))
{
db.GetVersion();
}
string hlcdFilePath = Path.Combine(Path.GetDirectoryName(filePath), "hlcd.sqlite");
new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath).Dispose();
}
catch (CriticalFileReadException e)
{
return e.Message;
}
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);
}
}
}