Rename Schema to Template , Clean up obsolete files
This commit is contained in:
226
MultiTool.Shared/Templates/TemplateLoader.vb
Normal file
226
MultiTool.Shared/Templates/TemplateLoader.vb
Normal file
@@ -0,0 +1,226 @@
|
||||
Imports System.IO
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports MultiTool.Shared.Winline
|
||||
Imports MultiTool.Shared.Helpers
|
||||
|
||||
Namespace Templates
|
||||
Public Class TemplateLoader
|
||||
Inherits BaseClass
|
||||
|
||||
Private ReadOnly ns As XNamespace = "http://www.w3.org/2001/XMLSchema"
|
||||
|
||||
Public Property TemplateList As List(Of Template)
|
||||
Public Property TemplateConfiguration As New TemplateConfig
|
||||
Public Property MappingConfiguration As New List(Of MappingConfig)
|
||||
|
||||
Private ReadOnly Database As MSSQLServer
|
||||
|
||||
Private Const VWEDI_XML_ITEMS = "VWEDI_XML_ITEMS"
|
||||
Private Const VWEDI_XML_MAPPING = "VWEDI_XML_MAPPING"
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig, pMSSQL As MSSQLServer)
|
||||
MyBase.New(pLogConfig, pLogConfig.GetLogger)
|
||||
Database = pMSSQL
|
||||
End Sub
|
||||
|
||||
Public Async Function LoadTemplates() As Task(Of Boolean)
|
||||
Try
|
||||
Dim oSql = $"SELECT * FROM [DD_ECM].[dbo].[TBEDI_XML_TEMPLATES]"
|
||||
Dim oTable As DataTable = Await Database.GetDatatableAsync(oSql)
|
||||
Dim oTemplates As New List(Of Template)
|
||||
|
||||
For Each oRow As DataRow In oTable.Rows
|
||||
Dim oTemplate As New Template With {
|
||||
.Name = GetRowItem(oRow, "NAME", String.Empty),
|
||||
.Description = GetRowItem(oRow, "DESCRIPTION", String.Empty),
|
||||
.FileName = GetRowItem(oRow, "FILE_NAME", String.Empty),
|
||||
.IsImport = GetRowItem(oRow, "IS_IMPORT", True)
|
||||
}
|
||||
|
||||
oTemplates.Add(oTemplate)
|
||||
Next
|
||||
|
||||
TemplateList = oTemplates
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Async Function LoadMappingConfiguration() As Task(Of Boolean)
|
||||
Try
|
||||
Dim oSql = $"SELECT * FROM [DD_ECM].[dbo].[{VWEDI_XML_MAPPING}]"
|
||||
Dim oTable As DataTable = Await Database.GetDatatableAsync(oSql)
|
||||
Dim oMappingConfigList As New List(Of MappingConfig)
|
||||
|
||||
For Each oRow As DataRow In oTable.Rows
|
||||
Dim oTemplate As New MappingConfig With {
|
||||
.OrderKey = GetRowItem(oRow, "ORDER_KEY", String.Empty),
|
||||
.SourceName = GetRowItem(oRow, "SOURCE_NAME", String.Empty),
|
||||
.SourceItem = GetRowItem(oRow, "SOURCE_ITEM", String.Empty),
|
||||
.SourceRegex = GetRowItem(oRow, "SOURCE_REGEX", String.Empty),
|
||||
.DestinationName = GetRowItem(oRow, "DESTINATION_NAME", String.Empty),
|
||||
.DestinationItem = GetRowItem(oRow, "DESTINATION_ITEM", String.Empty),
|
||||
.DestinationValue = GetRowItem(oRow, "DESTINATION_VALUE", String.Empty)
|
||||
}
|
||||
|
||||
oMappingConfigList.Add(oTemplate)
|
||||
Next
|
||||
|
||||
MappingConfiguration = oMappingConfigList
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Async Function LoadTemplateConfiguration() As Task(Of Boolean)
|
||||
Try
|
||||
Dim oSql = $"SELECT * FROM [DD_ECM].[dbo].[{VWEDI_XML_ITEMS}]"
|
||||
Dim oTable As DataTable = Await Database.GetDatatableAsync(oSql)
|
||||
Dim oItems As New List(Of ColumnConfig)
|
||||
|
||||
For Each oRow As DataRow In oTable.Rows
|
||||
Dim oColumn As New ColumnConfig() With {
|
||||
.Template = GetRowItem(oRow, "TEMPLATE_NAME", String.Empty),
|
||||
.Table = GetRowItem(oRow, "XML_TABLE", String.Empty),
|
||||
.Name = GetRowItem(oRow, "XML_ITEM", String.Empty),
|
||||
.Type = ColumnConfig.ConvertType(GetRowItem(oRow, "DATA_TYPE", String.Empty)),
|
||||
.OrderKey = GetRowItem(oRow, "ORDER_KEY", 0),
|
||||
.IsReadOnly = GetRowItem(oRow, "IS_READ_ONLY", False),
|
||||
.IsVisible = GetRowItem(oRow, "IS_VISIBLE", True),
|
||||
.IsRequired = GetRowItem(oRow, "IS_REQUIRED", False),
|
||||
.IsHead = GetRowItem(oRow, "IS_HEAD", True),
|
||||
.[Function] = New ColumnConfig.ColumnFunction With {
|
||||
.Id = GetRowItem(oRow, "FUNCTION_ID", 0),
|
||||
.Name = GetRowItem(oRow, "FUNCTION_NAME", String.Empty),
|
||||
.Params = GetRowItem(oRow, "FUNCTION_PARAMETERS", String.Empty)
|
||||
}
|
||||
}
|
||||
|
||||
oItems.Add(oColumn)
|
||||
Next
|
||||
|
||||
TemplateConfiguration = New TemplateConfig With {
|
||||
.Columns = oItems
|
||||
}
|
||||
|
||||
Return True
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function UpdateTemplateFromFile(pTemplate As Template, pInputDirectory As String) As Template
|
||||
Dim oFullPath = Path.Combine(pInputDirectory, pTemplate.FileName)
|
||||
|
||||
If Not IO.File.Exists(oFullPath) Then
|
||||
Throw New FileNotFoundException(oFullPath)
|
||||
End If
|
||||
|
||||
Dim oElements = GetTemplateElements(oFullPath)
|
||||
|
||||
For Each oElement In oElements
|
||||
Dim oColumns = GetElementColumns(oElement)
|
||||
Dim oTemplateColumns As New List(Of Template.Column)
|
||||
|
||||
For Each oColumn As XElement In oColumns
|
||||
Dim oName = XmlData.GetElementAttribute(oColumn, "name")
|
||||
Dim oMinOccurs = XmlData.GetElementAttribute(oColumn, "minOccurs")
|
||||
Dim oMaxOccurs = XmlData.GetElementAttribute(oColumn, "maxOccurs")
|
||||
Dim oType = GetElementType(oColumn)
|
||||
Dim oRequired = False
|
||||
|
||||
If oMinOccurs = 1 And oMaxOccurs = 1 Then
|
||||
oRequired = True
|
||||
End If
|
||||
|
||||
Dim oTemplateColumn As New Template.Column With {
|
||||
.Name = oName,
|
||||
.DataType = oType,
|
||||
.IsRequired = oRequired
|
||||
}
|
||||
oTemplateColumns.Add(oTemplateColumn)
|
||||
Next
|
||||
|
||||
pTemplate.Tables.Add(New Template.Table With {
|
||||
.Name = XmlData.GetElementAttribute(oElement, "name"),
|
||||
.Columns = oTemplateColumns
|
||||
})
|
||||
|
||||
Next
|
||||
|
||||
Return pTemplate
|
||||
End Function
|
||||
|
||||
Public Function UpdateTemplateFromDatabase(pTemplate As Template, pTemplateConfig As TemplateConfig) As Template
|
||||
If pTemplateConfig Is Nothing Then
|
||||
Return pTemplate
|
||||
End If
|
||||
|
||||
For Each oTable In pTemplate.Tables
|
||||
For Each oColumn As Template.Column In oTable.Columns
|
||||
Dim oConfig = pTemplateConfig.GetColumn(oColumn.Name)
|
||||
|
||||
If oConfig Is Nothing Then
|
||||
oConfig = New ColumnConfig With {
|
||||
.IsRequired = oColumn.IsRequired,
|
||||
.Name = oColumn.Name
|
||||
}
|
||||
End If
|
||||
|
||||
oColumn.Config = oConfig
|
||||
Next
|
||||
Next
|
||||
|
||||
Return pTemplate
|
||||
End Function
|
||||
|
||||
Public Function GetElementType(pElement As XElement) As Constants.ColumnType
|
||||
Dim oTypeString = XmlData.GetElementAttribute(pElement, "type")
|
||||
|
||||
If oTypeString Is Nothing Then
|
||||
Dim oRestrictionElement As XElement = pElement.
|
||||
Descendants(ns + "restriction").
|
||||
FirstOrDefault()
|
||||
|
||||
oTypeString = XmlData.GetElementAttribute(oRestrictionElement, "base")
|
||||
End If
|
||||
|
||||
Select Case oTypeString
|
||||
Case Constants.TEMPLATE_TYPE_DATE
|
||||
Return Constants.ColumnType.Date
|
||||
Case Constants.TEMPLATE_TYPE_INTEGER
|
||||
Return Constants.ColumnType.Integer
|
||||
Case Constants.TEMPLATE_TYPE_DECIMAL
|
||||
Return Constants.ColumnType.Decimal
|
||||
Case Constants.TEMPLATE_TYPE_BOOLEAN
|
||||
Return Constants.ColumnType.Boolean
|
||||
Case Else
|
||||
Return Constants.ColumnType.String
|
||||
End Select
|
||||
End Function
|
||||
|
||||
Public Function GetTemplateElements(pTemplateFilePath As String) As List(Of XElement)
|
||||
Dim oText As String = IO.File.ReadAllText(pTemplateFilePath)
|
||||
Dim oDoc = XDocument.Parse(oText)
|
||||
|
||||
Return XmlData.GetElementsFromElement(oDoc, "choice", ns)
|
||||
End Function
|
||||
|
||||
Public Function GetElementColumns(pElement As XElement) As List(Of XElement)
|
||||
Return XmlData.GetElementsFromElement(pElement, "sequence", ns)
|
||||
End Function
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user