Patterns: Add Controls and Windream Modules

This commit is contained in:
Jonathan Jenne 2021-11-04 14:19:21 +01:00
parent e5f7fcd05a
commit bd176e3de0
5 changed files with 222 additions and 61 deletions

View File

@ -3,9 +3,11 @@ Imports System.Text.RegularExpressions
Namespace [PatternModule] Namespace [PatternModule]
Public Class BaseModule Public Class BaseModule
Friend ReadOnly Logger As Logger Private ReadOnly Logger As Logger
Private ReadOnly MyRegex As Regex = New Regex("{#(\w+)#([\:\.\w\s_-]+)}+") Private ReadOnly MyRegex As Regex = New Regex("{#(\w+)#([\:\.\w\s_-]+)}+")
Private ReadOnly SqlPhrases As New List(Of String) From {
"SELECT ", "UPDATE ", "DELETE ", "EXEC "
}
Private Const MAX_TRY_COUNT = 500 Private Const MAX_TRY_COUNT = 500
@ -21,23 +23,37 @@ Namespace [PatternModule]
pCounter += 1 pCounter += 1
End Sub End Sub
Public Function ReplacePattern(input As String, type As String, replacement As String) As String Public Function ReplacePattern(pInput As String, pType As String, pReplacement As String) As String
Dim elements As MatchCollection = MyRegex.Matches(input) Dim oElements As MatchCollection = MyRegex.Matches(pInput)
If IsNothing(replacement) Then If IsNothing(pReplacement) Then
Return input Return pInput
End If End If
For Each element As Match In elements Dim oIsSQL As Boolean = False
For Each oPhrase In SqlPhrases
If pInput.Contains(oPhrase) Then
oIsSQL = True
Exit For
End If
Next
If oIsSQL = True Then
Logger.Debug("Input string is most likely an SQL Query, masking quotes in replacement string.")
pReplacement = pReplacement.Replace("'", "''")
End If
For Each oElement As Match In oElements
' if group 1 contains the 'pattern' the replace whole group with 'replacement' ' if group 1 contains the 'pattern' the replace whole group with 'replacement'
' and return it ' and return it
If element.Groups(1).Value = type Then If oElement.Groups(1).Value = pType Then
Return Regex.Replace(input, element.Groups(0).Value, replacement) Logger.Debug("Replacing Placeholder with [{0}]", pReplacement)
Return Regex.Replace(pInput, oElement.Groups(0).Value, pReplacement)
End If End If
Next Next
' no replacement made ' no replacement made
Return input Return pInput
End Function End Function
Public Function ContainsPatternAndValue(pInput As String, pType As String, pValue As String) As Boolean Public Function ContainsPatternAndValue(pInput As String, pType As String, pValue As String) As Boolean
@ -45,10 +61,10 @@ Namespace [PatternModule]
For Each oElement As Match In oElements For Each oElement As Match In oElements
' Pattern in pInput ' Pattern in pInput
Dim t As String = oElement.Groups(1).Value Dim oType As String = oElement.Groups(1).Value
Dim v As String = oElement.Groups(2).Value Dim oValue As String = oElement.Groups(2).Value
If t = pType And v = pValue Then If oType = pType And oValue = pValue Then
Return True Return True
End If End If
Next Next
@ -84,16 +100,16 @@ Namespace [PatternModule]
Return False Return False
End Function End Function
Public Function GetNextPattern(input As String, type As String) As Pattern Public Function GetNextPattern(pInput As String, pType As String) As Pattern
Dim elements As MatchCollection = MyRegex.Matches(input) Dim oElements As MatchCollection = MyRegex.Matches(pInput)
For Each element As Match In elements For Each oElement As Match In oElements
' Pattern in pInput ' Pattern in pInput
Dim t As String = element.Groups(1).Value Dim oType As String = oElement.Groups(1).Value
Dim v As String = element.Groups(2).Value Dim oValue As String = oElement.Groups(2).Value
If t = type Then If oType = pType Then
Return New Pattern(t, v) Return New Pattern(oType, oValue)
End If End If
Next Next

View File

