// 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 System.Reflection;
using System.Security;
using Ringtoets.HydraRing.Calculation.Data;
using Ringtoets.HydraRing.Calculation.Providers;
using Ringtoets.HydraRing.IO;
namespace Ringtoets.HydraRing.Calculation.Services
{
///
/// Service for:
///
/// -
/// generating an initialization script that is necessary for performing Hydra-Ring calculations;
///
/// -
/// providing the corresponding file paths.
///
///
///
internal class HydraRingInitializationService
{
private const string hydraRingBinariesSubDirectory = "HydraRing";
private const string iniFileExtension = ".ini";
private const string databaseFileExtension = ".sql";
private const string logFileExtension = ".log";
private readonly int mechanismId;
private readonly int sectionId;
// Working directories
private readonly string hydraRingDirectory;
private readonly string hlcdDirectory;
///
/// Creates a new instance of the class.
///
/// The failure mechanism type.
/// The section id.
/// The HLCD directory.
/// The working directory.
public HydraRingInitializationService(HydraRingFailureMechanismType failureMechanismType, int sectionId, string hlcdDirectory, string temporaryWorkingDirectory)
{
mechanismId = new FailureMechanismDefaultsProvider().GetFailureMechanismDefaults(failureMechanismType).MechanismId;
this.sectionId = sectionId;
TemporaryWorkingDirectory = temporaryWorkingDirectory;
this.hlcdDirectory = hlcdDirectory;
hydraRingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), hydraRingBinariesSubDirectory);
}
///
/// Gets the ini file path.
///
public string IniFilePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, sectionId + iniFileExtension);
}
}
///
/// Gets the database creation script file path.
///
public string DatabaseCreationScriptFilePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, sectionId + databaseFileExtension);
}
}
///
/// Gets the log file path.
///
public string LogFilePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, sectionId + logFileExtension);
}
}
///
/// Gets the output file path.
///
public string OutputFilePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, HydraRingFileConstants.DesignTablesFileName);
}
}
///
/// Gets the output database path.
///
public string OutputDatabasePath
{
get
{
return Path.Combine(TemporaryWorkingDirectory, HydraRingFileConstants.OutputDatabaseFileName);
}
}
///
/// Gets the HLCD file path.
///
public string HlcdFilePath
{
get
{
return Path.Combine(hlcdDirectory, HydraRingFileConstants.HlcdDatabaseFileName);
}
}
///
/// Gets the path of the MechanismComputation.exe file.
///
public string MechanismComputationExeFilePath
{
get
{
return Path.Combine(hydraRingDirectory, HydraRingFileConstants.HydraRingExecutableFileName);
}
}
///
/// Gets the directory in which Hydra-Ring will place temporary input and output files created during a
/// calculation.
///
public string TemporaryWorkingDirectory { get; private set; }
///
/// Generates the initialization script necessary for performing Hydra-Ring calculations.
///
/// The initialization script.
/// Thrown when an I/O error occurred while opening the file.
/// Thrown when the path can't be accessed due to missing permissions.
/// Thrown when the path can't be accessed due to missing permissions.
public void WriteInitializationScript()
{
var initializationFileContent = string.Join(Environment.NewLine,
"section = " + sectionId,
"mechanism = " + mechanismId,
"alternative = 1", // Fixed: no support for piping
"layer = 1", // Fixed: no support for revetments
"logfile = " + sectionId + logFileExtension,
"outputverbosity = basic",
"outputtofile = file",
"projectdbfilename = " + sectionId + databaseFileExtension,
"outputfilename = " + HydraRingFileConstants.DesignTablesFileName,
"configdbfilename = " + ConfigurationDatabaseFilePath,
"hydraulicdbfilename = " + HlcdFilePath);
File.WriteAllText(IniFilePath, initializationFileContent);
}
///
/// Gets the path of the configuration database file.
///
private string ConfigurationDatabaseFilePath
{
get
{
return Path.Combine(hydraRingDirectory, HydraRingFileConstants.ConfigurationDatabaseFileName);
}
}
}
}