Imports System.IO

Public Class ClassDOC_SEARCH
    Private Shared DT_RESULTLIST_OPTIONS As DataTable
    Private Shared DT_RESULTLIST_SEL_VALUES As DataTable
    Private Shared ENTITY_ID As Integer
    Private Shared RECORD_ID As Integer
    Private Shared AsyncRunning As Boolean = False
    Private Shared Function GET_DOCSEARCH_DATATABLE(Optional BG As Boolean = False)
        Try
            Dim OPTION1
            Dim OPTION2
            Dim OPTION3
            Dim OPTION4
            Dim OPTION5
            Dim VALUE_CONF1
            Dim VALUE_CONF2
            Dim VALUE_CONF3
            Dim VALUE_CONF4

            If DT_RESULTLIST_OPTIONS.Rows.Count = 0 Then
                OPTION1 = 0
                OPTION2 = 0
                OPTION3 = 0
                OPTION4 = 0
                OPTION5 = 0
            Else
                OPTION1 = DT_RESULTLIST_OPTIONS.Rows(0).Item("OPTION1_CTRL")
                OPTION2 = DT_RESULTLIST_OPTIONS.Rows(0).Item("OPTION2_CTRL")
                OPTION3 = DT_RESULTLIST_OPTIONS.Rows(0).Item("OPTION3_CTRL")
                OPTION4 = DT_RESULTLIST_OPTIONS.Rows(0).Item("OPTION4_CTRL")
                OPTION5 = DT_RESULTLIST_OPTIONS.Rows(0).Item("OPTION5_CTRL")
            End If
            If DT_RESULTLIST_SEL_VALUES.Rows.Count = 0 Then
                VALUE_CONF1 = 0
                VALUE_CONF2 = 0
                VALUE_CONF3 = 0
                VALUE_CONF4 = 0

            Else
                VALUE_CONF1 = DT_RESULTLIST_SEL_VALUES.Rows(0).Item("GUID")
                Try
                    VALUE_CONF2 = DT_RESULTLIST_SEL_VALUES.Rows(1).Item("GUID")
                Catch ex As Exception
                    VALUE_CONF2 = 0
                End Try
                Try
                    VALUE_CONF3 = DT_RESULTLIST_SEL_VALUES.Rows(2).Item("GUID")
                Catch ex As Exception
                    VALUE_CONF3 = 0
                End Try
                Try
                    VALUE_CONF4 = DT_RESULTLIST_SEL_VALUES.Rows(3).Item("GUID")
                Catch ex As Exception
                    VALUE_CONF4 = 0
                End Try

            End If

            Dim SQL_DOC_SEARCH
            If CURRENT_SEARCH_TYPE = "ENTITY" Then
                SQL_DOC_SEARCH = String.Format("SELECT DISTINCT T.*," & vbNewLine & _
                    "[dbo].[FNPMO_GET_CONTROL_VALUES_DOC_ENTITY] ({3},T.DocID,{0}) AS OPTION1," & vbNewLine & _
                    "[dbo].[FNPMO_GET_CONTROL_VALUES_DOC_ENTITY] ({4},T.DocID,{0}) AS OPTION2," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX1') AS DOCIDX1," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX2') AS DOCIDX2," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX3') AS DOCIDX3," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX4') AS DOCIDX4," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN1','{1}') AS DROPDOWN1," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN2','{1}') AS DROPDOWN2," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN3','{1}') AS DROPDOWN3," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN4','{1}') AS DROPDOWN4" & vbNewLine & _
                    "FROM " & vbNewLine & _
                    "VWPMO_DOC_SEARCH T" & vbNewLine & _
                    "WHERE T.DocID IN (select DISTINCT T.DOC_ID from TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD T1 WHERE T.RECORD_ID = T1.GUID AND T1.FORM_ID = {0}) " & vbNewLine & _
                    "AND T.DocID IN (SELECT dwObjectID FROM VWPMO_WD_OBJECT_RIGHTS WHERE dwUserOrGroupID = {2} AND dwObjectID = T.DocID)", ENTITY_ID, USER_LANGUAGE, USERID_FK_INT_ECM, OPTION1, OPTION2)
                If CURRENT_ENTITYSQL_WHERE <> "" Then
                    Dim where = CURRENT_ENTITYSQL_WHERE.Replace("WHERE [RECORD-ID] IN ", "")

                    SQL_DOC_SEARCH = SQL_DOC_SEARCH & " " & vbNewLine & String.Format("AND T.DocID IN (SELECT DISTINCT DOC_ID from TBPMO_DOC_RECORD_LINK WHERE RECORD_ID IN ({0}))", where)
                End If
                If BG = True Then
                    If IsNothing(CURRENT_DT_DOC_ENTITY_SEARCH) Then
                        If AsyncRunning = True Then
                            MsgBox("Entity-Search already running in background! Please wait a while!", MsgBoxStyle.Information)
                            Return Nothing
                        End If
                        Dim async As New ClassAsyncReturnDT(SQL_DOC_SEARCH, "LOAD_ENTITY_SEARCH_AT STARTUP")
                        async.bw.RunWorkerAsync()
                        While async.bw.IsBusy
                            Application.DoEvents()
                        End While
                        CURRENT_DT_DOC_ENTITY_SEARCH = async.dt
                        AsyncRunning = False
                    End If

                End If

            ElseIf CURRENT_SEARCH_TYPE = "RECORD" Then
                SQL_DOC_SEARCH = String.Format("SELECT DISTINCT T.* ," & vbNewLine & _
               "ISNULL(T1.VALUE,'') AS OPTION1," & vbNewLine & _
               "ISNULL(T2.VALUE,'') AS OPTION2," & vbNewLine & _
               "ISNULL(T3.VALUE,'') AS OPTION3," & vbNewLine & _
               "ISNULL(T4.VALUE,'') AS OPTION4," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX1') AS DOCIDX1," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX2') AS DOCIDX2," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX3') AS DOCIDX3," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX4') AS DOCIDX4," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN1','{7}') AS DROPDOWN1," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN2','{7}') AS DROPDOWN2," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN3','{7}') AS DROPDOWN3," & vbNewLine & _
               "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN4','{7}') AS DROPDOWN4," & vbNewLine & _
                "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC.GUID) AS VALUE1," & vbNewLine & _
                "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC1.GUID) AS VALUE2," & vbNewLine & _
                "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC2.GUID) AS VALUE3," & vbNewLine & _
                "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC3.GUID) AS VALUE4" & vbNewLine & _
               "FROM " & vbNewLine & _
               "VWPMO_DOC_SEARCH T " & vbNewLine & _
               "INNER JOIN TBPMO_DOC_RECORD_LINK TRL ON T.DocID = TRL.DOC_ID" & vbNewLine & _
               "INNER JOIN TBPMO_RECORD TR	ON TRL.RECORD_ID = TR.GUID" & vbNewLine & _
               "INNER JOIN VWPMO_WD_OBJECT_RIGHTS TRIGHTS ON T.DocID = TRIGHTS.dwObjectID" & vbNewLine & _
                "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC ON TR.FORM_ID  = TRC.ENTITY_ID AND TRC.LANGUAGE = '{7}' AND TRC.GUID = {8}" & vbNewLine & _
                "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC1 ON TR.FORM_ID  = TRC1.ENTITY_ID AND TRC1.LANGUAGE = '{7}' AND TRC1.GUID = {9}" & vbNewLine & _
                "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC2 ON TR.FORM_ID  = TRC2.ENTITY_ID AND TRC2.LANGUAGE = '{7}' AND TRC2.GUID = {10}" & vbNewLine & _
                "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC3 ON TR.FORM_ID  = TRC3.ENTITY_ID AND TRC3.LANGUAGE = '{7}' AND TRC3.GUID = {11}" & vbNewLine & _
               "LEFT JOIN TBPMO_CONTROL_VALUE T1 ON TRL.RECORD_ID = T1.RECORD_ID AND T1.CONTROL_ID = {2} " & vbNewLine & _
               "LEFT JOIN TBPMO_CONTROL_VALUE T2 ON TRL.RECORD_ID = T2.RECORD_ID AND T2.CONTROL_ID = {3} " & vbNewLine & _
               "LEFT JOIN TBPMO_CONTROL_VALUE T3 ON TRL.RECORD_ID = T3.RECORD_ID AND T3.CONTROL_ID = {4} " & vbNewLine & _
               "LEFT JOIN TBPMO_CONTROL_VALUE T4 ON TRL.RECORD_ID = T4.RECORD_ID AND T4.CONTROL_ID = {5} " & vbNewLine & _
               "WHERE TRL.RECORD_ID = {0} AND TR.FORM_ID  = {1} AND TRIGHTS.dwUserOrGroupID = {6}", RECORD_ID, ENTITY_ID, OPTION1, OPTION2, OPTION3, OPTION4, USERID_FK_INT_ECM, USER_LANGUAGE, VALUE_CONF1, VALUE_CONF2, VALUE_CONF3, VALUE_CONF4)
            ElseIf CURRENT_SEARCH_TYPE = "FULLTEXT" Then
                Dim sqlft = SQL_FULLTEXT
                sqlft = sqlft.Replace("@FULLTEXT", CURRENT_FULLTEXT_PATTERN)
                SQL_DOC_SEARCH = String.Format("SELECT DISTINCT T.*," & vbNewLine & _
                    "[dbo].[FNPMO_GET_CONTROL_VALUES_DOC_ENTITY] ({3},T.DocID,{0}) AS OPTION1," & vbNewLine & _
                    "[dbo].[FNPMO_GET_CONTROL_VALUES_DOC_ENTITY] ({4},T.DocID,{0}) AS OPTION2," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX1') AS DOCIDX1," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX2') AS DOCIDX2," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX3') AS DOCIDX3," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{0},'DOCIDX4') AS DOCIDX4," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN1','{1}') AS DROPDOWN1," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN2','{1}') AS DROPDOWN2," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN3','{1}') AS DROPDOWN3," & vbNewLine & _
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{0},'DROPDOWN4','{1}') AS DROPDOWN4" & vbNewLine & _
                    "FROM " & vbNewLine & _
                    "VWPMO_DOC_SEARCH T" & vbNewLine & _
                    "INNER JOIN TBPMO_DOC_RECORD_LINK TRL ON T.DocID = TRL.DOC_ID" & vbNewLine & _
                    "INNER JOIN TBPMO_RECORD TR ON TRL.RECORD_ID = TR.GUID" & vbNewLine & _
                    "INNER JOIN VWPMO_WD_OBJECT_RIGHTS TRIGHTS ON T.DocID = TRIGHTS.dwObjectID" & vbNewLine & _
                    "LEFT OUTER JOIN TBPMO_CONTROL_VALUE TCTRL ON TR.GUID = TCTRL.RECORD_ID" & vbNewLine & _
                   "WHERE TR.FORM_ID = {0} AND TRIGHTS.dwUserOrGroupID = {2} AND (" & vbNewLine & _
                   "TCTRL.VALUE LIKE '%{6}%' OR" & vbNewLine & _
                   "T.DocID IN ({5}" & vbNewLine & _
                   "))", ENTITY_ID, USER_LANGUAGE, USERID_FK_INT_ECM, OPTION1, OPTION2, sqlft, CURRENT_FULLTEXT_PATTERN)
            ElseIf CURRENT_SEARCH_TYPE = "NODE_DOWN" Then
                SQL_DOC_SEARCH = String.Format("SELECT DISTINCT T.* ," & vbNewLine & _
             "ISNULL(T1.VALUE,'') AS OPTION1," & vbNewLine & _
             "ISNULL(T2.VALUE,'') AS OPTION2," & vbNewLine & _
             "ISNULL(T3.VALUE,'') AS OPTION3," & vbNewLine & _
             "ISNULL(T4.VALUE,'') AS OPTION4," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX1') AS DOCIDX1," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX2') AS DOCIDX2," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX3') AS DOCIDX3," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX4') AS DOCIDX4," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN1','{7}') AS DROPDOWN1," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN2','{7}') AS DROPDOWN2," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN3','{7}') AS DROPDOWN3," & vbNewLine & _
             "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN4','{7}') AS DROPDOWN4," & vbNewLine & _
              "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC.GUID) AS VALUE1," & vbNewLine & _
              "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC1.GUID) AS VALUE2," & vbNewLine & _
              "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC2.GUID) AS VALUE3," & vbNewLine & _
              "(SELECT VALUE FROM TBPMO_DOC_VALUES WHERE DocID = T.DocID AND CONFIG_ID = TRC3.GUID) AS VALUE4" & vbNewLine & _
             "FROM " & vbNewLine & _
             "VWPMO_DOC_SEARCH T " & vbNewLine & _
             "INNER JOIN TBPMO_DOC_RECORD_LINK TRL ON T.DocID = TRL.DOC_ID" & vbNewLine & _
             "INNER JOIN TBPMO_RECORD TR ON TRL.RECORD_ID = TR.GUID" & vbNewLine & _
             "INNER JOIN TBPMO_STRUCTURE_NODES_USER_TEMP TTEMP ON TR.GUID = TTEMP.RECORD_ID" & vbNewLine & _
             "INNER JOIN VWPMO_WD_OBJECT_RIGHTS TRIGHTS ON T.DocID = TRIGHTS.dwObjectID" & vbNewLine & _
              "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC ON TR.FORM_ID  = TRC.ENTITY_ID AND TRC.LANGUAGE = '{7}' AND TRC.GUID = {8}" & vbNewLine & _
              "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC1 ON TR.FORM_ID  = TRC1.ENTITY_ID AND TRC1.LANGUAGE = '{7}' AND TRC1.GUID = {9}" & vbNewLine & _
              "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC2 ON TR.FORM_ID  = TRC2.ENTITY_ID AND TRC2.LANGUAGE = '{7}' AND TRC2.GUID = {10}" & vbNewLine & _
              "LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC3 ON TR.FORM_ID  = TRC3.ENTITY_ID AND TRC3.LANGUAGE = '{7}' AND TRC3.GUID = {11}" & vbNewLine & _
             "LEFT JOIN TBPMO_CONTROL_VALUE T1 ON TRL.RECORD_ID = T1.RECORD_ID AND T1.CONTROL_ID = {2} " & vbNewLine & _
             "LEFT JOIN TBPMO_CONTROL_VALUE T2 ON TRL.RECORD_ID = T2.RECORD_ID AND T2.CONTROL_ID = {3} " & vbNewLine & _
             "LEFT JOIN TBPMO_CONTROL_VALUE T3 ON TRL.RECORD_ID = T3.RECORD_ID AND T3.CONTROL_ID = {4} " & vbNewLine & _
             "LEFT JOIN TBPMO_CONTROL_VALUE T4 ON TRL.RECORD_ID = T4.RECORD_ID AND T4.CONTROL_ID = {5} " & vbNewLine & _
             "WHERE TR.FORM_ID  = {1} AND TTEMP.USER_ID = {0} AND TRIGHTS.dwUserOrGroupID = {6}", USER_GUID, ENTITY_ID, OPTION1, OPTION2, OPTION3, OPTION4, USERID_FK_INT_ECM, USER_LANGUAGE, VALUE_CONF1, VALUE_CONF2, VALUE_CONF3, VALUE_CONF4)
            ElseIf CURRENT_SEARCH_TYPE = "FULLTEXT_SUBNODES" Then
                Dim sqlft = SQL_FULLTEXT
                sqlft = sqlft.Replace("@FULLTEXT", CURRENT_FULLTEXT_PATTERN)
                SQL_DOC_SEARCH = String.Format("SELECT DISTINCT T.*," & vbNewLine & _
                    "[dbo].[FNPMO_GET_CONTROL_VALUES_DOC_ENTITY] ({4},T.DocID,{1}) AS OPTION1," & vbNewLine & _
                    "[dbo].[FNPMO_GET_CONTROL_VALUES_DOC_ENTITY] ({5},T.DocID,{1}) AS OPTION2," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX1') AS DOCIDX1," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX2') AS DOCIDX2," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX3') AS DOCIDX3," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_IDX] (T.DocID,{1},'DOCIDX4') AS DOCIDX4," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN1','{0}') AS DROPDOWN1," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN2','{0}') AS DROPDOWN2," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN3','{0}') AS DROPDOWN3," & vbNewLine & _
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN] (T.DocID,{1},'DROPDOWN4','{0}') AS DROPDOWN4" & vbNewLine & _
                   "FROM " & vbNewLine & _
                   "VWPMO_DOC_SEARCH T" & vbNewLine & _
                  "INNER JOIN TBPMO_DOC_RECORD_LINK TRL ON T.DocID = TRL.DOC_ID" & vbNewLine & _
                  "INNER JOIN TBPMO_RECORD TR ON TRL.RECORD_ID = TR.GUID" & vbNewLine & _
                  "INNER JOIN TBPMO_STRUCTURE_NODES_USER_TEMP TTEMP ON TR.GUID = TTEMP.RECORD_ID" & vbNewLine & _
                  "INNER JOIN VWPMO_WD_OBJECT_RIGHTS TRIGHTS ON T.DocID = TRIGHTS.dwObjectID" & vbNewLine & _
                  "WHERE TR.FORM_ID  = {1} AND TTEMP.USER_ID = {2} AND TRIGHTS.dwUserOrGroupID = {3}" & vbNewLine & _
                  "AND T.DocID IN ({6})", USER_LANGUAGE, ENTITY_ID, USER_GUID, USERID_FK_INT_ECM, OPTION1, OPTION2, sqlft)
            End If
            If LogErrorsOnly = False Then ClassLogger.Add(" >> SQL_DOC_SEARCH: " & SQL_DOC_SEARCH, False)
            If BG = True And CURRENT_SEARCH_TYPE = "ENTITY" Then
                If Not IsNothing(CURRENT_DT_DOC_ENTITY_SEARCH) Then
                    Return CURRENT_DT_DOC_ENTITY_SEARCH
                End If

            End If
            Return ClassDatabase.Return_Datatable(SQL_DOC_SEARCH, True)
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in GET_DOCSEARCH_DATATABLE: " & ex.Message, True)
            Return Nothing
        End Try
    End Function
    Public Shared Function Get_DOCRESULTS(vDT_RESULTLIST_OPTIONS As DataTable, vDT_RESULTLIST_SELVALUES As DataTable, DT_WINDREAM_RESULTLIST As DataTable, ENT_ID As Integer, vType As String, Optional ftString As String = "", Optional BG As Boolean = False)
        ENTITY_ID = ENT_ID
        DT_RESULTLIST_OPTIONS = vDT_RESULTLIST_OPTIONS
        DT_RESULTLIST_SEL_VALUES = vDT_RESULTLIST_SELVALUES
        Return GET_DOCSEARCH_DATATABLE(BG)
    End Function
    Public Shared Function Get_DOC_RESULTTABLE(vDT_RESULTLIST_OPTIONS As DataTable, vDT_RESULTLIST_SELVALUES As DataTable, DT_WINDREAM_RESULTLIST As DataTable, ENT_ID As Integer, REC_ID As Integer, Optional BG As Boolean = False, Optional Preload As Boolean = False)
        Dim COL_ARRAY_RESULTLIST As List(Of String)
        ENTITY_ID = ENT_ID
        DT_RESULTLIST_OPTIONS = vDT_RESULTLIST_OPTIONS
        DT_RESULTLIST_SEL_VALUES = vDT_RESULTLIST_SELVALUES
        RECORD_ID = REC_ID
        Try
            Dim sw As New Stopwatch
            sw.Start()
            Dim elapsed As Double
            Dim DT_FILE_RESULT As DataTable = GET_DOCSEARCH_DATATABLE(BG)
            CURRENT_DOC_RESULTS = DT_FILE_RESULT
            If IsNothing(DT_FILE_RESULT) Then
                ClassLogger.Add(">> ATTENTION: GET_DOCSEARCH_DATATABLE IS NOTHING!!", False)
                Return Nothing
            End If
            elapsed = sw.Elapsed.TotalSeconds
            sw.Stop()
            sw.Reset()
            Console.WriteLine(">> GET_DOCSEARCH_DATATABLE took " & Format(elapsed, "0.000000000") & " seconds")
            If LogErrorsOnly = False Then ClassLogger.Add(" >> Run GET_DOCSEARCH_DATATABLE took " & Format(elapsed, "0.000000000") & " seconds", False)
            If Preload = True Then
                Return Nothing
            End If
            sw.Start()
            Dim DTGrid As New DataTable
            Dim ColArrayDEFINITION As New List(Of String)
            Dim listcheck As New List(Of String)
            For Each row As DataRow In DT_WINDREAM_RESULTLIST.Rows
                Dim type = row.Item("TYPE_ID")
                Dim COLNAME = row.Item("HEADER_CAPTION")
                If type = 2 Then
                    listcheck.Add(COLNAME)
                End If
            Next
            For Each col1 As String In listcheck
                Dim collist As DataColumn = DT_FILE_RESULT.Columns(col1)
                If Not IsNothing(collist) Then
                    collist.DataType = GetType(Boolean)
                End If
            Next
            Try
                'Die Icon Colum erstellen und konfigurieren
                Dim colIcon As New System.Data.DataColumn()
                colIcon.DataType = GetType(Image)
                colIcon.ColumnName = "ICON"
                colIcon.Caption = ""
                DTGrid.Columns.Add(colIcon)
                '
                Dim colPath As New System.Data.DataColumn()
                colPath.DataType = GetType(String)
                colPath.ColumnName = "FULLPATH"
                colPath.Caption = "Fullpath"
                DTGrid.Columns.Add(colPath)

                Dim colOtype As New System.Data.DataColumn()
                colOtype.DataType = GetType(String)
                colOtype.ColumnName = "OBJECTTYPE"
                colOtype.Caption = "Objecttype"
                DTGrid.Columns.Add(colOtype)
                Dim colDocID As New System.Data.DataColumn()
                colDocID.DataType = GetType(Int32)
                colDocID.ColumnName = "DocID"
                colDocID.Caption = "DocID"
                DTGrid.Columns.Add(colDocID)


                If Not DT_WINDREAM_RESULTLIST Is Nothing And DT_WINDREAM_RESULTLIST.Rows.Count >= 1 Then
                    Dim ColCount As Integer = 1
                    For Each rw As DataRow In DT_WINDREAM_RESULTLIST.Rows
                        Dim ColumnName = rw.Item("HEADER_CAPTION")
                        Dim Width = rw.Item("WIDTH")
                        If IsNothing(Width) Or IsDBNull(Width) Then
                            Width = 200
                        End If
                        'Die Spalte definieren
                        DTGrid.Columns.Add(ColumnName)
                        ColCount += 1
                    Next
                    'Jetzt das Array zusammenbauen um die Spaltenwerte zu erhalten
                    For Each rw As DataRow In DT_WINDREAM_RESULTLIST.Rows
                        ColArrayDEFINITION.Add(rw.Item("COLUMN_VIEW"))
                    Next
                End If
            Catch ex As Exception
                MsgBox("Unexpected Error in Construct Datatable for GridResult: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
            elapsed = sw.Elapsed.TotalSeconds
            sw.Stop()
            sw.Reset()
            Console.WriteLine(">> DTGrid-Definition took " & Format(elapsed, "0.000000000") & " seconds")
            If LogErrorsOnly = False Then ClassLogger.Add(" >> DTGrid-Definition took " & Format(elapsed, "0.000000000") & " seconds", False)
            sw.Start()
            COL_ARRAY_RESULTLIST = ColArrayDEFINITION

            For Each col1 As String In listcheck
                Dim collist As DataColumn = DTGrid.Columns(col1)
                If Not IsNothing(collist) Then
                    collist.DataType = GetType(Boolean)
                End If
            Next


            For Each FILE_ROW As DataRow In DT_FILE_RESULT.Rows
                Dim fullpath = FILE_ROW.Item("FULL_FILENAME")
                Dim objecttype = FILE_ROW.Item("OBJECTTYPE")
                Dim DocID = FILE_ROW.Item("DocID")
                'Dim Folderpath = Path.GetDirectoryName(fullpath)
                Dim filename = Path.GetFileName(fullpath)
                Dim extension = Path.GetExtension(fullpath)
                '                Try
                Dim NewRow As DataRow
                NewRow = DTGrid.NewRow()

                'Icon zuweisen
                Select Case extension.ToUpper
                    Case ".csv".ToUpper
                        NewRow.Item(0) = My.Resources.doc_excel_csv
                    Case ".txt".ToUpper
                        NewRow.Item(0) = My.Resources.text
                    Case ".pdf".ToUpper
                        NewRow.Item(0) = My.Resources.pdf
                    Case ".doc".ToUpper
                        NewRow.Item(0) = My.Resources.doc
                    Case ".docx".ToUpper
                        NewRow.Item(0) = My.Resources.doc
                    Case ".xls".ToUpper
                        NewRow.Item(0) = My.Resources.xls
                    Case ".xlsx".ToUpper
                        NewRow.Item(0) = My.Resources.xls
                    Case ".ppt".ToUpper
                        NewRow.Item(0) = My.Resources.ppt
                    Case ".pptx".ToUpper
                        NewRow.Item(0) = My.Resources.ppt
                    Case ".dwg".ToUpper
                        NewRow.Item(0) = My.Resources.dwg
                    Case ".dxf".ToUpper
                        NewRow.Item(0) = My.Resources.dxf
                    Case ".msg".ToUpper
                        NewRow.Item(0) = My.Resources.email_go
                    Case ".msg".ToUpper
                        NewRow.Item(0) = My.Resources.email_go
                    Case Else
                        NewRow.Item(0) = My.Resources._blank
                End Select
                'Den Filepath mitgeben
                NewRow.Item(1) = fullpath
                NewRow.Item(2) = objecttype
                NewRow.Item(3) = DocID
                Dim i = 4 'Fängt bei 2 an, um die definierten Spalten zu überspringen
                For Each Colname As String In ColArrayDEFINITION
                    Dim rowvalue
                    Try
                        rowvalue = FILE_ROW.Item(Colname)
                        'If LogErrorsOnly = False Then ClassLogger.Add(" >> rowvalue for Colname '" & Colname & "': '" & rowvalue.ToString & "'", False)
                    Catch ex As Exception
                        If CURRENT_SEARCH_TYPE = "RECORD" Then ClassLogger.Add("Unexpected Error in GetVariableValue for Column '" & Colname & "'- Error: " & ex.Message)
                        If Colname.ToString.StartsWith("doct") Or Colname.ToString.StartsWith("Dokum") Then
                            rowvalue = ""
                        Else
                            If CURRENT_SEARCH_TYPE <> "RECORD" Then
                                rowvalue = "Not displayable in Group-Search"
                            Else
                                rowvalue = "Error getting rowvalue"
                            End If

                        End If
                    End Try
                    NewRow.Item(i) = rowvalue.ToString
                    i += 1
                Next
                DTGrid.Rows.Add(NewRow)
                '                Catch ex As Exception
                '                    ClassLogger.Add("Unexpected Error in CreateRow for doc '" & filename & "- Error: " & ex.Message)
                '                End Try
            Next


            Return DTGrid

            elapsed = sw.Elapsed.TotalSeconds
            sw.Stop()
            sw.Reset()
            If LogErrorsOnly = False Then ClassLogger.Add(" >> Run Get_DOC_RESULTTABLE took " & Format(elapsed, "0.000000000") & " seconds", False)
        Catch ex As Exception
            MsgBox("Unexpected Error in Get_DOC_RESULTTABLE: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Shared Function REFRESH_DOC_TABLE_RESULTS()
        Try
            Dim execute = "EXEC[dbo].[PRPMO_DOCRESULT_CHANGE]"
            Return ClassDatabase.Execute_non_Query_withConn(execute, 1)
        Catch ex As Exception
            MsgBox("Unexpected Error in REFRESH_TABLE_RESULTS: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
    Public Shared Function CREATE_DOC_RELATED_LINKS(DOC_ID As Integer, RECORD_ID As Integer)
        Try
            Dim execute = String.Format("EXEC [dbo].[PRPMO_DOC_CREATE_NEW_DOC] {0},{1},'{2}'", DOC_ID, RECORD_ID, USER_USERNAME)
            If ClassDatabase.Execute_non_Query(execute) = True Then
                If LICENSE_SITE_PROXY = True And DD_LIB_Standards.clsDatabase.DB_PROXY_INITIALIZED Then
                    execute = String.Format("EXEC [dbo].[PRPROXY_DOC_CREATE_NEW_DOC] {0},{1},'{2}'", DOC_ID, RECORD_ID, USER_USERNAME)
                    ClassDatabase.Execute_non_Query(execute, True)
                End If
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox("Unexpected Error in CREATE_DOC_RELATED_LINKS: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
    Public Shared Function SET_WD_RIGHTS(ByVal RESULT_DOCID, ByVal RESULT_DOC_PATH)
        Try
            If CURRENT_RECORD_ID = 0 Then
                MsgBox("No Record selected - Please select one!", MsgBoxStyle.Exclamation)
                Return False
            End If
            If LogErrorsOnly = False Then ClassLogger.Add("#### SETTING RIGHTS FOR FILE ######", False)
            If ClassWDRights.Init = True Then
                If ClassWDRights.Doc_Renew_Rights(RESULT_DOCID, RESULT_DOC_PATH, True) Then
                    If ClassWDRights.MSG_RESULT <> "" Then
                        Dim msg = "Achtung: einige Rechte konnten nicht gesetzt werden: " & vbNewLine & ClassWDRights.MSG_RESULT.MSG_RESULT
                        If USER_LANGUAGE <> "de-DE" Then
                            msg = "Attention: some rights could not be set: " & vbNewLine & ClassWDRights.MSG_RESULT.MSG_RESULT
                        End If
                        ClassHelper.MSGBOX_Handler("INFO", "Information:", msg)
                    End If
                    Return True
                Else
                    Return False
                End If
            Else
                MsgBox("Could no initialize right-module - Check the log!", MsgBoxStyle.Exclamation)
                Return False
            End If
            'DD_Rights.clsLogger.Init(Application.UserAppDataPath() & "\Log", USER_USERNAME)
            'If DD_Rights.clsDatabase.Init(MyConnectionString) = True Then
            '    If DD_Rights.ClassRights.Init(ENTITY_ID, LogErrorsOnly) = True Then
            '        If DD_Rights.ClassRights.Collect_Users(CURRENT_RECORD_ID, ENTITY_ID) = True Then
            '            If DD_Rights.ClassRights.File_DeleteAndSetRight(RESULT_DOCID, RESULT_DOC_PATH, True) Then
            '                If DD_Rights.ClassRights.MSG_RESULT <> "" Then
            '                    Dim msg = "Achtung: einige Rechte konnten nicht gesetzt werden: " & vbNewLine & DD_Rights.ClassRights.MSG_RESULT
            '                    If USER_LANGUAGE <> "de-DE" Then
            '                        msg = "Attention: some rights could not be set: " & vbNewLine & DD_Rights.ClassRights.MSG_RESULT
            '                    End If
            '                    MsgBox(msg, MsgBoxStyle.Exclamation)
            '                    Return True
            '                Else
            '                    Return True
            '                End If
            '            Else
            '                Return False
            '            End If
            '        Else
            '            Return False
            '        End If
            '    Else
            '        Return False
            '    End If
            'Else
            '    Return False
            'End If
        Catch ex As Exception
            MsgBox("Unexpected Error in SET_WD_RIGHTS: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
End Class
