// 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.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 CsvImporterSoilProfilesException : Exception
{
public CsvImporterSoilProfilesException(string message)
: base(message) {}
}
public class CsvImporterSoilProfiles
{
public CsvImporterSoilProfiles(string fileName)
{
ErrorMessages.Clear();
ThrowHelper.ThrowIfStringArgumentNullOrEmpty(fileName, StringResourceNames.CsvFileNotValid);
ThrowHelper.ThrowIfFileNotExist(fileName, StringResourceNames.CsvFileNotFound);
CultureInfo oldCultureInfo = 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);
// The definition of the csv contains one extra column (soiltype) which is now obsolete.
// So number of columns may be 4 or 5.
if ((headers.Length < 4) || (headers.Length > 5))
{
string csvHeaderError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderError");
throw new CsvImporterSoilProfilesException($"{fileName} : {csvHeaderError}");
}
const string fieldsoilprofile_id = "soilprofile_id";
int colIndexSoilProfileId = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldsoilprofile_id);
CheckColumn(colIndexSoilProfileId, fileName, fieldsoilprofile_id);
const string fieldtop_level = "top_level";
int colIndexTopLevel = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldtop_level);
CheckColumn(colIndexTopLevel, fileName, fieldtop_level);
const string fieldsoil_name = "soil_name";
int colIndexSoilName = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldsoil_name);
CheckColumn(colIndexSoilName, fileName, fieldsoil_name);
int colIndexIsAquifer = CsvReaderUtilities.GetHeaderIndexByString(headers, CsvColumnNames.IsAquiferColumnName);
CheckColumn(colIndexIsAquifer, fileName, CsvColumnNames.IsAquiferColumnName);
var index = 1;
while (csv.ReadNextRecord())
{
var soilProfile = new SoilProfileRecord();
int colIndex = -1; // Keep track of column for error message
try
{
soilProfile.SoilProfileRecordId = index;
index++;
// Required columns
colIndex = colIndexSoilProfileId;
soilProfile.SoilProfileId = csv[colIndexSoilProfileId];
colIndex = colIndexTopLevel;
soilProfile.TopLevel = Convert.ToDouble(csv[colIndexTopLevel]);
colIndex = colIndexSoilName;
soilProfile.SoilName = csv[colIndexSoilName];
colIndex = colIndexIsAquifer;
soilProfile.IsAquifer = Convert.ToBoolean(csv[colIndex]);
ImportedItems.Add(soilProfile);
}
catch (Exception e)
{
string csvSoilProfileError = string.Format(LocalizationManager.GetTranslatedText(GetType(), "csvSoilProfileError"), soilProfile.SoilProfileId, colIndex + 1);
ErrorMessages.Add($"Record {index} : {csvSoilProfileError + e.Message}");
}
}
}
}
finally
{
Thread.CurrentThread.CurrentCulture = oldCultureInfo;
}
}
public List ImportedItems { get; } = new();
public List ErrorMessages { get; set; } = new();
private void CheckColumn(int index, string fileName, string fieldName)
{
if (index < 0)
{
string csvHeaderFieldError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderFieldError");
throw new CsvImporterSoilProfilesException($"{fileName} : {csvHeaderFieldError} {fieldName}");
}
}
public class SoilProfileRecord
{
public int SoilProfileRecordId { get; set; }
public string SoilProfileId { get; set; }
public double TopLevel { get; set; }
public string SoilName { get; set; }
public bool IsAquifer { get; set; }
}
}