Add Template Selection, load templates from Database

This commit is contained in:
Jonathan Jenne
2021-11-18 14:56:29 +01:00
parent 15b553efc3
commit 0356699473
21 changed files with 395 additions and 200 deletions

View File

@@ -0,0 +1,45 @@
Imports MultiTool.Shared.Constants
Namespace Schemas
Public Class ColumnConfig
Public Property Name As String
Public Property Table As String
Public Property Type As ColumnType
Public Property Template As String
Public Property OrderKey As Integer
Public Property IsHead As Boolean
Public Property IsReadOnly As Boolean
Public Property IsVisible As Boolean
Public Property IsRequired As Boolean
Public Property [Function] As ColumnFunction
Public Class ColumnFunction
Public Id As XmlFunction
Public Name As String
Public Params As String
End Class
Public Shared Function ConvertType(pType As String) As ColumnType
Select Case pType
Case DB_TYPE_DATE
Return ColumnType.Date
Case DB_TYPE_DECIMAL
Return ColumnType.Date
Case DB_TYPE_BOOLEAN
Return ColumnType.Boolean
Case DB_TYPE_INTEGER
Return ColumnType.Integer
Case Else
Return ColumnType.String
End Select
End Function
End Class
End Namespace

View File

@@ -0,0 +1,17 @@
Imports MultiTool.Shared.Winline
Namespace Schemas
''' <summary>
''' Class for loading column/field config from database
''' </summary>
Public Class Configuration
Public Columns As List(Of ColumnConfig)
Public Function GetColumn(pName As String) As ColumnConfig
Return Columns.
Where(Function(c) c.Name = pName).
FirstOrDefault()
End Function
End Class
End Namespace

View File

@@ -1,9 +1,14 @@
Imports MultiTool.Shared.Winline
Imports System.IO
Imports MultiTool.Shared.Winline
Namespace Schemas
Public Class Schema
Public Property Tables As New List(Of Table)
Public Property Name As String
Public Property FileName As String
Public Property Description As String
Public Property IsImport As Boolean
Public Property Tables As New List(Of Table)
Class Table
Public Property Name As String

View File

@@ -1,7 +1,8 @@
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports MultiTool.Shared.Winline
Imports MultiTool.Shared.Helpers
Namespace Schemas
Public Class SchemaLoader
@@ -9,42 +10,95 @@ Namespace Schemas
Private ReadOnly ns As XNamespace = "http://www.w3.org/2001/XMLSchema"
Public SchemaList As List(Of FileInfo)
Public SchemaList As List(Of Schema)
Public TemplateConfiguration As New Configuration
Public Sub New(pLogConfig As LogConfig)
Private Database As MSSQLServer
Private InputDirectory As String
Private Const VWEDI_XML_ITEMS = "VWEDI_XML_ITEMS"
Public Sub New(pLogConfig As LogConfig, pMSSQL As MSSQLServer, pInputDirectory As String)
MyBase.New(pLogConfig, pLogConfig.GetLogger)
Database = pMSSQL
InputDirectory = pInputDirectory
End Sub
Public Function LoadFiles(pSchemaDirectory) As Boolean
If pSchemaDirectory = String.Empty Then
Throw New ArgumentNullException("SchemaDirectory")
End If
Logger.Info("Loading files from directory [{0}]", pSchemaDirectory)
Public Async Function LoadTemplates() As Task(Of Boolean)
Try
Dim oDirectory As New DirectoryInfo(pSchemaDirectory)
Dim oFiles = oDirectory.GetFiles()
Dim oSql = $"SELECT * FROM [DD_ECM].[dbo].[TBEDI_XML_TEMPLATES]"
Dim oTable As DataTable = Await Database.GetDatatableAsync(oSql)
Dim oTemplates As New List(Of Schema)
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
For Each oRow As DataRow In oTable.Rows
Dim oTemplate As New Schema 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)
}
SchemaList = oFiles.ToList()
oTemplates.Add(oTemplate)
Next
SchemaList = oTemplates
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 Configuration With {
.Columns = oItems
}
Return True
Catch ex As Exception
Logger.Error(ex)
Throw New IO.IOException($"Could not load files from directory {pSchemaDirectory}", ex)
Return False
End Try
End Function
Public Function GetSchemaFromFile(pSchemaFilePath As String) As Schema
Dim oSchema As New Schema
Dim oFileInfo As New FileInfo(pSchemaFilePath)
Dim oElements = GetSchemaElements(pSchemaFilePath)
Public Function UpdateSchemaFromFile(pSchema As Schema, pInputDirectory As String) As Schema
Dim oFullPath = Path.Combine(pInputDirectory, pSchema.FileName)
oSchema.Name = oFileInfo.Name
If Not IO.File.Exists(oFullPath) Then
Throw New FileNotFoundException(oFullPath)
End If
Dim oElements = GetSchemaElements(oFullPath)
For Each oElement In oElements
Dim oColumns = GetElementColumns(oElement)
@@ -69,17 +123,17 @@ Namespace Schemas
oSchemaColumns.Add(oSchemaColumn)
Next
oSchema.Tables.Add(New Schema.Table With {
pSchema.Tables.Add(New Schema.Table With {
.Name = XmlData.GetElementAttribute(oElement, "name"),
.Columns = oSchemaColumns
})
Next
Return oSchema
Return pSchema
End Function
Public Function UpdateSchemaWithDatabaseConfiguration(pSchema As Schema, pTemplateConfig As Winline.Configuration) As Schema
Public Function UpdateSchemaFromDatabase(pSchema As Schema, pTemplateConfig As Configuration) As Schema
If pTemplateConfig Is Nothing Then
Return pSchema
End If
@@ -89,7 +143,7 @@ Namespace Schemas
Dim oConfig = pTemplateConfig.GetColumn(oColumn.Name)
If oConfig Is Nothing Then
oConfig = New Winline.ColumnConfig With {
oConfig = New ColumnConfig With {
.IsRequired = oColumn.IsRequired,
.Name = oColumn.Name
}