// Copyright (C) Stichting Deltares 2018. 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.Linq;
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 XmlStabilityElementName = "Stability";
public const string XmlStabilityOutsideElementName = "StabilityOutside";
public const string XmlPipingWtiElementName = "PipingWti";
public const string XmlPipingBlighElementName = "PipingBligh";
public const string XmlPipingSellmeijerElementName = "PipingSellmeijer";
public const string XmlPipingSellmeijerProbabilisticElementName = "PipingSellmeijerProbabilistic";
public const string XmlPipingIJkdijkElementName = "PipingIJkdijk";
public const string XmlOvertoppingElementName = "Overtopping";
public const string XmlMStabParametersElementName = "MStabParameters";
public const string XmlMStabIsCalculateAllStabilityProjectsAtOnceElementName = "IsCalculateAllStabilityProjectsAtOnce";
public const string XmlMStabCalculationModelElementName = "CalculationModel";
public const string XmlMStabShearStrengthElementName = "ShearStrength";
public const string XmlMStabProbabilisticElementName = "Probabilistic";
public const string XmlMStabSearchMethodElementName = "SearchMethod";
public const string XmlMStabUseZonesElementName = "UseZones";
public const string XmlMStabIsOverrulePLLineCreationMethodName = "IsOverrulePLLineCreationMethod";
public const string XmlMStabPLLineCreationMethodname = "PLLineCreationMethod";
///
/// 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.XmlStabilityElementName);
if (element != null)
calculationModules.Stability = 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;
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlPipingSellmeijerProbabilisticElementName);
if (element != null)
calculationModules.PipingSellmeijerProbabilistic = int.Parse(element.Value) != 0;
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlPipingIJkdijkElementName);
if (element != null)
calculationModules.PipingIJkdijk = int.Parse(element.Value) != 0;
element = calculationModulesElement.Element(CalculationParametersAssembler.XmlOvertoppingElementName);
if (element != null)
calculationModules.Overtopping = int.Parse(element.Value) != 0;
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.XmlMStabShearStrengthElementName);
if (element != null)
mstabParameters.ShearStrength = (MStabShearStrength)
Enum.Parse(typeof (MStabShearStrength), 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.XmlMStabProbabilisticElementName);
if (element != null)
mstabParameters.IsProbabilistic = int.Parse(element.Value) != 0;
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;
}
element = mstabParametersElement.Element(
CalculationParametersAssembler.XmlMStabIsOverrulePLLineCreationMethodName);
if (element != null)
mstabParameters.IsOverrulePLLineCreationMethod = int.Parse(element.Value) != 0;
element =
mstabParametersElement.Element(CalculationParametersAssembler.XmlMStabPLLineCreationMethodname);
if (element != null)
mstabParameters.PLLineCreationMethod = (PLLineCreationMethod)Enum.Parse(typeof(PLLineCreationMethod), element.Value, true);
else
mstabParameters.PLLineCreationMethod = PLLineCreationMethod.None;
calculationParameters.MStabParameters = mstabParameters;
}
return calculationParameters;
}
#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
}
}