Zooflow: Refactor Search for
This commit is contained in:
@@ -52,11 +52,13 @@ Public Class SearchRunner
|
||||
|
||||
Private ReadOnly Environment As Environment
|
||||
Private ReadOnly SearchTitle As String
|
||||
Public ReadOnly Property UserId As Integer
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig, pEnvironment As Environment, pSearchTitle As String)
|
||||
MyBase.New(pLogConfig)
|
||||
Environment = pEnvironment
|
||||
SearchTitle = pSearchTitle
|
||||
UserId = My.Application.User.UserId
|
||||
End Sub
|
||||
|
||||
Public Function RunWithDataTable(pDatatable As DataTable) As SearchResult
|
||||
@@ -98,44 +100,13 @@ Public Class SearchRunner
|
||||
End Function
|
||||
|
||||
Public Async Function RunWithSearchTerm(pSearchTerm As String, pDateFrom As Date, pDateTo As Date, pSearchTokens As IEnumerable(Of Search.SearchToken.AttributeValueToken), 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}"
|
||||
Dim oUserId = My.Application.User.UserId
|
||||
Dim oDateConstraint = GetDateConstraint(pDateFrom, pDateTo)
|
||||
|
||||
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 {oUserId},'{pDateFrom}','{oDate2}'"
|
||||
If Await My.Database.ExecuteNonQueryIDBAsync(oProc) = True Then
|
||||
oDateConstraint = $"{_ActiveDateAttribute}~DATEPART"
|
||||
End If
|
||||
End If
|
||||
Await InsertSearchTokens(pSearchTokens)
|
||||
|
||||
Await My.Database.ExecuteNonQueryIDBAsync($"DELETE FROM TBIDB_SEARCH_INPUT_USER WHERE USR_ID = {oUserId}")
|
||||
|
||||
If pSearchTokens IsNot Nothing AndAlso pSearchTokens.Count > 0 Then
|
||||
|
||||
For Each oToken In pSearchTokens
|
||||
Dim oSQLInsert As String = $"
|
||||
INSERT INTO [dbo].[TBIDB_SEARCH_INPUT_USER] ([USR_ID], [ATTR_ID], [ATTR_TITLE], [TERM_ID], [OPERATOR])
|
||||
VALUES ({oUserId}, {oToken.AttributeId}, '{oToken.AttributeTitle}', {oToken.TermId}, 'AND')"
|
||||
|
||||
Dim oResult = Await My.Database.ExecuteNonQueryIDBAsync(oSQLInsert)
|
||||
Next
|
||||
|
||||
End If
|
||||
|
||||
Dim oSQL = $"EXEC PRIDB_SEARCH_TEXT_GET_RESULTS {oUserId},'{oSearchTerm}','{oDateConstraint}'"
|
||||
Dim oSQL = $"EXEC PRIDB_SEARCH_TEXT_GET_RESULTS {UserId},'{pSearchTerm}','{oDateConstraint}'"
|
||||
|
||||
If Await My.Database.ExecuteNonQueryIDBAsync(oSQL) = True Then
|
||||
Dim oDTDocResult = Await My.Database.GetDatatableIDBAsync(BaseSearchSQL)
|
||||
@@ -169,6 +140,63 @@ Public Class SearchRunner
|
||||
End If
|
||||
End Function
|
||||
|
||||
Private Async Function GetDateConstraint(pDateFrom As Date, pDateTo As Date) As Task(Of String)
|
||||
Dim oSimpleDateConstraint = $"{_ActiveDateAttribute}~{_ActiveDateConstraint}"
|
||||
Dim oExplicitConstraint = Await MaybeSetExplicitDateConstraint(pDateFrom, pDateTo)
|
||||
|
||||
If IsNothing(oExplicitConstraint) Then
|
||||
Return oSimpleDateConstraint
|
||||
Else
|
||||
Return oExplicitConstraint
|
||||
End If
|
||||
End Function
|
||||
|
||||
Private Async Function InsertSearchTokens(pTokens As IEnumerable(Of Search.SearchToken.AttributeValueToken)) As Task
|
||||
Logger.Debug("Deleting previous user tokens..")
|
||||
Await My.Database.ExecuteNonQueryIDBAsync($"DELETE FROM TBIDB_SEARCH_INPUT_USER WHERE USR_ID = {UserId}")
|
||||
|
||||
If pTokens Is Nothing Then
|
||||
Logger.Warn("Token Object was nothing!")
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If pTokens.Count = 0 Then
|
||||
Logger.Warn("Token Object was empty!")
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
For Each oToken In pTokens
|
||||
Dim oSQLInsert As String = $"
|
||||
INSERT INTO [dbo].[TBIDB_SEARCH_INPUT_USER] ([USR_ID], [ATTR_ID], [ATTR_TITLE], [TERM_ID], [OPERATOR])
|
||||
VALUES ({UserId}, {oToken.AttributeId}, '{oToken.AttributeTitle}', {oToken.TermId}, 'AND')"
|
||||
|
||||
Dim oResult = Await My.Database.ExecuteNonQueryIDBAsync(oSQLInsert)
|
||||
Logger.Warn("Inserting Tokens failed!")
|
||||
Next
|
||||
End Function
|
||||
|
||||
|
||||
Private Async Function MaybeSetExplicitDateConstraint(pDateFrom As Date, pDateTo As Date) As Task(Of String)
|
||||
If pDateFrom.Equals(Date.MinValue) = False Then
|
||||
ExplicitDate = True
|
||||
|
||||
Dim oDateTo As Date
|
||||
If pDateTo.Equals(Date.MinValue) Then
|
||||
oDateTo = pDateTo
|
||||
Else
|
||||
oDateTo = pDateFrom
|
||||
End If
|
||||
Dim oProc = $"EXEC PRIDB_SEARCH_ADD_USR_DATE {UserId},'{pDateFrom}','{oDateTo}'"
|
||||
If Await My.Database.ExecuteNonQueryIDBAsync(oProc) = True Then
|
||||
Return $"{_ActiveDateAttribute}~DATEPART"
|
||||
Else
|
||||
Return Nothing
|
||||
End If
|
||||
End If
|
||||
|
||||
Return Nothing
|
||||
End Function
|
||||
|
||||
Private Sub Form_Closed(sender As Object, e As EventArgs)
|
||||
RaiseEvent Closed(sender, 0)
|
||||
End Sub
|
||||
|
||||
Reference in New Issue
Block a user