Monitor/GUIs.Monitor/Forms/frmMonitor.vb
2023-04-05 11:03:17 +02:00

1237 lines
47 KiB
VB.net

Imports System.Text.RegularExpressions
Imports DevExpress.Utils
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraTab
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Columns
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraCharts
Imports DigitalData.Controls.DocumentViewer
Imports DigitalData.GUIs.Common
Imports DigitalData.GUIs.Monitor.Constants
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Language
Imports DigitalData.Modules.Language.Utils
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Patterns
Imports DevExpress.XtraEditors
Public Class frmMonitor
Private ReadOnly InvisibleColumnMarkers As New List(Of String) From {"[HTML]", "[SQL]", "[FILENAME]"}
Private ReadOnly VisibleColumnMarkers As New List(Of String) From {"[SEARCH|"}
Private ReadOnly SQLColumns As New List(Of String) From {"SELECT1", "SELECT2", "SELECT3", "SELECT4"}
Private ReadOnly DocViewColumns As New List(Of String) From {"DOCVIEW1", "DOCVIEW2"}
Private ReadOnly Property HtmlViewColumns As New List(Of String) From {"HTML1", "HTML2"}
Private ReadOnly Property DataColumns As List(Of String) = SQLColumns.
Concat(DocViewColumns).
Concat(HtmlViewColumns).
ToList
Private Enum TristateBoolean
[False]
[True]
Indeterminate
End Enum
Private ReadOnly DisplayColumns As New List(Of String) From {"COLUMN1", "COLUMN2", "COLUMN3", "ADDED_WHEN", "STATE", "ICON"}
Private SQLResultGrids As List(Of GridControl)
Private SQLResultTabs As List(Of XtraTabPage)
'Private SQLResultGridIndex As Integer = 0
Private ActiveSQLResultGrid As GridControl
Private FileResultViewers As List(Of DocumentViewer)
Private FileResultTabs As List(Of XtraTabPage)
Private FileResultViewerIndex As Integer = 0
Private HtmlResultViewers As List(Of RichEditControl)
Private HtmlResultTabs As List(Of XtraTabPage)
Private TreeListResults As TreeList
Private GridControlResults As GridControl
Private GridViewResults As GridView
Private ChartViewResultContainer As SplitContainerControl
Private InvisibleMarkedColumns As New List(Of GridColumn)
Private VisibleMarkedColumns As New List(Of GridColumn)
Private ActiveSearch As Search = Nothing
Private HyperLinkColumnSearchIds As New Dictionary(Of String, Integer)
Private GridBuilder As GridBuilder
Private ControlHelper As Common.ControlHelper
Private SearchLoader As SearchLoader
Private ParamLoader As ParameterLoader
Private LogConfig As LogConfig
Private Logger As Logger
Private ConfigManager As ConfigManager(Of Config)
Private Database As MSSQLServer
Private FormHelper As FormHelper
Private Patterns As Patterns2
Private Workspace As Common.DocumentResultList.Workspace(Of Config)
Private SplitContainerBuilderHTML As SplitContainerBuilder
Private SplitContainerBuilderSQL As SplitContainerBuilder
Private Validator As Validator
Private GridLoader As GridLoader
Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, "Digital Data", "Monitor")
Logger = LogConfig.GetLogger()
ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath, Application.CommonAppDataPath, Application.StartupPath)
ConfigManager.Config.Debug = LogConfig.Debug
Patterns = New Patterns2(LogConfig)
FormHelper = New FormHelper(LogConfig, Me)
ControlHelper = New Common.ControlHelper(LogConfig)
Workspace = New DocumentResultList.Workspace(Of Config)(LogConfig, ConfigManager, WorkspaceManager1)
SplitContainerBuilderHTML = New SplitContainerBuilder(LogConfig, Me, SplitContainerFileHTML)
SplitContainerBuilderHTML.InitAutoResize()
SplitContainerBuilderSQL = New SplitContainerBuilder(LogConfig, Me, SplitContainerSQL)
SplitContainerBuilderSQL.InitAutoResize()
If ConfigManager.Config.ConnectionString = String.Empty Then
Dim oSQLConfig As New frmSQLConfig(LogConfig)
If oSQLConfig.ShowDialog() = DialogResult.OK Then
ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString
ConfigManager.Save()
Application.Restart()
Else
FormHelper.ShowErrorMessage(New ApplicationException("No Database configured. Application will close!"), "Form Load")
Application.Exit()
End If
End If
Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString)
Database = New MSSQLServer(LogConfig, oConnectionString)
SearchLoader = New SearchLoader(LogConfig, ConfigManager.Config, Database)
ParamLoader = New ParameterLoader(LogConfig, Database, LayoutControl1)
GridBuilder = New GridBuilder()
Validator = New Validator(LogConfig, LayoutControl1, AdornerUIManager2, ControlHelper)
GridLoader = New GridLoader(LogConfig, SvgImageCollection1)
InitGrid()
InitTreeList()
'InitChartView()
InitChartContainer()
Dim oGrids As GridView() = New List(Of GridView) From {GridView1, GridView2, GridView3, GridView4}.ToArray
GridBuilder.SetDefaults(oGrids)
GridBuilder.SetClipboardHandler(oGrids)
GridBuilder.SetReadOnlyOptions(oGrids)
SQLResultGrids = New List(Of GridControl) From {GridControl1, GridControl2, GridControl3, GridControl4}
SQLResultTabs = New List(Of XtraTabPage) From {XtraTabPageSQL1, XtraTabPageSQL2, XtraTabPageSQL3, XtraTabPageSQL4}
FileResultViewers = New List(Of DocumentViewer) From {DocumentViewer1, DocumentViewer2}
FileResultTabs = New List(Of XtraTabPage) From {XtraTabPageFile1, XtraTabPageFile2}
HtmlResultViewers = New List(Of RichEditControl) From {RichEditControl1, RichEditControl2}
HtmlResultTabs = New List(Of XtraTabPage) From {XtraTabPageHtml1, XtraTabPageHtml2}
LoadSearches()
Dim oLicense = LoadGDPicture()
For Each oGrid In SQLResultGrids
AddHandler oGrid.Enter, Sub()
ActiveSQLResultGrid = oGrid
btnExportDetails.Enabled = True
End Sub
AddHandler oGrid.Leave, Sub()
ActiveSQLResultGrid = Nothing
btnExportDetails.Enabled = False
End Sub
Next
For Each oViewer As DocumentViewer In FileResultViewers
oViewer.Init(LogConfig, oLicense)
Next
SplitContainerSQL.Collapsed = True
SplitContainerFileHTML.Collapsed = True
SetResultCount(0)
SetVersion()
Catch ex As Exception
FormHelper.ShowErrorMessage(ex, "frmStart_Load")
End Try
End Sub
Private Function LoadGDPicture() As String
Dim oSQL = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'"
Return Database.GetScalarValue(oSQL)
End Function
Private Async Sub buttonSearch_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles buttonSearch.ItemClick
Await LoadData()
End Sub
Private Async Function LoadData() As Threading.Tasks.Task(Of Boolean)
Try
SplashScreenManager1.ShowWaitForm()
TreeListResults.DataSource = Nothing
GridControlResults.DataSource = Nothing
If cmbSearches.EditValue Is Nothing Then
Return False
End If
Dim oSearch As Search = cmbSearches.EditValue
InvisibleMarkedColumns.Clear()
VisibleMarkedColumns.Clear()
HyperLinkColumnSearchIds.Clear()
HideAllTabs()
Dim oMissingParams = Validator.Validate(oSearch)
If oMissingParams = True Then
Return False
End If
If oSearch.ExpectedParameterCount > 0 And oSearch.Parameters.Count <> oSearch.ExpectedParameterCount Then
Logger.Error($"[{0}] parameter were defined, but [{1}] parameter were expected!", oSearch.Parameters.Count, oSearch.ExpectedParameterCount)
FormHelper.ShowErrorMessage("Die Suche erwartet mehr Parameter als definiert wurden!", "LoadSearches")
Return False
End If
Dim oControls As List(Of Control) = LayoutControl1.Controls.Cast(Of Control).ToList()
Dim oSQL As String
Dim oTable As DataTable
If oSearch.SQLCommand.Length > 0 Then
oSQL = Patterns.ReplaceControlValues(oSearch.SQLCommand, oControls)
Logger.Debug($"SQL after replacing placeholder: [{0}]", oSQL)
oTable = Await Database.GetDatatableAsync(oSQL)
End If
Dim oStartTime = Now
If oSearch.ReturnType = ReturnTypeEnum.TreeView Then
' Baum
SetResultVisbility(ReturnTypeEnum.TreeView)
If oTable Is Nothing Then
Return False
End If
TreeListResults.DataSource = oTable
lbResultCount.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
Dim oMaxLength = 0
For Each oRow As DataRow In oTable.Rows
Dim oProcess = oRow.Item("COLUMN1")
Dim oLength = oProcess.ToString.Length
If oLength > oMaxLength Then
oMaxLength = oLength
End If
Next
GridLoader.InitTreeListColumns(TreeListResults, oMaxLength)
' Show all columns in DisplayColumns List
For Each oColumn In TreeListResults.Columns
oColumn.Visible = DisplayColumns.Contains(oColumn.FieldName)
Next
GridBuilder.SetDateTimeColumns(TreeListResults)
Dim oStateColumn As TreeListColumn = TreeListResults.Columns.Item("STATE")
For Each oNode As TreeListNode In TreeListResults.Nodes
ExpandNodes(oNode, Function(n)
Dim oObjectValue = n.GetValue(oStateColumn)
Dim oValue As String = NotNull(oObjectValue.ToString, String.Empty)
Return oValue IsNot Nothing AndAlso (oValue = State.STATE_WARNING Or oValue = State.STATE_FAILURE)
End Function)
Next
For Each oNode As TreeListNode In TreeListResults.Nodes
SetExpandedForChildNodes(oNode)
Next
SetResultCount(TreeListResults.AllNodesCount)
TreeListResults.Focus()
ElseIf oSearch.ReturnType = Constants.ReturnTypeEnum.ChartView Then
' Charts
SetResultVisbility(ReturnTypeEnum.ChartView)
' Oberer SplitContainer
Dim splitContainer1 As SplitContainerControl = CType(ChartViewResultContainer.Panel1.Controls(0), SplitContainerControl)
' Unterer SplitContainer
Dim splitContainer2 As SplitContainerControl = CType(ChartViewResultContainer.Panel2.Controls(0), SplitContainerControl)
ChartViewResultContainer.PanelVisibility = SplitPanelVisibility.Both
splitContainer1.PanelVisibility = SplitPanelVisibility.Both
splitContainer2.PanelVisibility = SplitPanelVisibility.Both
Dim chartArray(4) As ChartControl
' TopLeft
chartArray(0) = CType(splitContainer1.Panel1.Controls(0), ChartControl)
' TopRight
chartArray(1) = CType(splitContainer1.Panel2.Controls(0), ChartControl)
' BottomLeft
chartArray(2) = CType(splitContainer2.Panel1.Controls(0), ChartControl)
' BottomRight
chartArray(3) = CType(splitContainer2.Panel2.Controls(0), ChartControl)
For i As Integer = 0 To 3
chartArray(i).Titles.Clear()
chartArray(i).Series.Clear()
Next
ResizeCharContainer()
Dim oChartParameters As List(Of ChartParameter)
oChartParameters = SearchLoader.ChartParameters.Where(Function(p) p.SearchId = oSearch.Id And p.ChartType <> ChartTypeEnum.Undefined).ToList()
For Each oChartParameterSet In oChartParameters
Dim ChartViewResults As ChartControl
Select Case oChartParameterSet.ChartPos
Case ChartPosEnum.TopLeft
ChartViewResults = chartArray(0)
Case ChartPosEnum.TopRight
ChartViewResults = chartArray(1)
Case ChartPosEnum.BottomLeft
ChartViewResults = chartArray(2)
Case ChartPosEnum.BottomRight
ChartViewResults = chartArray(3)
Case Else
ChartViewResults = chartArray(0)
End Select
SetChartTitle(ChartViewResults, oChartParameterSet.Title)
Dim oSerie As Series = Await GetChartSerie(oChartParameterSet, oControls)
ChartViewResults.Series.Add(oSerie)
Next
SetPanelVisibility(chartArray)
ChartViewResultContainer.Focus()
Else
' Tabelle
SetResultVisbility(ReturnTypeEnum.Table)
If oTable Is Nothing Then
Return False
End If
GridControlResults.DataSource = oTable
GridViewResults.PopulateColumns()
GridBuilder.SetDateTimeColumns(GridViewResults)
lbResultCount.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
For Each oColumn As GridColumn In GridViewResults.Columns
If oColumn.ColumnType = GetType(Integer) Then
oColumn.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center
End If
Next
InvisibleMarkedColumns = GridViewResults.Columns.AsEnumerable.
Where(Function(column)
Dim oCaption = column.FieldName.ToUpper.Trim
Return InvisibleColumnMarkers.Any(Function(marker) oCaption.EndsWith(marker))
End Function).ToList()
For Each oColumn In InvisibleMarkedColumns
oColumn.VisibleIndex = -1
Next
VisibleMarkedColumns = GridViewResults.Columns.AsEnumerable.
Where(Function(column)
Dim oCaption = column.FieldName.ToUpper.Trim
Return VisibleColumnMarkers.Any(Function(marker) oCaption.Contains(marker))
End Function).ToList()
For Each oColumn In VisibleMarkedColumns
If oColumn.FieldName.Contains("[SEARCH|") Then
Dim oUrlEdit As New RepositoryItemHyperLinkEdit() With
{
.SingleClick = True
}
GridControlResults.RepositoryItems.Add(oUrlEdit)
For index = 0 To GridViewResults.Columns.Count - 1
If GridViewResults.Columns.Item(index).FieldName = oColumn.FieldName Then
GridViewResults.Columns.Item(index).ColumnEdit = oUrlEdit
' Column Caption setzen
Dim colTitle As String = oColumn.FieldName.Substring(0, oColumn.FieldName.IndexOf("["))
GridViewResults.Columns.Item(index).Caption = colTitle
' Add SearchId in Dictionary
HyperLinkColumnSearchIds.Add(oColumn.FieldName, GetSearchIdFromColumnFieldName(oColumn.FieldName))
End If
Next
AddHandler oUrlEdit.OpenLink, AddressOf HyperLinkEdit_OpenLinkAsync
End If
Next
SetColumnsToDisAllowEditInGridViewResults()
SetResultCount(GridViewResults.RowCount)
GridControlResults.Focus()
End If
GridViewResults.FocusInvalidRow()
GridViewResults.BestFitColumns()
Workspace.LoadWorkspace(oSearch.Id.ToString)
btnExportMain.Enabled = True
Dim oTotalTime = Now.Subtract(oStartTime)
lbSearchTime.Caption = oTotalTime.ToString("mm':'ss")
Return True
Catch ex As Exception
FormHelper.ShowErrorMessage(ex, "LoadData")
Return False
Finally
SplashScreenManager1.CloseWaitForm()
End Try
End Function
Private Async Function GetChartSerie(pChartParameters As ChartParameter, pControls As List(Of Control)) As Threading.Tasks.Task(Of Series)
Dim oSerie As Series
Select Case pChartParameters.ChartType
Case ChartTypeEnum.Bar
oSerie = New Series(pChartParameters.Title, ViewType.Bar)
Case ChartTypeEnum.Line
oSerie = New Series(pChartParameters.Title, ViewType.Line)
Case ChartTypeEnum.Area
oSerie = New Series(pChartParameters.Title, ViewType.Area)
Case ChartTypeEnum.Pie
oSerie = New Series(pChartParameters.Title, ViewType.Pie)
oSerie.Label.TextPattern = "{V}"
oSerie.LegendTextPattern = "{A}"
Case Else
Return Nothing
End Select
Dim oSQL As String = Patterns.ReplaceControlValues(pChartParameters.SQLCommand, pControls)
Logger.Debug($"SQL after replacing placeholder: [{0}]", oSQL)
Dim oTable As DataTable = Await Database.GetDatatableAsync(oSQL)
Dim xAxisTitle As String = pChartParameters.Argument
Dim yAxisTitle As String = pChartParameters.Value
For Each oRow In oTable.Rows
Dim value1 As String = oRow.Item(xAxisTitle)
Dim value2 As String = oRow.Item(yAxisTitle)
oSerie.Points.Add(New SeriesPoint(value1, value2))
Next
oSerie.ArgumentScaleType = ScaleType.Qualitative
Return oSerie
End Function
Private Sub SetChartTitle(pChartView As ChartControl, pTitle As String)
' Wenn bereits ein Titel vorhanden ist, wird kein neuer ergänzt
' relevant bei mehrere Charts in einem ChartControl
If pChartView.Titles.Count = 0 Then
Dim oTitle As ChartTitle = New ChartTitle With {
.Text = pTitle
}
pChartView.Titles.Add(oTitle)
End If
End Sub
Private Sub SetPanelVisibility(chartArray() As ChartControl)
If chartArray.Length < 4 Then
Exit Sub
End If
Dim showTop As Boolean = True
Dim showBottom As Boolean = True
If chartArray(0).Titles.Count = 0 And chartArray(1).Titles.Count = 0 Then
showTop = False
End If
If chartArray(2).Titles.Count = 0 And chartArray(3).Titles.Count = 0 Then
showBottom = False
End If
If showBottom = False Then
ChartViewResultContainer.PanelVisibility = SplitPanelVisibility.Panel1
ElseIf showTop = False Then
ChartViewResultContainer.PanelVisibility = SplitPanelVisibility.Panel2
End If
If showTop = True Then
Dim showLeft As Boolean = chartArray(0).Titles.Count > 0
Dim showRight As Boolean = chartArray(1).Titles.Count > 0
Dim splitContainer1 As SplitContainerControl = CType(ChartViewResultContainer.Panel1.Controls(0), SplitContainerControl)
If showRight = False Then
splitContainer1.PanelVisibility = SplitPanelVisibility.Panel1
ElseIf showLeft = False Then
splitContainer1.PanelVisibility = SplitPanelVisibility.Panel2
End If
End If
If showBottom = True Then
Dim showLeft As Boolean = chartArray(2).Titles.Count > 0
Dim showRight As Boolean = chartArray(3).Titles.Count > 0
Dim splitContainer2 As SplitContainerControl = CType(ChartViewResultContainer.Panel2.Controls(0), SplitContainerControl)
If showRight = False Then
splitContainer2.PanelVisibility = SplitPanelVisibility.Panel1
ElseIf showLeft = False Then
splitContainer2.PanelVisibility = SplitPanelVisibility.Panel2
End If
End If
End Sub
Private Sub SetResultVisbility(pSearchMode As ReturnTypeEnum)
Select Case pSearchMode
Case ReturnTypeEnum.TreeView
GridControlResults.Visible = False
GridControlResults.Dock = DockStyle.None
TreeListResults.Visible = True
TreeListResults.Dock = DockStyle.Fill
ChartViewResultContainer.Visible = False
ChartViewResultContainer.Dock = DockStyle.None
Case ReturnTypeEnum.ChartView
GridControlResults.Visible = False
GridControlResults.Dock = DockStyle.None
TreeListResults.Visible = False
TreeListResults.Dock = DockStyle.None
ChartViewResultContainer.Visible = True
ChartViewResultContainer.Dock = DockStyle.Fill
Case ReturnTypeEnum.Table
GridControlResults.Visible = True
GridControlResults.Dock = DockStyle.Fill
TreeListResults.Visible = False
TreeListResults.Dock = DockStyle.None
ChartViewResultContainer.Visible = False
ChartViewResultContainer.Dock = DockStyle.None
End Select
End Sub
''' <summary>
''' Linklogik für Hyperlink-Spalten
''' </summary>
Private Async Function HyperLinkEdit_OpenLinkAsync(sender As Object, e As OpenLinkEventArgs) As Threading.Tasks.Task
Dim searchId As Integer = HyperLinkColumnSearchIds(GridViewResults.FocusedColumn.FieldName)
Dim oSearch As Search = SearchLoader.Searches.Where(Function(search) search.Id = searchId).SingleOrDefault()
cmbSearches.EditValue = oSearch ' Es wird jetzt erwartet, dass das ChangeValue-Event der ComboBox ausgelöst wird!
Dim oSearchParameter As SearchParameter = oSearch.Parameters.Where(Function(param) param.Required = True).FirstOrDefault()
If oSearchParameter Is Nothing And oSearch.ExpectedParameterCount > 0 Then
Logger.Error($"[{0}] parameter were defined, but [{1}] parameter were expected!", oSearch.Parameters.Count, oSearch.ExpectedParameterCount)
FormHelper.ShowErrorMessage("Die Suche erwartet 1 Parameter, aber es wurde kein Parameter gefunden!", "LoadSearches")
Return
Else
Dim oControl As Control = LayoutControl1.Controls.Find(oSearchParameter.PatternTitle, True).FirstOrDefault()
oControl.Text = e.EditValue
End If
Await LoadData()
' e.EditValue = "www.google.de"
e.Handled = True
End Function
Private Function GetSearchIdFromColumnFieldName(pFieldName As String) As Integer
If String.IsNullOrEmpty(pFieldName) = True Then
Return 0
End If
Dim startIndex = pFieldName.IndexOf("|") + 1
Dim endIndex = pFieldName.IndexOf("]")
Dim length = endIndex - startIndex
Dim searchIdText = pFieldName.Substring(startIndex, length)
Dim retValue As Integer
If (Integer.TryParse(searchIdText, retValue) = False) Then
retValue = 0
End If
Return retValue
End Function
Private Sub SetColumnsToDisAllowEditInGridViewResults()
For Each oColumn As GridColumn In GridViewResults.Columns
If TypeOf oColumn.ColumnEdit IsNot RepositoryItemHyperLinkEdit Then
' Verbietet Spaltenweise die Bearbeitung
oColumn.OptionsColumn.AllowEdit = False
End If
Next
End Sub
Private Sub SetExpandedForChildNodes(pRootNode As TreeListNode, Optional pDepth As Integer = 0)
If pDepth > 10 Then
Exit Sub
End If
For Each oNode As TreeListNode In pRootNode.Nodes
Try
Dim oExpanded As TristateBoolean = NotNull(oNode.Item("EXPANDED"), TristateBoolean.Indeterminate)
ExpandCollapseNode(oNode, oExpanded)
SetExpandedForChildNodes(oNode, pDepth + 1)
Catch ex As Exception
Logger.Error(ex)
End Try
Next
End Sub
Private Sub LoadSearches()
Try
SearchLoader.LoadSearchParameters()
SearchLoader.LoadChartParameters()
SearchLoader.LoadSearches()
cmbSearches.Properties.Items.Clear()
cmbSearches.Properties.Items.AddRange(SearchLoader.Searches)
If cmbSearches.EditValue IsNot Nothing Then
Dim oOldSearch As Search = DirectCast(cmbSearches.EditValue, Search)
Dim oNewSearch As Search = SearchLoader.Searches.Where(Function(search) search.Id = oOldSearch.Id).SingleOrDefault()
If oNewSearch IsNot Nothing Then
cmbSearches.EditValue = oNewSearch
End If
End If
Catch ex As Exception
FormHelper.ShowErrorMessage(ex, "LoadSearches")
End Try
End Sub
Private Sub cmbSearches_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbSearches.SelectedValueChanged
DisposeTreeList()
InitTreeList()
DisposeGrid()
InitGrid()
SplitContainerSQL.Collapsed = True
SplitContainerFileHTML.Collapsed = True
btnExportMain.Enabled = False
btnExportDetails.Enabled = False
SetResultCount(0)
LoadSearch()
HideAllTabs()
End Sub
Private Sub LoadSearch()
Try
If TypeOf cmbSearches.SelectedItem IsNot Search Then
Exit Sub
End If
Dim oSearch As Search = CType(cmbSearches.SelectedItem, Search)
ActiveSearch = oSearch
AdornerUIManager2.Hide()
AdornerUIManager2.Elements.Clear()
Root.Clear(disposeItemAndControls:=False)
LayoutControl1.Clear(True, True)
TreeListResults.DataSource = Nothing
GridControlResults.DataSource = Nothing
If oSearch.Parameters.Count = 0 Then
lbParams.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never
Else
lbParams.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always
End If
ParamLoader.LoadParameters(oSearch)
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
Private Function GetParameterItems(pParam As SearchParameter) As Object
Select Case pParam.ItemType
Case Constants.ItemTypeEnum.List
Return pParam.ItemString.Split(";"c).ToList()
Case Constants.ItemTypeEnum.SQL
Dim oSQL = pParam.ItemString
Dim oTable = Database.GetDatatable(oSQL)
Return oTable
End Select
End Function
Private Sub TreeListResults_FocusedNodeChanged(sender As Object, e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs)
If e.Node Is Nothing Then
Exit Sub
End If
Try
Dim oValues As Dictionary(Of String, String) = GetValuesFromNode(e.Node, DataColumns)
Dim oSQLCommands = oValues.
Where(Function(v) v.Key.StartsWith("SELECT")).
Where(Function(v) v.Value IsNot Nothing).
ToDictionary(Function(v) v.Key, Function(v) v.Value)
Dim oFilePaths = oValues.
Where(Function(v) v.Key.StartsWith("DOCVIEW")).
Where(Function(v) v.Value IsNot Nothing).
ToDictionary(Function(v) v.Key, Function(v) v.Value)
Dim oHtmlDocuments = oValues.
Where(Function(v) v.Key.StartsWith("HTML")).
Where(Function(v) v.Value IsNot Nothing).
ToDictionary(Function(v) v.Key, Function(v) v.Value)
Dim oAllTabs = SQLResultTabs.
Concat(FileResultTabs).
Concat(HtmlResultTabs)
For Each oTabPage In oAllTabs
oTabPage.PageVisible = False
Next
SplitContainerSQL.Collapsed = True
SplitContainerFileHTML.Collapsed = True
For Each oSQLCommand As KeyValuePair(Of String, String) In oSQLCommands
Try
Dim oExtracted = ExtractTitle(oSQLCommand.Value)
Dim oCommand = oExtracted.Item1
Dim oTitle = NotNull(oExtracted.Item2, oSQLCommand.Key)
Dim oTable As DataTable = Database.GetDatatable(oCommand)
If oTable Is Nothing Then
Continue For
End If
Dim oGridIndex = Integer.Parse(oSQLCommand.Key.Last()) - 1
Dim oGridControl As GridControl = SQLResultGrids.Item(oGridIndex)
If oGridIndex = 0 Then
ActiveSQLResultGrid = oGridControl
End If
FillResultGrid(oGridControl, oTable, oTitle)
Catch ex As Exception
Logger.Error(ex)
Continue For
End Try
Next
For Each oFile As KeyValuePair(Of String, String) In oFilePaths
Try
Dim oExtracted = ExtractTitle(oFile.Value)
Dim oPath = oExtracted.Item1
Dim oTitle = oExtracted.Item2
If oPath Is Nothing Then
Continue For
End If
If Not IO.File.Exists(oPath) Then
Continue For
End If
Dim oViewerIndex = Integer.Parse(oFile.Key.Last()) - 1
Dim oViewer As DocumentViewer = FileResultViewers.Item(oViewerIndex)
FillResultViewer(oViewer, oPath, oTitle)
Catch ex As Exception
Logger.Error(ex)
Continue For
End Try
Next
For Each oFile As KeyValuePair(Of String, String) In oHtmlDocuments
Try
Dim oExtracted = ExtractTitle(oFile.Value)
Dim oHtml = oExtracted.Item1
Dim oTitle = oExtracted.Item2
If oHtml Is Nothing Then
Continue For
End If
Dim oViewerIndex = Integer.Parse(oFile.Key.Last()) - 1
Dim oViewer As RichEditControl = HtmlResultViewers.Item(oViewerIndex)
FillResultHtmlViewer(oViewer, oHtml, oTitle)
Catch ex As Exception
Logger.Error(ex)
Continue For
End Try
Next
Catch ex As Exception
FormHelper.ShowErrorMessage(ex, "TreeListResults_FocusedNodeChanged")
End Try
End Sub
Private Function ExtractTitle(pValue As String) As Tuple(Of String, String)
Dim oTitleRegex As New Regex("([\s\S]*)#&#([\s\S]*)#&#$")
If oTitleRegex.IsMatch(pValue) Then
Dim oMatch = oTitleRegex.Match(pValue)
Dim oValue = oMatch.Groups.Item(1).Value
Dim oTitle = oMatch.Groups.Item(2).Value
Return New Tuple(Of String, String)(oValue, oTitle)
Else
Return New Tuple(Of String, String)(pValue, Nothing)
End If
End Function
Private Sub ExpandNodes(RootNode As TreeListNode, Condition As Predicate(Of TreeListNode))
For Each oNode As TreeListNode In RootNode.Nodes
ExpandNodes(oNode, Condition)
If Condition(oNode) = True Then
oNode.Expand()
ExpandParentNode(oNode)
End If
Next
End Sub
Private Sub ExpandParentNode(ChildNode As TreeListNode)
If ChildNode.ParentNode IsNot Nothing Then
ChildNode.ParentNode.Expand()
ExpandParentNode(ChildNode.ParentNode)
End If
End Sub
Private Sub ExpandCollapseNode(pNode As TreeListNode, pState As TristateBoolean)
Select Case pState
Case TristateBoolean.False
pNode.Collapse()
Case TristateBoolean.True
pNode.Expand()
Case Else
'noop
End Select
End Sub
Private Sub FillResultHtmlViewer(RichEditControl As RichEditControl, Html As String, Title As String)
RichEditControl.HtmlText = Html
Dim oTabPage = DirectCast(RichEditControl.Parent, XtraTabPage)
oTabPage.PageVisible = True
oTabPage.Text = NotNull(Title, oTabPage.Text)
XtraTabControlFileHTML.SelectedTabPage = oTabPage
End Sub
Private Sub FillResultViewer(DocumentViewer As DocumentViewer, Path As String, Title As String)
DocumentViewer.LoadFile(Path)
Dim oTabPage = DirectCast(DocumentViewer.Parent, XtraTabPage)
oTabPage.PageVisible = True
oTabPage.Text = NotNull(Title, oTabPage.Text)
XtraTabControlFileHTML.SelectedTabPage = oTabPage
End Sub
Private Sub FillResultGrid(GridControl As GridControl, Table As DataTable, Title As String)
GridControl.DataSource = Table
GridControl.DefaultView.PopulateColumns()
Dim oTabPage = DirectCast(GridControl.Parent, XtraTabPage)
oTabPage.PageVisible = True
oTabPage.Text = $"{NotNull(Title, oTabPage.Text)} ({Table.Rows.Count})"
XtraTabControlSQL.SelectedTabPage = oTabPage
End Sub
Private Function GetValuesFromNode(Node As TreeListNode, ColumnNames As List(Of String)) As Dictionary(Of String, String)
Dim oValues As New Dictionary(Of String, String)
For Each oColumnName In ColumnNames
Dim oValue = MaybeGetValueForColumn(Node, oColumnName)
oValues.Add(oColumnName, oValue)
Next
Return oValues
End Function
Private Function MaybeGetValueForColumn(Node As TreeListNode, FieldName As String) As String
Dim oColumn = TreeListResults.Columns.Item(FieldName)
If oColumn Is Nothing Then
Return Nothing
End If
Dim oValue = Node.GetValue(oColumn)?.ToString
If oValue Is String.Empty Then
Return Nothing
Else
Return oValue
End If
End Function
Private Sub TreeListResults_CustomDrawNodeCell(sender As Object, e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs)
Dim oColumn = TreeListResults.Columns.Item("STATE")
Dim oState = NotNull(e.Node.GetValue(oColumn), Nothing)
If oState Is Nothing Then
Exit Sub
End If
Dim oColor As Color = Nothing
Select Case oState.ToString
Case State.STATE_SUCCESS
oColor = Color.LightGreen
Case State.STATE_FAILURE
oColor = Color.LightCoral
Case State.STATE_WARNING
oColor = Color.Yellow
Case State.STATE_WAITING
oColor = Color.LightSkyBlue
End Select
e.Appearance.BackColor = oColor
e.Appearance.Options.UseBackColor = True
e.Handled = False
End Sub
Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
Dim oSQLConfig As New frmSQLConfig(LogConfig) With {
.ConnectionString = ConfigManager.Config.ConnectionString
}
If oSQLConfig.ShowDialog() = DialogResult.OK Then
ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString
ConfigManager.Save()
Database = New MSSQLServer(LogConfig, oSQLConfig.ConnectionString)
LoadSearches()
End If
End Sub
Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnExportDetails.ItemClick
If ActiveSQLResultGrid Is Nothing Then
Exit Sub
End If
XtraSaveFileDialog1.Filter = "Excel Files (*.xlsx)|*.xlsx"
XtraSaveFileDialog1.FileName = $"{ActiveSearch.Title}_Detail.xlsx"
If XtraSaveFileDialog1.ShowDialog() = DialogResult.OK Then
ActiveSQLResultGrid.ExportToXlsx(XtraSaveFileDialog1.FileName)
End If
End Sub
Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnExportMain.ItemClick
If ActiveSearch Is Nothing Then
Exit Sub
End If
XtraSaveFileDialog1.Filter = "Excel Files (*.xlsx)|*.xlsx"
XtraSaveFileDialog1.FileName = $"{ActiveSearch.Title}.xlsx"
If XtraSaveFileDialog1.ShowDialog() = DialogResult.OK Then
If ActiveSearch.ReturnType = ReturnTypeEnum.TreeView Then
TreeListResults.ExportToXlsx(XtraSaveFileDialog1.FileName)
Else
GridControlResults.ExportToXlsx(XtraSaveFileDialog1.FileName)
End If
End If
End Sub
Private Async Sub frmMonitor_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.F5 Then
Debug.Write("Debug.Write")
Console.WriteLine("Console.WriteLine")
Debug.Print("Debug.Print")
Await LoadData()
End If
End Sub
Private Sub TreeListResults_MouseClick(sender As Object, e As MouseEventArgs)
Dim oInfo As TreeListHitInfo = TreeListResults.CalcHitInfo(New Point(e.X, e.Y))
If oInfo.InRowCell Then
Dim oNode = oInfo.Node
Dim oValues As Dictionary(Of String, String) = GetValuesFromNode(oNode, DataColumns)
Console.WriteLine()
Dim oShouldToggleSQL = False
Dim oShouldToggleFile = False
For Each oValue As KeyValuePair(Of String, String) In oValues
If oValue.Key.Contains("SELECT") AndAlso oValue.Value IsNot Nothing Then
oShouldToggleSQL = True
Exit For
End If
Next
For Each oValue As KeyValuePair(Of String, String) In oValues
If oValue.Key.Contains("DOCVIEW") AndAlso oValue.Value IsNot Nothing Then
oShouldToggleFile = True
Exit For
End If
If oValue.Key.Contains("HTML") AndAlso oValue.Value IsNot Nothing Then
oShouldToggleFile = True
Exit For
End If
Next
If oShouldToggleSQL Then
SplitContainerSQL.Collapsed = Not SplitContainerSQL.Collapsed
End If
If oShouldToggleFile Then
SplitContainerFileHTML.Collapsed = Not SplitContainerFileHTML.Collapsed
End If
End If
End Sub
Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadSearches.ItemClick
Try
LoadSearches()
TreeListResults.DataSource = Nothing
GridControlResults.DataSource = Nothing
Catch ex As Exception
FormHelper.ShowErrorMessage(ex, "Suchen neu laden")
End Try
End Sub
Private Sub GridViewResults_FocusedRowChanged(sender As Object, e As Views.Base.FocusedRowChangedEventArgs)
If GridViewResults.FocusedRowHandle < 0 Then
Exit Sub
End If
HideAllTabs()
ActiveSQLResultGrid = Nothing
Try
Dim oRow As DataRow = GridViewResults.GetDataRow(GridViewResults.FocusedRowHandle)
Dim oHtmlResultViewerIndex As Integer = 0
Dim oGridResultIndex As Integer = 0
Dim oViewerResultIndex As Integer = 0
For Each oColumn As GridColumn In InvisibleMarkedColumns
Dim oValue = oRow.ItemEx(oColumn.FieldName, String.Empty)
If oValue.Length > 0 Then
If oColumn.FieldName.EndsWith("[HTML]") Then
Dim oViewer As RichEditControl = HtmlResultViewers.Item(oHtmlResultViewerIndex)
Dim oTitle = oColumn.FieldName.Replace("[HTML]", "").Trim()
oHtmlResultViewerIndex += 1
FillResultHtmlViewer(oViewer, oValue, oTitle)
SplitContainerFileHTML.Collapsed = False
ElseIf oColumn.FieldName.EndsWith("[SQL]") Then
Dim oTable As DataTable = Database.GetDatatable(oValue)
If oTable Is Nothing Then
Continue For
End If
Dim oTitle = oColumn.FieldName.Replace("[SQL]", "").Trim()
Dim oGridControl As GridControl = SQLResultGrids.Item(oGridResultIndex)
If oGridResultIndex = 0 Then
ActiveSQLResultGrid = oGridControl
End If
oGridResultIndex += 1
FillResultGrid(oGridControl, oTable, oTitle)
SplitContainerSQL.Collapsed = False
ElseIf oColumn.FieldName.EndsWith("[FILENAME]") Then
If oValue Is Nothing Then
Continue For
End If
If Not IO.File.Exists(oValue) Then
Continue For
End If
Dim oViewer As DocumentViewer = FileResultViewers.Item(oViewerResultIndex)
Dim oTitle = oColumn.FieldName.Replace("[FILENAME]", "").Trim()
oViewerResultIndex += 1
FillResultViewer(oViewer, oValue, oTitle)
SplitContainerFileHTML.Collapsed = False
End If
End If
Next
If ActiveSQLResultGrid Is Nothing Then
btnExportDetails.Enabled = False
Else
btnExportDetails.Enabled = True
End If
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
Private Sub GridViewResults_ColumnFilterChanged(sender As Object, e As EventArgs)
Dim oView As GridView = sender
SetResultCount(oView.DataRowCount)
End Sub
Private Sub HideAllTabs()
Dim oAllTabs = SQLResultTabs.
Concat(FileResultTabs).
Concat(HtmlResultTabs)
For Each oTabPage In oAllTabs
oTabPage.PageVisible = False
Next
SplitContainerSQL.Collapsed = True
SplitContainerFileHTML.Collapsed = True
End Sub
Private Async Sub btnResetLayout_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnResetLayout.ItemClick
If ActiveSearch IsNot Nothing Then
Workspace.ResetWorkspace(ActiveSearch.Id.ToString)
DisposeTreeList()
InitTreeList()
DisposeGrid()
InitGrid()
HideAllTabs()
Await LoadData()
SetResultCount(0)
End If
End Sub
Private Sub DisposeGrid()
GridViewResults.Dispose()
GridViewResults = Nothing
GridControlResults.Dispose()
GridControlResults = Nothing
End Sub
Private Sub SetResultCount(pResults As Integer)
lbResultCount.Caption = String.Format(lbResultCount.Tag, pResults)
End Sub
Private Sub SetVersion()
lbVersion.Caption = String.Format(lbVersion.Tag, System.Reflection.Assembly.GetEntryAssembly().GetName().Version)
End Sub
Private Sub InitGrid()
GridControlResults = GridLoader.InitGrid()
GridViewResults = DirectCast(GridControlResults.DefaultView, GridView)
AddHandler GridViewResults.FocusedRowChanged, AddressOf GridViewResults_FocusedRowChanged
AddHandler GridViewResults.ColumnFilterChanged, AddressOf GridViewResults_ColumnFilterChanged
SplitContainerSQL.Panel1.Controls.Add(GridControlResults)
End Sub
Private Sub DisposeTreeList()
TreeListResults.Dispose()
TreeListResults = Nothing
End Sub
Private Sub InitTreeList()
TreeListResults = GridLoader.InitTreeList()
AddHandler TreeListResults.FocusedNodeChanged, AddressOf TreeListResults_FocusedNodeChanged
AddHandler TreeListResults.MouseClick, AddressOf TreeListResults_MouseClick
AddHandler TreeListResults.CustomDrawNodeCell, AddressOf TreeListResults_CustomDrawNodeCell
SplitContainerSQL.Panel1.Controls.Add(TreeListResults)
End Sub
Private Sub InitChartContainer()
ChartViewResultContainer = GridLoader.InitChartViewResultContainer()
SplitContainerSQL.Panel1.Controls.Add(ChartViewResultContainer)
End Sub
Private Sub ResizeCharContainer()
If ChartViewResultContainer.Visible = True Then
Dim splitContainer1 As SplitContainerControl = CType(ChartViewResultContainer.Panel1.Controls(0), SplitContainerControl)
Dim splitContainer2 As SplitContainerControl = CType(ChartViewResultContainer.Panel2.Controls(0), SplitContainerControl)
ChartViewResultContainer.SplitterPosition = CInt(ChartViewResultContainer.Height / 2)
splitContainer1.SplitterPosition = CInt(splitContainer1.Width / 2)
splitContainer2.SplitterPosition = CInt(splitContainer2.Width / 2)
End If
End Sub
Private Sub BarButtonItem2_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
If ActiveSearch Is Nothing Then
Exit Sub
End If
Workspace.SaveWorkspace(ActiveSearch.Id.ToString)
End Sub
Private Sub BarButtonItem3_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
Try
Process.Start(LogConfig.LogDirectory)
Catch ex As Exception
FormHelper.ShowErrorMessage(ex, "Log Verzeichnis öffnen")
End Try
End Sub
End Class