Execute SQL function when loading document

This commit is contained in:
Jonathan Jenne
2022-05-03 12:29:08 +02:00
parent 623e1717d6
commit 5fe4079028
4 changed files with 57 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ Imports MultiTool.Common.Templates
Imports MultiTool.Common.Winline Imports MultiTool.Common.Winline
Imports MultiTool.Common.Winline.Entities Imports MultiTool.Common.Winline.Entities
Imports MultiTool.Common.Constants Imports MultiTool.Common.Constants
Imports DigitalData.Modules.Database
Namespace Documents Namespace Documents
Public Class DocumentLoader Public Class DocumentLoader
@@ -16,6 +17,7 @@ Namespace Documents
Private ReadOnly MappingConfig As MappingConfig Private ReadOnly MappingConfig As MappingConfig
Private ReadOnly TemplateConfig As TemplateConfig Private ReadOnly TemplateConfig As TemplateConfig
Private ReadOnly ApplicationConfig As Config Private ReadOnly ApplicationConfig As Config
Private ReadOnly Patterns As Patterns
Public Property Files As New List(Of Document) Public Property Files As New List(Of Document)
@@ -45,12 +47,13 @@ Namespace Documents
End Sub End Sub
End Class End Class
Public Sub New(pLogConfig As LogConfig, pWinline As WinlineData, pMappingConfig As MappingConfig, pTemplateConfig As TemplateConfig, pApplicationConfig As Config) Public Sub New(pLogConfig As LogConfig, pWinline As WinlineData, pDatabase As MSSQLServer, pMappingConfig As MappingConfig, pTemplateConfig As TemplateConfig, pGeneralConfig As GeneralConfig, pApplicationConfig As Config)
MyBase.New(pLogConfig) MyBase.New(pLogConfig, pDatabase)
Winline = pWinline Winline = pWinline
MappingConfig = pMappingConfig MappingConfig = pMappingConfig
TemplateConfig = pTemplateConfig TemplateConfig = pTemplateConfig
ApplicationConfig = pApplicationConfig ApplicationConfig = pApplicationConfig
Patterns = New Patterns(pLogConfig, pGeneralConfig)
End Sub End Sub
Public Async Function LoadFiles(pTemplate As Template, pMandator As Mandator) As Task(Of Boolean) Public Async Function LoadFiles(pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
@@ -314,34 +317,60 @@ Namespace Documents
' Set mandator befor applying any functions that depend on a valid mandator ' Set mandator befor applying any functions that depend on a valid mandator
pDocument.Mandator = oMandator pDocument.Mandator = oMandator
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With { RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {.RunningFunction = "Winline-Funktionen"})
.RunningFunction = "Winline-Funktionen"
})
pDocument = Await ApplyDefinedItemFunctionsForImportAsync(pDocument, oMandator, pTemplate) pDocument = Await ApplyDefinedItemFunctionsForImportAsync(pDocument, oMandator, pTemplate)
pDocument = ApplyDynamicItemFunctionsForImport(pDocument, oMandator) pDocument = ApplyDynamicItemFunctionsForImport(pDocument, oMandator)
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With { RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {.RunningFunction = "Preis-Funktionen"})
.RunningFunction = "Preis-Funktionen"
})
If ApplicationConfig.AutomaticPriceCalculation = True Then If ApplicationConfig.AutomaticPriceCalculation = True Then
' These functions will only be applied if the document does not have errors ' These functions will only be applied if the document does not have errors
pDocument = Await MaybeApplyPriceFunctions(pDocument, oMandator, pTemplate) pDocument = Await MaybeApplyPriceFunctions(pDocument, oMandator, pTemplate)
End If End If
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With { RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {.RunningFunction = "Feld-Funktionen"})
.RunningFunction = "Feld-Funktionen"
})
pDocument = ApplySQLFunctionForImport(pDocument, TemplateConfig.SqlItems)
' This function needs to be the last one because ' This function needs to be the last one because
' it can relate to any previously set value ' it can relate to any previously set value
ApplyFieldFunctionForImport(pDocument, oMandator, pTemplate) pDocument = ApplyFieldFunctionForImport(pDocument, oMandator, pTemplate)
End If End If
Return pDocument Return pDocument
End Function End Function
Private Function ApplySQLFunctionForImport(pDocument As Document, pSQLConfig As List(Of TemplateConfigItem)) As Document
For Each oSQLConfigItem In pSQLConfig
' FieldList is a list of fields that will be changed
' Example: Setting SQL for Article StorageLocation will invoke the sql for each row
Dim oRowList = pDocument.Rows.
Where(Function(row) row.Fields.Any(Function(field) field.Key = oSQLConfigItem.Name)).
ToList()
For Each oRow As DocumentRow In oRowList
Dim oSQL = oSQLConfigItem.Function.Params
Dim oField = oRow.Fields.
Where(Function(field) field.Key = oSQLConfigItem.Name).
SingleOrDefault()
oSQL = Patterns.ReplaceForImport(pDocument, oRow, oSQL)
Dim oValue = Database.GetScalarValue(oSQL)
If oValue IsNot Nothing Then
oField.Value.External = oValue
oField.Value.Final = oValue
End If
Next
Next
Return pDocument
End Function
''' <summary> ''' <summary>
''' Apply price calculation to the documents products ''' Apply price calculation to the documents products
''' '''

