Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseReader.cs
===================================================================
diff -u -r00cd167e5b3fe6e817e063ab654459c90e550aea -r3b368e38644b163189a58233f9fc1fb9701bf68c
--- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseReader.cs (.../HydraulicLocationConfigurationDatabaseReader.cs) (revision 00cd167e5b3fe6e817e063ab654459c90e550aea)
+++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseReader.cs (.../HydraulicLocationConfigurationDatabaseReader.cs) (revision 3b368e38644b163189a58233f9fc1fb9701bf68c)
@@ -1,4 +1,4 @@
-// Copyright (C) Stichting Deltares 2017. All rights reserved.
+// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of Ringtoets.
//
@@ -23,14 +23,15 @@
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.Util.Builders;
using log4net;
-using Ringtoets.HydraRing.IO.Properties;
+using Riskeer.HydraRing.IO.Properties;
-namespace Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabase
+namespace Riskeer.HydraRing.IO.HydraulicLocationConfigurationDatabase
{
///
/// Class for reading information from a hydraulic location configuration database (HLCD).
@@ -53,14 +54,33 @@
public HydraulicLocationConfigurationDatabaseReader(string databaseFilePath) : base(databaseFilePath) {}
///
+ /// Reads the hydraulic location configuration database.
+ ///
+ /// The track id to read the location configurations for.
+ /// A read hydraulic location configuration database.
+ /// Thrown when hydraulic location configuration database
+ /// could not be read.
+ /// Thrown when the database returned incorrect values for
+ /// required properties.
+ public ReadHydraulicLocationConfigurationDatabase Read(long trackId)
+ {
+ IEnumerable configurationSettings = IsScenarioInformationTablePresent()
+ ? GetConfigurationSettings()
+ : null;
+
+ return new ReadHydraulicLocationConfigurationDatabase(GetLocationIdsByTrackId(trackId),
+ configurationSettings);
+ }
+
+ ///
/// Gets the location ids from the database, based upon .
///
/// The hydraulic boundary track id.
- /// A dictionary with pairs of Hrd location id (key) and location id (value) as found in the database.
+ /// A collection of as found in the database.
/// Thrown when the database query failed.
/// Thrown when the database returned incorrect values for
/// required properties.
- public Dictionary GetLocationIdsByTrackId(long trackId)
+ private IEnumerable GetLocationIdsByTrackId(long trackId)
{
var trackParameter = new SQLiteParameter
{
@@ -89,34 +109,145 @@
/// Gets the location ids from the database, based upon .
///
/// A parameter containing the hydraulic boundary track id.
- /// A dictionary with pairs of Hrd location id (key) and location id (value) as found in the database.
+ /// A collection of as 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)
+ private IEnumerable GetLocationIdsFromDatabase(SQLiteParameter trackParameter)
{
- var dictionary = new Dictionary();
string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetLocationIdsByTrackIdQuery();
+ var locationLookup = new Dictionary();
+
using (IDataReader dataReader = CreateDataReader(query, trackParameter))
{
while (MoveNext(dataReader))
{
- long key = Convert.ToInt64(dataReader[LocationsTableDefinitions.HrdLocationId]);
- long value = Convert.ToInt64(dataReader[LocationsTableDefinitions.LocationId]);
+ long hrdLocationId = Convert.ToInt64(dataReader[LocationsTableDefinitions.HrdLocationId]);
+ long hlcdLocationId = Convert.ToInt64(dataReader[LocationsTableDefinitions.LocationId]);
// Must be unique
- if (dictionary.ContainsKey(key))
+ if (locationLookup.ContainsKey(hrdLocationId))
{
log.Warn(Resources.HydraulicLocationConfigurationDatabaseReader_GetLocationIdFromDatabase_Ambiguous_Row_Found_Take_First);
}
else
{
- dictionary.Add(key, value);
+ locationLookup[hrdLocationId] = hlcdLocationId;
}
}
}
- return dictionary;
+ return locationLookup.Select(lookup => new ReadHydraulicLocationMapping(lookup.Key, lookup.Value)).ToArray();
}
+
+ ///
+ /// Gets the hydraulic location configuration settings from the database.
+ ///
+ /// A collection of the read hydraulic configuration database settings.
+ /// Thrown when the database query failed.
+ /// Thrown when the database returned incorrect values for
+ /// required properties.
+ private IEnumerable GetConfigurationSettings()
+ {
+ try
+ {
+ return GetConfigurationSettingsFromDatabase();
+ }
+ catch (SQLiteException exception)
+ {
+ string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationDatabaseReader_Critical_Unexpected_Exception);
+ throw new CriticalFileReadException(message, exception);
+ }
+ }
+
+ ///
+ /// Determines whether the table related to the scenario information is present in the database.
+ ///
+ /// true if the table is present; false otherwise.
+ /// Thrown when the information could not be read from the database file.
+ /// Thrown when the database returned incorrect values for
+ /// required properties.
+ private bool IsScenarioInformationTablePresent()
+ {
+ string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetIsScenarioInformationPresentQuery();
+
+ try
+ {
+ using (IDataReader dataReader = CreateDataReader(query))
+ {
+ if (dataReader.Read())
+ {
+ return Convert.ToBoolean(dataReader[ScenarioInformationTableDefinitions.IsScenarioInformationPresent]);
+ }
+
+ string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column);
+ throw new CriticalFileReadException(message);
+ }
+ }
+ catch (SQLiteException exception)
+ {
+ string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationDatabaseReader_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 the hydraulic location configuration settings from the database.
+ ///
+ /// A collection of the read hydraulic configuration database settings.
+ /// Thrown when the database query failed.
+ /// Thrown when the database returned incorrect values for
+ /// required properties.
+ private IEnumerable GetConfigurationSettingsFromDatabase()
+ {
+ string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetScenarioInformationQuery();
+ var readSettings = new List();
+ using (IDataReader dataReader = CreateDataReader(query))
+ {
+ while (MoveNext(dataReader))
+ {
+ readSettings.Add(ReadSetting(dataReader));
+ }
+ }
+
+ return readSettings;
+ }
+
+ ///
+ /// Reads the hydraulic location configuration setting from the database.
+ ///
+ /// The which is used to read the data.
+ /// The read .
+ /// Thrown when the database returned incorrect values for
+ /// required properties.
+ private ReadHydraulicLocationConfigurationDatabaseSettings ReadSetting(IDataReader reader)
+ {
+ try
+ {
+ var scenarioName = reader.Read(ScenarioInformationTableDefinitions.ScenarioName);
+ var year = reader.Read(ScenarioInformationTableDefinitions.Year);
+ var scope = reader.Read(ScenarioInformationTableDefinitions.Scope);
+ var seaLevel = reader.Read(ScenarioInformationTableDefinitions.SeaLevel);
+ var riverDischarge = reader.Read(ScenarioInformationTableDefinitions.RiverDischarge);
+ var lakeLevel = reader.Read(ScenarioInformationTableDefinitions.LakeLevel);
+ var windDirection = reader.Read(ScenarioInformationTableDefinitions.WindDirection);
+ var windSpeed = reader.Read(ScenarioInformationTableDefinitions.WindSpeed);
+ var comment = reader.Read(ScenarioInformationTableDefinitions.Comment);
+
+ return new ReadHydraulicLocationConfigurationDatabaseSettings(scenarioName, year, scope,
+ seaLevel, riverDischarge, lakeLevel,
+ windDirection, windSpeed, comment);
+ }
+ catch (ConversionException e)
+ {
+ string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column);
+ throw new LineParseException(message, e);
+ }
+ }
}
}
\ No newline at end of file