2021-11-16 16:16:46 +01:00

143 lines
5.1 KiB
VB.net

Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports DigitalData.Modules.Logging
Namespace Schemas
Public Class SchemaLoader
Inherits BaseClass
Private ReadOnly ns As XNamespace = "http://www.w3.org/2001/XMLSchema"
Public SchemaList As List(Of FileInfo)
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig, pLogConfig.GetLogger)
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)
Try
Dim oDirectory As New DirectoryInfo(pSchemaDirectory)
Dim oFiles = oDirectory.GetFiles()
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
SchemaList = oFiles.ToList()
Return True
Catch ex As Exception
Logger.Error(ex)
Throw New IO.IOException($"Could not load files from directory {pSchemaDirectory}", ex)
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)
oSchema.Name = oFileInfo.Name
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
oSchema.Tables.Add(New Schema.Table With {
.Name = XmlData.GetElementAttribute(oElement, "name"),
.Columns = oSchemaColumns
})
Next
Return oSchema
End Function
Public Function UpdateSchemaWithDatabaseConfiguration(pSchema As Schema, pTemplateConfig As Winline.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 Winline.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