// 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.IO;
using Deltares.Standard;
using Deltares.Standard.Logging;
namespace Deltares.Dam.Data.CsvImporters
{
public class CsvImporter
{
private string folderName;
private const string FileExtension = "csv";
private const string FilePattern = "{0}\\{1}." + FileExtension;
private string locationsFileNamePart = "locations";
private string segmentsFileNamePart = "segments";
private string soilProfilesFileNamePart = "soilprofiles";
private string surfaceLinesFileNamePart = "surfacelines";
private string characteristicPointFileNamePart = "characteristicpoints";
private string ScenariosFileNamePart = "scenarios";
private List errorMessages = new List();
private List locationRecords = new List();
private List segmentRecords = new List();
private List characteristicPointsRecords = new List();
private List soilProfilesRecords = new List();
private List surfaceLinesRecords = new List();
private List scenariosRecords = new List();
private string ImportFolderLocation
{
get { return folderName != null && folderName.EndsWith("\\") ? folderName.Substring(0, folderName.Length - 1) : folderName; }
}
private string LocationsFileName
{
get { return string.Format(FilePattern, ImportFolderLocation, locationsFileNamePart); }
}
private string SegmentsFileName
{
get { return string.Format(FilePattern, ImportFolderLocation, segmentsFileNamePart); }
}
private string SoilProfilesFileName
{
get { return string.Format(FilePattern, ImportFolderLocation, soilProfilesFileNamePart); }
}
private string SurfaceLinesFileName
{
get { return string.Format(FilePattern, ImportFolderLocation, surfaceLinesFileNamePart); }
}
private string CharacteristicPointsFileName
{
get { return string.Format(FilePattern, ImportFolderLocation, characteristicPointFileNamePart); }
}
private string ScenariosFileName
{
get { return string.Format(FilePattern, ImportFolderLocation, ScenariosFileNamePart); }
}
public List ErrorMessages
{
get { return errorMessages; }
set { errorMessages = value; }
}
public List LocationRecords
{
get { return locationRecords; }
set { locationRecords = value; }
}
public List SegmentRecords
{
get { return segmentRecords; }
}
public List CharacteristicPointsRecords
{
get { return characteristicPointsRecords; }
}
public List SoilProfilesRecords
{
get { return soilProfilesRecords; }
}
public List SurfaceLinesRecords
{
get { return surfaceLinesRecords; }
}
public List ScenariosRecords
{
get { return scenariosRecords; }
}
///
/// Imports the locations.
///
/// Name of the file.
/// The DamType is not (yet) read from the csv file; we assign the specified DamType to all locations that are read
private void ImportLocations(string fileName, DamType damType)
{
// Locations are optional, so check if the locations are available before trying to import
if (File.Exists(fileName))
{
try
{
var csvImporterLocations = new CsvImporterLocations(fileName);
locationRecords = csvImporterLocations.ImportedItems;
foreach (var errorMessage in csvImporterLocations.ErrorMessages)
{
LogMessage logmessage = new LogMessage(LogMessageType.Error, this, errorMessage);
errorMessages.Add(logmessage);
}
foreach (var locationRecord in locationRecords)
{
locationRecord.DamType = damType;
}
}
catch (CsvImporterSegmentsException e)
{
LogMessage logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
}
}
///
/// Imports the segments.
///
/// Name of the file.
private void ImportSegments(string fileName)
{
try
{
CsvImporterSegments csvImporterSegments = new CsvImporterSegments(fileName);
segmentRecords = csvImporterSegments.ImportedItems;
foreach (var errorMessage in csvImporterSegments.ErrorMessages)
{
LogMessage logmessage = new LogMessage(LogMessageType.Error, this, errorMessage);
errorMessages.Add(logmessage);
}
}
catch (CsvImporterSegmentsException e)
{
LogMessage logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
}
///
/// Imports the characteristic points.
///
/// Name of the file.
private void ImportCharacteristicPoints(string fileName)
{
try
{
CsvImporterCharacteristicPoints csvImporterCharacteristicPoints = new CsvImporterCharacteristicPoints(fileName);
characteristicPointsRecords = csvImporterCharacteristicPoints.ImportedItems;
foreach (var errorMessage in csvImporterCharacteristicPoints.ErrorMessages)
{
LogMessage logmessage = new LogMessage(LogMessageType.Error, this, errorMessage);
errorMessages.Add(logmessage);
}
}
catch (CsvImporterCharacteristicPointsException e)
{
LogMessage logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
}
///
/// Imports the soil profiles.
///
/// Name of the file.
private void ImportSoilProfiles(string fileName)
{
// 1D Soilprofiles are not mandatory. The project can use 2D-geometries
if (File.Exists(fileName))
{
try
{
CsvImporterSoilProfiles csvImporterSoilProfiles = new CsvImporterSoilProfiles(fileName);
soilProfilesRecords = csvImporterSoilProfiles.ImportedItems;
foreach (var errorMessage in csvImporterSoilProfiles.ErrorMessages)
{
LogMessage logmessage = new LogMessage(LogMessageType.Error, this, errorMessage);
errorMessages.Add(logmessage);
}
}
catch (CsvImporterSoilProfilesException e)
{
LogMessage logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
}
}
///
/// Imports the surface lines.
///
/// Name of the file.
private void ImportSurfaceLines(string fileName)
{
try
{
CsvImporterSurfaceLines csvImporterSurfaceLines = new CsvImporterSurfaceLines(fileName);
surfaceLinesRecords = csvImporterSurfaceLines.ImportedItems;
foreach (var errorMessage in csvImporterSurfaceLines.ErrorMessages)
{
LogMessage logmessage = new LogMessage(LogMessageType.Error, this, errorMessage);
errorMessages.Add(logmessage);
}
}
catch (CsvImporterSurfaceLinesException e)
{
LogMessage logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
}
///
/// Imports the scenarios.
///
/// Name of the file.
private void ImportScenarios(string fileName)
{
try
{
var csvImporterScenarios = new CsvImporterScenarios(fileName);
scenariosRecords = csvImporterScenarios.ImportedItems;
foreach (var errorMessage in csvImporterScenarios.ErrorMessages)
{
var logmessage = new LogMessage(LogMessageType.Error, this, errorMessage);
errorMessages.Add(logmessage);
}
}
catch (CsvImporterScenariosException e)
{
var logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
catch(Exception e)
{
var logmessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
errorMessages.Add(logmessage);
}
}
///
/// Imports the CSV data from directory.
///
/// The CSV dir.
/// if set to true [is import only locations].
/// The progress.
/// The DamType is not (yet) read from the csv file; we assign the specified DamType to all locations that are read
/// Type of the dam project.
public void ImportCsvDataFromDirectory(string csvDir, bool isImportOnlyLocations, ProgressDelegate progress, DamType damType, DamProjectType damProjectType)
{
folderName = csvDir;
if (progress != null) progress(0.03);
ImportLocations(LocationsFileName, damType);
if (!isImportOnlyLocations)
{
if (progress != null) progress(0.08);
ImportSegments(SegmentsFileName);
if (progress != null) progress(0.12);
ImportCharacteristicPoints(CharacteristicPointsFileName);
if (progress != null) progress(0.16);
ImportSoilProfiles(SoilProfilesFileName);
if (progress != null) progress(0.2);
ImportSurfaceLines(SurfaceLinesFileName);
// Scenario file is required for design projects and optional for the rest
if (damProjectType == DamProjectType.Design)
{
if (progress != null) progress(0.25);
ImportScenarios(ScenariosFileName);
}
else
{
if (File.Exists(ScenariosFileName))
{
if (progress != null) progress(0.25);
ImportScenarios(ScenariosFileName);
}
}
if (progress != null) progress(0.30);
}
}
public override string ToString()
{
return "CsvImporter";
}
}
}