Imports System.Text.RegularExpressions
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
'''
''' Replaces placeholders in an sql marked as final sql in the template.
'''
'''
''' This SQL does not replace field-value placeholders!
''' This is a globally applied function!
'''
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
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.FileName
Case "FILENAME_REPORT"
oValue = pReportFileName
Case Else
oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
End Select
If oValue <> "" Then
pString = pString.Replace(oPlaceholderString, oValue)
End If
End Select
Next
Return pString
End Function
'''
'''
'''
'''
'''
'''
'''
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
Dim oPlaceholderType As String = oMatch.Groups.Item(1)?.Value
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
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 "FILENAME"
oValue = pDocument.FileName
Case Else
oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
End Select
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
Case "ACCOUNTNUMBER"
oValue = pDocument.Account.Id
Case "RUNNINGNUMBER"
oValue = pDocument.RunningNumber
Case Else
oValue = ReplaceBasicValues(oPlaceholderValue, pMandator)
End Select
If oValue <> "" Then
oString = oString.Replace(oPlaceholderString, oValue)
End If
End Select
Next
Return oString
End Function
'''
''' Replaces basic values in the CONST namespace that only depend on the current mandator
'''
''' Value that will be used to replaced the placeholder
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