230 lines
8.3 KiB
VB.net
230 lines
8.3 KiB
VB.net
Imports System.Text.RegularExpressions
|
|
Imports System.Windows.Forms
|
|
Imports DigitalData.Controls.LookupGrid
|
|
Imports DigitalData.Modules.Logging
|
|
Imports DigitalData.Modules.ZooFlow
|
|
Imports WINDREAMLib
|
|
|
|
''' <summary>
|
|
''' Defines common Functions for Checking for and replacing placeholders.
|
|
''' This Class also includes a child class `Pattern` for passing around Patterns.
|
|
'''
|
|
''' The format of all placeholders is:
|
|
''' {#TYPE#VALUE}
|
|
'''
|
|
''' Some Examples:
|
|
''' {#INT#USERNAME}
|
|
''' {#CTRL#CMB_2}
|
|
''' {#WMI#String 39}
|
|
''' </summary>
|
|
Public Class Patterns2
|
|
''' <summary>
|
|
''' Complex patterns that rely on Windream
|
|
''' </summary>
|
|
Public Const PATTERN_WMI = "WMI"
|
|
|
|
''' <summary>
|
|
''' Complex patterns that rely on IDB Attribute values
|
|
''' </summary>
|
|
Public Const PATTERN_IDBA = "IDBA"
|
|
|
|
''' <summary>
|
|
''' Complex patterns that rely on Control Values
|
|
''' </summary>
|
|
Public Const PATTERN_CTRL = "CTRL"
|
|
|
|
''' <summary>
|
|
''' Simple patterns that rely on Data from the TBDD_USER table
|
|
''' </summary>
|
|
Public Const PATTERN_USER = "USER"
|
|
|
|
Public Const USER_VALUE_PRENAME = "PRENAME"
|
|
Public Const USER_VALUE_SURNAME = "SURNAME"
|
|
Public Const USER_VALUE_EMAIL = "EMAIL"
|
|
Public Const USER_VALUE_SHORTNAME = "SHORTNAME"
|
|
Public Const USER_VALUE_LANGUAGE = "LANGUAGE"
|
|
Public Const USER_VALUE_USER_ID = "USER_ID"
|
|
Public Const VALUE_PROFILE_ID = "PROFILE_ID"
|
|
Public Const VALUE_PROFILE_TITLE = "PROFILE_TITLE"
|
|
|
|
Private ReadOnly LogConfig As LogConfig
|
|
Private ReadOnly Logger As Logger
|
|
Private ReadOnly Base As PatternModule.BaseModule
|
|
|
|
Private ReadOnly ControlPanel As Panel
|
|
Private ReadOnly IDBActive As Boolean
|
|
|
|
Private ReadOnly Modules As New List(Of IModule)
|
|
|
|
Public Sub New(pLogConfig As LogConfig)
|
|
LogConfig = pLogConfig
|
|
Logger = pLogConfig.GetLogger()
|
|
Base = New PatternModule.BaseModule(LogConfig)
|
|
|
|
Modules.AddRange({
|
|
New PatternModule.Internal(LogConfig),
|
|
New PatternModule.Clipboard(LogConfig),
|
|
New PatternModule.Controls(LogConfig),
|
|
New PatternModule.User(LogConfig)
|
|
})
|
|
End Sub
|
|
|
|
Public Sub New(pLogConfig As LogConfig, pControlPanel As Panel, pIDBActive As Boolean)
|
|
MyClass.New(pLogConfig)
|
|
ControlPanel = pControlPanel
|
|
IDBActive = pIDBActive
|
|
End Sub
|
|
|
|
Public Function ReplaceAllValues(pInput As String, pPanel As Panel, pUser As State.UserState) As String
|
|
Dim oResult = pInput
|
|
|
|
For Each oModule In Modules
|
|
Try
|
|
Dim oArgs = GetReplaceMapForModule(oModule, pPanel, pUser)
|
|
oResult = oModule.Replace(oResult, oArgs)
|
|
Catch ex As Exception
|
|
Logger.Warn("Placeholders for String [{0}] could not be replaced completely in Module [{1}]. Skipping.", pInput, oModule.GetType.Name)
|
|
Logger.Error(ex)
|
|
End Try
|
|
Next
|
|
|
|
Return oResult
|
|
End Function
|
|
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
|
Return oArgs
|
|
End Function
|
|
|
|
#Region "Helper Functions"
|
|
''' <summary>
|
|
''' Wraps a pattern-type and -value in the common format: {#type#value}
|
|
''' </summary>
|
|
Public Function WrapPatternValue(pType As String, pValue As String) As String
|
|
Return New Pattern(pType, pValue).ToString
|
|
End Function
|
|
|
|
Public Function HasAnyPatterns(input) As Boolean
|
|
Return Modules.Any(Function(m) Base.HasPattern(input, m.PatternIdentifier))
|
|
End Function
|
|
|
|
Public Function HasComplexPatterns(input As String) As Boolean
|
|
Return Modules.
|
|
Where(Function(m) m.IsComplex = True).
|
|
Any(Function(m) Base.HasPattern(input, m.PatternIdentifier))
|
|
End Function
|
|
|
|
Public Function HasOnlySimplePatterns(input As String) As Boolean
|
|
Return Not HasComplexPatterns(input)
|
|
End Function
|
|
#End Region
|
|
|
|
End Class |