// Copyright (C) Stichting Deltares 2021. 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;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers
{
///
/// Default implementation for the interface
///
///
/// This base class inherits from this class just fixes
/// the dto type to XElement
///
/// The domain object type
public abstract class DtoDocumentAssembler : IDtoAssembler
where TDomainInputObject : new()
where TDomainOutputObject : new()
{
///
/// Initializes a new instance of the DtoDocumentAssembler class
///
/// Sets the element name
/// The text reader stream for reading in ther xsd file which is embedded as an resource
public DtoDocumentAssembler(string elementName, string elementNamespace, string embeddedXsdPath)
: this(elementName, elementNamespace, embeddedXsdPath, null, null)
{
}
///
/// Initializes a new instance of the DtoDocumentAssembler class
///
/// Sets the element name
/// The text reader stream for reading in ther xsd file which is embedded as an resource
public DtoDocumentAssembler(string elementName, string elementNamespace, string embeddedXsdPath,
DtoAssembler inputAssembler, DtoAssembler outputAssembler)
{
if (elementName == null)
throw new ArgumentNullException("elementName");
if (elementNamespace == null)
throw new ArgumentNullException("elementNamespace");
this.ElementName = elementName;
this.ElementNamespace = elementNamespace;
this.OutputAssembler = outputAssembler;
this.InputAssembler = inputAssembler;
//Stream xsdStream = Common.GetEmbeddedFile(Assembly.GetExecutingAssembly(), embeddedXsdPath);
//this.Schema = XDocument.Load(new StreamReader(xsdStream));
}
///
/// Gets the xml element name
///
public string ElementName { get; private set; }
///
/// Gets or sets the namespace value for the element
///
public string ElementNamespace { get; private set; }
///
/// Gets the xml schema (if any)
///
public XDocument Schema { get; private set; }
///
///
///
public DtoAssembler InputAssembler { get; set; }
///
///
///
public DtoAssembler OutputAssembler { get; set; }
#region IDtoAssembler Members
public virtual TDomainOutputObject CreateOutputObject(XDocument dtoDocument)
{
if (dtoDocument == null)
throw new ArgumentNullException("dtoDocument");
if (this.OutputAssembler != null)
{
// the document should contain the element only once!
XElement el = (from x in dtoDocument.Descendants()
where x.Name.LocalName == this.OutputAssembler.ElementName
select x).Single();
return this.OutputAssembler.CreateDomainObject(el);
}
return default(TDomainOutputObject);
}
public virtual TDomainInputObject CreateInputObject(XDocument dtoDocument)
{
if (dtoDocument == null)
throw new ArgumentNullException("dtoDocument");
if (this.InputAssembler != null)
{
// the document should contain the element only once!
XElement el = (from x in dtoDocument.Descendants()
where x.Name.LocalName == this.InputAssembler.ElementName
select x).Single();
return this.InputAssembler.CreateDomainObject(el);
}
return default(TDomainInputObject);
}
public virtual XDocument CreateDataTransferObject(TDomainInputObject domainInputObj)
{
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
// add the file identification to the document
var fileId = new FileIdentification(Assembly.GetCallingAssembly());
var fileIdAssembler = new FileIdentificationAssembler();
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace tns = this.ElementNamespace;
XNamespace tns1 = fileIdAssembler.ElementNamespace;
var root = new XElement(tns + this.ElementName,
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(XNamespace.Xmlns + "tns", tns.NamespaceName),
new XAttribute(XNamespace.Xmlns + "tns1", tns1.NamespaceName));
doc.Add(root);
doc.Root.Add(fileId.ToDto(fileIdAssembler, tns));
if (this.InputAssembler != null)
{
XNamespace tns2 = this.InputAssembler.ElementNamespace;
root.Add(new XAttribute(XNamespace.Xmlns + "tns2", tns2.NamespaceName));
doc.Root.Add(domainInputObj.ToDto(this.InputAssembler, tns));
}
return doc;
}
#endregion
public TDomainOutputObject CreateOutputObject(string fileName)
{
return this.CreateOutputObject(this.CreateDocument(fileName));
}
public TDomainInputObject CreateInputObject(string fileName)
{
return this.CreateInputObject(this.CreateDocument(fileName));
}
private XDocument CreateDocument(string fileName)
{
if (!fileName.HasValidStringValue())
throw new ArgumentNullException("fileName");
XDocument doc = XDocument.Load(fileName);
return doc;
}
}
}