// Copyright (C) Stichting Deltares 2025. 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.Standard.IO;
using Deltares.Standard.Language;
using LumenWorks.Framework.IO.Csv;
namespace Deltares.Dam.Data.CsvImporters;
public class CsvImporterSegmentsException : Exception
{
public CsvImporterSegmentsException(string message)
: base(message) {}
}
public class CsvImporterSegments
{
public CsvImporterSegments(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() != 4)
{
string csvHeaderError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderError");
throw new CsvImporterSegmentsException($"{fileName} : {csvHeaderError}");
}
const string fieldsement_id = "segment_id";
int colIndexSegmentId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldsement_id);
CheckColumn(colIndexSegmentId, fileName, fieldsement_id);
const string fieldsoilprofile_id = "soilprofile_id";
int colIndexSoilProfileId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldsoilprofile_id);
const string fieldsoilgeometry2D_name = "soilgeometry2D_name";
int colIndexGeometry2dName = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldsoilgeometry2D_name);
// soil profile and geometry2D are in bothe optional but at least one has to be there.
if ((colIndexSoilProfileId < 0) && (colIndexGeometry2dName < 0))
{
string csvHeaderFieldError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderFieldError");
throw new CsvImporterSoilProfilesException($"{fileName} : {csvHeaderFieldError} {fieldsoilprofile_id} / {fieldsoilgeometry2D_name}");
}
const string fieldprobability = "probability";
int colIndexProbability = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldprobability);
CheckColumn(colIndexProbability, fileName, fieldprobability);
const string fieldcalculation_type = "calculation_type";
int colIndexCalculationType = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldcalculation_type);
CheckColumn(colIndexCalculationType, fileName, fieldcalculation_type);
var index = 1;
while (csv.ReadNextRecord())
{
var segment = new SegmentRecord();
int colIndex = -1; // Keep track of column for error message
try
{
segment.SegmentId = csv[colIndexSegmentId];
segment.SegmentRecordId = index;
index++;
if (colIndexSoilProfileId > -1)
{
colIndex = colIndexSoilProfileId;
segment.SoilProfileId = csv[colIndexSoilProfileId];
}
if (colIndexGeometry2dName > -1)
{
colIndex = colIndexGeometry2dName;
segment.SoilGeometry2DName = csv[colIndexGeometry2dName];
}
segment.Probability = Convert.ToDouble(csv[colIndexProbability]);
string typeAsString = csv[colIndexCalculationType];
if (typeAsString == "Stability")
{
segment.SegmentFailureMechanismType = FailureMechanismSystemType.StabilityInside;
}
else if (typeAsString == "Piping")
{
segment.SegmentFailureMechanismType = FailureMechanismSystemType.Piping;
}
else
{
string csvSegmentCalculationTypeError = LocalizationManager.GetTranslatedText(GetType(), "csvSegmentCalculationTypeError");
ErrorMessages.Add($"{fileName} : {segment.SegmentId} {csvSegmentCalculationTypeError}");
}
ImportedItems.Add(segment);
}
catch (Exception e)
{
string csvSegmentError = String.Format(LocalizationManager.GetTranslatedText(GetType(), "csvSegmentError"), segment.SegmentId, colIndex + 1);
ErrorMessages.Add(csvSegmentError + 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 CsvImporterSegmentsException($"{fileName} : {csvHeaderFieldError} {fieldName}");
}
}
public class SegmentRecord
{
public int SegmentRecordId { get; set; }
public string SegmentId { get; set; }
public string SoilProfileId { get; set; }
public double Probability { get; set; }
public FailureMechanismSystemType SegmentFailureMechanismType { get; set; }
public string SoilGeometry2DName { get; set; }
}
}