This commit is contained in:
Jonathan Jenne
2021-10-27 10:32:04 +02:00
parent 8dfc659ef8
commit a519b93f47
18 changed files with 351 additions and 165 deletions

View File

@@ -1,20 +1,12 @@
Imports System.ComponentModel
Imports System.IO
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports System.Xml.Serialization
Imports System.Xml.XPath
Imports DigitalData.Modules.Language
Imports System.IO
Imports DigitalData.Modules.Logging
Imports ImporterShared.Documents
Imports ImporterShared.Schemas.Orders
Imports ImporterShared.Schemas
Namespace Documents
Public Class DocumentLoader
Inherits BaseClass
Private ReadOnly Winline As Winline.Data
Private ReadOnly Serializer As Serializer
Public Files As New List(Of Document)
@@ -22,10 +14,10 @@ Namespace Documents
Public Sub New(pLogConfig As LogConfig, pWinline As Winline.Data)
MyBase.New(pLogConfig, pLogConfig.GetLogger())
Winline = pWinline
Serializer = New Serializer(pLogConfig)
End Sub
Public Function LoadFiles(pInputDirectory) As Boolean
Public Function LoadFiles(pInputDirectory As String, pSchema As Schema) As Boolean
If pInputDirectory = String.Empty Then
Throw New ArgumentNullException("InputDirectory")
End If
@@ -40,7 +32,7 @@ Namespace Documents
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
For Each oFile In oFiles
Dim oDocument = LoadFile(oFile)
Dim oDocument = LoadFile(oFile, pSchema)
Files.Add(oDocument)
Next
@@ -53,57 +45,110 @@ Namespace Documents
End Try
End Function
Public Function LoadFile(pFileInfo As FileInfo) As Document
Public Function LoadFile(pFileInfo As FileInfo, pSchema As Schema) As Document
Dim oFileList As New List(Of FileInfo) From {pFileInfo}
Logger.Info("Loading file [{0}]", pFileInfo.Name)
Return oFileList.
Select(AddressOf WrapFileInfo).
Select(AddressOf LoadDocumentData2).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators)).
SingleOrDefault()
Try
Return oFileList.
Select(AddressOf WrapFileInfo).
Select(Function(d) IncludeSchema(d, pSchema)).
Select(Function(d) LoadDocumentData(d, pSchema)).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators)).
SingleOrDefault()
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function LoadDocumentData2(pDocument As Document) As Document
Private Function IncludeSchema(pDocument As Document, pSchema As Schema) As Document
pDocument.Schema = pSchema
Return pDocument
End Function
''' <summary>
''' Loads a single document from the FullName Property in the Document Object
''' </summary>
''' <example>
'''
''' A document might look like this:
''' <MESOWebService>
''' <Row1></Row1>
''' <Row2></Row2>
''' <Row3></Row3>
''' </MESOWebService>
'''
''' </example>
Private Function LoadDocumentData(pDocument As Document, pSchema As Schema) As Document
Dim oText As String = IO.File.ReadAllText(pDocument.FullName)
Dim oDoc = XDocument.Parse(oText)
Dim oRootElement As XElement = XmlData.GetElement(oDoc, "MESOWebService")
If oRootElement Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein MESOWebService-Element")
End If
Dim oTemplateName = XmlData.GetElementAttribute(oRootElement, "Template")
If oTemplateName Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein Template-Attribut")
End If
Dim oTemplateType = XmlData.GetElementAttribute(oRootElement, "TemplateType")
If oTemplateType Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein TemplateType-Attribut")
End If
Dim oOption = XmlData.GetElementAttribute(oRootElement, "option")
If oOption Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein option-Attribut")
End If
Dim oPrintVoucher = XmlData.GetElementAttribute(oRootElement, "printVoucher")
If oPrintVoucher Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein printVoucher-Attribut")
End If
Dim oRowElements As List(Of XElement) = oRootElement.Elements.ToList
' The first level of Elements are the document Rows
Dim oTopLevelElements As List(Of XElement) = oRootElement.Elements.ToList
Dim oDocumentRows As New List(Of DocumentRow)
Dim oRows As New List(Of DocumentRow)
For Each oElement As XElement In oRowElements
For Each oTopLevelElement As XElement In oTopLevelElements
Dim oFields As New Dictionary(Of String, DocumentRow.FieldValue)
Dim oSubElements = oElement.Descendants().ToList()
Dim oSubElements = oTopLevelElement.Descendants().ToList()
Dim oTable = pSchema.Tables.
Where(Function(t) t.Name = oTopLevelElement.Name).
FirstOrDefault()
For Each oSubElement As XElement In oSubElements
Dim oSchemaField = oTable.Columns.
Where(Function(c) c.Name = oSubElement.Name).
SingleOrDefault()
oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With {
.Original = oSubElement.Value,
.Final = oSubElement.Value
.Final = oSubElement.Value,
.DataType = oSchemaField.DataType
})
Next
' Create a DocumentRow object for each Top Level Element
Dim oRow = New DocumentRow With {
.Name = oElement.Name.ToString,
.Name = oTopLevelElement.Name.ToString,
.Fields = oFields
}
oRows.Add(oRow)
oDocumentRows.Add(oRow)
Next
' Update the document
pDocument.TemplateName = oTemplateName
pDocument.TemplateType = oTemplateType
pDocument.Rows = oRows
pDocument.Option = oOption
pDocument.PrintVoucher = oPrintVoucher
pDocument.Rows = oDocumentRows
Return pDocument
End Function
@@ -121,26 +166,13 @@ Namespace Documents
Logger.Warn("Mandator not found for File [{0}]", pDocument.File.Name)
End If
pDocument = MatchOrderData(pDocument, oMandator)
pDocument = MatchDocumentData(pDocument, oMandator)
pDocument.Mandator = oMandator.Id
'If TypeOf pDocument.Data Is Schemas.Orders.Input.MESOWebService Then
' Dim oMandator = Winline.FindMatchingMandatorFromOrder(pDocument.Data)
' Dim oData As Schemas.Orders.Input.MESOWebService = MatchOrderData(pDocument.Data, oMandator)
' If oMandator Is Nothing Then
' Logger.Warn("Mandator not found for File [{0}]", pDocument.File.Name)
' End If
' pDocument.Mandator = oMandator.Id
' pDocument.Data = oData
'End If
Return pDocument
End Function
Private Function MatchOrderData(pDocument As Document, pMandator As Winline.Mandator) As Document
Private Function MatchDocumentData(pDocument As Document, pMandator As Winline.Mandator) As Document
Dim oYear = Winline.GetWinLineYear()
If pMandator Is Nothing Then