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]
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 SqlPhrases As New List(Of String) From {
"SELECT ", "UPDATE ", "DELETE ", "EXEC "
}
Private Const MAX_TRY_COUNT = 500
@@ -21,23 +23,37 @@ Namespace [PatternModule]
pCounter += 1
End Sub
Public Function ReplacePattern(input As String, type As String, replacement As String) As String
Dim elements As MatchCollection = MyRegex.Matches(input)
Public Function ReplacePattern(pInput As String, pType As String, pReplacement As String) As String
Dim oElements As MatchCollection = MyRegex.Matches(pInput)
If IsNothing(replacement) Then
Return input
If IsNothing(pReplacement) Then
Return pInput
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'
' and return it
If element.Groups(1).Value = type Then
Return Regex.Replace(input, element.Groups(0).Value, replacement)
If oElement.Groups(1).Value = pType Then
Logger.Debug("Replacing Placeholder with [{0}]", pReplacement)
Return Regex.Replace(pInput, oElement.Groups(0).Value, pReplacement)
End If
Next
' no replacement made
Return input
Return pInput
End Function
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
' Pattern in pInput
Dim t As String = oElement.Groups(1).Value
Dim v As String = oElement.Groups(2).Value
Dim oType As String = oElement.Groups(1).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
End If
Next
@@ -84,16 +100,16 @@ Namespace [PatternModule]
Return False
End Function
Public Function GetNextPattern(input As String, type As String) As Pattern
Dim elements As MatchCollection = MyRegex.Matches(input)
Public Function GetNextPattern(pInput As String, pType As String) As Pattern
Dim oElements As MatchCollection = MyRegex.Matches(pInput)
For Each element As Match In elements
For Each oElement As Match In oElements
' Pattern in pInput
Dim t As String = element.Groups(1).Value
Dim v As String = element.Groups(2).Value
Dim oType As String = oElement.Groups(1).Value
Dim oValue As String = oElement.Groups(2).Value
If t = type Then
Return New Pattern(t, v)
If oType = pType Then
Return New Pattern(oType, oValue)
End If
Next

View File

@@ -4,55 +4,62 @@ Imports DigitalData.Modules.Logging
Namespace [PatternModule]
''' <summary>
''' Simple patterns that only rely on .NET functions
''' Patterns for control values on a panel
''' </summary>
Public Class Controls
Inherits BaseModule
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 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 oPanel As Panel = pReplaceMap.Item("CTRL_VALUE_PANEL")
Dim oPanel As Panel = pReplaceMap.Item(CTRL_VALUE_PANEL)
While ContainsPattern(oResult, PatternIdentifier)
Dim oControlName As String = GetNextPattern(oResult, PatternIdentifier).Value
Dim oControl As Control = oPanel.Controls.Find(oControlName, False).FirstOrDefault()
Try
Dim oControlName As String = GetNextPattern(oResult, PatternIdentifier).Value
Dim oControl As Control = oPanel.Controls.Find(oControlName, False).FirstOrDefault()
If oControl IsNot Nothing Then
Dim oReplaceValue As String
Select Case oControl.GetType.ToString
Case GetType(TextBox).ToString
oReplaceValue = oControl.Text
Case GetType(LookupControl3).ToString
Dim oLookupControl3 As LookupControl3 = oControl
If oLookupControl3.Properties.SelectedValues.Count = 1 Then
oReplaceValue = oLookupControl3.Properties.SelectedValues.Item(0)
Else
If oControl IsNot Nothing Then
Dim oReplaceValue As String
Select Case oControl.GetType.ToString
Case GetType(TextBox).ToString
oReplaceValue = oControl.Text
Case GetType(LookupControl3).ToString
Dim oLookupControl3 As LookupControl3 = oControl
If oLookupControl3.Properties.SelectedValues.Count = 1 Then
oReplaceValue = oLookupControl3.Properties.SelectedValues.Item(0)
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"
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"
End Select
End Select
oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue)
End If
oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue)
End If
Catch ex As Exception
Logger.Error(ex)
Finally
IncrementCounterOrThrow(oCounter)
End Try
IncrementCounterOrThrow(oCounter)
End While
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