// Copyright (C) Stichting Deltares 2017. 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.Text;
using System.Threading;
using Deltares.Standard.IO;
using Deltares.Standard.Language;
using LumenWorks.Framework.IO.Csv;
namespace Deltares.Dam.Data.CsvImporters
{
public class CsvImporterSoilProfilesException : ApplicationException
{
public CsvImporterSoilProfilesException(string message)
: base(message)
{
}
}
public class CsvImporterSoilProfiles
{
private List soilProfileRecords = new List();
private List errorMessages = new List();
public class SoilProfileRecord
{
public int SoilProfileRecordId { get; set; }
public string SoilProfileId { get; set; }
public double TopLevel { get; set; }
public string SoilName { get; set; }
}
private void CheckColumn(int index, string fileName, string fieldName)
{
if (index < 0)
{
var csvHeaderFieldError = LocalizationManager.GetTranslatedText(this.GetType(), "csvHeaderFieldError");
throw new CsvImporterSoilProfilesException(String.Format("{0} : {1} {2}", fileName, csvHeaderFieldError,
fieldName));
}
}
public CsvImporterSoilProfiles(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);
// The definition of the csv contains one extra column (soiltype) which is now obsolete.
// So number of columns may be 3 or 4.
if ((headers.Count() < 3) || (headers.Count() > 4))
{
var csvHeaderError = LocalizationManager.GetTranslatedText(this.GetType(), "csvHeaderError");
throw new CsvImporterSoilProfilesException(String.Format("{0} : {1}", 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);
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++;
colIndex = colIndexSoilProfileId;
soilProfile.SoilProfileId = csv[colIndexSoilProfileId];
colIndex = colIndexTopLevel;
soilProfile.TopLevel = Convert.ToDouble(csv[colIndexTopLevel]);
colIndex = colIndexSoilName;
soilProfile.SoilName = csv[colIndexSoilName];
soilProfileRecords.Add(soilProfile);
}
catch (Exception e)
{
var csvSoilProfileError = String.Format(LocalizationManager.GetTranslatedText(this.GetType(), "csvSoilProfileError"), soilProfile.SoilProfileId, colIndex + 1);
errorMessages.Add(String.Format("Record {0} : {1}", index, csvSoilProfileError + e.Message));
}
}
}
}
finally
{
Thread.CurrentThread.CurrentCulture = oldcur;
}
}
public List ImportedItems
{
get { return soilProfileRecords; }
}
public List ErrorMessages
{
get { return errorMessages; }
set { errorMessages = value; }
}
}
}