Zugferd: prepare loading different specs per document schema version
This commit is contained in:
@@ -12,6 +12,13 @@ Public Class ZUGFeRDInterface
|
||||
Private ReadOnly _logger As Logger
|
||||
Private ReadOnly _Options As ZugferdOptions
|
||||
|
||||
' These constants define the specification markers for the different
|
||||
' zugferd document schema versions. These markers need to be used to
|
||||
' define the property map in the database (column SPECIFICATION).
|
||||
Public Const ZUGFERD_SPEC_DEFAULT = "DEFAULT"
|
||||
Public Const ZUGFERD_SPEC_10 = "ZUGFERD_10"
|
||||
Public Const ZUGFERD_SPEC_2x = "ZUGFERD_2x"
|
||||
|
||||
Private ReadOnly ValidFilenames As New List(Of String) From {
|
||||
PDFEmbeds.ZUGFERD_XML_FILENAME.ToUpper,
|
||||
PDFEmbeds.FACTUR_X_XML_FILENAME_DE.ToUpper,
|
||||
@@ -39,6 +46,13 @@ Public Class ZUGFeRDInterface
|
||||
Public Property AllowZugferd_2_x_Schema As Boolean = True
|
||||
End Class
|
||||
|
||||
Public Class ZugferdResult
|
||||
Public Property DataFileName As String
|
||||
Public Property XPathObject As XPathDocument
|
||||
Public Property SchemaObject As Object
|
||||
Public Property Specification As String
|
||||
End Class
|
||||
|
||||
''' <summary>
|
||||
''' Create a new instance of ZUGFeRDInterface
|
||||
''' </summary>
|
||||
@@ -91,14 +105,14 @@ Public Class ZUGFeRDInterface
|
||||
''' </summary>
|
||||
''' <param name="Path"></param>
|
||||
''' <exception cref="ZUGFeRDExecption"></exception>
|
||||
Public Function ExtractZUGFeRDFileWithGDPicture(Path As String) As Tuple(Of String, Object)
|
||||
Dim oXmlDocument = ValidateZUGFeRDFileWithGDPicture(Path)
|
||||
Public Function ExtractZUGFeRDFileWithGDPicture(Path As String) As ZugferdResult
|
||||
Dim oResult = ValidateZUGFeRDFileWithGDPicture(Path)
|
||||
|
||||
If IsNothing(oXmlDocument.Item2) Then
|
||||
If IsNothing(oResult.SchemaObject) Then
|
||||
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
|
||||
End If
|
||||
|
||||
Return SerializeZUGFeRDDocument(oXmlDocument)
|
||||
Return SerializeZUGFeRDDocument(oResult)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
@@ -106,14 +120,14 @@ Public Class ZUGFeRDInterface
|
||||
''' </summary>
|
||||
''' <param name="Stream"></param>
|
||||
''' <exception cref="ZUGFeRDExecption"></exception>
|
||||
Public Function ExtractZUGFeRDFileWithGDPicture(Stream As Stream) As Tuple(Of String, Object)
|
||||
Dim oXmlDocument = ValidateZUGFeRDFileWithGDPicture(Stream)
|
||||
Public Function ExtractZUGFeRDFileWithGDPicture(Stream As Stream) As ZugferdResult
|
||||
Dim oResult = ValidateZUGFeRDFileWithGDPicture(Stream)
|
||||
|
||||
If IsNothing(oXmlDocument.Item2) Then
|
||||
If IsNothing(oResult.SchemaObject) Then
|
||||
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei.")
|
||||
End If
|
||||
|
||||
Return SerializeZUGFeRDDocument(oXmlDocument)
|
||||
Return SerializeZUGFeRDDocument(oResult)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
@@ -122,7 +136,7 @@ Public Class ZUGFeRDInterface
|
||||
''' <param name="pStream"></param>
|
||||
''' <exception cref="ZUGFeRDExecption"></exception>
|
||||
''' <returns>The embedded xml data as an XPath document</returns>
|
||||
Public Function ValidateZUGFeRDFileWithGDPicture(pStream As Stream) As Tuple(Of String, XPathDocument)
|
||||
Public Function ValidateZUGFeRDFileWithGDPicture(pStream As Stream) As ZugferdResult
|
||||
Dim oEmbedExtractor = New PDFEmbeds(_logConfig)
|
||||
|
||||
Try
|
||||
@@ -150,7 +164,7 @@ Public Class ZUGFeRDInterface
|
||||
''' <param name="pPath"></param>
|
||||
''' <exception cref="ZUGFeRDExecption"></exception>
|
||||
''' <returns>The embedded xml data as an XPath document</returns>
|
||||
Public Function ValidateZUGFeRDFileWithGDPicture(pPath As String) As Tuple(Of String, XPathDocument)
|
||||
Public Function ValidateZUGFeRDFileWithGDPicture(pPath As String) As ZugferdResult
|
||||
Dim oEmbedExtractor = New PDFEmbeds(_logConfig)
|
||||
|
||||
Try
|
||||
@@ -172,7 +186,7 @@ Public Class ZUGFeRDInterface
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function HandleEmbeddedFiles(pResults As List(Of PDFEmbeds.EmbeddedFile)) As Tuple(Of String, XPathDocument)
|
||||
Private Function HandleEmbeddedFiles(pResults As List(Of PDFEmbeds.EmbeddedFile)) As ZugferdResult
|
||||
If pResults Is Nothing Then
|
||||
Throw New ZUGFeRDExecption(ErrorType.NoZugferd, "Datei ist keine ZUGFeRD Datei, weil die Attachments nicht gelesen werden konnten.")
|
||||
End If
|
||||
@@ -202,7 +216,10 @@ Public Class ZUGFeRDInterface
|
||||
|
||||
Try
|
||||
Using oStream As New MemoryStream(oAllowedResult.FileContents)
|
||||
Return New Tuple(Of String, XPathDocument)(oAllowedResult.FileName, New XPathDocument(oStream))
|
||||
Return New ZugferdResult With {
|
||||
.DataFileName = oAllowedResult.FileName,
|
||||
.XPathObject = New XPathDocument(oStream)
|
||||
}
|
||||
End Using
|
||||
|
||||
Catch ex As ZUGFeRDExecption
|
||||
@@ -216,37 +233,41 @@ Public Class ZUGFeRDInterface
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function SerializeZUGFeRDDocument(pDocument As Tuple(Of String, XPathDocument)) As Tuple(Of String, Object)
|
||||
Public Function SerializeZUGFeRDDocument(pResult As ZugferdResult) As ZugferdResult
|
||||
Try
|
||||
Dim oNavigator As XPathNavigator = pDocument.Item2.CreateNavigator()
|
||||
Dim oNavigator As XPathNavigator = pResult.XPathObject.CreateNavigator()
|
||||
Dim oReader As XmlReader
|
||||
Dim oObject As Object = Nothing
|
||||
|
||||
Dim oAllowedTypes As New List(Of Type)
|
||||
Dim oObject As Object = Nothing
|
||||
Dim oSpecification As String = Nothing
|
||||
|
||||
Dim oAllowedTypes As New Dictionary(Of String, Type)
|
||||
|
||||
If _Options.AllowZugferd_1_0_Schema Then
|
||||
oAllowedTypes.Add(GetType(ZUGFeRD.Version1_0.CrossIndustryDocumentType))
|
||||
oAllowedTypes.Add(ZUGFERD_SPEC_10, GetType(ZUGFeRD.Version1_0.CrossIndustryDocumentType))
|
||||
End If
|
||||
|
||||
If _Options.AllowZugferd_2_x_Schema Then
|
||||
oAllowedTypes.AddRange(New List(Of Type) From {
|
||||
GetType(ZUGFeRD.Version2_0.CrossIndustryInvoiceType),
|
||||
GetType(ZUGFeRD.Version2_1_1.CrossIndustryInvoiceType),
|
||||
GetType(ZUGFeRD.Version2_2_FacturX.CrossIndustryInvoiceType)
|
||||
})
|
||||
oAllowedTypes.Add(ZUGFERD_SPEC_2x, GetType(ZUGFeRD.Version2_0.CrossIndustryInvoiceType))
|
||||
oAllowedTypes.Add(ZUGFERD_SPEC_2x, GetType(ZUGFeRD.Version2_1_1.CrossIndustryInvoiceType))
|
||||
oAllowedTypes.Add(ZUGFERD_SPEC_2x, GetType(ZUGFeRD.Version2_2_FacturX.CrossIndustryInvoiceType))
|
||||
End If
|
||||
|
||||
For Each oType In oAllowedTypes
|
||||
_logger.Debug("Trying Type [{0}]", oType.FullName)
|
||||
Dim oSerializer As New XmlSerializer(oType)
|
||||
Dim oTypeName As String = oType.Value.FullName
|
||||
Dim oSerializer As New XmlSerializer(oType.Value)
|
||||
_logger.Debug("Trying Type [{0}]", oTypeName)
|
||||
|
||||
Try
|
||||
oReader = oNavigator.ReadSubtree()
|
||||
|
||||
oObject = oSerializer.Deserialize(oReader)
|
||||
_logger.Debug("Serializing with type [{0}] succeeded", oType.FullName)
|
||||
oSpecification = oType.Key
|
||||
|
||||
_logger.Debug("Serializing with type [{0}] succeeded", oTypeName)
|
||||
Exit For
|
||||
Catch ex As Exception
|
||||
_logger.Debug("Serializing with type [{0}] failed", oType.FullName)
|
||||
_logger.Debug("Serializing with type [{0}] failed", oTypeName)
|
||||
_logger.Debug(ex.Message)
|
||||
_logger.Error(ex.InnerException?.Message)
|
||||
End Try
|
||||
@@ -256,7 +277,10 @@ Public Class ZUGFeRDInterface
|
||||
Throw New ApplicationException("No Types matched the given document. Document could not be serialized.")
|
||||
End If
|
||||
|
||||
Return New Tuple(Of String, Object)(pDocument.Item1, oObject)
|
||||
pResult.Specification = oSpecification
|
||||
pResult.SchemaObject = oObject
|
||||
|
||||
Return pResult
|
||||
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
Public Class XmlItemProperty
|
||||
Public IsRequired As Boolean
|
||||
Public IsGrouped As Boolean
|
||||
|
||||
Public TableName As String
|
||||
Public TableColumn As String
|
||||
Public Description As String
|
||||
Public IsRequired As Boolean
|
||||
Public IsGrouped As Boolean
|
||||
Public GroupScope As String
|
||||
|
||||
''' <summary>
|
||||
''' Document version, eg. ZUGFeRD Schema version
|
||||
''' </summary>
|
||||
Public Specification As String
|
||||
End Class
|
||||
Reference in New Issue
Block a user