// 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.SQLite;
using Ringtoets.HydraRing.Calculation.Exceptions;
using Ringtoets.HydraRing.Calculation.Properties;
using Ringtoets.HydraRing.Calculation.Readers;
namespace Ringtoets.HydraRing.Calculation.Parsers
{
///
/// Helper class for methods that apply for parsing output from a Hydra-Ring database.
///
internal static class HydraRingDatabaseParseHelper
{
///
/// Parses the Hydra-Ring output database.
///
/// The path to the directory which contains
/// the output of the Hydra-Ring calculation.
/// The query to perform when reading the database.
/// The section id to get the output for.
/// The exception message when there is no result.
/// A with the key of the column and the value.
/// Thrown when any input parameter is null.
/// Thrown when the reader encounters an error while
/// reading the database.
public static Dictionary ReadSingleLine(string workingDirectory,
string query,
int sectionId,
string exceptionMessage)
{
ValidateParameters(workingDirectory, query, exceptionMessage);
try
{
using (var reader = new HydraRingDatabaseReader(workingDirectory, query, sectionId))
{
return ReadLineFromReader(exceptionMessage, reader);
}
}
catch (SQLiteException e)
{
throw new HydraRingFileParserException(Resources.Parse_Cannot_read_result_in_output_file, e);
}
}
///
/// Tries to read a result from the reader and throws an exception if no row could be read.
///
/// The message to use in the exception when reading fails.
/// The reader to read a row from.
/// A single row from the reader.
/// Thrown when no row could be read from the
/// .
private static Dictionary ReadLineFromReader(string exceptionMessage, HydraRingDatabaseReader reader)
{
Dictionary result = reader.ReadLine();
if (result != null)
{
return result;
}
throw new HydraRingFileParserException(exceptionMessage);
}
private static void ValidateParameters(string workingDirectory,
string query,
string exceptionMessage)
{
if (workingDirectory == null)
{
throw new ArgumentNullException(nameof(workingDirectory));
}
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}
if (exceptionMessage == null)
{
throw new ArgumentNullException(nameof(exceptionMessage));
}
}
}
}