// Copyright (C) Stichting Deltares 2017. 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.Collections.Generic;
using System.Linq;
using Core.Common.Base.IO;
using Core.Common.IO.Exceptions;
using log4net;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.IO.Properties;
namespace Ringtoets.Common.IO.Configurations.Export
{
///
/// Base class for exporting a calculation configuration and storing it as an XML file.
///
/// The
/// to use for exporting .
/// The type to export.
/// The type used to convert
/// into before writing to XML using a .
///
public abstract class SchemaCalculationConfigurationExporter : IFileExporter
where TWriter : SchemaCalculationConfigurationWriter
where TCalculation : class, ICalculation
where TConfiguration : class, IConfigurationItem
{
private static readonly ILog log = LogManager.GetLogger(typeof(SchemaCalculationConfigurationExporter));
private readonly IEnumerable configuration;
private readonly TWriter writer;
///
/// Creates a new instance of .
///
/// The hierarchy of calculations to export.
/// The path of the XML file to export to.
/// Thrown when is null.
/// Thrown when is invalid or when any element
/// of is null.
protected SchemaCalculationConfigurationExporter(IEnumerable calculations, string filePath)
{
if (calculations == null)
{
throw new ArgumentNullException(nameof(calculations));
}
writer = CreateWriter(filePath);
configuration = ToConfiguration(calculations).ToArray();
}
public bool Export()
{
try
{
writer.Write(configuration);
}
catch (CriticalFileWriteException e)
{
log.ErrorFormat(Resources.CalculationConfigurationExporter_Export_ExceptionMessage_0_no_configuration_exported, e.Message);
return false;
}
return true;
}
///
/// Creates the writer that will be used in the .
///
/// The path of the file to export to.
/// A new .
protected abstract TWriter CreateWriter(string filePath);
///
/// Converts the to a .
///
/// The to convert.
/// A new instance of with values set equal
/// to properties of .
protected abstract TConfiguration ToConfiguration(TCalculation calculation);
///
/// Converts a sequence of into a sequence of .
///
/// The sequence to be converted into a sequence of
/// .
/// The converted of .
/// Thrown when an element of
/// isn't a nor a .
private IEnumerable ToConfiguration(IEnumerable calculations)
{
foreach (ICalculationBase child in calculations)
{
var innerGroup = child as CalculationGroup;
if (innerGroup != null)
{
yield return ToConfiguration(innerGroup);
}
var calculation = child as TCalculation;
if (calculation != null)
{
yield return ToConfiguration(calculation);
}
if (innerGroup == null && calculation == null)
{
throw new ArgumentException($"Cannot export calculation of type '{child.GetType()}' using this exporter.");
}
}
}
private CalculationGroupConfiguration ToConfiguration(CalculationGroup group)
{
return new CalculationGroupConfiguration(group.Name, ToConfiguration(group.Children).ToArray());
}
}
}