This commit is contained in:
Jonathan Jenne
2021-10-27 10:32:04 +02:00
parent 8dfc659ef8
commit a519b93f47
18 changed files with 351 additions and 165 deletions

View File

@@ -3,4 +3,12 @@
GLN = 1
EAN = 2
End Enum
Public Enum ColumnType As Integer
[String]
[Integer]
[Date]
[Boolean]
[Decimal]
End Enum
End Class

View File

@@ -1,10 +1,12 @@
Imports System.IO
Imports ImporterShared.Schemas
Namespace Documents
Public Class Document
Public File As FileInfo
Public Mandator As String
Public Type As DocumentType
Public Schema As Schema
Public Mandator As String
Public Selected As Boolean = False
''' <summary>
@@ -12,7 +14,6 @@ Namespace Documents
''' </summary>
Public CreatedAt As Date
Public TemplateName As String
Public TemplateType As Integer
Public [Option] As Integer

View File

@@ -1,20 +1,12 @@
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 System.IO
Imports DigitalData.Modules.Logging
Imports ImporterShared.Documents
Imports ImporterShared.Schemas.Orders
Imports ImporterShared.Schemas
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)
@@ -22,10 +14,10 @@ Namespace Documents
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
Public Function LoadFiles(pInputDirectory As String, pSchema As Schema) As Boolean
If pInputDirectory = String.Empty Then
Throw New ArgumentNullException("InputDirectory")
End If
@@ -40,7 +32,7 @@ Namespace Documents
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
For Each oFile In oFiles
Dim oDocument = LoadFile(oFile)
Dim oDocument = LoadFile(oFile, pSchema)
Files.Add(oDocument)
Next
@@ -53,57 +45,110 @@ Namespace Documents
End Try
End Function
Public Function LoadFile(pFileInfo As FileInfo) As Document
Public Function LoadFile(pFileInfo As FileInfo, pSchema As Schema) As Document
Dim oFileList As New List(Of FileInfo) From {pFileInfo}
Logger.Info("Loading file [{0}]", pFileInfo.Name)
Return oFileList.
Select(AddressOf WrapFileInfo).
Select(AddressOf LoadDocumentData2).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators)).
SingleOrDefault()
Try
Return oFileList.
Select(AddressOf WrapFileInfo).
Select(Function(d) IncludeSchema(d, pSchema)).
Select(Function(d) LoadDocumentData(d, pSchema)).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators)).
SingleOrDefault()
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function LoadDocumentData2(pDocument As Document) As Document
Private Function IncludeSchema(pDocument As Document, pSchema As Schema) As Document
pDocument.Schema = pSchema
Return pDocument
End Function
''' <summary>
''' Loads a single document from the FullName Property in the Document Object
''' </summary>
''' <example>
'''
''' A document might look like this:
''' <MESOWebService>
''' <Row1></Row1>
''' <Row2></Row2>
''' <Row3></Row3>
''' </MESOWebService>
'''
''' </example>
Private Function LoadDocumentData(pDocument As Document, pSchema As Schema) 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 oRootElement Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein MESOWebService-Element")
End If
Dim oTemplateName = XmlData.GetElementAttribute(oRootElement, "Template")
If oTemplateName Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein Template-Attribut")
End If
Dim oTemplateType = XmlData.GetElementAttribute(oRootElement, "TemplateType")
If oTemplateType Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein TemplateType-Attribut")
End If
Dim oOption = XmlData.GetElementAttribute(oRootElement, "option")
If oOption Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein option-Attribut")
End If
Dim oPrintVoucher = XmlData.GetElementAttribute(oRootElement, "printVoucher")
If oPrintVoucher Is Nothing Then
Throw New Exceptions.MalformedXmlException("Datei enthält kein printVoucher-Attribut")
End If
Dim oRowElements As List(Of XElement) = oRootElement.Elements.ToList
' The first level of Elements are the document Rows
Dim oTopLevelElements As List(Of XElement) = oRootElement.Elements.ToList
Dim oDocumentRows As New List(Of DocumentRow)
Dim oRows As New List(Of DocumentRow)
For Each oElement As XElement In oRowElements
For Each oTopLevelElement As XElement In oTopLevelElements
Dim oFields As New Dictionary(Of String, DocumentRow.FieldValue)
Dim oSubElements = oElement.Descendants().ToList()
Dim oSubElements = oTopLevelElement.Descendants().ToList()
Dim oTable = pSchema.Tables.
Where(Function(t) t.Name = oTopLevelElement.Name).
FirstOrDefault()
For Each oSubElement As XElement In oSubElements
Dim oSchemaField = oTable.Columns.
Where(Function(c) c.Name = oSubElement.Name).
SingleOrDefault()
oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With {
.Original = oSubElement.Value,
.Final = oSubElement.Value
.Final = oSubElement.Value,
.DataType = oSchemaField.DataType
})
Next
' Create a DocumentRow object for each Top Level Element
Dim oRow = New DocumentRow With {
.Name = oElement.Name.ToString,
.Name = oTopLevelElement.Name.ToString,
.Fields = oFields
}
oRows.Add(oRow)
oDocumentRows.Add(oRow)
Next
' Update the document
pDocument.TemplateName = oTemplateName
pDocument.TemplateType = oTemplateType
pDocument.Rows = oRows
pDocument.Option = oOption
pDocument.PrintVoucher = oPrintVoucher
pDocument.Rows = oDocumentRows
Return pDocument
End Function
@@ -121,26 +166,13 @@ Namespace Documents
Logger.Warn("Mandator not found for File [{0}]", pDocument.File.Name)
End If
pDocument = MatchOrderData(pDocument, oMandator)
pDocument = MatchDocumentData(pDocument, oMandator)
pDocument.Mandator = oMandator.Id
'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(pDocument As Document, pMandator As Winline.Mandator) As Document
Private Function MatchDocumentData(pDocument As Document, pMandator As Winline.Mandator) As Document
Dim oYear = Winline.GetWinLineYear()
If pMandator Is Nothing Then

View File

@@ -15,6 +15,7 @@
Public Original As String = ""
Public External As String = ""
Public Final As String = ""
Public DataType As Constants.ColumnType = Constants.ColumnType.String
Public Overrides Function ToString() As String
Return Final

View File

@@ -0,0 +1,49 @@
Public Class Exceptions
Public MustInherit Class DocumentShowException
Inherits ApplicationException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class WebServiceException
Inherits ApplicationException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class NoMandatorException
Inherits DocumentShowException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class MultipleAccountsException
Inherits DocumentShowException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class NoAccountException
Inherits DocumentShowException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class MalformedXmlException
Inherits ApplicationException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
End Class

View File

@@ -100,6 +100,7 @@
<Compile Include="Documents\DocumentRow.vb" />
<Compile Include="Documents\DocumentType.vb" />
<Compile Include="Documents\DocumentLoader.vb" />
<Compile Include="Exceptions.vb" />
<Compile Include="IDictionaryEx.vb" />
<Compile Include="IEnumerableEx.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
@@ -127,6 +128,7 @@
<Compile Include="Schemas\Schema.vb" />
<Compile Include="Schemas\SchemaLoader.vb" />
<Compile Include="Serializer.vb" />
<Compile Include="Winline\Configuration.vb" />
<Compile Include="Winline\Entities\Account.vb" />
<Compile Include="Winline\Data.vb" />
<Compile Include="Winline\Entities\Contact.vb" />

View File

@@ -1,14 +1,5 @@
Namespace Schemas
Public Class Schema
Public Enum ColumnType As Integer
[String]
[Integer]
[Date]
[Boolean]
[Decimal]
End Enum
Public Property Tables As New List(Of Table)
Public Property Name As String
@@ -20,9 +11,8 @@
Class Column
Public Property Name As String
Public Property Required As String
Public Property DataType As ColumnType
Public Property DataType As Constants.ColumnType
End Class
End Class
End Namespace

View File

@@ -41,8 +41,11 @@ Namespace Schemas
Public Function GetSchemaFromFile(pSchemaFilePath As String) As Schema
Dim oSchema As New Schema
Dim oFileInfo As New FileInfo(pSchemaFilePath)
Dim oElements = GetSchemaElements(pSchemaFilePath)
oSchema.Name = oFileInfo.Name
For Each oElement In oElements
Dim oColumns = GetElementColumns(oElement)
Dim oSchemaColumns As New List(Of Schema.Column)
@@ -76,7 +79,7 @@ Namespace Schemas
Return oSchema
End Function
Public Function GetElementType(pElement As XElement) As Schema.ColumnType
Public Function GetElementType(pElement As XElement) As Constants.ColumnType
Dim oTypeString = XmlData.GetElementAttribute(pElement, "type")
If oTypeString Is Nothing Then
@@ -89,15 +92,15 @@ Namespace Schemas
Select Case oTypeString
Case "xs:date"
Return Schema.ColumnType.Date
Return Constants.ColumnType.Date
Case "xs:integer"
Return Schema.ColumnType.Integer
Return Constants.ColumnType.Integer
Case "xs:decimal"
Return Schema.ColumnType.Decimal
Return Constants.ColumnType.Decimal
Case "xs:boolean"
Return Schema.ColumnType.Boolean
Return Constants.ColumnType.Boolean
Case Else
Return Schema.ColumnType.String
Return Constants.ColumnType.String
End Select
End Function

View File

@@ -0,0 +1,7 @@
''' <summary>
''' Class for loading column/field config from database
''' </summary>
Public Class Configuration
End Class