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 DigitalData.Modules.Logging Imports ImporterShared.Documents Imports ImporterShared.Schemas.Orders 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) 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 If pInputDirectory = String.Empty Then Throw New ArgumentNullException("InputDirectory") End If Logger.Info("Loading files from directory [{0}]", pInputDirectory) Try Dim oDirectory As New DirectoryInfo(pInputDirectory) Dim oFiles = oDirectory.GetFiles() Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory) Files = oFiles. Select(AddressOf WrapFileInfo). Select(AddressOf LoadDocumentData2). ToList() Return True Catch ex As Exception Logger.Error(ex) Throw New IO.IOException($"Could not load files from directory {pInputDirectory}", ex) End Try End Function 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") Dim oTemplateName = XmlData.GetElementAttribute(oRootElement, "Template") Dim oTemplateType = XmlData.GetElementAttribute(oRootElement, "TemplateType") Dim oOption = XmlData.GetElementAttribute(oRootElement, "option") Dim oPrintVoucher = XmlData.GetElementAttribute(oRootElement, "printVoucher") Dim oRowElements As List(Of XElement) = oRootElement.Elements.ToList 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() If pMandator Is Nothing Then Return pData End If Dim oHead As Input.MESOWebServiceEXIMVRG_ordersT025 = pData.Items. Where(Function(h) TypeOf h Is Input.MESOWebServiceEXIMVRG_ordersT025). SetValue(Sub(h As Input.MESOWebServiceEXIMVRG_ordersT025) Dim oAccount = Winline.TryGetAccount(h.Fakt_Kontonummer, pMandator) If oAccount IsNot Nothing Then h.Fakt_Kontonummer = oAccount.Id h.Fakt_Name = oAccount.Name End If End Sub). SetValue(Sub(h As Input.MESOWebServiceEXIMVRG_ordersT025) Dim oAccount = Winline.TryGetAccount(h.Lief_Kontonummer, pMandator) If oAccount IsNot Nothing Then h.Lief_Kontonummer = oAccount.Id h.Lief_Name = oAccount.Name End If End Sub). FirstOrDefault() Dim oPositions As List(Of Input.MESOWebServiceEXIMVRG_ordersT026) = pData.Items. Where(Function(p) TypeOf p Is Input.MESOWebServiceEXIMVRG_ordersT026). SetValue(Sub(p) Dim oArticleNumber = Winline.TryGetArticleNumber(p.Artikelnummer, pMandator) If oArticleNumber IsNot Nothing Then p.Artikelnummer = oArticleNumber End If End Sub). Select(Of Input.MESOWebServiceEXIMVRG_ordersT026)(Function(i) i). ToList() pData.Items = New List(Of Object) From {oHead}. Concat(oPositions). ToArray() 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 oDocument = oXmlDocument.CreateNavigator() ' Dim oTemplateName = GetTemplateName(oDocument) ' Dim oDocumentType = DocumentMatch.GetDocumentTypeFromTemplateName(oTemplateName) ' Dim oSchemaType As Type = DocumentMatch.GetDocumentSchemaFromDocumentType(oDocumentType) ' ' Read data the first time, working copy ' 'Using oReader = oNavigator.ReadSubtree() ' ' Dim oSerializer = Serializer.GetSerializer(oSchemaType) ' ' pDocument.Data = oSerializer.Deserialize(oReader) ' 'End Using ' ' Read data the second time, archive copy ' 'Using oReader = oNavigator.ReadSubtree() ' ' Dim oSerializer = Serializer.GetSerializer(oSchemaType) ' ' pDocument.DataOriginal = oSerializer.Deserialize(oReader) ' 'End Using ' 'Dim oInstance As Object = Activator.CreateInstance(oSchemaType) ' 'Dim oProps = oSchemaType.GetProperties() ' '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", "") Return oTemplateName End Function End Class End Namespace