First Working Version with New parser

This commit is contained in:
Jonathan Jenne
2021-10-15 14:56:46 +02:00
parent 3596786137
commit 12aa22ebdf
33 changed files with 3066 additions and 2533 deletions

View File

@@ -1,19 +1,17 @@
Imports System.IO
Imports EDIDocumentImport.Data
Namespace Documents
Public Class Document
Public File As FileInfo
Public Type As DocumentType
Public Mandator As String
Public Data As Object
Public DataOriginal As Object
Public DataOutput As Object
Public DataString As String
Public Type As DocumentType
Public Selected As Boolean = False
Public TemplateName As String
Public TemplateType As Integer
Public [Option] As Integer
Public PrintVoucher As Integer
Public ReadOnly Property FullName As String
Get
Return File?.FullName
@@ -26,19 +24,30 @@ Namespace Documents
End Get
End Property
Public Shared Function GetDocumentTypeFromTemplateName(pTemplateName As String) As DocumentType
Return DocumentMatch.TypeMatchingTable.
Where(Function(kv) pTemplateName.Contains(kv.Key)).
Select(Function(kv) kv.Value).
FirstOrDefault()
End Function
Public Rows As New List(Of DocumentRow)
Public Shared Function GetDocumentSchemaFromDocumentType(pDocumentType As DocumentType) As Type
Return DocumentMatch.SchemaMatchingTable.
Where(Function(kv) pDocumentType = kv.Key).
Select(Function(kv) kv.Value).
FirstOrDefault()
End Function
' Public Type As DocumentType
' Public Data As Object
' Public DataOriginal As Object
' Public DataOutput As Object
' Public DataString As String
' Public Selected As Boolean = False
' Public Shared Function GetDocumentTypeFromTemplateName(pTemplateName As String) As DocumentType
' Return DocumentMatch.TypeMatchingTable.
' Where(Function(kv) pTemplateName.Contains(kv.Key)).
' Select(Function(kv) kv.Value).
' FirstOrDefault()
' End Function
' Public Shared Function GetDocumentSchemaFromDocumentType(pDocumentType As DocumentType) As Type
' Return DocumentMatch.SchemaMatchingTable.
' Where(Function(kv) pDocumentType = kv.Key).
' Select(Function(kv) kv.Value).
' FirstOrDefault()
' End Function
End Class
End Namespace

View File

