using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.Language;
using LumenWorks.Framework.IO.Csv;
namespace Deltares.Standard.IO
{
///
/// Importer of csv file with aquo standard format.
///
/// Each row is a property. The property should be described conform aquo standard (Namespace, LocalID, Version, ObjectID, Parameter, Grootheid, Eenheid, Hoedanigheid, Object, Waardebepalingsmethode, Waardebewerkingsmethode, ResultaatNumeriekeWaarde, ResultaatAlfanumeriekeWaarde, Resultaatclassificatie)
///
public class AquoImporter : Importer
{
private const string FieldId = "Bekledinglaag.Identificatie";
private const string FieldPropertyName = "Grootheid.Code";
private const string FieldPropertyValue = "ResultaatNumeriekeWaarde";
private const string FieldPropertyStringvalue = "ResultaatAlfanumeriekeWaarde";
private const string FieldPropertyClassificationvalue = "Kwaliteitsoordeel.code"; //"Resultaatclassificatie"
private const string IsTrueValue = "ja";
private static readonly List errorMessages = new List();
///
/// Error messages during import
///
public static IList ErrorMessages
{
get
{
return errorMessages;
}
}
///
/// Import the data from the given file. The factory creates and contains the objects
///
public override void Import()
{
Read(FileName, Factory);
}
private static void CheckColumn(int index, string fileName, string fieldName)
{
if (index < 0)
{
string csvHeaderFieldError = LocalizationManager.GetTranslatedText(typeof(AquoImporter), "csvHeaderFieldError");
throw new Exception(String.Format("{0} : {1} {2}", fileName, csvHeaderFieldError, fieldName));
}
}
///
/// Reads the specified file.
///
/// Name of the file.
/// The factory.
private void Read(string fileName, IDataFactory factory)
{
if (factory == null)
{
throw new FormatException("Method Read(). Factory cannot be null.");
}
if (FieldDefinition == null)
{
throw new FormatException("Method Read(). FieldDefinition cannot be null.");
}
if (Type == null)
{
throw new FormatException("Method Read(). Type cannot be null.");
}
CultureInfo oldcur = Thread.CurrentThread.CurrentCulture;
DataEventPublisher.InvokeWithoutPublishingEvents(() =>
{
var rowIndex = 0;
try
{
Thread.CurrentThread.CurrentCulture = CsvReaderUtilities.DetermineCultureForFile(fileName);
using (var csv = new CsvReader(new StreamReader(fileName), true, ';'))
{
string[] headers = csv.GetFieldHeaders();
int colIndexId = CsvReaderUtilities.GetHeaderIndexByString(headers, FieldId);
CheckColumn(colIndexId, fileName, FieldId);
int colIndexPropertyName = CsvReaderUtilities.GetHeaderIndexByString(headers, FieldPropertyName);
CheckColumn(colIndexPropertyName, fileName, FieldPropertyName);
int colIndexPropertyValue = CsvReaderUtilities.GetHeaderIndexByString(headers, FieldPropertyValue);
CheckColumn(colIndexPropertyValue, fileName, FieldPropertyValue);
int colIndexPropertyStringValue = CsvReaderUtilities.GetHeaderIndexByString(headers, FieldPropertyStringvalue);
CheckColumn(colIndexPropertyValue, fileName, FieldPropertyStringvalue);
int colIndexPropertyJudgementValue = CsvReaderUtilities.GetHeaderIndexByString(headers, FieldPropertyClassificationvalue);
CheckColumn(colIndexPropertyValue, fileName, FieldPropertyClassificationvalue);
while (csv.ReadNextRecord())
{
rowIndex++;
try
{
var asphaltZone = factory.GetObject(csv[colIndexId], Type, null);
if (asphaltZone != null)
{
foreach (var definition in FieldDefinition.Definitions.Cast())
{
if (definition.Name == csv[colIndexPropertyName])
{
if (!string.IsNullOrWhiteSpace(csv[colIndexPropertyJudgementValue]))
{
SetField(definition, asphaltZone, csv[colIndexPropertyJudgementValue] == IsTrueValue);
}
else if (!string.IsNullOrWhiteSpace(csv[colIndexPropertyStringValue]))
{
SetField(definition, asphaltZone, csv[colIndexPropertyStringValue]);
}
else
{
var value = definition.GetBaseValue(Convert.ToDouble(csv[colIndexPropertyValue]));
SetField(definition, asphaltZone, value);
}
break;
}
}
}
}
catch (Exception e)
{
string csvAquoPropertiesError = LocalizationManager.GetTranslatedText(this, "csvAquoPropertiesError");
errorMessages.Add(string.Format(csvAquoPropertiesError, fileName, rowIndex) + " (" + e.Message + ")");
}
}
}
}
finally
{
Thread.CurrentThread.CurrentCulture = oldcur;
}
});
}
}
}