// Copyright (C) Stichting Deltares 2024. 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.Standard.IO;
using Deltares.Standard.Language;
using LumenWorks.Framework.IO.Csv;
namespace Deltares.Dam.Data.CsvImporters;
public class CsvImporterAquifersException : Exception
{
public CsvImporterAquifersException(string message)
: base(message) {}
}
public class CsvImporterAquifers
{
public CsvImporterAquifers(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);
if (headers.Length != 2)
{
string csvHeaderError = LocalizationManager.GetTranslatedText(GetType(), "csvHeaderError");
throw new CsvImporterAquifersException($"{fileName} : {csvHeaderError}");
}
const string fieldStixFilename = "stix_filename";
int colIndexStixFilename = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldStixFilename);
CheckColumn(colIndexStixFilename, fileName, fieldStixFilename);
const string fieldLayerName = "layer_name";
int colIndexLayerName = CsvReaderUtilities.GetHeaderIndexByString(headers, fieldLayerName);
CheckColumn(colIndexLayerName, fileName, fieldLayerName);
var index = 1;
while (csv.ReadNextRecord())
{
var aquiferRecord = new AquiferRecord();
int colIndex = -1; // Keep track of column for error message
try
{
aquiferRecord.AquiferRecordId = index;
index++;
// Required columns
colIndex = colIndexStixFilename;
aquiferRecord.StixFileName = csv[colIndexStixFilename];
colIndex = colIndexLayerName;
aquiferRecord.LayerName = csv[colIndexLayerName];
if (string.IsNullOrEmpty(aquiferRecord.StixFileName) || string.IsNullOrEmpty(aquiferRecord.LayerName))
{
if (string.IsNullOrEmpty(aquiferRecord.StixFileName) && string.IsNullOrEmpty(aquiferRecord.LayerName))
{
continue; // ignore empty records
}
string recordError;
if (string.IsNullOrEmpty(aquiferRecord.StixFileName))
{
recordError = string.Format(LocalizationManager.GetTranslatedText(GetType(), "noStixFileNameForAquiferRecordError"), index);
}
else
{
recordError = string.Format(LocalizationManager.GetTranslatedText(GetType(), "noLayerNameForAquiferRecordError"), index);
}
ErrorMessages.Add($"{recordError}");
}
else
{
ImportedItems.Add(aquiferRecord);
}
}
catch (Exception e)
{
string csvAquiferError = string.Format(LocalizationManager.GetTranslatedText(GetType(), "csvAquiferError"),
colIndex + 1);
ErrorMessages.Add($"Record {index}: {csvAquiferError + 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 CsvImporterAquifersException($"{fileName} : {csvHeaderFieldError}{fieldName}");
}
}
public class AquiferRecord
{
public int AquiferRecordId { get; set; }
public string StixFileName { get; set; }
public string LayerName { get; set; }
}
}