67 lines
2.2 KiB
VB.net

Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Logging
Imports MultiTool.Shared.Documents
Imports MultiTool.Shared.Templates
Public Class Patterns
Inherits BaseClass
Private GeneralConfig As GeneralConfig
Public Sub New(pLogConfig As LogConfig, pGeneralConfig As GeneralConfig)
MyBase.New(pLogConfig)
GeneralConfig = pGeneralConfig
End Sub
Public Function Replace(pDocument As Document, pRow As DocumentRow, oString As String)
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oMatches As MatchCollection = oRegex.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 "FIELD"
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 = ""
Select Case oMatch.Groups.Item(2).Value.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 Else
oValue = ""
End Select
If oValue <> "" Then
oString = oString.Replace(oPlaceholderValue, oValue)
End If
End Select
Next
Return oString
End Function
End Class