// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// 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.Globalization;
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
{
public CsvImporterScenarios(string fileName)
{
ErrorMessages.Clear();
ThrowHelper.ThrowIfStringArgumentNullOrEmpty(fileName, StringResourceNames.CsvFileNotValid);
ThrowHelper.ThrowIfFileNotExist(fileName, StringResourceNames.CsvFileNotFound);
CultureInfo oldcur = Thread.CurrentThread.CurrentCulture;
try
{
Thread.CurrentThread.CurrentCulture = CsvReaderUtilities.DetermineCultureForFile(fileName);
using (var csv = new CsvReader(new StreamReader(fileName), true, ';'))
{
string[] headers = CsvImporterHelper.GetFieldHeaders(this, csv);
if (headers.Count() < 2)
{
string csvHeaderError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderError");
throw new CsvImporterSegmentsException($"{fileName} : {csvHeaderError}");
}
const string fieldLocationId = CsvColumnNames.LocationColumnName;
int colIndexLocationId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldLocationId);
CheckColumn(colIndexLocationId, fileName, fieldLocationId);
const string fieldLocationScenarioId = CsvColumnNames.LocationScenarioColumnName;
int colIndexLocationScenarioId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldLocationScenarioId);
CheckColumn(colIndexLocationScenarioId, fileName, fieldLocationScenarioId);
int colIndexRiverLevel = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.RiverLevelColumnName);
int colIndexRiverLevelLow = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.RiverLevelLowColumnName);
int colIndexDikeTableHeight = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.DikeTableHeightColumnName);
int colIndexSafetyFactorPiping = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.SafetyFactorPiping);
int colIndexSafetyFactorStabilityInnerSlope = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.SafetyFactorStabilityInnerslope);
int colIndexSafetyFactorStabilityOuterSlope = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.SafetyFactorStabilityOuterslope);
int colIndexUpliftCriterionPiping = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.UpliftCriterionPiping);
int colIndexUpliftCriterionStability = CsvReaderUtilities.GetHeaderIndexByString(headers, ModelFactorCsvIdentifiers.UpliftCriterionStability);
int colIndexPlLineOffsetBelowDikeTopAtRiver = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PlLineOffsetBelowDikeTopAtRiverColumnName);
int colIndexPlLineOffsetBelowDikeTopAtPolder = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PlLineOffsetBelowDikeTopAtPolderColumnName);
int colIndexPlLineOffsetBelowShoulderBaseInside = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PlLineOffsetBelowShoulderBaseInsideColumnName);
int colIndexPlLineOffsetBelowDikeToeAtPolder = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PlLineOffsetBelowDikeToeAtPolderColumnName);
int colIndexPlLineOffsetBelowDikeCrestMiddle = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PlLineOffsetBelowDikeCrestMiddleColumnName);
int colIndexPlLineOffsetFactorBelowShoulderCrest = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PlLineOffsetFactorBelowShoulderCrestColumnName);
int colIndexUsePlLineOffsetBelowDikeCrestMiddle = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.UsePlLineOffsetBelowDikeCrestMiddleColumnName);
int colIndexUsePlLineOffsetFactorBelowShoulderCrest = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.UsePlLineOffsetFactorBelowShoulderCrestColumnName);
int colIndexHeadPl3 = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.HeadPl3ColumnName);
int colIndexHeadPl4 = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.HeadPl4ColumnName);
int colIndexHeadPl3OldFormat = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.HeadPl3OldFormatColumnName);
int colIndexHeadPl4OldFormat = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.HeadPl4OldFormatColumnName);
int colIndexPolderLevel = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.PolderLevelColumnName);
int colIndexHeadPl2 = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.HeadPl2ColumnName);
var index = 1;
while (csv.ReadNextRecord())
{
var 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;
if (colIndexRiverLevel > -1)
{
colIndex = colIndexRiverLevel;
scenario.RiverLevel = Convert.ToDouble(csv[colIndexRiverLevel]);
}
if (colIndexRiverLevelLow > -1)
{
colIndex = colIndexRiverLevelLow;
scenario.RiverLevelLow = Convert.ToDouble(csv[colIndex]);
}
if (colIndexDikeTableHeight > -1)
{
colIndex = colIndexDikeTableHeight;
scenario.DikeTableHeight = Convert.ToDouble(csv[colIndexDikeTableHeight]);
}
if (colIndexSafetyFactorPiping > -1)
{
colIndex = colIndexSafetyFactorPiping;
scenario.RequiredSafetyFactorPiping = Convert.ToDouble(csv[colIndex]);
}
if (colIndexSafetyFactorStabilityInnerSlope > -1)
{
colIndex = colIndexSafetyFactorStabilityInnerSlope;
scenario.RequiredSafetyFactorStabilityInnerSlope = Convert.ToDouble(csv[colIndex]);
}
if (colIndexSafetyFactorStabilityOuterSlope > -1)
{
colIndex = colIndexSafetyFactorStabilityOuterSlope;
scenario.RequiredSafetyFactorStabilityOuterSlope = 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]);
}
else
{
if (colIndexHeadPl3OldFormat > -1)
{
colIndex = colIndexHeadPl3OldFormat;
scenario.HeadPl3 = Convert.ToDouble(csv[colIndex]);
}
}
if (colIndexHeadPl4 > -1)
{
colIndex = colIndexHeadPl4;
scenario.HeadPl4 = Convert.ToDouble(csv[colIndex]);
}
else
{
if (colIndexHeadPl4OldFormat > -1)
{
colIndex = colIndexHeadPl4OldFormat;
scenario.HeadPl4 = Convert.ToDouble(csv[colIndex]);
}
}
if (colIndexPolderLevel > -1)
{
colIndex = colIndexPolderLevel;
scenario.PolderLevel = Convert.ToDouble(csv[colIndex]);
}
if (colIndexHeadPl2 > -1)
{
colIndex = colIndexHeadPl2;
scenario.HeadPl2 = Convert.ToDouble(csv[colIndex]);
}
ImportedItems.Add(scenario);
}
catch (Exception e)
{
string csvScenarioError = String.Format(LocalizationManager.GetTranslatedText(GetType(), "csvScenarioError"), scenario.LocationId, colIndex + 1);
ErrorMessages.Add(csvScenarioError + e.Message);
}
}
}
}
finally
{
Thread.CurrentThread.CurrentCulture = oldcur;
}
}
public List ImportedItems { get; } = new List();
public List ErrorMessages { get; set; } = new List();
private void CheckColumn(int index, string fileName, string fieldName)
{
if (index < 0)
{
string csvHeaderFieldError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderFieldError");
throw new CsvImporterScenariosException($"{fileName} : {csvHeaderFieldError} {fieldName}");
}
}
public class ScenarioRecord
{
public ScenarioRecord()
{
LocationScenarioId = null;
}
public int ScenarioRecordId { get; set; }
public string LocationId { get; set; } //required
public string LocationScenarioId { get; set; } //required
public double? RiverLevel { get; set; } //optional
public double? RiverLevelLow { get; set; } //optional
public double? DikeTableHeight { get; set; } //optional
public double? PlLineOffsetBelowDikeTopAtRiver { get; set; } //optional
public double? PlLineOffsetBelowDikeTopAtPolder { get; set; } //optional
public double? PlLineOffsetBelowShoulderBaseInside { get; set; } //optional
public double? PlLineOffsetBelowDikeToeAtPolder { get; set; } //optional
public double? PlLineOffsetBelowDikeCrestMiddle { get; set; } //optional
public double? PlLineOffsetFactorBelowShoulderCrest { get; set; } //optional
public bool? UsePlLineOffsetBelowDikeCrestMiddle { get; set; } //optional
public bool? UsePlLineOffsetFactorBelowShoulderCrest { get; set; } //optional
public double? HeadPl3 { get; set; } //optional
public double? HeadPl4 { get; set; } //optional
public double? PolderLevel { get; set; } // optional
public double? HeadPl2 { get; set; } // Optional
public double? UpliftCriterionPiping { get; set; } //optional
public double? UpliftCriterionStability { get; set; } //optional
public double? RequiredSafetyFactorPiping { get; set; } //optional
public double? RequiredSafetyFactorStabilityInnerSlope { get; set; } //optional
public double? RequiredSafetyFactorStabilityOuterSlope { get; set; } //optional
}
}