// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets 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.IO;
using Core.Common.IO.Exceptions;
using Core.Common.Utils.Builders;
using log4net;
using Ringtoets.Common.Data;
using Ringtoets.HydraRing.Data;
using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext;
using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext;
using Ringtoets.Integration.Forms.PresentationObjects;
using Ringtoets.Integration.Plugin.Properties;
namespace Ringtoets.Integration.Plugin.FileImporters
{
///
/// Imports locations read from an Hydraulic boundary .sqlite file (SqlLite database file) to a
/// collection of in a .
///
public class HydraulicBoundaryDatabaseImporter : IDisposable
{
private readonly ILog log = LogManager.GetLogger(typeof(HydraulicBoundaryDatabaseImporter));
private string hydraulicBoundaryDatabaseFilePath;
private HydraulicBoundarySqLiteDatabaseReader hydraulicBoundaryDatabaseReader;
private HydraulicLocationConfigurationSqLiteDatabaseReader hydraulicLocationConfigurationDatabaseReader;
///
/// Validates the file and opens a connection.
///
/// The path to the file to read.
/// Thrown when:
/// - The given file at cannot be read.
/// - The file 'HLCD.sqlite' in the same folder as cannot be read.
public void ValidateAndConnectTo(string filePath)
{
hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(filePath);
hydraulicBoundaryDatabaseFilePath = filePath;
var fileDirectory = Path.GetDirectoryName(filePath);
if (fileDirectory == null)
{
throw new ArgumentNullException("filePath");
}
var hlcdFilePath = Path.Combine(fileDirectory, "hlcd.sqlite");
if (!File.Exists(hlcdFilePath))
{
var message = new FileReaderErrorMessageBuilder(filePath).Build(Resources.HydraulicBoundaryDatabaseImporter_HLCD_sqlite_Not_Found);
throw new CriticalFileReadException(message);
}
hydraulicLocationConfigurationDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath);
}
///
/// Gets the version of the database.
///
/// The database version.
public string GetHydraulicBoundaryDatabaseVersion()
{
return hydraulicBoundaryDatabaseReader.GetVersion();
}
///
/// Creates a new instance of , based upon the data read from
/// the hydraulic boundary database file, and saved into .
///
/// to set the newly
/// created .
/// True if the import was successful, false otherwise.
public bool Import(HydraulicBoundaryDatabaseContext targetItem)
{
if (hydraulicBoundaryDatabaseReader == null)
{
throw new InvalidOperationException(Resources.HydraulicBoundaryDatabaseImporter_File_not_opened);
}
var importResult = GetHydraulicBoundaryDatabase(hydraulicBoundaryDatabaseFilePath);
if (importResult == null)
{
return false;
}
AddImportedDataToModel(targetItem.Parent, importResult);
log.Info(Resources.HydraulicBoundaryDatabaseImporter_Import_All_hydraulic_locations_read);
return true;
}
public void Dispose()
{
if (hydraulicBoundaryDatabaseReader != null)
{
hydraulicBoundaryDatabaseReader.Dispose();
hydraulicBoundaryDatabaseReader = null;
}
if (hydraulicLocationConfigurationDatabaseReader != null)
{
hydraulicLocationConfigurationDatabaseReader.Dispose();
hydraulicLocationConfigurationDatabaseReader = null;
}
}
private void HandleException(Exception e)
{
var message = string.Format(Resources.HydraulicBoundaryDatabaseImporter_ErrorMessage_0_file_skipped, e.Message);
log.Error(message);
}
private HydraulicBoundaryDatabase GetHydraulicBoundaryDatabase(string path)
{
// Get region
var regionId = GetRegionId();
if (regionId == 0)
{
return null;
}
try
{
var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = path,
Version = hydraulicBoundaryDatabaseReader.GetVersion()
};
// Locations directory of HLCD location ids and HRD location ids
var locationidsDictionary = hydraulicLocationConfigurationDatabaseReader.GetLocationsIdByRegionId(regionId);
// Prepare query to fetch hrd locations
hydraulicBoundaryDatabaseReader.PrepareReadLocation();
while (hydraulicBoundaryDatabaseReader.HasNext)
{
HrdLocation hrdLocation = hydraulicBoundaryDatabaseReader.ReadLocation();
long locationId;
locationidsDictionary.TryGetValue(hrdLocation.HrdLocationId, out locationId);
var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(
locationId,
hrdLocation.Name,
hrdLocation.LocationX,
hrdLocation.LocationY);
hydraulicBoundaryDatabase.Locations.Add(hydraulicBoundaryLocation);
}
return hydraulicBoundaryDatabase;
}
catch (Exception e)
{
if (e is LineParseException || e is CriticalFileReadException)
{
HandleException(e);
return null;
}
throw;
}
}
private long GetRegionId()
{
try
{
return hydraulicBoundaryDatabaseReader.GetRegionId();
}
catch (Exception e)
{
if (e is LineParseException || e is CriticalFileReadException)
{
HandleException(e);
return 0;
}
throw;
}
}
private static void AddImportedDataToModel(AssessmentSectionBase assessmentSection, HydraulicBoundaryDatabase importedData)
{
assessmentSection.HydraulicBoundaryDatabase = importedData;
assessmentSection.NotifyObservers();
}
}
}