// 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.Data;
using System.Data.SQLite;
using Core.Common.IO.Exceptions;
using Core.Common.IO.Readers;
using Core.Common.Utils.Builders;
using log4net;
using Ringtoets.HydraRing.IO.Properties;
namespace Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext
{
///
/// This class reads an HLCD database file and reads location ids from this database.
///
public class HydraulicLocationConfigurationSqLiteDatabaseReader : SqLiteDatabaseReaderBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(HydraulicLocationConfigurationSqLiteDatabaseReader));
///
/// Creates a new instance of ,
/// which will use the as its source.
///
/// The path of the database file to open.
/// Thrown when:
///
/// - The contains invalid characters.
/// - No file could be found at .
///
///
public HydraulicLocationConfigurationSqLiteDatabaseReader(string databaseFilePath) : base(databaseFilePath) {}
///
/// Gets the location id from the database, based upon and .
///
/// Hydraulic boundary region id.
/// Hydraulic boundary location id.
/// The location id found in the database, or 0 otherwise.
/// Thrown when the database query failed.
/// Thrown when the database returned incorrect values for
/// required properties.
public int GetLocationId(long regionId, long hrdLocationId)
{
var locationIdQuery = HydraulicLocationConfigurationDatabaseQueryBuilder.GetLocationIdQuery();
var regionParameter = new SQLiteParameter
{
DbType = DbType.String,
ParameterName = LocationsTableDefinitions.RegionId,
Value = regionId
};
var hrdLocationParameter = new SQLiteParameter
{
DbType = DbType.String,
ParameterName = LocationsTableDefinitions.HrdLocationId,
Value = hrdLocationId
};
try
{
return GetLocationIdFromDatabase(locationIdQuery, regionParameter, hrdLocationParameter);
}
catch (InvalidCastException exception)
{
var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column);
throw new LineParseException(message, exception);
}
catch (SQLiteException exception)
{
var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception);
throw new CriticalFileReadException(message, exception);
}
}
private int GetLocationIdFromDatabase(string locationIdQuery, SQLiteParameter regionParameter, SQLiteParameter hrdLocationParameter)
{
using (SQLiteDataReader dataReader = CreateDataReader(locationIdQuery, regionParameter, hrdLocationParameter))
{
if (!dataReader.Read())
{
return 0;
}
var locationCount = Convert.ToInt32(dataReader[LocationsTableDefinitions.Count]);
// Must be unique
if (locationCount > 1)
{
log.Warn(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_GetLocationIdFromDatabase_Ambiguous_Row_Found_Take_First);
}
return Convert.ToInt32(dataReader[LocationsTableDefinitions.LocationId]);
}
}
}
}