View File

@@ -56,11 +56,7 @@ Public Class ReportGenerator(Of TReport As IReport)
{"Lagerstand", "Text10"} {"Lagerstand", "Text10"}
}) })
Dim oSQLConfig = TemplateConfig.Items. pDocument = FillFieldValuesFromSQL(pDocument, TemplateConfig.SqlItems, oFilePath)
Where(Function(item) item.Function.Name = Constants.FUNCTION_SQL).
ToList()
pDocument = FillFieldValuesFromSQL(pDocument, oSQLConfig, oFilePath)
Dim oHeadRow = pDocument.Rows. Dim oHeadRow = pDocument.Rows.
Where(Function(r) r.TableName.EndsWith("T025")). Where(Function(r) r.TableName.EndsWith("T025")).
@@ -116,12 +112,12 @@ Public Class ReportGenerator(Of TReport As IReport)
ToList() ToList()
For Each oRow As DocumentRow In oRowList For Each oRow As DocumentRow In oRowList
Dim oSQL = oSQLConfigItem.Function.Params
Dim oField = oRow.Fields. Dim oField = oRow.Fields.
Where(Function(field) field.Key = oSQLConfigItem.Name). Where(Function(field) field.Key = oSQLConfigItem.Name).
SingleOrDefault() SingleOrDefault()
Dim oSQL = oSQLConfigItem.Function.Params oSQL = Patterns.ReplaceForImportFinalSQL(pDocument, pReportFileName, oSQL)
oSQL = Patterns.ReplaceForImport(pDocument, oRow, pReportFileName, oSQL)
Dim oValue = Database.GetScalarValue(oSQL) Dim oValue = Database.GetScalarValue(oSQL)

View File

@@ -8,6 +8,14 @@ Namespace Templates
Public Class TemplateConfig Public Class TemplateConfig
Public Property Items As List(Of TemplateConfigItem) Public Property Items As List(Of TemplateConfigItem)
Public ReadOnly Property SqlItems As List(Of TemplateConfigItem)
Get
Return Items.
Where(Function(item) item.Function.Name = Constants.FUNCTION_SQL).
ToList()
End Get
End Property
Public Function GetColumn(pName As String, pTable As String) As TemplateConfigItem Public Function GetColumn(pName As String, pTable As String) As TemplateConfigItem
Return Items. Return Items.
Where(Function(c) c.Name = pName And c.Table = pTable). Where(Function(c) c.Name = pName And c.Table = pTable).

View File

@@ -91,7 +91,11 @@ Public Class frmImportMain
lookupMandator.ForceInitialize() lookupMandator.ForceInitialize()
lookupMandator.Properties.View.BestFitColumns() lookupMandator.Properties.View.BestFitColumns()
DocumentLoader = New DocumentLoader(LogConfig, Winline, My.MappingConfiguration, My.TemplateConfiguration, ConfigManager.Config) DocumentLoader = New DocumentLoader(LogConfig, Winline, Database,
My.MappingConfiguration,
My.TemplateConfiguration,
My.GeneralConfiguration,
ConfigManager.Config)
DocumentCleaner = New DocumentCleaner(LogConfig, CurrentTemplate) DocumentCleaner = New DocumentCleaner(LogConfig, CurrentTemplate)
GridLoader = New GridLoader(LogConfig) GridLoader = New GridLoader(LogConfig)