@ -4,55 +4,62 @@ Imports DigitalData.Modules.Logging
Namespace [PatternModule] Namespace [PatternModule]
''' <summary> ''' <summary>
''' Simple patterns that only rely on .NET functions ''' Patterns for control values on a panel
''' </summary> ''' </summary>
Public Class Controls Public Class Controls
Inherits BaseModule Inherits BaseModule
Implements IModule Implements IModule
Public Const CTRL_VALUE_PANEL = "PANEL" Public Const CTRL_VALUE_PANEL = "CTRL_VALUE_PANEL"
Public Property PatternIdentifier As String = "CTRL" Implements IModule.PatternIdentifier Public Property PatternIdentifier As String = "CTRL" Implements IModule.PatternIdentifier
Public Property IsComplex As Boolean = True Implements IModule.IsComplex Public Property IsComplex As Boolean = True Implements IModule.IsComplex
Private ReadOnly Logger As Logger
Public Sub New(pLogConfig As LogConfig) Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig) MyBase.New(pLogConfig)
Logger = pLogConfig.GetLogger()
End Sub End Sub
Public Function Replace(pInput As String, pReplaceMap As Dictionary(Of String, Object)) As String Implements IModule.Replace Public Function Replace(pInput As String, pReplaceMap As Dictionary(Of String, Object)) As String Implements IModule.Replace
Dim oResult = pInput Dim oResult = pInput
Dim oCounter = 0 Dim oCounter = 0
Dim oPanel As Panel = pReplaceMap.Item("CTRL_VALUE_PANEL") Dim oPanel As Panel = pReplaceMap.Item(CTRL_VALUE_PANEL)
While ContainsPattern(oResult, PatternIdentifier) While ContainsPattern(oResult, PatternIdentifier)
Dim oControlName As String = GetNextPattern(oResult, PatternIdentifier).Value Try
Dim oControl As Control = oPanel.Controls.Find(oControlName, False).FirstOrDefault() Dim oControlName As String = GetNextPattern(oResult, PatternIdentifier).Value
Dim oControl As Control = oPanel.Controls.Find(oControlName, False).FirstOrDefault()
If oControl IsNot Nothing Then If oControl IsNot Nothing Then
Dim oReplaceValue As String Dim oReplaceValue As String
Select Case oControl.GetType.ToString Select Case oControl.GetType.ToString
Case GetType(TextBox).ToString Case GetType(TextBox).ToString
oReplaceValue = oControl.Text oReplaceValue = oControl.Text
Case GetType(LookupControl3).ToString Case GetType(LookupControl3).ToString
Dim oLookupControl3 As LookupControl3 = oControl Dim oLookupControl3 As LookupControl3 = oControl
If oLookupControl3.Properties.SelectedValues.Count = 1 Then If oLookupControl3.Properties.SelectedValues.Count = 1 Then
oReplaceValue = oLookupControl3.Properties.SelectedValues.Item(0) oReplaceValue = oLookupControl3.Properties.SelectedValues.Item(0)
Else Else
oReplaceValue = "0"
End If
Case GetType(ComboBox).ToString
oReplaceValue = oControl.Text
Case GetType(CheckBox).ToString
Dim oCheckBox As CheckBox = oControl
oReplaceValue = oCheckBox.Checked
Case Else
oReplaceValue = "0" oReplaceValue = "0"
End If End Select
Case GetType(ComboBox).ToString
oReplaceValue = oControl.Text
Case GetType(CheckBox).ToString
Dim oCheckBox As CheckBox = oControl
oReplaceValue = oCheckBox.Checked
Case Else
oReplaceValue = "0"
End Select
oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue) oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue)
End If End If
Catch ex As Exception
Logger.Error(ex)
Finally
IncrementCounterOrThrow(oCounter)
End Try
IncrementCounterOrThrow(oCounter)
End While End While
Return oResult Return oResult

View File

