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_FIELD = "FIELD"
Public Const FUNCTION_RUNNINGNUMBER = "RUNNINGNUMBER" 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_DATE = "xs:date"
Public Const TEMPLATE_TYPE_INTEGER = "xs:integer" Public Const TEMPLATE_TYPE_INTEGER = "xs:integer"
Public Const TEMPLATE_TYPE_DECIMAL = "xs:decimal" Public Const TEMPLATE_TYPE_DECIMAL = "xs:decimal"

View File

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

View File

@ -2,21 +2,29 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports MultiTool.Common.Documents Imports MultiTool.Common.Documents
Imports MultiTool.Common.Templates Imports MultiTool.Common.Templates
Imports MultiTool.Common.Constants
Imports MultiTool.Common.Winline.Entities Imports MultiTool.Common.Winline.Entities
Public Class Patterns Public Class Patterns
Inherits BaseClass Inherits BaseClass
Private ReadOnly GeneralConfig As GeneralConfig Private ReadOnly GeneralConfig As GeneralConfig
Private ReadOnly Regex As New Regex("{#(\w+)#([\w\s_-]+)}+")
Public Sub New(pLogConfig As LogConfig, pGeneralConfig As GeneralConfig) Public Sub New(pLogConfig As LogConfig, pGeneralConfig As GeneralConfig)
MyBase.New(pLogConfig) MyBase.New(pLogConfig)
GeneralConfig = pGeneralConfig GeneralConfig = pGeneralConfig
End Sub End Sub
Public Function ReplaceForImport(pDocument As Documents.Document, pRow As DocumentRow, pReportFileName As String, oString As String) ''' <summary>
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+") ''' Replaces placeholders in an sql marked as final sql in the template.
Dim oMatches As MatchCollection = oRegex.Matches(oString) ''' </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 For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value 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 Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper Select Case oPlaceholderType.ToUpper
Case "FIELD" Case PLACEHOLDER_CONST
Dim oFieldName = oPlaceholderValue
Dim oTargetField = pRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
oString = oString.Replace(oPlaceholderString, oTargetField.Value.Final)
Case "CONST"
Dim oValue = "" Dim oValue = ""
Select Case oPlaceholderValue.ToUpper 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" Case "FILENAME"
oValue = pDocument.FileName oValue = pDocument.FileName
Case "FILENAME_REPORT" Case "FILENAME_REPORT"
oValue = pReportFileName oValue = pReportFileName
Case "MANDATORDB"
oValue = pDocument.Mandator.Database
Case Else Case Else
oValue = "" oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
End Select End Select
If oValue <> "" Then If oValue <> "" Then
oString = oString.Replace(oPlaceholderString, oValue) pString = pString.Replace(oPlaceholderString, oValue)
End If End If
End Select End Select
Next Next
Return oString Return pString
End Function End Function
Public Function ReplaceForExport(pDocument As ExportDocument, pMandator As Mandator, oString As String) ''' <summary>
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+") '''
Dim oMatches As MatchCollection = oRegex.Matches(oString) ''' </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 For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value 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 Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper 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 = "" Dim oValue = ""
Select Case oPlaceholderValue.ToUpper Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR" Case "FILENAME"
oValue = GeneralConfig.GetWinLineYear() oValue = pDocument.FileName
Case "MESOCOMP" Case Else
oValue = pMandator.Id oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
Case "USERNAME" End Select
oValue = Environment.UserName
Case "CURRENTDATE" If oValue <> "" Then
oValue = Now.ToString() 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" Case "FILENAME"
oValue = pDocument.FilenameExport oValue = pDocument.FilenameExport
@ -106,11 +123,9 @@ Public Class Patterns
Case "RUNNINGNUMBER" Case "RUNNINGNUMBER"
oValue = pDocument.RunningNumber oValue = pDocument.RunningNumber
Case "MANDATORDB"
oValue = pMandator.Database
Case Else Case Else
oValue = "" oValue = ReplaceBasicValues(oPlaceholderValue, pMandator)
End Select End Select
@ -122,4 +137,31 @@ Public Class Patterns
Return oString Return oString
End Function 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 End Class

View File

@ -965,7 +965,7 @@ Namespace Winline
End If End If
End Function 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 Try
Dim oSql As String = Patterns.ReplaceForExport(pDocument, pMandator, pTemplate.FinalSQL) Dim oSql As String = Patterns.ReplaceForExport(pDocument, pMandator, pTemplate.FinalSQL)
Return Await Database.ExecuteNonQueryAsync(oSql) Return Await Database.ExecuteNonQueryAsync(oSql)
@ -976,6 +976,17 @@ Namespace Winline
End Try End Try
End Function 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 Private Function GetDocumentFromDataRow(pDataRow As DataRow) As ExportDocument
Dim oAccountNumber = pDataRow.Item("ACCOUNT_NUMBER") Dim oAccountNumber = pDataRow.Item("ACCOUNT_NUMBER")
Dim oRunningNumber As String = pDataRow.Item("RUNNING_NUMBER") Dim oRunningNumber As String = pDataRow.Item("RUNNING_NUMBER")

View File

@ -245,7 +245,7 @@ Public Class frmExportMain
SplashScreenManager.SetWaitFormDescription(oMessage) SplashScreenManager.SetWaitFormDescription(oMessage)
Await WebService.ExportDocumentFromWinline(oDocument, CurrentTemplate, oMandator) 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 If oFinalSqlResult = False Then
Throw New DatabaseException("FinalSQL was not executed successfully!") Throw New DatabaseException("FinalSQL was not executed successfully!")