197 lines
7.5 KiB
VB.net
197 lines
7.5 KiB
VB.net
Imports System.IO
|
|
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Logging
|
|
Imports MultiTool.Shared.Winline
|
|
Imports MultiTool.Shared.Helpers
|
|
|
|
Namespace Schemas
|
|
Public Class SchemaLoader
|
|
Inherits BaseClass
|
|
|
|
Private ReadOnly ns As XNamespace = "http://www.w3.org/2001/XMLSchema"
|
|
|
|
Public SchemaList As List(Of Schema)
|
|
Public TemplateConfiguration As New Configuration
|
|
|
|
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 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 Schema)
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
Return False
|
|
|
|
End Try
|
|
End Function
|
|
|
|
Public Function UpdateSchemaFromFile(pSchema As Schema, pInputDirectory As String) As Schema
|
|
Dim oFullPath = Path.Combine(pInputDirectory, pSchema.FileName)
|
|
|
|
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)
|
|
Dim oSchemaColumns As New List(Of Schema.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 oSchemaColumn As New Schema.Column With {
|
|
.Name = oName,
|
|
.DataType = oType,
|
|
.IsRequired = oRequired
|
|
}
|
|
oSchemaColumns.Add(oSchemaColumn)
|
|
Next
|
|
|
|
pSchema.Tables.Add(New Schema.Table With {
|
|
.Name = XmlData.GetElementAttribute(oElement, "name"),
|
|
.Columns = oSchemaColumns
|
|
})
|
|
|
|
Next
|
|
|
|
Return pSchema
|
|
End Function
|
|
|
|
Public Function UpdateSchemaFromDatabase(pSchema As Schema, pTemplateConfig As Configuration) As Schema
|
|
If pTemplateConfig Is Nothing Then
|
|
Return pSchema
|
|
End If
|
|
|
|
For Each oTable In pSchema.Tables
|
|
For Each oColumn As Schema.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 pSchema
|
|
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.SCHEMA_TYPE_DATE
|
|
Return Constants.ColumnType.Date
|
|
Case Constants.SCHEMA_TYPE_INTEGER
|
|
Return Constants.ColumnType.Integer
|
|
Case Constants.SCHEMA_TYPE_DECIMAL
|
|
Return Constants.ColumnType.Decimal
|
|
Case Constants.SCHEMA_TYPE_BOOLEAN
|
|
Return Constants.ColumnType.Boolean
|
|
Case Else
|
|
Return Constants.ColumnType.String
|
|
End Select
|
|
End Function
|
|
|
|
Public Function GetSchemaElements(pSchemaFilePath As String) As List(Of XElement)
|
|
Dim oText As String = IO.File.ReadAllText(pSchemaFilePath)
|
|
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
|