// Copyright (C) Stichting Deltares 2019. All rights reserved.
//
// This file is part of the application DAM - Clients Library.
//
// 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.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Deltares.Standard;
namespace Deltares.Dam.Data.Assemblers
{
using Standard.Extensions;
///
///
///
public class CalculationParametersAssembler : DtoAssembler
{
#region Constant declarations
///
/// Holds the common part of the path to the embedded resource
///
private const string EmbeddedResourcePath = "Deltares.Dam.Data.Xsd";
public const string XmlCalculationModulesElementName = "CalculationModules";
public const string XmlStabilityInsideElementName = "StabilityInside";
public const string XmlStabilityOutsideElementName = "StabilityOutside";
public const string XmlPipingWtiElementName = "PipingWti";
public const string XmlPipingBlighElementName = "PipingBligh";
public const string XmlPipingSellmeijerElementName = "PipingSellmeijer";
// Obsolete module names
public const string XmlObsoletePipingSellmeijerProbabilisticElementName = "PipingSellmeijerProbabilistic";
public const string XmlObsoleteStabilityElementName = "Stability";
public const string XmlObsoletePipingIJkdijkElementName = "PipingIJkdijk";
public const string XmlObsoleteOvertoppingElementName = "Overtopping";
public const string XmlMStabParametersElementName = "MStabParameters";
public const string XmlMStabIsCalculateAllStabilityProjectsAtOnceElementName = "IsCalculateAllStabilityProjectsAtOnce";
public const string XmlMStabCalculationModelElementName = "CalculationModel";
public const string XmlMStabSearchMethodElementName = "SearchMethod";
public const string XmlMStabUseZonesElementName = "UseZones";
// Obsolete Mstab parameters
public const string XmlObsoleteMStabProbabilisticElementName = "Probabilistic";
public const string XmlObsoleteMStabIsOverrulePLLineCreationMethodName = "IsOverrulePLLineCreationMethod";
public const string XmlObsoleteMStabPLLineCreationMethodname = "PLLineCreationMethod";
public const string XmlObsoleteMStabShearStrengthElementName = "ShearStrength";
///
/// Holds the xml element name
///
private const string XmlElementName = "CalculationParameters";
///
/// Holds the name of the xml schema
///
private const string XmlSchemaName = "CalculationParametersDefinition";
///
/// Holds the xml namespace
///
private const string XmlElementNamespace = "http://deltares.nl/2008/" + XmlSchemaName;
///
/// Holds the xsd resource path
///
private const string XsdEmbeddedResourcePath = EmbeddedResourcePath + "." + XmlSchemaName + ".xsd";
#endregion
///
///
///
public CalculationParametersAssembler()
: base(XmlElementName, XmlElementNamespace, Assembly.GetExecutingAssembly().GetEmbeddedFile(XsdEmbeddedResourcePath))
{
}
public override CalculationParameters CreateDomainObject(XElement dtoObj)
{
CalculationParameters calculationParameters = base.CreateDomainObject(dtoObj);
// Calculation modules
XElement calculationModulesElement = dtoObj.Element(XmlCalculationModulesElementName);
if (calculationModulesElement != null)
{
CalculationModules calculationModules = new CalculationModules();
XElement element = calculationModulesElement.Element(CalculationParametersAssembler.XmlStabilityInsideElementName);
if (element != null)
{
calculationModules.StabilityInside = int.Parse(element.Value) != 0;
}
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlStabilityOutsideElementName);
if (element != null)
{
calculationModules.StabilityOutside = int.Parse(element.Value) != 0;
}
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlPipingWtiElementName);
if (element != null)
{
calculationModules.PipingWti = int.Parse(element.Value) != 0;
}
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlPipingBlighElementName);
if (element != null)
{
calculationModules.PipingBligh = int.Parse(element.Value) != 0;
}
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlPipingSellmeijerElementName);
if (element != null)
{
calculationModules.PipingSellmeijer = int.Parse(element.Value) != 0;
}
// Check for obsolete keys
ThrowIfObsoleteKeyNameFound(calculationModulesElement, XmlObsoleteStabilityElementName);
ThrowIfObsoleteKeyNameFound(calculationModulesElement, XmlObsoletePipingSellmeijerProbabilisticElementName);
ThrowIfObsoleteKeyNameFound(calculationModulesElement, XmlObsoletePipingIJkdijkElementName);
ThrowIfObsoleteKeyNameFound(calculationModulesElement, XmlObsoleteOvertoppingElementName);
calculationParameters.CalculationModules = calculationModules;
}
// MStab parameters
XElement mstabParametersElement = dtoObj.Element(XmlMStabParametersElementName);
if (mstabParametersElement != null)
{
MStabParameters mstabParameters = new MStabParameters();
XElement element;
element = mstabParametersElement.Element(CalculationParametersAssembler.XmlMStabIsCalculateAllStabilityProjectsAtOnceElementName);
if (element != null)
{
mstabParameters.IsCalculateAllStabilityProjectsAtOnce = int.Parse(element.Value) != 0;
}
element = mstabParametersElement.Element(CalculationParametersAssembler.XmlMStabCalculationModelElementName);
if (element != null)
{
mstabParameters.Model = (MStabModelType)Enum.Parse(typeof(MStabModelType), element.Value, true);
}
element = mstabParametersElement.Element(CalculationParametersAssembler.XmlMStabSearchMethodElementName);
if (element != null)
{
mstabParameters.SearchMethod = (MStabSearchMethod)Enum.Parse(typeof(MStabSearchMethod), element.Value, true);
}
element = mstabParametersElement.Element(CalculationParametersAssembler.XmlMStabUseZonesElementName);
mstabParameters.CalculationOptions = new MStabCalculationOptions();
mstabParameters.CalculationOptions.ZonesType = MStabZonesType.NoZones;
if (element != null)
{
int useZones = int.Parse(element.Value);
mstabParameters.CalculationOptions.ZonesType = (MStabZonesType)useZones;
}
// Check for obsolete keys
ThrowIfObsoleteKeyNameFound(mstabParametersElement, XmlObsoleteMStabProbabilisticElementName);
ThrowIfObsoleteKeyNameFound(mstabParametersElement, XmlObsoleteMStabIsOverrulePLLineCreationMethodName);
ThrowIfObsoleteKeyNameFound(mstabParametersElement, XmlObsoleteMStabPLLineCreationMethodname);
ThrowIfObsoleteKeyNameFound(mstabParametersElement, XmlObsoleteMStabShearStrengthElementName);
calculationParameters.MStabParameters = mstabParameters;
}
return calculationParameters;
}
private static void ThrowIfObsoleteKeyNameFound(XElement calculationModulesElement, string obsoleteElementKeyName)
{
XElement obsoleteElement = calculationModulesElement.Element(obsoleteElementKeyName);
if (obsoleteElement != null)
{
throw new DtoAssemblerException(String.Format("The specified parameter '{0}' is obsolete", obsoleteElementKeyName));
}
}
#region Schema Validator Method
public bool ValidateSchema(XDocument doc, out string message)
{
bool result = true;
string errorMessage = String.Empty;
XmlSchemaSet schemas = new XmlSchemaSet();
Stream xsdStream = Common.GetEmbeddedFile(Assembly.GetExecutingAssembly(), XsdEmbeddedResourcePath);
schemas.Add(XmlElementNamespace, XmlReader.Create(xsdStream));
doc.Validate(schemas, (o, e) => { result = false; errorMessage = e.Message; }, true);
message = errorMessage;
return result;
}
#endregion
}
}