// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the application DAM - Clients Library. // // DAM - UI 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.IO; using System.Linq; using System.Threading; using Deltares.Dam.Data.Importers; using Deltares.Standard.IO; using Deltares.Standard.Language; using LumenWorks.Framework.IO.Csv; namespace Deltares.Dam.Data.CsvImporters { public class CsvImporterScenariosException : ApplicationException { public CsvImporterScenariosException(string message) : base(message) { } } public class CsvImporterScenarios { private List scenarioRecords = new List(); private List errorMessages = new List(); public class ScenarioRecord { public ScenarioRecord() { LocationScenarioId = null; } private double upliftCriterionPiping = 1.2; private double upliftCriterionStability = 1.2; private double safetyFactorPiping = 1.0; private double safetyFactorStabilityInnerSlope = 1.0; private double safetyFactorStabilityOuterSlope = 1.0; public int ScenarioRecordId { get; set; } public string LocationId { get; set; } //v public string LocationScenarioId { get; set; } //v public double? WaterHeight { get; set; } //v public double? WaterHeightLow { get; set; } //o public double? DikeTableHeight { get; set; } //v public double? WaterHeightDecimeringsHoogte { get; set; } //o public double? MaxWaterHeight { get; set; } //o public double ProbabilityOfFailureStabilityInnerSlope { get; set; } //o public double ProbabilityOfFailureStabilityOuterSlope { get; set; } //o public double ProbabilityOfFailurePiping { get; set; } //o public double? PlLineOffsetBelowDikeTopAtRiver { get; set; } //o public double? PlLineOffsetBelowDikeTopAtPolder { get; set; } //o public double? PlLineOffsetBelowShoulderBaseInside { get; set; } //o public double? PlLineOffsetBelowDikeToeAtPolder { get; set; } //o public double? PlLineOffsetBelowDikeCrestMiddle { get; set; } //o public double? PlLineOffsetFactorBelowShoulderCrest { get; set; } //o public bool? UsePlLineOffsetBelowDikeCrestMiddle { get; set; } //o public bool? UsePlLineOffsetFactorBelowShoulderCrest { get; set; } //o public double? HeadPl3 { get; set; } //o public double? HeadPl4 { get; set; } //o public double UpliftCriterionPiping { get { return upliftCriterionPiping; } set { upliftCriterionPiping = value; } } public double UpliftCriterionStability { get { return upliftCriterionStability; } set { upliftCriterionStability = value; } } public double SafetyFactorPiping { get { return safetyFactorPiping; } set { safetyFactorPiping = value; } } public double SafetyFactorStabilityInnerSlope { get { return safetyFactorStabilityInnerSlope; } set { safetyFactorStabilityInnerSlope = value; } } public double SafetyFactorStabilityOuterSlope { get { return safetyFactorStabilityOuterSlope; } set { safetyFactorStabilityOuterSlope = value; } } } private void CheckColumn(int index, string fileName, string fieldName) { if (index < 0) { var csvHeaderFieldError = LocalizationManager.GetTranslatedText(this.GetType(), "csvHeaderFieldError"); throw new CsvImporterScenariosException(String.Format("{0} : {1} {2}", fileName, csvHeaderFieldError, fieldName)); } } public CsvImporterScenarios(string fileName) { errorMessages.Clear(); ThrowHelper.ThrowIfStringArgumentNullOrEmpty(fileName, StringResourceNames.CsvFileNotValid); ThrowHelper.ThrowIfFileNotExist(fileName, StringResourceNames.CsvFileNotFound); var oldcur = Thread.CurrentThread.CurrentCulture; try { Thread.CurrentThread.CurrentCulture = CsvReaderUtilities.DetermineCultureForFile(fileName); using (CsvReader csv = new CsvReader(new StreamReader(fileName), true, ';')) { string[] headers = CsvImporterHelper.GetFieldHeaders(this, csv); if (headers.Count() < 4) { var csvHeaderError = LocalizationManager.GetTranslatedText(this.GetType(), "csvHeaderError"); throw new CsvImporterSegmentsException(String.Format("{0} : {1}", fileName, csvHeaderError)); } const string fieldLocationId = "location_id"; int colIndexLocationId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldLocationId); CheckColumn(colIndexLocationId, fileName, fieldLocationId); const string fieldLocationScenarioId = "location_scenario_id"; int colIndexLocationScenarioId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldLocationScenarioId); CheckColumn(colIndexLocationScenarioId, fileName, fieldLocationScenarioId); const string fieldWaterHeight = "water_height"; int colIndexWaterHeight = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldWaterHeight); CheckColumn(colIndexWaterHeight, fileName, fieldWaterHeight); const string fieldWaterHeightLow = "water_height_low"; int colIndexWaterHeightLow = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldWaterHeightLow); const string fieldDikeTableHeight = "dike_table_height"; int colIndexDikeTableHeight = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldDikeTableHeight); CheckColumn(colIndexDikeTableHeight, fileName, fieldDikeTableHeight); const string fieldWaterHeightDecimeringsHoogte = "water_height_decimerings_hoogte"; int colIndexWaterHeightDecimeringsHoogte = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldWaterHeightDecimeringsHoogte); const string fieldMaxWaterHeight = "max_waterheight"; int colIndexMaxWaterHeight = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldMaxWaterHeight); int colIndexSafetyFactorPiping = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.SafetyFactorPiping); int colIndexSafetyFactorStabilityInnerSlope = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.SafetyFactorStabilityInnerslope); int colIndexSafetyFactorStabilityOuterSlope = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.SafetyFactorStabilityOuterslope); int colIndexProbabilityOfFailurePiping = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.ProbabilityOfFailurePiping); int colIndexProbabilityOfFailureStabilityInnerSlope = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.ProbabilityOfFailureStabilityInnerslope); int colIndexProbabilityOfFailureStabilityOuterSlope = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.ProbabilityOfFailureStabilityOuterslope); int colIndexUpliftCriterionPiping = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.UpliftCriterionPiping); int colIndexUpliftCriterionStability = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.UpliftCriterionStability); const string fieldPlLineOffsetBelowDikeTopAtRiver = "PLLineOffsetBelowDikeTopAtRiver"; int colIndexPlLineOffsetBelowDikeTopAtRiver = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldPlLineOffsetBelowDikeTopAtRiver); const string fieldPlLineOffsetBelowDikeTopAtPolder = "PLLineOffsetBelowDikeTopAtPolder"; int colIndexPlLineOffsetBelowDikeTopAtPolder = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldPlLineOffsetBelowDikeTopAtPolder); const string fieldPlLineOffsetBelowShoulderBaseInside = "PLLineOffsetBelowShoulderBaseInside"; int colIndexPlLineOffsetBelowShoulderBaseInside = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldPlLineOffsetBelowShoulderBaseInside); const string fieldPlLineOffsetBelowDikeToeAtPolder = "PLLineOffsetBelowDikeToeAtPolder"; int colIndexPlLineOffsetBelowDikeToeAtPolder = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldPlLineOffsetBelowDikeToeAtPolder); const string fieldPlLineOffsetBelowDikeCrestMiddle = "PLLineOffsetBelowDikeCrestMiddle"; int colIndexPlLineOffsetBelowDikeCrestMiddle = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldPlLineOffsetBelowDikeCrestMiddle); const string fieldPlLineOffsetFactorBelowShoulderCrest = "PLLineOffsetFactorBelowShoulderCrest"; int colIndexPlLineOffsetFactorBelowShoulderCrest = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldPlLineOffsetFactorBelowShoulderCrest); const string fieldUsePlLineOffsetBelowDikeCrestMiddle = "UsePLLineOffsetBelowDikeCrestMiddle"; int colIndexUsePlLineOffsetBelowDikeCrestMiddle = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldUsePlLineOffsetBelowDikeCrestMiddle); const string fieldUsePlLineOffsetFactorBelowShoulderCrest = "UsePLLineOffsetFactorBelowShoulderCrest"; int colIndexUsePlLineOffsetFactorBelowShoulderCrest = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldUsePlLineOffsetFactorBelowShoulderCrest); const string fieldHeadPl3 = "HeadPL3"; int colIndexHeadPl3 = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldHeadPl3); const string fieldHeadPl4 = "HeadPL4"; int colIndexHeadPl4 = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldHeadPl4); var index = 1; while (csv.ReadNextRecord()) { ScenarioRecord scenario = new ScenarioRecord(); int colIndex = -1; // Keep track of column for error message try { scenario.ScenarioRecordId = index; index++; scenario.LocationId = csv[colIndexLocationId]; // faassen: changed location scenario id type from int to string (default value was -1 now null is used) string value = csv[colIndexLocationScenarioId]; scenario.LocationScenarioId = string.IsNullOrWhiteSpace(value) ? null : value; colIndex = colIndexWaterHeight; scenario.WaterHeight = Convert.ToDouble(csv[colIndexWaterHeight]); if (colIndexWaterHeightLow > -1) { colIndex = colIndexWaterHeightLow; scenario.WaterHeightLow = Convert.ToDouble(csv[colIndex]); } else { scenario.WaterHeightLow = scenario.WaterHeight; } colIndex = colIndexDikeTableHeight; scenario.DikeTableHeight = Convert.ToDouble(csv[colIndexDikeTableHeight]); if (colIndexWaterHeightDecimeringsHoogte > -1) { colIndex = colIndexWaterHeightDecimeringsHoogte; scenario.WaterHeightDecimeringsHoogte = Convert.ToDouble(csv[colIndex]); } if (colIndexMaxWaterHeight > -1) { colIndex = colIndexMaxWaterHeight; scenario.MaxWaterHeight = Convert.ToDouble(csv[colIndex]); } if (colIndexSafetyFactorPiping > -1) { colIndex = colIndexSafetyFactorPiping; scenario.SafetyFactorPiping = Convert.ToDouble(csv[colIndex]); } if (colIndexSafetyFactorStabilityInnerSlope > -1) { colIndex = colIndexSafetyFactorStabilityInnerSlope; scenario.SafetyFactorStabilityInnerSlope = Convert.ToDouble(csv[colIndex]); } if (colIndexSafetyFactorStabilityOuterSlope > -1) { colIndex = colIndexSafetyFactorStabilityOuterSlope; scenario.SafetyFactorStabilityOuterSlope = Convert.ToDouble(csv[colIndex]); } if (colIndexProbabilityOfFailurePiping > -1) { colIndex = colIndexProbabilityOfFailurePiping; scenario.ProbabilityOfFailurePiping = Convert.ToDouble(csv[colIndex]); } if (colIndexProbabilityOfFailureStabilityInnerSlope > -1) { colIndex = colIndexProbabilityOfFailureStabilityInnerSlope; scenario.ProbabilityOfFailureStabilityInnerSlope = Convert.ToDouble(csv[colIndex]); } if (colIndexProbabilityOfFailureStabilityOuterSlope > -1) { colIndex = colIndexProbabilityOfFailureStabilityOuterSlope; scenario.ProbabilityOfFailureStabilityOuterSlope = Convert.ToDouble(csv[colIndex]); } if (colIndexUpliftCriterionPiping > -1) { colIndex = colIndexUpliftCriterionPiping; scenario.UpliftCriterionPiping = Convert.ToDouble(csv[colIndex]); } if (colIndexUpliftCriterionStability > -1) { colIndex = colIndexUpliftCriterionStability; scenario.UpliftCriterionStability = Convert.ToDouble(csv[colIndex]); } if (colIndexPlLineOffsetBelowDikeTopAtRiver > -1) { colIndex = colIndexPlLineOffsetBelowDikeTopAtRiver; scenario.PlLineOffsetBelowDikeTopAtRiver = Convert.ToDouble(csv[colIndex]); } if (colIndexPlLineOffsetBelowDikeTopAtPolder > -1) { colIndex = colIndexPlLineOffsetBelowDikeTopAtPolder; scenario.PlLineOffsetBelowDikeTopAtPolder = Convert.ToDouble(csv[colIndex]); } if (colIndexPlLineOffsetBelowShoulderBaseInside > -1) { colIndex = colIndexPlLineOffsetBelowShoulderBaseInside; scenario.PlLineOffsetBelowShoulderBaseInside = Convert.ToDouble(csv[colIndex]); } if (colIndexPlLineOffsetBelowDikeToeAtPolder > -1) { colIndex = colIndexPlLineOffsetBelowDikeToeAtPolder; scenario.PlLineOffsetBelowDikeToeAtPolder = Convert.ToDouble(csv[colIndex]); } if (colIndexPlLineOffsetBelowDikeCrestMiddle > -1) { colIndex = colIndexPlLineOffsetBelowDikeCrestMiddle; scenario.PlLineOffsetBelowDikeCrestMiddle = Convert.ToDouble(csv[colIndex]); } if (colIndexPlLineOffsetFactorBelowShoulderCrest > -1) { colIndex = colIndexPlLineOffsetFactorBelowShoulderCrest; scenario.PlLineOffsetFactorBelowShoulderCrest = Convert.ToDouble(csv[colIndex]); } if (colIndexUsePlLineOffsetBelowDikeCrestMiddle > -1) { colIndex = colIndexUsePlLineOffsetBelowDikeCrestMiddle; scenario.UsePlLineOffsetBelowDikeCrestMiddle = Convert.ToBoolean(csv[colIndex]); } if (colIndexUsePlLineOffsetFactorBelowShoulderCrest > -1) { colIndex = colIndexUsePlLineOffsetFactorBelowShoulderCrest; scenario.UsePlLineOffsetFactorBelowShoulderCrest = Convert.ToBoolean(csv[colIndex]); } if (colIndexHeadPl3 > -1) { colIndex = colIndexHeadPl3; scenario.HeadPl3 = Convert.ToDouble(csv[colIndex]); } if (colIndexHeadPl4 > -1) { colIndex = colIndexHeadPl4; scenario.HeadPl4 = Convert.ToDouble(csv[colIndex]); } scenarioRecords.Add(scenario); } catch (Exception e) { var csvScenarioError = String.Format(LocalizationManager.GetTranslatedText(this.GetType(), "csvScenarioError"), scenario.LocationId, colIndex + 1); errorMessages.Add(csvScenarioError + e.Message); } } } } finally { Thread.CurrentThread.CurrentCulture = oldcur; } } public List ImportedItems { get { return scenarioRecords; } } public List ErrorMessages { get { return errorMessages; } set { errorMessages = value; } } } }