Imports System.IO 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 LoadDocumentData). Select(Function(oDocument) Return MatchDataFromWinLine(oDocument, Winline.Mandators) End Function). 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 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 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) 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 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