Imports DD_Clipboard_Watcher.ClassProfileFilter Imports DD_Clipboard_Watcher.ClassConstants Imports DevExpress.XtraEditors Imports DigitalData.GUIs.Common Imports DigitalData.Modules Imports DigitalData.Modules.Database Imports DigitalData.Modules.Language Imports DigitalData.Modules.ZooFlow Public Class frmProfileMatch Private PrimaryFont As New Font("Segoe UI", 12, FontStyle.Bold) Private SecondaryFont As New Font("Segoe UI", 10) Private OpenForms As New List(Of IResultForm) Private ShouldHideInitially As Boolean = False Public Class DocumentSearch Public Guid As Integer Public DataTable As DataTable Public TabIndex As Integer Public TabCaption As String Public ProfileId As Integer Public SQLCommand As String End Class Private Enum ProfileType ANY = 0 DOCS_ONLY = 1 DATA_ONLY = 2 End Enum Private Sub frmProfileMatch_Load(sender As Object, e As EventArgs) Handles Me.Load If Not ConfigManager.Config.MatchWindowLocation.IsEmpty Then Location = ConfigManager.Config.MatchWindowLocation End If If Not ConfigManager.Config.MatchWindowSize.IsEmpty Then Size = ConfigManager.Config.MatchWindowSize End If Dim oCreatedTiles = CreateTiles() If oCreatedTiles = -1 Then Exit Sub End If If oCreatedTiles = 0 Then Logger.Warn("No Results found for ""{0}""", CURRENT_CLIPBOARD_CONTENTS) Me.Close() End If Dim oMatchString = IIf(oCreatedTiles = 1, "1 Match", $"{oCreatedTiles} Matches") Label1.Text = String.Format(Label1.Text, oMatchString, CURRENT_CLIPBOARD_CONTENTS) ' Open Result Forms directly if only one match found If oCreatedTiles = 1 Then Dim oProfile As ProfileData = CURRENT_MATCHING_PROFILES.First() OpenResultForms(oProfile.Guid, oProfile.ProfileType) ShouldHideInitially = True End If End Sub Private Sub frmProfileMatch_Shown(sender As Object, e As EventArgs) Handles Me.Shown If ShouldHideInitially Then Hide() End If End Sub Function CreateTiles() As Integer Try Dim oCreatedTiles As Integer = 0 Dim oDocumentGroup = TileControlMatch.Groups.Item("TileGroupDocuments") Dim oDataGroup = TileControlMatch.Groups.Item("TileGroupData") Dim oDataDocumentsGroup = TileControlMatch.Groups.Item("TileGroupDocumentsData") oDocumentGroup.Items.Clear() oDataGroup.Items.Clear() For Each oProfile As ProfileData In CURRENT_MATCHING_PROFILES If oProfile.ProfileType = PROFILE_TYPE_DATA_DOCS Then If oProfile.CountData > 0 And oProfile.CountDocs > 0 Then Dim oCountText = oProfile.CountData + oProfile.CountDocs Dim oItem = CreateTile(oProfile, $"{oCountText} Ergebnisse") oDataDocumentsGroup.Items.Add(oItem) oCreatedTiles += 1 End If End If If oProfile.ProfileType = PROFILE_TYPE_DATA_DOCS Or oProfile.ProfileType = PROFILE_TYPE_DOCS_ONLY Then If oProfile.CountDocs > 0 Then Dim oItem = CreateTile(oProfile, $"{oProfile.CountDocs} Dokumente") oDocumentGroup.Items.Add(oItem) oCreatedTiles += 1 End If End If If oProfile.ProfileType = PROFILE_TYPE_DATA_DOCS Or oProfile.ProfileType = PROFILE_TYPE_DATA_ONLY Then If oProfile.CountData > 0 Then Dim oItem = CreateTile(oProfile, $"{oProfile.CountData} Datensätze") oDataGroup.Items.Add(oItem) oCreatedTiles += 1 End If End If Next Return oCreatedTiles Catch ex As Exception Logger.Error(ex) MsgBox("Error while creating profile tiles!" & vbNewLine & ex.Message) Return -1 End Try End Function Private Function CreateTile(Profile As ProfileData, CountText As String) As TileItem Dim oItem As New TileItem() With {.Tag = Profile.Guid} oItem.Elements.Clear() Dim oNameElement = New TileItemElement With { .Text = Profile.Name, .TextAlignment = TileItemContentAlignment.TopLeft } oNameElement.Appearance.Normal.Font = PrimaryFont oItem.Elements.Add(oNameElement) Dim oCommentElement = New TileItemElement With { .Text = Profile.Comment, .TextAlignment = TileItemContentAlignment.MiddleLeft } oCommentElement.Appearance.Normal.Font = SecondaryFont oItem.Elements.Add(oCommentElement) Dim oCountElement = New TileItemElement With { .Text = GetCountText(Profile, CountText), .TextAlignment = TileItemContentAlignment.BottomRight } oCountElement.Appearance.Normal.Font = SecondaryFont oItem.Elements.Add(oCountElement) Return oItem End Function Private Function GetCountText(Profile As ProfileData, CountText As String) As String Dim oText As String = "No implemented" If Profile.CountData = INVALID_COUNT_SQL Then oText = "Invalid SQL!" ElseIf Profile.CountData = NO_COUNT_SQL Then oText = "No SQL!" Else oText = CountText End If If Profile.CountDocs = INVALID_COUNT_SQL Then oText = "Invalid SQL!" ElseIf Profile.CountDocs = NO_COUNT_SQL Then oText = "No SQL!" Else oText = CountText End If Return oText End Function Private Sub frmProfileMatch_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing Try ConfigManager.Config.MatchWindowSize = Size ConfigManager.Config.MatchWindowLocation = Location ConfigManager.Save() CURRENT_CLIPBOARD_CONTENTS = Nothing Catch ex As Exception Logger.Error(ex) Logger.Info("Error in Save FormLayout: " & ex.Message) End Try End Sub Private Sub TileControlMatch_ItemClick(sender As Object, e As TileItemEventArgs) Handles TileControlMatch.ItemClick Dim oItem As TileItem = e.Item Dim oProfileId As Integer = oItem.Tag Select Case oItem.Group.Name Case TileGroupData.Name OpenResultForms(oProfileId, ProfileType.DATA_ONLY) Case TileGroupDocuments.Name OpenResultForms(oProfileId, ProfileType.DOCS_ONLY) Case Else OpenResultForms(oProfileId, ProfileType.ANY) End Select Hide() End Sub Private Async Function LoadSearchesAsync(MatchingProfiles As List(Of ProfileData)) As Task(Of List(Of DocumentSearch)) Return Await Task.Run(Function() Return DoLoadSearches(MatchingProfiles) End Function) End Function Private Function DoLoadSearches(MatchingProfiles As List(Of ProfileData)) As List(Of DocumentSearch) Dim oMatchingIds = String.Join(",", MatchingProfiles.Select(Function(p) p.Guid).ToArray()) Dim oSQL As String = $"SELECT * FROM TBCW_PROF_DOC_SEARCH WHERE ACTIVE = 1 AND PROFILE_ID in ({oMatchingIds}) ORDER BY TAB_INDEX" Dim oSearchesDataTable = Database.GetDatatable(oSQL) Dim oDocSearches As New List(Of DocumentSearch) For Each oRow As DataRow In oSearchesDataTable.Rows Dim oGuid As Integer = oRow.Item("GUID") Dim oProfileId As Integer = oRow.Item("PROFILE_ID") Dim oTabTitle As String = oRow.Item("TAB_TITLE") Dim oConnectionId As Integer = oRow.Item("CONN_ID") oSQL = oRow.Item("SQL_COMMAND") oSQL = clsPatterns.ReplaceAllValues(oSQL, USER_PRENAME, USER_SURNAME, USER_SHORTNAME, USER_EMAIL, USER_ID, oProfileId) Dim oDatatable As DataTable = GetDatatable(oSQL, oConnectionId) oDocSearches.Add(New DocumentSearch() With { .Guid = oGuid, .DataTable = oDatatable, .ProfileId = oProfileId, .TabCaption = oTabTitle, .SQLCommand = oSQL }) Next Return oDocSearches End Function Private Async Sub OpenResultForms(ProfileId As Integer, OpenType As ProfileType) Dim oMatchingProfiles As New List(Of ProfileData) ' TODO: Implement Show All ' Click on specific profile Dim oProfile As ProfileData = CURRENT_MATCHING_PROFILES. Where(Function(p) p.Guid = ProfileId). First() oMatchingProfiles.Add(oProfile) Dim oSearches As List(Of DocumentSearch) = Await LoadSearchesAsync(oMatchingProfiles) Dim oEnvironment = GetEnvironment() Dim oParams = GetParams(oProfile, oSearches) If OpenType = ProfileType.ANY Or OpenType = ProfileType.DOCS_ONLY Then ' Show Result Document Form Dim oForm As New frmDocumentResultList(LogConfig, oEnvironment, oParams) ' TODO: Reopen match form 'AddHandler oForm.FormClosed, AddressOf ProfileResultForm_Closed 'OpenForms.Add(oForm) 'Dim oForm As New frmResultDoc(Me, oMatchingProfiles) 'AddHandler oForm.FormClosed, AddressOf ProfileResultForm_Closed 'OpenForms.Add(oForm) oForm.Show() End If If OpenType = ProfileType.ANY Or OpenType = ProfileType.DATA_ONLY Then ' Show Result Data Form Dim oForm As New frmResultSQL(Me, oMatchingProfiles) AddHandler oForm.FormClosed, AddressOf ProfileResultForm_Closed OpenForms.Add(oForm) oForm.Show() End If End Sub Private Function GetParams(Profile As ProfileData, DocumentSearches As List(Of DocumentSearch)) As DocumentResultParams Dim oResults As New List(Of DocumentResult) For Each oSearch In DocumentSearches oResults.Add(New DocumentResult() With { .Datatable = oSearch.DataTable, .Title = oSearch.TabCaption }) Next Dim oParams As New DocumentResultParams() With { .WindowGuid = GetWindowId(Profile, DocumentSearches), .Results = oResults } Return oParams End Function Private Function GetWindowId(Profile As ProfileData, Searches As List(Of DocumentSearch)) As String Dim oNameSlug = Utils.ConvertTextToSlug(Profile.Name) Dim oSearchGuids = Searches.Select(Function(s) s.Guid).ToArray Dim oWindowGuid = $"{Profile.Guid}-{oNameSlug}-{String.Join("-", oSearchGuids)}" Return oWindowGuid End Function Private Function GetEnvironment() As Environment Dim oUser As New State.UserState() With { .DateFormat = USER_DATE_FORMAT, .Email = USER_EMAIL, .GivenName = USER_PRENAME, .Language = USER_LANGUAGE, .MachineName = System.Environment.MachineName, .ShortName = USER_SHORTNAME, .Surname = USER_SURNAME, .UserId = USER_ID, .UserName = USER_USERNAME } Dim oSettings As New State.SettingsState() With { .GdPictureKey = "" } Dim oEnvironment As New Environment() With { .Database = Nothing, .Modules = Nothing, .User = oUser, .Settings = oSettings } Return oEnvironment End Function Private Function GetDatatable(SqlCommand As String, ConnectionId As Integer) Try Dim oRow As MyDataset.TBDD_CONNECTIONRow = DT_CONNECTIONS.AsEnumerable(). Where(Function(r) r.GUID = ConnectionId). FirstOrDefault() Dim oConnectionType As String = oRow.SQL_PROVIDER Select Case oRow.SQL_PROVIDER Case Constants.PROVIDER_MSSQL Dim oSQL As New MSSQLServer(LogConfig, oRow.SERVER, oRow.DATENBANK, oRow.USERNAME, oRow.PASSWORD) Return oSQL.GetDatatable(SqlCommand) Case Constants.PROVIDER_ODBC Dim oODBC As New ODBC(LogConfig, oRow.SERVER, oRow.USERNAME, oRow.PASSWORD) Return oODBC.GetDatatable(SqlCommand) Case Else Dim oOracle As New Database.Oracle(LogConfig, oRow.SERVER, oRow.DATENBANK, oRow.USERNAME, oRow.PASSWORD) Return oOracle.GetDatatable(SqlCommand) End Select Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Private Sub ProfileResultForm_Closed(sender As Object, e As FormClosedEventArgs) Dim oShouldOpenAgain As Boolean = False Dim oThisForm = New List(Of IResultForm) From {sender} If TypeOf sender Is frmResultDoc Or TypeOf sender Is frmResultSQL Then For Each oForm As IResultForm In OpenForms ' Determine if frmProfileMatch should be shown If oForm.ShouldReturnToMatchForm Then oShouldOpenAgain = True End If Next End If ' If frmProfileMatch should be shown, close all windows of this profile If oShouldOpenAgain Then For Each oForm As Form In OpenForms.Except(oThisForm) ' Remove the Handler to prevent a loop RemoveHandler oForm.FormClosed, AddressOf ProfileResultForm_Closed oForm.Close() Next Show() End If End Sub Private Sub AblaufSucheAnzeigenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AblaufSucheAnzeigenToolStripMenuItem.Click frmTreeView.ShowDialog() End Sub End Class