From bd176e3de02570c95686647544a6870dfb7d1ecc Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Thu, 4 Nov 2021 14:19:21 +0100 Subject: [PATCH] Patterns: Add Controls and Windream Modules --- Modules.Patterns/Modules/BaseModule.vb | 56 ++++++++----- Modules.Patterns/Modules/Controls.vb | 67 ++++++++------- Modules.Patterns/Modules/Windream.vb | 53 ++++++++++++ Modules.Patterns/Patterns.vbproj | 1 + Modules.Patterns/Patterns2.vb | 112 +++++++++++++++++++++---- 5 files changed, 225 insertions(+), 64 deletions(-) create mode 100644 Modules.Patterns/Modules/Windream.vb diff --git a/Modules.Patterns/Modules/BaseModule.vb b/Modules.Patterns/Modules/BaseModule.vb index f6829e7e..39ca4da4 100644 --- a/Modules.Patterns/Modules/BaseModule.vb +++ b/Modules.Patterns/Modules/BaseModule.vb @@ -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 diff --git a/Modules.Patterns/Modules/Controls.vb b/Modules.Patterns/Modules/Controls.vb index 738b78f8..1e9cb880 100644 --- a/Modules.Patterns/Modules/Controls.vb +++ b/Modules.Patterns/Modules/Controls.vb @@ -4,55 +4,62 @@ Imports DigitalData.Modules.Logging Namespace [PatternModule] ''' - ''' Simple patterns that only rely on .NET functions + ''' Patterns for control values on a panel ''' 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() - - 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 + 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 + 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 - - oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue) - End If - - IncrementCounterOrThrow(oCounter) + End Select + + oResult = ReplacePattern(oResult, PatternIdentifier, oReplaceValue) + End If + Catch ex As Exception + Logger.Error(ex) + Finally + IncrementCounterOrThrow(oCounter) + End Try + End While Return oResult diff --git a/Modules.Patterns/Modules/Windream.vb b/Modules.Patterns/Modules/Windream.vb new file mode 100644 index 00000000..f7557e2b --- /dev/null +++ b/Modules.Patterns/Modules/Windream.vb @@ -0,0 +1,53 @@ +Imports System.Windows.Forms +Imports DigitalData.Controls.LookupGrid +Imports DigitalData.Modules.Logging + +Namespace [PatternModule] + ''' + ''' Patterns for Windream Indicies + ''' + 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 diff --git a/Modules.Patterns/Patterns.vbproj b/Modules.Patterns/Patterns.vbproj index 1e8c7443..eefd03d6 100644 --- a/Modules.Patterns/Patterns.vbproj +++ b/Modules.Patterns/Patterns.vbproj @@ -81,6 +81,7 @@ + diff --git a/Modules.Patterns/Patterns2.vb b/Modules.Patterns/Patterns2.vb index 5dcc70ef..27b55a86 100644 --- a/Modules.Patterns/Patterns2.vb +++ b/Modules.Patterns/Patterns2.vb @@ -91,29 +91,113 @@ Public Class Patterns2 Return oResult 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) 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 - oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, System.Environment.UserName) - oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, System.Environment.MachineName) - oArgs.Add(PatternModule.Internal.INT_VALUE_DOMAIN, System.Environment.UserDomainName) - oArgs.Add(PatternModule.Internal.INT_VALUE_DATE, Now.ToShortDateString) + Try + oArgs.Add(PatternModule.Internal.INT_VALUE_USERNAME, System.Environment.UserName) + oArgs.Add(PatternModule.Internal.INT_VALUE_MACHINE, System.Environment.MachineName) + 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 - oArgs.Add(PatternModule.User.USER_VALUE_EMAIL, pUser.Email) - oArgs.Add(PatternModule.User.USER_VALUE_LANGUAGE, pUser.Language) - oArgs.Add(PatternModule.User.USER_VALUE_PRENAME, pUser.GivenName) - oArgs.Add(PatternModule.User.USER_VALUE_SHORTNAME, pUser.ShortName) - oArgs.Add(PatternModule.User.USER_VALUE_SURNAME, pUser.Surname) - oArgs.Add(PatternModule.User.USER_VALUE_USER_ID, pUser.UserId) - oArgs.Add(PatternModule.User.USER_VALUE_USER_NAME, pUser.UserName) + Try + oArgs.Add(PatternModule.User.USER_VALUE_EMAIL, pUser.Email) + oArgs.Add(PatternModule.User.USER_VALUE_LANGUAGE, pUser.Language) + oArgs.Add(PatternModule.User.USER_VALUE_PRENAME, pUser.GivenName) + oArgs.Add(PatternModule.User.USER_VALUE_SHORTNAME, pUser.ShortName) + oArgs.Add(PatternModule.User.USER_VALUE_SURNAME, pUser.Surname) + 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 - 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