@ -0,0 +1,53 @@
Imports System.Windows.Forms
Imports DigitalData.Controls.LookupGrid
Imports DigitalData.Modules.Logging
Namespace [PatternModule]
''' <summary>
''' Patterns for Windream Indicies
''' </summary>
Public Class Windream
Inherits BaseModule
Implements IModule
Public Const WM_VALUE_DOCUMENT = "WM_DOCUMENT"
Public Property PatternIdentifier As String = "WMI" Implements IModule.PatternIdentifier
Public Property IsComplex As Boolean = True Implements IModule.IsComplex
Private ReadOnly Logger As Logger
Public Sub New(pLogConfig As LogConfig)
MyBase.New(pLogConfig)
Logger = pLogConfig.GetLogger()
End Sub
Public Function Replace(pInput As String, pReplaceMap As Dictionary(Of String, Object)) As String Implements IModule.Replace
Dim oResult = pInput
Dim oCounter = 0
Dim pWMObject As WINDREAMLib.WMObject = pReplaceMap.Item(WM_VALUE_DOCUMENT)
While ContainsPattern(oResult, PatternIdentifier)
Try
Dim oIndexName As String = GetNextPattern(oResult, PatternIdentifier).Value
Dim oWMValue As Object = pWMObject.GetVariableValue(oIndexName)
If oWMValue Is Nothing Then
Logger.Warn("Value for Index [{0}] is empty and will not be used for replacing. Skipping.")
Return oResult
End If
oResult = ReplacePattern(oResult, PatternIdentifier, oWMValue.ToString)
Catch ex As Exception
Logger.Error(ex)
Return oResult
Finally
IncrementCounterOrThrow(oCounter)
End Try
End While
Return oResult
End Function
End Class
End Namespace

View File

@ -81,6 +81,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Constants.vb" /> <Compile Include="Constants.vb" />
<Compile Include="Modules\Windream.vb" />
<Compile Include="Modules\User.vb" /> <Compile Include="Modules\User.vb" />
<Compile Include="Modules\IModule.vb" /> <Compile Include="Modules\IModule.vb" />
<Compile Include="Modules\Clipboard.vb" /> <Compile Include="Modules\Clipboard.vb" />

View File

@ -91,29 +91,113 @@ Public Class Patterns2
Return oResult Return oResult
End Function End Function
Private Function GetReplaceMapForModule(pModule As IModule, pPanel As Panel, pUser As State.UserState) As Dictionary(Of String, Object) Public Function ReplaceUserValues(pInput As String, pUser As State.UserState) As String
Dim oResult = pInput
Dim oModule = GetModule(Of PatternModule.User)()
Dim oArgs = GetReplaceMapForModule(oModule, pUser:=pUser)
oResult = DoReplaceForModule(oResult, oModule, oArgs)
Return oResult
End Function
Public Function ReplaceControlValues(pInput As String, pPanel As Panel) As String
Dim oResult = pInput
Dim oModule = GetModule(Of PatternModule.Controls)()
Dim oArgs = GetReplaceMapForModule(oModule, pPanel:=pPanel)
oResult = DoReplaceForModule(oResult, oModule, oArgs)
Return oResult
End Function
Public Function ReplaceWindreamValues(pInput As String, pWMObject As WMObject) As String
Dim oResult = pInput
Dim oModule = GetModule(Of PatternModule.Windream)()
Dim oArgs = GetReplaceMapForModule(oModule, pWMObject:=pWMObject)
oResult = DoReplaceForModule(oResult, oModule, oArgs)
Return oResult
End Function
Public Function ReplaceInternalValues(pInput As String) As String
Dim oResult = pInput
Dim oInternalModule = GetModule(Of PatternModule.Internal)()
Dim oInternalArgs = GetReplaceMapForModule(oInternalModule)
oResult = DoReplaceForModule(oResult, oInternalModule, oInternalArgs)
Dim oClipboardModule = GetModule(Of PatternModule.Clipboard)()
Dim oClipboardArgs = GetReplaceMapForModule(oClipboardModule)
oResult = DoReplaceForModule(oResult, oClipboardModule, oClipboardArgs)
Return oResult
End Function
Private Function DoReplaceForModule(pInput As String, pModule As IModule, pArgs As Dictionary(Of String, Object)) As String
Try
pInput = pModule.Replace(pInput, pArgs)
Catch ex As Exception
Logger.Warn("Placeholders for String [{0}] could not be replaced completely in Module [{1}]. Skipping.", pInput, pModule.GetType.Name)
Logger.Error(ex)
End Try
Return pInput
End Function
Private Function GetModule(Of ModuleT)() As IModule
Return Modules.
Where(Function(m) TypeOf m Is ModuleT).
SingleOrDefault()
End Function
Private Function GetReplaceMapForModule(pModule As IModule, Optional pPanel As Panel = Nothing, Optional pUser As State.UserState = Nothing, Optional pWMObject As WMObject = Nothing) As Dictionary(Of String, Object)
Dim oArgs As New Dictionary(Of String, Object) Dim oArgs As New Dictionary(Of String, Object)
If TypeOf pModule Is PatternModule.Clipboard Then If TypeOf pModule Is PatternModule.Clipboard Then
oArgs.Add(PatternModule.Clipboard.CLIP_VALUE_BOARD, My.Computer.Clipboard.GetText()) Try
oArgs.Add(PatternModule.Clipboard.CLIP_VALUE_BOARD, My.Computer.Clipboard.GetText())
Catch ex As Exception
Logger.Error(ex)
End Try
ElseIf TypeOf pModule Is PatternModule.Internal Then ElseIf TypeOf pModule Is PatternModule.Internal Then
oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, System.Environment.UserName) Try
oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, System.Environment.MachineName) oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, System.Environment.UserName)
oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, System.Environment.UserDomainName) oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, System.Environment.MachineName)
oArgs.Add(PatternModule.Internal.INT_VALUE_DATE, Now.ToShortDateString) oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, System.Environment.UserDomainName)
oArgs.Add(PatternModule.Internal.INT_VALUE_DATE, Now.ToShortDateString)
Catch ex As Exception
Logger.Error(ex)
End Try
ElseIf TypeOf pModule Is PatternModule.User Then ElseIf TypeOf pModule Is PatternModule.User Then
oArgs.Add(PatternModule.User.USER_VALUE_EMAIL, pUser.Email) Try
oArgs.Add(PatternModule.User.USER_VALUE_LANGUAGE, pUser.Language) oArgs.Add(PatternModule.User.USER_VALUE_EMAIL, pUser.Email)
oArgs.Add(PatternModule.User.USER_VALUE_PRENAME, pUser.GivenName) oArgs.Add(PatternModule.User.USER_VALUE_LANGUAGE, pUser.Language)
oArgs.Add(PatternModule.User.USER_VALUE_SHORTNAME, pUser.ShortName) oArgs.Add(PatternModule.User.USER_VALUE_PRENAME, pUser.GivenName)
oArgs.Add(PatternModule.User.USER_VALUE_SURNAME, pUser.Surname) oArgs.Add(PatternModule.User.USER_VALUE_SHORTNAME, pUser.ShortName)
oArgs.Add(PatternModule.User.USER_VALUE_USER_ID, pUser.UserId) oArgs.Add(PatternModule.User.USER_VALUE_SURNAME, pUser.Surname)
oArgs.Add(PatternModule.User.USER_VALUE_USER_NAME, pUser.UserName) oArgs.Add(PatternModule.User.USER_VALUE_USER_ID, pUser.UserId)
oArgs.Add(PatternModule.User.USER_VALUE_USER_NAME, pUser.UserName)
Catch ex As Exception
Logger.Error(ex)
End Try
ElseIf TypeOf pModule Is PatternModule.Controls Then ElseIf TypeOf pModule Is PatternModule.Controls Then
oArgs.Add(PatternModule.Controls.CTRL_VALUE_PANEL, pPanel) Try
oArgs.Add(PatternModule.Controls.CTRL_VALUE_PANEL, pPanel)
Catch ex As Exception
Logger.Error(ex)
End Try
ElseIf TypeOf pModule Is PatternModule.Windream Then
Try
oArgs.Add(PatternModule.Windream.WM_VALUE_DOCUMENT, pWMObject)
Catch ex As Exception
Logger.Error(ex)
End Try
End If End If