Depending Controls

This commit is contained in:
Jonathan Jenne 2020-03-16 17:04:40 +01:00
parent 8c0afa58de
commit ed5a1233ae
3 changed files with 81 additions and 18 deletions

View File

@ -352,6 +352,8 @@ Public Class ClassControls
Dim oDatatable As DataTable = ClassDatabase.Return_Datatable(oSQL) Dim oDatatable As DataTable = ClassDatabase.Return_Datatable(oSQL)
If Not IsNothing(oDatatable) Then If Not IsNothing(oDatatable) Then
LOGGER.Debug("Found [{0}] depending controls for [{1}]", oDatatable.Rows.Count, Control.Name)
For Each oRow As DataRow In oDatatable.Rows For Each oRow As DataRow In oDatatable.Rows
Dim oControlName As String = NotNull(oRow.Item("NAME"), "") Dim oControlName As String = NotNull(oRow.Item("NAME"), "")
Dim oConnectionId As Integer = NotNull(oRow.Item("CONNECTION_ID"), -1) Dim oConnectionId As Integer = NotNull(oRow.Item("CONNECTION_ID"), -1)
@ -366,9 +368,11 @@ Public Class ClassControls
oControlSql = ClassPatterns.ReplaceInternalValues(oControlSql) oControlSql = ClassPatterns.ReplaceInternalValues(oControlSql)
oControlSql = ClassPatterns.ReplaceControlValues(oControlSql, Panel) oControlSql = ClassPatterns.ReplaceControlValues(oControlSql, Panel)
LOGGER.Debug("SQL After Preparing: [{0}]", oControlSql)
LOGGER.Debug("Setting new value for [{0}]", oControlName)
SetDependingControlResult(oControlName, oControlSql, oConnectionId) SetDependingControlResult(oControlName, oControlSql, oConnectionId)
Next Next
End If End If
Catch ex As Exception Catch ex As Exception
LOGGER.Error(ex) LOGGER.Error(ex)
@ -377,6 +381,11 @@ Public Class ClassControls
Private Sub SetDependingControlResult(IndexName As String, SqlCommand As String, SqlConnectionId As Integer) Private Sub SetDependingControlResult(IndexName As String, SqlCommand As String, SqlConnectionId As Integer)
Try Try
If SqlCommand Is Nothing OrElse SqlCommand = String.Empty Then
LOGGER.Warn("New Value for Index [{0}] could not be set. Supplied SQL is empty.")
Exit Sub
End If
Dim oConnectionString = ClassFormFunctions.GetConnectionString(SqlConnectionId) Dim oConnectionString = ClassFormFunctions.GetConnectionString(SqlConnectionId)
Dim oDatatable As DataTable = ClassDatabase.Return_Datatable_CS(SqlCommand, oConnectionString) Dim oDatatable As DataTable = ClassDatabase.Return_Datatable_CS(SqlCommand, oConnectionString)
Dim oFoundControl As Control = Nothing Dim oFoundControl As Control = Nothing
@ -404,10 +413,21 @@ Public Class ClassControls
End If End If
If TypeOf oFoundControl Is TextBox Then If TypeOf oFoundControl Is TextBox Then
DirectCast(oFoundControl, TextBox).Text = oDatatable.Rows.Item(0).Item(0) If oDatatable.Rows.Count > 0 Then
Dim oFirstRow As DataRow = oDatatable.Rows.Item(0)
If oFirstRow.ItemArray.Length > 0 Then
Dim oValue = oFirstRow.Item(0).ToString()
LOGGER.Debug("Setting Value for control [{0}]: [{1}]", oFoundControl.Name, oValue)
DirectCast(oFoundControl, TextBox).Text = oValue
End If
End If
ElseIf TypeOf oFoundControl Is LookupControl2 Then ElseIf TypeOf oFoundControl Is LookupControl2 Then
LOGGER.Debug("Setting Value for control [{0}]: [{1}]", oFoundControl.Name, "DATATABLE")
DirectCast(oFoundControl, LookupControl2).DataSource = oDatatable DirectCast(oFoundControl, LookupControl2).DataSource = oDatatable
ElseIf TypeOf oFoundControl Is ComboBox Then ElseIf TypeOf oFoundControl Is ComboBox Then
LOGGER.Debug("Setting Value for control [{0}]: [{1}]", oFoundControl.Name, "DATATABLE")
DirectCast(oFoundControl, ComboBox).DataSource = oDatatable DirectCast(oFoundControl, ComboBox).DataSource = oDatatable
End If End If
Catch ex As Exception Catch ex As Exception