@@ -1,4 +1,6 @@
Imports System.IO
Imports System.ComponentModel
Imports System.IO
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports System.Xml.Serialization
Imports System.Xml.XPath
@@ -36,12 +38,9 @@ Namespace Documents
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
Files = oFiles.
Select(AddressOf WrapFileInfo).
Select(AddressOf LoadDocumentData).
Select(Function(oDocument)
Return MatchDataFromWinLine(oDocument, Winline.Mandators)
End Function).
ToList()
Select(AddressOf WrapFileInfo).
Select(AddressOf LoadDocumentData2).
ToList()
Return True
@@ -52,26 +51,66 @@ Namespace Documents
End Try
End Function
Private Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Winline.Mandator)) As Document
Dim oMandators As List(Of Winline.Mandator) = pMandators.
Where(Function(m) m.IsWhitelisted = True).
OrderBy(Function(m) m.Order).
ToList()
Private Function LoadDocumentData2(pDocument As Document) 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 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)
Dim oTemplateName = XmlData.GetElementAttribute(oRootElement, "Template")
Dim oTemplateType = XmlData.GetElementAttribute(oRootElement, "TemplateType")
Dim oOption = XmlData.GetElementAttribute(oRootElement, "option")
Dim oPrintVoucher = XmlData.GetElementAttribute(oRootElement, "printVoucher")
If oMandator Is Nothing Then
Logger.Warn("Mandator not found for File [{0}]", pDocument.File.Name)
End If
Dim oRowElements As List(Of XElement) = oRootElement.Elements.ToList
pDocument.Mandator = oMandator.Id
pDocument.Data = oData
End If
Dim oRows As New List(Of DocumentRow)
For Each oElement As XElement In oRowElements
Dim oFields As New Dictionary(Of String, String)
Dim oSubElements = oElement.Descendants().ToList()
For Each oSubElement As XElement In oSubElements
oFields.Add(oSubElement.Name.ToString, oSubElement.Value)
Next
Dim oRow = New DocumentRow With {
.Name = oElement.Name.ToString,
.Fields = oFields
}
oRows.Add(oRow)
Next
pDocument.TemplateName = oTemplateName
pDocument.TemplateType = oTemplateType
pDocument.Rows = oRows
pDocument.Option = oOption
pDocument.PrintVoucher = oPrintVoucher
Return pDocument
End Function
'Private Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Winline.Mandator)) As Document
' Dim oMandators As List(Of Winline.Mandator) = pMandators.
' Where(Function(m) m.IsWhitelisted = True).
' OrderBy(Function(m) m.Order).
' ToList()
' 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(pData As Input.MESOWebService, pMandator As Winline.Mandator) As Input.MESOWebService
Dim oYear = Winline.GetWinLineYear()
@@ -115,51 +154,77 @@ Namespace Documents
Return pData
End Function
Private Function WrapFileInfo(pFileInfo As FileInfo) As Document
Return New Document With {.File = pFileInfo}
End Function
Private Function LoadDocumentData(pDocument As Document) As Document
Using oFileStream As New FileStream(pDocument.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
Try
Dim oXmlDocument = New XPathDocument(oFileStream)
Dim oNavigator = oXmlDocument.CreateNavigator()
Dim oTemplateName = GetTemplateName(oNavigator)
Dim oDocumentType = DocumentMatch.GetDocumentTypeFromTemplateName(oTemplateName)
Dim oSchemaType = DocumentMatch.GetDocumentSchemaFromDocumentType(oDocumentType)
' Read data the first time, working copy
Using oReader = oNavigator.ReadSubtree()
Dim oSerializer = Serializer.GetSerializer(oSchemaType)
pDocument.Data = oSerializer.Deserialize(oReader)
'Private Function LoadDocumentData(pDocument As Document) As Document
' Using oFileStream As New FileStream(pDocument.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
' Try
' Dim oXmlDocument = New XPathDocument(oFileStream)
' Dim oDocument = oXmlDocument.CreateNavigator()
' Dim oTemplateName = GetTemplateName(oDocument)
' Dim oDocumentType = DocumentMatch.GetDocumentTypeFromTemplateName(oTemplateName)
' Dim oSchemaType As Type = DocumentMatch.GetDocumentSchemaFromDocumentType(oDocumentType)
End Using
' ' Read data the first time, working copy
' 'Using oReader = oNavigator.ReadSubtree()
' ' Dim oSerializer = Serializer.GetSerializer(oSchemaType)
' ' pDocument.Data = oSerializer.Deserialize(oReader)
' Read data the second time, archive copy
Using oReader = oNavigator.ReadSubtree()
Dim oSerializer = Serializer.GetSerializer(oSchemaType)
pDocument.DataOriginal = oSerializer.Deserialize(oReader)
' 'End Using
End Using
' ' Read data the second time, archive copy
' 'Using oReader = oNavigator.ReadSubtree()
' ' Dim oSerializer = Serializer.GetSerializer(oSchemaType)
' ' pDocument.DataOriginal = oSerializer.Deserialize(oReader)
pDocument.Type = oDocumentType
Return pDocument
Catch ex As Exception
Logger.Error(ex)
' 'End Using
Dim oException As Exception = ex
If ex.InnerException IsNot Nothing Then
oException = ex.InnerException
End If
' 'Dim oInstance As Object = Activator.CreateInstance(oSchemaType)
' 'Dim oProps = oSchemaType.GetProperties()
Throw oException
End Try
End Using
End Function
' 'Dim oHead = oProps.
' ' Where(Function(p) p.Name = "Head").
' ' SingleOrDefault()
' 'Dim oDescriptionHead As CustomAttributeData = oHead.CustomAttributes.
' ' Where(Function(d) d.AttributeType.Equals(GetType(DescriptionAttribute))).
' ' SingleOrDefault()
' 'Dim oDescriptionHeadValue = oDescriptionHead.ConstructorArguments.First().Value
' 'Dim oHeadXml = oDocument.Select($"//MESOWebService/{oDescriptionHeadValue}")
' 'Dim oPositions As PropertyInfo = oProps.
' ' Where(Function(p) p.Name = "Positions").
' ' SingleOrDefault()
' 'Dim oDescriptionPos As CustomAttributeData = oPositions.CustomAttributes.
' ' Where(Function(d) d.AttributeType.Equals(GetType(DescriptionAttribute))).
' ' SingleOrDefault()
' 'Dim oDescriptionPosValue = oDescriptionPos.ConstructorArguments.First().Value
' 'Dim oPosXml = oDocument.Select($"//MESOWebService/{oDescriptionPosValue}")
' pDocument.Type = oDocumentType
' Return pDocument
' Catch ex As Exception
' Logger.Error(ex)
' Dim oException As Exception = ex
' If ex.InnerException IsNot Nothing Then
' oException = ex.InnerException
' End If
' Throw oException
' End Try
' End Using
'End Function
Private Function GetTemplateName(pDocument As XPathNavigator) As String
Dim oTemplateName = pDocument.
SelectSingleNode("//MESOWebService").
GetAttribute("Template", "")
SelectSingleNode("//MESOWebService").
GetAttribute("Template", "")
Return oTemplateName
End Function

View File

@@ -8,7 +8,7 @@
}
Public Shared Property SchemaMatchingTable As New Dictionary(Of DocumentType, Type) From {
{DocumentType.Order, GetType(Schemas.Orders.Input.MESOWebService)}
{DocumentType.Order, GetType(Schemas.OrderSchema)}
}
Public Shared Function GetDocumentTypeFromTemplateName(pTemplateName As String) As DocumentType

View File

@@ -0,0 +1,4 @@
Public Class DocumentRow
Public Name As String
Public Fields As Dictionary(Of String, String)
End Class