77 lines
2.5 KiB
VB.net
77 lines
2.5 KiB
VB.net
Imports System.IO
|
|
Imports System.Xml
|
|
Imports System.Xml.Serialization
|
|
Imports System.Xml.XPath
|
|
Imports DigitalData.Modules.Logging
|
|
Imports EDIDocumentImport.DocumentInfo
|
|
|
|
Public Class DocumentLoader
|
|
Inherits Base
|
|
|
|
Public Config As Config
|
|
Public Files As New List(Of Document)
|
|
|
|
Public Sub New(pLogConfig As LogConfig, pConfig As Config)
|
|
MyBase.New(pLogConfig, pLogConfig.GetLogger())
|
|
Config = pConfig
|
|
End Sub
|
|
|
|
Public Function LoadFiles() As Boolean
|
|
If Config.InputDirectory = String.Empty Then
|
|
Throw New ArgumentNullException("InputDirectory")
|
|
End If
|
|
|
|
Logger.Info("Loading files from directory [{0}]", Config.InputDirectory)
|
|
|
|
Try
|
|
Dim oDirectory As New DirectoryInfo(Config.InputDirectory)
|
|
Dim oFiles = oDirectory.GetFiles()
|
|
|
|
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
|
|
|
|
Files = oFiles.
|
|
Select(AddressOf WrapFileInfo).
|
|
Select(AddressOf LoadDocumentData).
|
|
ToList()
|
|
|
|
Return True
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Throw New IO.IOException($"Could not load files from directory {Config.InputDirectory}", ex)
|
|
|
|
End Try
|
|
End Function
|
|
|
|
Private Function WrapFileInfo(pFileInfo As FileInfo) As Document
|
|
Return New Document With {.File = pFileInfo}
|
|
End Function
|
|
|
|
Public Function LoadDocumentData(pDocument As Document) As Document
|
|
Using oFileStream As New FileStream(pDocument.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
|
|
Dim oXmlDocument = New XPathDocument(oFileStream)
|
|
Dim oNavigator = oXmlDocument.CreateNavigator()
|
|
|
|
Using oReader = oNavigator.ReadSubtree()
|
|
Dim oTemplateName = GetTemplateName(oNavigator)
|
|
Dim oDocumentType = GetDocumentTypeFromTemplateName(oTemplateName)
|
|
Dim oSchemaType = GetDocumentSchemaFromDocumentType(oDocumentType)
|
|
Dim oSerializer As New XmlSerializer(oSchemaType)
|
|
Dim oObject = oSerializer.Deserialize(oReader)
|
|
|
|
pDocument.Data = oObject
|
|
pDocument.Type = oDocumentType
|
|
Return pDocument
|
|
End Using
|
|
End Using
|
|
End Function
|
|
|
|
Public Function GetTemplateName(pDocument As XPathNavigator) As String
|
|
Dim oTemplateName = pDocument.
|
|
SelectSingleNode("//MESOWebService").
|
|
GetAttribute("Template", "")
|
|
|
|
Return oTemplateName
|
|
End Function
|
|
End Class
|