using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Deltares.DamEngine.Data.Geotechnics;
namespace Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStability.Assemblers
{
///
///
///
public class SoilProfileAssembler : DtoAssembler
{
#region Constant declarations
///
/// Holds the common part of the path to the embedded resource
///
private const string EmbeddedResourcePath = "Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStability.Xsd";
///
/// Holds the xml element name
///
private const string XmlElementName = "Profile";
///
/// Holds the xml namespace
///
private const string XmlElementNamespace = "http://deltares.nl/2008/" + XmlShemaName;
public const string XmlLayerCollectionElementName = "Layers";
///
/// Holds the name of the xml schema
///
private const string XmlShemaName = "ProfileDefinition";
///
/// Holds the xsd resource path
///
private const string XsdEmbeddedResourcePath = EmbeddedResourcePath + "." + XmlShemaName + ".xsd";
#endregion
///
///
///
public SoilProfileAssembler()
: base(
XmlElementName, XmlElementNamespace,
Assembly.GetExecutingAssembly().GetEmbeddedFile(XsdEmbeddedResourcePath))
{
// ToDo zant_xml no X and Y property anymore?
// this.AddOrUpdateMapping("X", "XCoordinate");
// this.AddOrUpdateMapping("Y", "YCoordinate");
}
public override SoilProfile1D CreateDomainObject(XElement dtoObj)
{
SoilProfile1D soilProfile = base.CreateDomainObject(dtoObj);
// Layers
var soilLayerAssembler = new SoilLayerAssembler();
XElement layerCollectionElement = (from element in dtoObj.Descendants()
where element.Name.LocalName == XmlLayerCollectionElementName
select element).Single();
IEnumerable layerElementCollection = from element in layerCollectionElement.Descendants()
where element.Name.LocalName == soilLayerAssembler.ElementName
select element;
var soilDictionary = new Dictionary();
foreach (XElement layerElement in layerElementCollection)
{
SoilLayer1D soilLayer = soilLayerAssembler.CreateDomainObject(layerElement);
// Add soils
// NOTE: this solution is temporary. In future there will probably a collection of soils to pick from (see XML)
if (layerElement != null)
{
string soilName = layerElement.Attribute("SoilID").Value;
Soil soil;
if (soilDictionary.ContainsKey(soilName))
{
soil = soilDictionary[soilName];
}
else
{
//soil = new S();
soil = new Soil();
soil.Name = soilName;
soilDictionary.Add(soilName, soil);
}
soilLayer.Soil = soil;
}
soilProfile.Layers.Add(soilLayer);
}
return soilProfile;
}
public override XElement CreateDataTransferObject(SoilProfile1D soilProfile)
{
XElement el = base.CreateDataTransferObject(soilProfile);
// Layers
var soilLayerAssembler = new SoilLayerAssembler();
var layerCollectionElement = new XElement(XmlLayerCollectionElementName);
el.Add(layerCollectionElement);
foreach (SoilLayer1D soilLayer in soilProfile.Layers)
{
XElement layerElement = soilLayerAssembler.CreateDataTransferObject(soilLayer);
layerCollectionElement.Add(layerElement);
}
return el;
}
}
}