From ce78dea8cbdda4dd94584849ce5bf4c77809506c Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 22 Mar 2022 13:58:11 +0100 Subject: [PATCH] Add FIELD Function, fix virtual columns --- MultiTool.Shared/Documents/DocumentLoader.vb | 36 +++++++++++--------- MultiTool.Shared/Documents/DocumentRow.vb | 17 ++++++--- MultiTool.Shared/Templates/Template.vb | 6 ++++ MultiTool.Shared/Templates/TemplateLoader.vb | 31 +++++++++++++++-- 4 files changed, 67 insertions(+), 23 deletions(-) diff --git a/MultiTool.Shared/Documents/DocumentLoader.vb b/MultiTool.Shared/Documents/DocumentLoader.vb index 52c26c4..11bfe04 100644 --- a/MultiTool.Shared/Documents/DocumentLoader.vb +++ b/MultiTool.Shared/Documents/DocumentLoader.vb @@ -1,6 +1,7 @@ Imports System.IO Imports System.Text.RegularExpressions Imports DigitalData.Modules.Logging +Imports DigitalData.Modules.Language Imports MultiTool.Shared.Exceptions Imports MultiTool.Shared.Templates Imports MultiTool.Shared.Winline @@ -102,14 +103,12 @@ Namespace Documents ''' Loads a single document from the FullName Property in the Document Object ''' ''' - ''' ''' A document might look like this: ''' ''' ''' ''' ''' - ''' ''' Private Function LoadDocumentData(pDocument As Document, pTemplate As Template, pTemplateConfig As TemplateConfig) As Document Dim oText As String = IO.File.ReadAllText(pDocument.FullName) @@ -197,17 +196,6 @@ Namespace Documents oColumnSortKey += 1 Next - ' Create Virtual fields - Dim oVirtualColumns = pTemplateConfig.Items.Where(Function(item) item.IsVirtual And item.Table = oTable.Name).ToList() - For Each oColumn In oVirtualColumns - oFields.Add(oColumn.Name, New DocumentRow.FieldValue With { - .DataType = oColumn.Type, - .IsRequired = oColumn.IsRequired, - .SortKey = oColumn.OrderKey, - .IsVirtual = True - }) - Next - ' Create a DocumentRow object for each Top Level Element Dim oRow = New DocumentRow With { .SortKey = oRowSortKey, @@ -335,7 +323,7 @@ Namespace Documents Private Function ApplyFieldFunctionForImport(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Document For Each oRow As DocumentRow In pDocument.Rows - Dim oTable = pTemplate.Tables.Where(Function(table) table.Name = oRow.TableName).SingleOrDefault() + Dim oTable = pDocument.Schema.Tables.Where(Function(table) table.Name = oRow.TableName).SingleOrDefault() For Each oField In oRow.Fields If oTable Is Nothing Then @@ -357,10 +345,26 @@ Namespace Documents If IsNothing(oParam) Then Logger.Warn("FIELD function needs exactly one parameter!") - Return pDocument + Continue For End If + Dim oFieldName = oParam.Key + Dim oSubKey = oParam.Value + + Dim oReferencedField = oRow.Fields. + Where(Function(field) field.Key = oFieldName). + FirstOrDefault() + If IsNothing(oReferencedField) = False Then + Dim oValue As String = Utils.NotNull(oReferencedField.Value.GetValue(oSubKey), String.Empty) + + If oValue <> String.Empty Then + oField.Value.Final = oValue + End If + Else + Logger.Warn("Referenced Field [{0}] was not found!", oFieldName) + Continue For + End If End If Next @@ -400,8 +404,6 @@ Namespace Documents field.Value.Final = oMapping.DestinationValue End Sub) - - Else ' don't do anything diff --git a/MultiTool.Shared/Documents/DocumentRow.vb b/MultiTool.Shared/Documents/DocumentRow.vb index 525c3d5..999a99d 100644 --- a/MultiTool.Shared/Documents/DocumentRow.vb +++ b/MultiTool.Shared/Documents/DocumentRow.vb @@ -65,13 +65,22 @@ Public Property IsVirtual As Boolean = False Public Property SortKey As Integer = 0 + Public Function GetValue(pValueType) As String + Select Case pValueType + Case "Original" + Return Original + Case "External" + Return External + Case "Final" + Return Final + Case Else + Return Nothing + End Select + End Function + Public ReadOnly Property HasError As Boolean Get Return [Error] <> FieldError.None Or (IsRequired And Final = String.Empty) - - 'Return IsRequired = True And ( - ' [Error] <> FieldError.None Or Final = String.Empty - ') End Get End Property diff --git a/MultiTool.Shared/Templates/Template.vb b/MultiTool.Shared/Templates/Template.vb index 65975e6..d2d6b13 100644 --- a/MultiTool.Shared/Templates/Template.vb +++ b/MultiTool.Shared/Templates/Template.vb @@ -12,6 +12,9 @@ Namespace Templates Public Property Parameter2 As String Public Property FinalSQL As String + ''' + ''' Tabledata from XSD + ''' Public Property Tables As New List(Of Table) Public Property InputDirectory As String @@ -68,6 +71,9 @@ Namespace Templates End If End Function + ''' + ''' Table from XSD + ''' Class Table Public Property Name As String Public Property Columns As New List(Of Column) diff --git a/MultiTool.Shared/Templates/TemplateLoader.vb b/MultiTool.Shared/Templates/TemplateLoader.vb index c70b062..2d8129c 100644 --- a/MultiTool.Shared/Templates/TemplateLoader.vb +++ b/MultiTool.Shared/Templates/TemplateLoader.vb @@ -240,9 +240,11 @@ Namespace Templates Return pTemplate End If - For Each oTable In pTemplate.Tables + Dim oTemplate = CreateVirtualColumns(pTemplate, pTemplateConfig) + + For Each oTable In oTemplate.Tables For Each oColumn As Template.Column In oTable.Columns - Dim oConfig = pTemplateConfig.GetColumn(oColumn.Name, oTable.Name) + Dim oConfig As TemplateConfigItem = pTemplateConfig.GetColumn(oColumn.Name, oTable.Name) If oConfig Is Nothing Then oConfig = New TemplateConfigItem With { @@ -255,6 +257,31 @@ Namespace Templates Next Next + Return oTemplate + End Function + + Private Function CreateVirtualColumns(pTemplate As Template, pTemplateConfig As TemplateConfig) As Template + For Each oConfigItem In pTemplateConfig.Items + ' Find the table that relates to this config item + Dim oTable = pTemplate.Tables.Where(Function(table) table.Name = oConfigItem.Table).FirstOrDefault() + + If oTable Is Nothing Then + Logger.Warn("Table [{0}] for item [{1}] does exist in this Template!", oConfigItem.Table, oConfigItem.Name) + Continue For + End If + + Dim oColumnExists = oTable.Columns.Any(Function(column) column.Name = oConfigItem.Name) + + If oColumnExists = False And oConfigItem.IsVirtual = True Then + oTable.Columns.Add(New Template.Column() With { + .Name = oConfigItem.Name, + .Config = oConfigItem, + .DataType = Constants.ColumnType.String, + .IsRequired = False + }) + End If + Next + Return pTemplate End Function