Imports DigitalData.GUIs.Common Imports DigitalData.Modules.Base Imports DigitalData.Modules.Logging Imports DigitalData.Modules.ZooFlow Imports System.Threading.Tasks Public Class SearchRunner Inherits BaseClass Private Const SEARCH_FACT_DATE_DEFAULT As String = "ADDED_WHEN" Public Const CREATED_TOMORROW As String = "TOMORROW" Public Const CREATED_TODAY As String = "ECM_CREATED_TODAY" Public Const CREATED_YESTERDAY As String = "ECM_CREATED_YESTERDAY" Public Const CREATED_LAST_7_DAYS As String = "ECM_CREATED_LAST7DAYS" Public Const CREATED_YEAR_CURRENT As String = "ECM_CREATED_YEAR_CURRENT" Public Const CREATED_YEAR_LAST As String = "ECM_CREATED_YEAR_LAST" Public Const CREATED_MONTH_CURR As String = "ECM_CREATED_MONTH_CURRENT" Public Const CREATED_MONTH_LAST As String = "ECM_CREATED_MONTH_LAST" Public Enum DateConstraint Today Tomorrow Yesterday Last7Days CurrentMonth LastMonth CurrentYear LastYear Undefined End Enum Public Event NeedsRefresh As EventHandler(Of Integer) Public Event Closed As EventHandler(Of Integer) Private Property _ActiveDateConstraint As String = String.Empty Public ReadOnly Property ActiveDateConstraint As String Get Return _ActiveDateConstraint End Get End Property Private _ActiveDateAttribute As String = SEARCH_FACT_DATE_DEFAULT Public ReadOnly Property ActiveDateAttribute As String Get Return _ActiveDateAttribute End Get End Property Public Property BaseSearchSQL As String Public Property ExplicitDate As Boolean = False Private ReadOnly Environment As Environment Private ReadOnly SearchTitle As String Public Sub New(pLogConfig As LogConfig, pEnvironment As Environment, pSearchTitle As String) MyBase.New(pLogConfig) Environment = pEnvironment SearchTitle = pSearchTitle End Sub Public Function RunWithDataTable(pDatatable As DataTable) As SearchResult Return RunWithDataTable(pDatatable, "Suche") End Function Public Function RunWithDataTable(pDatatable As DataTable, pTitle As String) As SearchResult Dim oParams = GetParams(pTitle) oParams.Results.Add(New DocumentResultList.DocumentResult() With { .Title = pTitle, .Datatable = pDatatable }) If pDatatable.Rows.Count = 1 Then oParams.ShowFileList = False End If Dim oForm As New frmDocumentResultList(My.LogConfig, Environment, oParams) ' TODO: Implement, not needed right now 'AddHandler oForm.NeedsRefresh, AddressOf Form_NeedsRefresh AddHandler oForm.FormClosed, AddressOf Form_Closed oForm.Show() Return New SearchResult(pDatatable.Rows.Count) End Function Public Async Function RunWithSearchTerm(pSearchTerm As String) As Task(Of SearchResult) Return Await RunWithSearchTerm(pSearchTerm, Nothing, Nothing, Nothing) End Function Public Async Function RunWithSearchTerm(pSearchTerm As String, pSearchTitle As String) As Task(Of SearchResult) Return Await RunWithSearchTerm(pSearchTerm, Nothing, Nothing, pSearchTitle) End Function Public Async Function RunWithSearchTerm(pSearchTerm As String, pDateFrom As Date, pDateTo As Date) As Task(Of SearchResult) Return Await RunWithSearchTerm(pSearchTerm, pDateFrom, pDateTo, Nothing) End Function Public Async Function RunWithSearchTerm(pSearchTerm As String, pDateFrom As Date, pDateTo As Date, pSearchTitle As String) As Task(Of SearchResult) If pDateFrom.Equals(Date.MinValue) = False Then ExplicitDate = True End If Dim oSearchTerm = pSearchTerm Dim oWindowTitle = GetResultWindowString(pSearchTerm, pSearchTitle) Dim oParams = GetParams(oWindowTitle) Dim oDateConstraint = $"{_ActiveDateAttribute}~{_ActiveDateConstraint}" If ExplicitDate Then Dim oDate2 As Date If pDateTo.Equals(Date.MinValue) Then oDate2 = pDateTo Else oDate2 = pDateFrom End If Dim oProc = $"EXEC PRIDB_SEARCH_ADD_USR_DATE {My.Application.User.UserId},'{pDateFrom}','{oDate2}'" If Await My.Database.ExecuteNonQueryIDBAsync(oProc) = True Then oDateConstraint = $"{_ActiveDateAttribute}~DATEPART" End If End If Dim oSQL = $"EXEC PRIDB_SEARCH_TEXT_GET_RESULTS {My.Application.User.UserId},'{oSearchTerm}','{oDateConstraint}'" If Await My.Database.ExecuteNonQueryIDBAsync(oSQL) = True Then Dim oDTDocResult = Await My.Database.GetDatatableIDBAsync(BaseSearchSQL) Dim oRowCount = oDTDocResult.Rows.Count If oRowCount > 0 Then oParams.Results.Add(New DocumentResultList.DocumentResult() With { .Title = SearchTitle, .Datatable = oDTDocResult }) If oDTDocResult.Rows.Count = 1 Then oParams.ShowFileList = False End If Dim oForm As New frmDocumentResultList(My.LogConfig, Environment, oParams) ' TODO: Implement 'AddHandler oForm.NeedsRefresh, AddressOf Form_NeedsRefresh AddHandler oForm.FormClosed, AddressOf Form_Closed oForm.Show() Return New SearchResult(oRowCount) Else Return New SearchResult(oRowCount) End If Else Return New SearchResult("Error in FlowSearch - Check Your log") End If End Function Private Sub Form_Closed(sender As Object, e As EventArgs) RaiseEvent Closed(sender, 0) End Sub Private Function GetParams(pWindowTitle As String) As DocumentResultList.Params Dim oParams = New DocumentResultList.Params() With { .WindowGuid = SearchTitle, .WindowTitle = pWindowTitle, .OperationModeOverride = Modules.ZooFlow.Constants.OperationMode.ZooFlow, .ProfileGuid = 35452, .ColumnNames = New DocumentResultList.ColumnNames With { .ObjectIdColumn = "DocID" }, .ShowBackNavigation = False } Return oParams End Function Private Function GetResultWindowString(SearchContent As String, pWindowTitle As String) As String If pWindowTitle IsNot Nothing Then Return pWindowTitle End If If SearchContent <> String.Empty Then If My.Application.User.Language = State.UserState.LANG_DE_DE Then Return $"Suche Nach '{SearchContent}'" Else Return $"Search For '{SearchContent}'" End If Else If My.Application.User.Language = State.UserState.LANG_DE_DE Then Return $"Suche Datumsbegrenzt" Else Return $"Search via date" End If End If End Function Public Sub SetDateConstraint() _ActiveDateConstraint = String.Empty End Sub Public Sub SetDateConstraint(pConstraintName As String) _ActiveDateConstraint = pConstraintName End Sub Public Sub SetDateConstraint(pConstraint As DateConstraint) _ActiveDateConstraint = DateConstraintToConstant(pConstraint) End Sub Public Function DateConstraintToConstant(pConstraint As DateConstraint) As String Select Case pConstraint Case DateConstraint.Today Return CREATED_TODAY Case DateConstraint.Yesterday Return CREATED_YESTERDAY Case DateConstraint.Tomorrow Return CREATED_TOMORROW Case DateConstraint.Last7Days Return CREATED_LAST_7_DAYS Case DateConstraint.CurrentMonth Return CREATED_MONTH_CURR Case DateConstraint.LastMonth Return CREATED_MONTH_LAST Case DateConstraint.CurrentYear Return CREATED_YEAR_CURRENT Case DateConstraint.LastYear Return CREATED_YEAR_LAST Case Else Return String.Empty End Select End Function Public Function ConstantToDateConstraint(pConstant As String) As DateConstraint Select Case pConstant Case CREATED_TODAY Return DateConstraint.Today Case CREATED_YESTERDAY Return DateConstraint.Yesterday Case CREATED_TOMORROW Return DateConstraint.Tomorrow Case CREATED_LAST_7_DAYS Return DateConstraint.Last7Days Case CREATED_MONTH_CURR Return DateConstraint.CurrentMonth Case CREATED_MONTH_LAST Return DateConstraint.LastMonth Case CREATED_YEAR_CURRENT Return DateConstraint.CurrentYear Case CREATED_YEAR_LAST Return DateConstraint.LastYear Case Else Return DateConstraint.Undefined End Select End Function Public Sub SetDateAttribute(pAttributeName As String) _ActiveDateAttribute = pAttributeName End Sub Public Sub SetDateAttribute() _ActiveDateAttribute = SEARCH_FACT_DATE_DEFAULT End Sub Public Class SearchResult Public OK As Boolean = True Public Count As Integer = 0 Public ErrorMessage As String = String.Empty ''' ''' Returns a positive Search Result ''' Public Sub New(pCountResults As Integer) Count = pCountResults End Sub ''' ''' Returns a failed search result with an error message ''' ''' Public Sub New(pMessage As String) OK = False ErrorMessage = pMessage End Sub End Class End Class