// Copyright (C) Stichting Deltares 2018. 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.Util.Builders;
using Riskeer.HydraRing.IO.Properties;
namespace Riskeer.HydraRing.IO.HydraulicBoundaryDatabase
{
///
/// This class reads a hydraulic boundary database file and constructs a
/// instance from this database.
///
public class HydraulicBoundaryDatabaseReader : SqLiteDatabaseReaderBase
{
///
/// 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 HydraulicBoundaryDatabaseReader(string databaseFilePath) : base(databaseFilePath) {}
///
/// Reads the hydraulic boundary database.
///
/// A .
/// Thrown when the database contains incorrect values for required properties.
/// Thrown when the data cannot be read.
public ReadHydraulicBoundaryDatabase Read()
{
return new ReadHydraulicBoundaryDatabase(ReadTrackId(), ReadVersion(), ReadLocations().ToArray());
}
///
/// Reads the track Id from the hydraulic boundary database.
///
/// The track Id found in the database.
/// Thrown when the database contains incorrect values for required properties.
/// Thrown when the track Id cannot be read.
public long ReadTrackId()
{
try
{
using (IDataReader reader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetTrackIdQuery(),
new SQLiteParameter
{
DbType = DbType.String
}))
{
if (reader.Read())
{
return Convert.ToInt64(reader[GeneralTableDefinitions.TrackId]);
}
throw new CriticalFileReadException(new FileReaderErrorMessageBuilder(Path)
.Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column));
}
}
catch (InvalidCastException exception)
{
string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column);
throw new LineParseException(message, exception);
}
catch (SQLiteException exception)
{
string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database);
throw new CriticalFileReadException(message, exception);
}
}
///
/// Gets the version of the hydraulic boundary database.
///
/// The version found in the database, or if the version cannot be found.
/// Thrown when the version cannot be read..
public string ReadVersion()
{
try
{
using (IDataReader reader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetVersionQuery(), null))
{
if (reader.Read())
{
string version = Convert.ToString(reader[GeneralTableDefinitions.GeneratedVersion]);
if (!string.IsNullOrEmpty(version))
{
return version;
}
}
string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column);
throw new CriticalFileReadException(message);
}
}
catch (SQLiteException e)
{
string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.Error_HydraulicBoundaryLocation_read_from_database);
throw new CriticalFileReadException(message, e);
}
}
///
/// Reads the locations from the database.
///
/// Thrown when the database contains incorrect values for required properties.
private IEnumerable ReadLocations()
{
using (IDataReader reader = CreateDataReader(HydraulicBoundaryDatabaseQueryBuilder.GetRelevantLocationsQuery()))
{
while (MoveNext(reader))
{
yield return ReadLocation(reader);
}
}
}
///
/// Reads a location from the database.
///
/// A based on the data read from the database.
/// Thrown when the database contains incorrect values for required properties.
private ReadHydraulicBoundaryLocation ReadLocation(IDataReader reader)
{
try
{
var id = reader.Read(HrdLocationsTableDefinitions.HrdLocationId);
var name = reader.Read(HrdLocationsTableDefinitions.Name);
var x = reader.Read(HrdLocationsTableDefinitions.XCoordinate);
var y = reader.Read(HrdLocationsTableDefinitions.YCoordinate);
return new ReadHydraulicBoundaryLocation(id, name, x, y);
}
catch (ConversionException e)
{
string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column);
throw new LineParseException(message, e);
}
}
}
}