Rework Patterns to allow for row based and final sql replacement when importing

This commit is contained in:
Jonathan Jenne 2022-04-26 16:19:17 +02:00
parent 371c8e27cc
commit fa4ac39074
5 changed files with 104 additions and 47 deletions

View File

@ -10,6 +10,9 @@
Public Const FUNCTION_FIELD = "FIELD"
Public Const FUNCTION_RUNNINGNUMBER = "RUNNINGNUMBER"
Public Const PLACEHOLDER_CONST = "CONST"
Public Const PLACEHOLDER_FIELD = "FIELD"
Public Const TEMPLATE_TYPE_DATE = "xs:date"
Public Const TEMPLATE_TYPE_INTEGER = "xs:integer"
Public Const TEMPLATE_TYPE_DECIMAL = "xs:decimal"

View File

@ -43,6 +43,7 @@ Namespace Documents
Dim oDocumentErrors As List(Of String) = DocumentErrors.
Select(Function(err) err.ToString()).
ToList()
Return oDocumentErrors.
Concat(oRowErrors).
ToList()

View File

@ -2,21 +2,29 @@
Imports DigitalData.Modules.Logging
Imports MultiTool.Common.Documents
Imports MultiTool.Common.Templates
Imports MultiTool.Common.Constants
Imports MultiTool.Common.Winline.Entities
Public Class Patterns
Inherits BaseClass
Private ReadOnly GeneralConfig As GeneralConfig
Private ReadOnly Regex As New Regex("{#(\w+)#([\w\s_-]+)}+")
Public Sub New(pLogConfig As LogConfig, pGeneralConfig As GeneralConfig)
MyBase.New(pLogConfig)
GeneralConfig = pGeneralConfig
End Sub
Public Function ReplaceForImport(pDocument As Documents.Document, pRow As DocumentRow, pReportFileName As String, oString As String)
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oMatches As MatchCollection = oRegex.Matches(oString)
''' <summary>
''' Replaces placeholders in an sql marked as final sql in the template.
''' </summary>
''' <remarks>
''' This SQL does not replace field-value placeholders!
''' This is a globally applied function!
''' </remarks>
Public Function ReplaceForImportFinalSQL(pDocument As Document, pReportFileName As String, pString As String) As String
Dim oMatches As MatchCollection = Regex.Matches(pString)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
@ -24,56 +32,39 @@ Public Class Patterns
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case "FIELD"
Dim oFieldName = oPlaceholderValue
Dim oTargetField = pRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
oString = oString.Replace(oPlaceholderString, oTargetField.Value.Final)
Case "CONST"
Case PLACEHOLDER_CONST
Dim oValue = ""
Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR"
oValue = GeneralConfig.GetWinLineYear()
Case "MESOCOMP"
oValue = pDocument.Mandator.Id
Case "USERNAME"
oValue = Environment.UserName
Case "CURRENTDATE"
oValue = Now.ToString()
Case "FILENAME"
oValue = pDocument.FileName
Case "FILENAME_REPORT"
oValue = pReportFileName
Case "MANDATORDB"
oValue = pDocument.Mandator.Database
Case Else
oValue = ""
oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
End Select
If oValue <> "" Then
oString = oString.Replace(oPlaceholderString, oValue)
pString = pString.Replace(oPlaceholderString, oValue)
End If
End Select
Next
Return oString
Return pString
End Function
Public Function ReplaceForExport(pDocument As ExportDocument, pMandator As Mandator, oString As String)
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oMatches As MatchCollection = oRegex.Matches(oString)
''' <summary>
'''
''' </summary>
''' <param name="pDocument"></param>
''' <param name="pRow"></param>
''' <param name="pString"></param>
''' <returns></returns>
Public Function ReplaceForImport(pDocument As Document, pRow As DocumentRow, pString As String)
Dim oMatches As MatchCollection = Regex.Matches(pString)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
@ -81,22 +72,48 @@ Public Class Patterns
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case "CONST"
Case PLACEHOLDER_FIELD
Dim oFieldName = oPlaceholderValue
Dim oTargetField = pRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
pString = pString.Replace(oPlaceholderString, oTargetField.Value.Final)
Case PLACEHOLDER_CONST
Dim oValue = ""
Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR"
oValue = GeneralConfig.GetWinLineYear()
Case "FILENAME"
oValue = pDocument.FileName
Case "MESOCOMP"
oValue = pMandator.Id
Case Else
oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
Case "USERNAME"
oValue = Environment.UserName
End Select
Case "CURRENTDATE"
oValue = Now.ToString()
If oValue <> "" Then
pString = pString.Replace(oPlaceholderString, oValue)
End If
End Select
Next
Return pString
End Function
Public Function ReplaceForExport(pDocument As ExportDocument, pMandator As Mandator, oString As String)
Dim oMatches As MatchCollection = Regex.Matches(oString)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
Dim oPlaceholderType As String = oMatch.Groups.Item(1)?.Value
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case PLACEHOLDER_CONST
Dim oValue = ""
Select Case oPlaceholderValue.ToUpper
Case "FILENAME"
oValue = pDocument.FilenameExport
@ -106,11 +123,9 @@ Public Class Patterns
Case "RUNNINGNUMBER"
oValue = pDocument.RunningNumber
Case "MANDATORDB"
oValue = pMandator.Database
Case Else
oValue = ""
oValue = ReplaceBasicValues(oPlaceholderValue, pMandator)
End Select
@ -122,4 +137,31 @@ Public Class Patterns
Return oString
End Function
''' <summary>
''' Replaces basic values in the CONST namespace that only depend on the current mandator
''' </summary>
''' <returns>Value that will be used to replaced the placeholder</returns>
Public Function ReplaceBasicValues(pPlaceholderValue As String, pMandator As Mandator) As String
Select Case pPlaceholderValue.ToUpper
Case "MESOYEAR"
Return GeneralConfig.GetWinLineYear()
Case "MESOCOMP"
Return pMandator.Id
Case "USERNAME"
Return Environment.UserName
Case "MANDATORDB"
Return pMandator.Database
Case "CURRENTDATE"
Return Now.ToString()
Case Else
Return String.Empty
End Select
End Function
End Class

