First Working Version with New parser
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user