// 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.Collections.Generic; using System.Data; using System.Data.SQLite; using System.Linq; using Core.Common.Base.IO; 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 ids from the database, based upon . /// /// Hydraulic boundary track id. /// List of location id and Hrd location id found in the database. /// Thrown when the database query failed. /// Thrown when the database returned incorrect values for /// required properties. public Dictionary GetLocationsIdByTrackId(long trackId) { var trackParameter = new SQLiteParameter { DbType = DbType.String, ParameterName = TracksTableDefinitions.TrackId, Value = trackId }; try { return GetLocationIdsFromDatabase(trackParameter); } catch (SQLiteException exception) { string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception); throw new CriticalFileReadException(message, exception); } catch (InvalidCastException exception) { string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); throw new LineParseException(message, exception); } } /// /// Gets whether the preprocessor can be used for the given . /// /// Hydraulic boundary track id. /// The value found in the database; or false when the database /// is valid but outdated (no usePreprocessor column present). /// Thrown when the database query failed. /// Thrown when the database returned incorrect values for /// required properties. public bool GetCanUsePreprocessorByTrackId(long trackId) { var trackParameter = new SQLiteParameter { DbType = DbType.String, ParameterName = TracksTableDefinitions.TrackId, Value = trackId }; try { return GetCanUsePreprocessorFromDatabase(trackParameter); } catch (SQLiteException exception) { string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception); throw new CriticalFileReadException(message, exception); } catch (FormatException exception) { string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); throw new LineParseException(message, exception); } } /// /// Gets the location ids from the database, based upon . /// /// Hydraulic boundary track id. /// List of location id and Hrd location id found in the database. /// Thrown when the database query failed. /// Thrown when the database returned incorrect values for /// required properties. private Dictionary GetLocationIdsFromDatabase(SQLiteParameter trackParameter) { var dictionary = new Dictionary(); string locationIdQuery = HydraulicLocationConfigurationDatabaseQueryBuilder.GetLocationsIdByTrackIdQuery(); using (IDataReader dataReader = CreateDataReader(locationIdQuery, trackParameter)) { while (MoveNext(dataReader)) { long key = Convert.ToInt64(dataReader[LocationsTableDefinitions.HrdLocationId]); long value = Convert.ToInt64(dataReader[LocationsTableDefinitions.LocationId]); // Must be unique if (dictionary.ContainsKey(key)) { log.Warn(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_GetLocationIdFromDatabase_Ambiguous_Row_Found_Take_First); } else { dictionary.Add(key, value); } } } return dictionary; } /// /// Gets whether the preprocessor can be used for the given . /// /// Hydraulic boundary track id. /// The value found in the database; or false when the database /// is valid but outdated (no usePreprocessor column present). /// Thrown when the database query failed. /// Thrown when the database returned incorrect values for /// required properties. /// Thrown when no results could be found. private bool GetCanUsePreprocessorFromDatabase(SQLiteParameter trackParameter) { string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetUsePreprocessorByTrackIdQuery(); using (IDataReader dataReader = CreateDataReader(query, trackParameter)) { DataTable schemaTable = dataReader.GetSchemaTable(); DataColumn columnName = schemaTable.Columns[schemaTable.Columns.IndexOf("ColumnName")]; if (schemaTable.Rows.Cast().All(row => row[columnName].ToString() != RegionsTableDefinitions.UsePreprocessor)) { return false; } if (MoveNext(dataReader)) { return Convert.ToBoolean(dataReader[RegionsTableDefinitions.UsePreprocessor]); } string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception); throw new CriticalFileReadException(message); } } } }