View File

@ -965,7 +965,7 @@ Namespace Winline
End If
End Function
Public Async Function ExecuteFinalSQL(pDocument As ExportDocument, pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
Public Async Function ExecuteFinalSQLForExport(pDocument As ExportDocument, pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
Try
Dim oSql As String = Patterns.ReplaceForExport(pDocument, pMandator, pTemplate.FinalSQL)
Return Await Database.ExecuteNonQueryAsync(oSql)
@ -976,6 +976,17 @@ Namespace Winline
End Try
End Function
Public Async Function ExecuteFinalSQLForImport(pDocument As Document, pTemplate As Template, pReportFilename As String) As Task(Of Boolean)
Try
Dim oSql As String = Patterns.ReplaceForImportFinalSQL(pDocument, pReportFilename, pTemplate.FinalSQL)
Return Await Database.ExecuteNonQueryAsync(oSql)
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function GetDocumentFromDataRow(pDataRow As DataRow) As ExportDocument
Dim oAccountNumber = pDataRow.Item("ACCOUNT_NUMBER")
Dim oRunningNumber As String = pDataRow.Item("RUNNING_NUMBER")

View File

@ -245,7 +245,7 @@ Public Class frmExportMain
SplashScreenManager.SetWaitFormDescription(oMessage)
Await WebService.ExportDocumentFromWinline(oDocument, CurrentTemplate, oMandator)
Dim oFinalSqlResult = Await Winline.ExecuteFinalSQL(oDocument, CurrentTemplate, oMandator)
Dim oFinalSqlResult = Await Winline.ExecuteFinalSQLForExport(oDocument, CurrentTemplate, oMandator)
If oFinalSqlResult = False Then
Throw New DatabaseException("FinalSQL was not executed successfully!")