// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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.Collections.Generic; using System.Linq; using System.Reflection; using System.Xml.Linq; using Deltares.DamEngine.Data.Geotechnics; namespace Deltares.DamEngine.Calculators.KernelWrappers.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.DamMacroStabilityCommon.Xsd"; /// /// Holds the xml element name /// private const string xmlElementName = "Profile"; /// /// Holds the xml namespace /// private const string xmlElementNamespace = "http://deltares.nl/2008/" + xmlShemaName; /// /// The XML layer collection element name /// 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 /// /// Initializes a new instance of the class. /// public SoilProfileAssembler() : base( xmlElementName, xmlElementNamespace, Assembly.GetExecutingAssembly().GetEmbeddedFile(xsdEmbeddedResourcePath)) { } /// /// override for CreateDomainObject /// /// /// 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) { var xAttribute = layerElement.Attribute("SoilID"); if (xAttribute != null) { string soilName = xAttribute.Value; Soil soil; if (soilDictionary.ContainsKey(soilName)) { soil = soilDictionary[soilName]; } else { //soil = new S(); soil = new Soil { Name = soilName }; soilDictionary.Add(soilName, soil); } soilLayer.Soil = soil; } } soilProfile.Layers.Add(soilLayer); } return soilProfile; } /// /// Creates the data transfer object. /// /// The soil profile. /// 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; } } }