// 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.Data; using System.Data.SQLite; using Core.Common.Base.IO; using Core.Common.IO.Readers; using Core.Common.Utils.Builders; using Ringtoets.Common.IO.Properties; using Ringtoets.Common.IO.SoilProfile.Schema; namespace Ringtoets.Common.IO.SoilProfile { /// /// This class reads a D-Soil Model file and validates whether it meets required constraints. /// public class SoilDatabaseConstraintsReader : SqLiteDatabaseReaderBase { /// /// Creates a new instance of , /// that 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 SoilDatabaseConstraintsReader(string databaseFilePath) : base(databaseFilePath) {} /// /// Verifies that the database meets required constraints. /// /// Thrown when: /// /// Required information for constraint evaluation could not be read; /// The database segment names are not unique. /// /// public void VerifyConstraints() { try { ReadUniqueSegements(); } catch (SQLiteException exception) { throw new CriticalFileReadException( BuildMessageWithPath(string.Format( Resources.SoilDatabaseConstraintsReader_VerifyConstraints_Can_not_read_StochasticSoilModel_Perhaps_table_missing, StochasticSoilModelTableDefinitions.TableName)), exception); } try { ReadAllProbabilitiesValid(); } catch (SQLiteException exception) { throw new CriticalFileReadException( BuildMessageWithPath(string.Format( Resources.SoilDatabaseConstraintsReader_VerifyConstraints_Can_not_read_StochasticSoilProfile_Perhaps_table_missing, StochasticSoilProfileTableDefinitions.TableName)), exception); } } /// /// Verifies that the segments in the database are unique. /// /// Thrown when the execution the query failed. /// Thrown when: /// /// The database segments could not be read; /// The database segments are not unique. /// /// private void ReadUniqueSegements() { string query = SoilDatabaseQueryBuilder.GetSoilModelNamesUniqueQuery(); using (IDataReader dataReader = CreateDataReader(query)) { if (!dataReader.Read()) { throw new CriticalFileReadException( BuildMessageWithPath(Resources.SoilDatabaseConstraintsReader_VerifyConstraints_Unexpected_error_while_verifying_unique_StochasticSoilModel_names)); } if (!Convert.ToBoolean(dataReader[StochasticSoilModelTableDefinitions.AreSegmentsUnique])) { throw new CriticalFileReadException( BuildMessageWithPath(Resources.SoilDatabaseConstraintsReader_VerifyConstraints_Non_unique_StochasticSoilModel_names)); } } } /// /// Verifies that the probabilities in the database are valid. /// /// Thrown when the execution the query failed. /// Thrown when: /// /// The probabilities could not be read; /// One or more probabilities are not valid. /// /// /// A valid probability: /// /// has a value in the interval [0.0, 1.0]; /// is not null. /// /// private void ReadAllProbabilitiesValid() { string checkSegmentNameUniqueness = SoilDatabaseQueryBuilder.GetStochasticSoilProfileProbabilitiesValidQuery(); using (IDataReader dataReader = CreateDataReader(checkSegmentNameUniqueness)) { if (!dataReader.Read()) { throw new CriticalFileReadException( BuildMessageWithPath(Resources.SoilDatabaseConstraintsReader_VerifyConstraints_Unexpected_error_while_verifying_valid_StochasticSoilProfile_probability)); } if (!Convert.ToBoolean(dataReader[StochasticSoilProfileTableDefinitions.AllProbabilitiesValid])) { throw new CriticalFileReadException( BuildMessageWithPath(Resources.SoilDatabaseConstraintsReader_VerifyConstraints_Invalid_StochasticSoilProfile_probability)); } } } private string BuildMessageWithPath(string innerMessage) { return new FileReaderErrorMessageBuilder(Path).Build(innerMessage); } } }