View File

@ -19,6 +19,7 @@ Public Class ClassInit
Public Sub InitConfig() Public Sub InitConfig()
CONFIG = New ConfigManager(Of ClassConfig)(LOGCONFIG, Application.UserAppDataPath, Application.CommonAppDataPath) CONFIG = New ConfigManager(Of ClassConfig)(LOGCONFIG, Application.UserAppDataPath, Application.CommonAppDataPath)
LOGCONFIG.Debug = Not CONFIG.Config.LogErrorsOnly LOGCONFIG.Debug = Not CONFIG.Config.LogErrorsOnly
LOGGER.Info("Debug log set to: [{0}]", LOGCONFIG.Debug)
MyConnectionString = DecryptConnectionString(CONFIG.Config.ConnectionString) MyConnectionString = DecryptConnectionString(CONFIG.Config.ConnectionString)
LogErrorsOnly = CONFIG.Config.LogErrorsOnly LogErrorsOnly = CONFIG.Config.LogErrorsOnly

View File

@ -39,7 +39,7 @@ Public Class ClassPatterns
Public Const MAX_TRY_COUNT = 500 Public Const MAX_TRY_COUNT = 500
Private Shared regex As Regex = New Regex("{#(\w+)#([\w\s_-]+)}+") Private Shared regex As Regex = New Regex("{#(\w+)#([\w\d\s_-]+)}+")
Private Shared allPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL, PATTERN_IDBA, PATTERN_USER, PATTERN_INT} Private Shared allPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL, PATTERN_IDBA, PATTERN_USER, PATTERN_INT}
Private Shared complexPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL, PATTERN_IDBA} Private Shared complexPatterns As New List(Of String) From {PATTERN_WMI, PATTERN_CTRL, PATTERN_IDBA}
Private Shared simplePatterns As New List(Of String) From {PATTERN_USER, PATTERN_INT} Private Shared simplePatterns As New List(Of String) From {PATTERN_USER, PATTERN_INT}
@ -146,12 +146,28 @@ Public Class ClassPatterns
Dim result = input Dim result = input
Dim oTryCounter = 0 Dim oTryCounter = 0
LOGGER.Debug("Input String: [{0}]", input)
While ContainsPattern(result, PATTERN_CTRL) While ContainsPattern(result, PATTERN_CTRL)
LOGGER.Debug("ReplaceControlValues Try no. [{0}]", oTryCounter)
If oTryCounter > MAX_TRY_COUNT Then If oTryCounter > MAX_TRY_COUNT Then
Throw New Exception($"Max tries in ReplaceControlValues exceeded - Result so far [{result}].") Throw New Exception($"Max tries in ReplaceControlValues exceeded - Result so far [{result}].")
End If End If
Dim controlName As String = GetNextPattern(result, PATTERN_CTRL).Value LOGGER.Debug("Getting next pattern..")
Dim oNextPattern = GetNextPattern(result, PATTERN_CTRL)
If oNextPattern Is Nothing Then
LOGGER.Debug("No Next Pattern found. Exiting!")
Exit While
End If
LOGGER.Debug("Next Pattern Value: [{0}]", oNextPattern.Value)
LOGGER.Debug("Next Pattern Type: [{0}]", oNextPattern.Type)
Dim controlName As String = oNextPattern.Value
Dim oFoundControl As Control = Nothing Dim oFoundControl As Control = Nothing
Dim oFoundType As String = Nothing Dim oFoundType As String = Nothing
@ -160,9 +176,15 @@ Public Class ClassPatterns
Continue For Continue For
End If End If
LOGGER.Debug("Getting control metadata..")
Dim oMeta = DirectCast(oControl.Tag, ClassControls.ControlMeta) Dim oMeta = DirectCast(oControl.Tag, ClassControls.ControlMeta)
LOGGER.Debug("Checking Control Name matches..")
If oMeta.IndexName = controlName Then If oMeta.IndexName = controlName Then
LOGGER.Debug("Control Name matches! Matching Control: [{0}]", controlName)
oFoundControl = oControl oFoundControl = oControl
oFoundType = oMeta.IndexType oFoundType = oMeta.IndexType
Exit For Exit For
@ -173,28 +195,47 @@ Public Class ClassPatterns
Dim oValue As String = String.Empty Dim oValue As String = String.Empty
If TypeOf oFoundControl Is TextBox Then If TypeOf oFoundControl Is TextBox Then
oValue = DirectCast(oFoundControl, TextBox).Text Try
oValue = DirectCast(oFoundControl, TextBox).Text
Catch ex As Exception
LOGGER.Error(ex)
LOGGER.Warn("Control Value for TextBox [{0}] could not be retrieved!", oFoundControl.Name)
End Try
ElseIf TypeOf oFoundControl Is CheckBox Then ElseIf TypeOf oFoundControl Is CheckBox Then
oValue = IIf(DirectCast(oFoundControl, CheckBox).Checked, 1, 0) Try
oValue = IIf(DirectCast(oFoundControl, CheckBox).Checked, 1, 0)
Catch ex As Exception
LOGGER.Error(ex)
LOGGER.Warn("Control Value for CheckBox [{0}] could not be retrieved!", oFoundControl.Name)
End Try
ElseIf TypeOf oFoundControl Is LookupControl2 Then ElseIf TypeOf oFoundControl Is LookupControl2 Then
Dim oLookupControl = DirectCast(oFoundControl, LookupControl2) Try
Dim oLookupControl = DirectCast(oFoundControl, LookupControl2)
If oLookupControl.MultiSelect Then If oLookupControl.MultiSelect Then
Select Case oFoundType Select Case oFoundType
Case "INTEGER" Case "INTEGER"
oValue = String.Join(",", oLookupControl.SelectedValues) oValue = String.Join(",", oLookupControl.SelectedValues)
Case "VARCHAR" Case "VARCHAR"
Dim oWrapped = oLookupControl.SelectedValues.Select(Function(v) $"'{v}'") Dim oWrapped = oLookupControl.SelectedValues.Select(Function(v) $"'{v}'")
oValue = String.Join(",", oWrapped) oValue = String.Join(",", oWrapped)
End Select End Select
Else Else
oValue = NotNull(oLookupControl.SelectedValues.Item(0), "") oValue = NotNull(oLookupControl.SelectedValues.Item(0), "")
End If End If
Catch ex As Exception
LOGGER.Error(ex)
LOGGER.Warn("Control Value for LookupControl2 [{0}] could not be retrieved!", oFoundControl.Name)
End Try
Else Else
oValue = "" oValue = ""
End If End If
LOGGER.Debug("Retrieved Value from Control [{0}] is: [{1}]", oFoundControl.Name, oValue)
result = ReplacePattern(result, PATTERN_CTRL, oValue) result = ReplacePattern(result, PATTERN_CTRL, oValue)
Else
LOGGER.Warn("Control [{0}] not found!", controlName)
End If End If
oTryCounter += 1 oTryCounter += 1
@ -204,6 +245,7 @@ Public Class ClassPatterns
Catch ex As Exception Catch ex As Exception
LOGGER.Error(ex) LOGGER.Error(ex)
LOGGER.Info("Error in ReplaceControlValues:" & ex.Message) LOGGER.Info("Error in ReplaceControlValues:" & ex.Message)
Return input
End Try End Try
End Function End Function