Modules/GUIs.Monitor/frmMonitor.vb
2021-05-20 16:12:26 +02:00

411 lines
15 KiB
VB.net

Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraTreeList.Nodes
Imports DigitalData.Controls.SQLConfig
Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language.Utils
Imports DevExpress.XtraTab
Imports DigitalData.Controls.DocumentViewer
Imports DevExpress.XtraEditors
Imports DevExpress.XtraRichEdit
Public Class frmMonitor
Public Property LogConfig As LogConfig
Public Property ConfigManager As ConfigManager(Of Config)
Public Property Database As MSSQLServer
Private ReadOnly SearchKeys As New List(Of SearchKey)
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 HtmlViewColumns As New List(Of String) From {"HTML1", "HTML2"}
Private ReadOnly DataColumns As List(Of String) = SQLColumns.
Concat(DocViewColumns).
Concat(HtmlViewColumns).
ToList
Private ReadOnly DisplayColumns As New List(Of String) From {"COLUMN1", "COLUMN2", "COLUMN3"}
Private SQLResultGrids As List(Of GridControl)
Private SQLResultTabs As List(Of XtraTabPage)
Private FileResultViewers As List(Of DocumentViewer)
Private FileResultTabs As List(Of XtraTabPage)
Private HtmlResultViewers As List(Of RichEditControl)
Private HtmlResultTabs As List(Of XtraTabPage)
Private Enum NodeImage
[Default] = 0
SQL = 1
File = 2
Mail = 3
Success = 4
Failure = 5
Warning = 6
End Enum
Private GridBuilder As GridBuilder
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")
ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath)
Init(LogConfig)
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()
Else
ShowErrorMessage("No Database configured. Application will close!")
Application.Exit()
End If
End If
Database = New MSSQLServer(LogConfig, ConfigManager.Config.ConnectionString)
GridBuilder = New GridBuilder(New List(Of GridView) From {GridView1, GridView2, GridView3, GridView4})
GridBuilder.
WithDefaults().
WithDefaults(TreeListResults).
WithReadOnlyOptions().
WithReadOnlyOptions(TreeListResults)
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}
LoadSearchKeys()
Dim oLicense = LoadGDPicture()
For Each oViewer As DocumentViewer In FileResultViewers
oViewer.Init(LogConfig, oLicense)
Next
Catch ex As Exception
ShowErrorMessage(ex)
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 Sub buttonSearch_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles buttonSearch.ItemClick
InitTreeList()
LoadData()
End Sub
Private Function LoadData() As Boolean
Try
Dim oSQL As String = "EXEC PRDD_MONITORING_GET_TREEVIEW_RESULT 'TYPE1','sdsdd',2"
Dim oTable As DataTable = Database.GetDatatable(oSQL)
TreeListResults.DataSource = oTable
TreeListResults.PopulateColumns()
For Each oColumn In TreeListResults.Columns
oColumn.Visible = DisplayColumns.Contains(oColumn.FieldName)
Next
For Each oNode As TreeListNode In TreeListResults.Nodes
ExpandNodes(oNode)
Next
Return True
Catch ex As Exception
ShowErrorMessage(ex)
Return False
End Try
End Function
Private Sub LoadSearchKeys()
Try
Dim oSQL = ConfigManager.Config.SearchKeySQL
Dim oTable = Database.GetDatatable(oSQL)
For Each oRow As DataRow In oTable.Rows
SearchKeys.Add(New SearchKey With {
.Id = CInt(oRow.Item(0)),
.Title = oRow.Item(1).ToString,
.TypeName = oRow.Item(2).ToString
})
Next
cmbSearchKeys.Properties.Items.Clear()
cmbSearchKeys.Properties.Items.AddRange(SearchKeys)
Catch ex As Exception
ShowErrorMessage(ex)
End Try
End Sub
Private Sub InitTreeList()
TreeListResults.KeyFieldName = "GUID"
TreeListResults.ParentFieldName = "PARENT_ID"
End Sub
Private Sub cmbSearchKeys_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbSearchKeys.SelectedValueChanged
Dim oItem As SearchKey = CType(cmbSearchKeys.SelectedItem, SearchKey)
Select Case oItem.TypeName
Case "VARCHAR"
LayoutItemSearchValue_Date.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never
LayoutItemSearchValue_Text.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always
Case "DATE"
LayoutItemSearchValue_Date.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always
LayoutItemSearchValue_Text.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never
Case Else
LayoutItemSearchValue_Date.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never
LayoutItemSearchValue_Text.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always
End Select
End Sub
Private Sub TreeListResults_FocusedNodeChanged(sender As Object, e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs) Handles TreeListResults.FocusedNodeChanged
If e.Node Is Nothing Then
Exit Sub
End If
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
SplitContainerControl3.Collapsed = oSQLCommands.Count = 0
SplitContainerControl2.Collapsed = (oFilePaths.Count + oHtmlDocuments.Count) = 0
For Each oSQLCommand As KeyValuePair(Of String, String) In oSQLCommands
Try
Dim oTable As DataTable = Database.GetDatatable(oSQLCommand.Value)
If oTable Is Nothing Then
Continue For
End If
Dim oGridIndex = Integer.Parse(oSQLCommand.Key.Last()) - 1
Dim oGridControl As GridControl = SQLResultGrids.Item(oGridIndex)
FillResultGrid(oGridControl, oTable)
Catch ex As Exception
Logger.Error(ex)
Continue For
End Try
Next
For Each oFile As KeyValuePair(Of String, String) In oFilePaths
Try
Dim oPath = oFile.Value
If oPath Is Nothing Then
Continue For
End If
If 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)
Catch ex As Exception
Logger.Error(ex)
Continue For
End Try
Next
For Each oFile As KeyValuePair(Of String, String) In oHtmlDocuments
Try
Dim oHtml = oFile.Value
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)
Catch ex As Exception
Logger.Error(ex)
Continue For
End Try
Next
End Sub
Private Sub ExpandNodes(RootNode As TreeListNode)
For Each oNode As TreeListNode In RootNode.Nodes
ExpandNodes(oNode)
Dim oColumn = TreeListResults.Columns.Item("COLOR")
Dim oColorString = NotNull(oNode.GetValue(oColumn), Nothing)
If oColorString IsNot Nothing AndAlso oColorString = "Red" 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 FillResultHtmlViewer(RichEditControl As RichEditControl, Html As String)
RichEditControl.HtmlText = Html
Dim oTabPage = DirectCast(RichEditControl.Parent, XtraTabPage)
oTabPage.PageVisible = True
XtraTabControl1.SelectedTabPage = oTabPage
End Sub
Private Sub FillResultViewer(DocumentViewer As DocumentViewer, Path As String)
DocumentViewer.LoadFile(Path)
Dim oTabPage = DirectCast(DocumentViewer.Parent, XtraTabPage)
oTabPage.PageVisible = True
XtraTabControl1.SelectedTabPage = oTabPage
End Sub
Private Sub FillResultGrid(GridControl As GridControl, Table As DataTable)
GridControl.DataSource = Table
Dim oTabPage = DirectCast(GridControl.Parent, XtraTabPage)
oTabPage.PageVisible = True
XtraTabControl3.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) Handles TreeListResults.CustomDrawNodeCell
Dim oColumn = TreeListResults.Columns.Item("COLOR")
Dim oColorString = NotNull(e.Node.GetValue(oColumn), Nothing)
If oColorString Is Nothing Then
Exit Sub
End If
Dim oColor As Color = Nothing
Select Case oColorString.ToString
Case "Green"
oColor = Color.LightGreen
Case "Red"
oColor = Color.LightCoral
End Select
e.Appearance.BackColor = oColor
e.Appearance.Options.UseBackColor = True
e.Handled = False
End Sub
Private Sub TreeListResults_GetStateImage(sender As Object, e As DevExpress.XtraTreeList.GetStateImageEventArgs) Handles TreeListResults.GetStateImage
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).
Count()
Dim oFilePaths = oValues.
Where(Function(v) v.Key.StartsWith("DOCVIEW")).
Where(Function(v) v.Value IsNot Nothing).
Count()
Dim oHtmlDocuments = oValues.
Where(Function(v) v.Key.StartsWith("HTML")).
Where(Function(v) v.Value IsNot Nothing).
Count()
Dim oColumn = TreeListResults.Columns.Item("COLOR")
Dim oColorString = NotNull(e.Node.GetValue(oColumn), Nothing)
If oSQLCommands > 0 Then
e.NodeImageIndex = NodeImage.SQL
ElseIf oFilePaths > 0 Then
e.NodeImageIndex = NodeImage.File
ElseIf oHtmlDocuments > 0 Then
e.NodeImageIndex = NodeImage.Mail
Else
If oColorString Is Nothing Then
e.NodeImageIndex = NodeImage.Default
ElseIf oColorString = "Red" Then
e.NodeImageIndex = NodeImage.Failure
ElseIf oColorString = "Green" Then
e.NodeImageIndex = NodeImage.Success
ElseIf oColorString = "Yellow" Then
e.NodeImageIndex = NodeImage.Warning
End If
End If
End Sub
End Class
Friend Class SearchKey
Public Id As Integer
Public Title As String
Public TypeName As String
Public Overrides Function ToString() As String
Return Title
End Function
End Class