Imports System.IO
Imports DD_LIB_Standards
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 = 0
            Dim VALUE_CONF2 = 0
            Dim VALUE_CONF3 = 0
            Dim VALUE_CONF4 = 0
            Dim VALUE_CONF5 = 0
            Dim VALUE_CONF6 = 0
            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
            LOGGER.Info("DT_RESULTLIST_SEL_VALUES.Rows.Count: " & DT_RESULTLIST_SEL_VALUES.Rows.Count)

            For Each oRow As DataRow In DT_RESULTLIST_SEL_VALUES.Rows
                If oRow.Item("COLUMN_VIEW").ToString = "VALUE1" Then
                    VALUE_CONF1 = oRow.Item("GUID")
                ElseIf oRow.Item("COLUMN_VIEW").ToString = "VALUE2" Then
                    VALUE_CONF2 = oRow.Item("GUID")
                ElseIf oRow.Item("COLUMN_VIEW").ToString = "VALUE3" Then
                    VALUE_CONF3 = oRow.Item("GUID")
                ElseIf oRow.Item("COLUMN_VIEW").ToString = "VALUE4" Then
                    VALUE_CONF4 = oRow.Item("GUID")
                ElseIf oRow.Item("COLUMN_VIEW").ToString = "VALUE5" Then
                    VALUE_CONF5 = oRow.Item("GUID")
                ElseIf oRow.Item("COLUMN_VIEW").ToString = "VALUE6" Then
                    VALUE_CONF6 = oRow.Item("GUID")
                End If
            Next

            Dim SQL_DOC_SEARCH
            If CURRENT_SEARCH_TYPE = "ENTITY" Then
                SQL_DOC_SEARCH = String.Format("SELECT DISTINCT T.*," & vbNewLine &
                    "'NO OPTION IN ENTITY-SEARCH' AS OPTION1," & vbNewLine &
                    "'NO OPTION IN ENTITY-SEARCH' AS OPTION2," & vbNewLine &
                    "'NO INDEX IN ENTITY-SEARCH' AS DOCIDX1," & vbNewLine &
                    "'NO INDEX IN ENTITY-SEARCH' AS DOCIDX2," & vbNewLine &
                    "'NO INDEX IN ENTITY-SEARCH' AS DOCIDX3," & vbNewLine &
                    "'NO INDEX IN ENTITY-SEARCH' AS DOCIDX4," & vbNewLine &
                    "'NO DROPDOWN IN ENTITY-SEARCH' AS DROPDOWN1," & vbNewLine &
                    "'NO DROPDOWN IN ENTITY-SEARCH' AS DROPDOWN2," & vbNewLine &
                    "'NO DROPDOWN IN ENTITY-SEARCH' AS DROPDOWN3," & vbNewLine &
                    "'NO DROPDOWN IN ENTITY-SEARCH' 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 = SQL_DOC_RESULT_RECORD
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@ENTITY_ID", ENTITY_ID)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@USER_LANGUAGE", USER_LANGUAGE)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@VALUE_CONF1", VALUE_CONF1)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@VALUE_CONF2", VALUE_CONF2)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@VALUE_CONF3", VALUE_CONF3)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@VALUE_CONF4", VALUE_CONF4)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@VALUE_CONF5", VALUE_CONF5)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@VALUE_CONF6", VALUE_CONF6)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@OPTION1", OPTION1)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@OPTION2", OPTION2)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@OPTION3", OPTION3)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@OPTION4", OPTION4)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@RECORD_ID", RECORD_ID)
                SQL_DOC_SEARCH = SQL_DOC_SEARCH.ToString.Replace("@USERID_FK_INT_ECM", USERID_FK_INT_ECM)

                '  SQL_DOC_SEARCH = $"SELECT 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_VALUE] (T.DocID,{1},'DROPDOWN1','{7}',TRL.RECORD_ID) AS DROPDOWN1," & vbNewLine &
                ' $"[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN2','{7}',TRL.RECORD_ID) AS DROPDOWN2," & vbNewLine &
                ' $"[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN3','{7}',TRL.RECORD_ID) AS DROPDOWN3," & vbNewLine &
                ' $"[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN4','{7}',TRL.RECORD_ID) AS DROPDOWN4," & vbNewLine &
                ' $"COALESCE([dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC.GUID),'') AS VALUE1," & vbNewLine &
                ' $"COALESCE([dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC1.GUID),'') AS VALUE2," & vbNewLine &
                ' $"COALESCE([dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC2.GUID),'') AS VALUE3," & vbNewLine &
                ' $"COALESCE([dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC3.GUID),'') AS VALUE4," & vbNewLine &
                '$"COALESCE([dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC4.GUID),'') AS VALUE5," & vbNewLine &
                '$"COALESCE([dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC5.GUID),'') AS VALUE6," & vbNewLine &
                '$"dwAccessRight AS ACCESS_RIGHT" & 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 = '{USER_LANGUAGE}' AND TRC.GUID = {VALUE_CONF1}" & vbNewLine &
                '  $"LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC1 ON TR.FORM_ID  = TRC1.ENTITY_ID AND TRC1.LANGUAGE = '{USER_LANGUAGE}' AND TRC1.GUID = {VALUE_CONF2}" & vbNewLine &
                '  $"LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC2 ON TR.FORM_ID  = TRC2.ENTITY_ID AND TRC2.LANGUAGE = '{USER_LANGUAGE}' AND TRC2.GUID = {VALUE_CONF3}" & vbNewLine &
                '  $"LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC3 ON TR.FORM_ID  = TRC3.ENTITY_ID AND TRC3.LANGUAGE = '{USER_LANGUAGE}' AND TRC3.GUID = {VALUE_CONF4}" & vbNewLine &
                '  $"LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC4 ON TR.FORM_ID  = TRC4.ENTITY_ID AND TRC4.LANGUAGE = '{USER_LANGUAGE}' AND TRC4.GUID = {VALUE_CONF5}" & vbNewLine &
                '  $"LEFT JOIN TBPMO_DOCSEARCH_RESULTLIST_CONFIG TRC5 ON TR.FORM_ID  = TRC5.ENTITY_ID AND TRC5.LANGUAGE = '{USER_LANGUAGE}' AND TRC5.GUID = {VALUE_CONF6}" & vbNewLine &
                ' $"LEFT JOIN TBPMO_CONTROL_VALUE T1 ON TRL.RECORD_ID = T1.RECORD_ID AND T1.CONTROL_ID = {OPTION1} " & vbNewLine &
                ' $"LEFT JOIN TBPMO_CONTROL_VALUE T2 ON TRL.RECORD_ID = T2.RECORD_ID AND T2.CONTROL_ID = {OPTION2} " & vbNewLine &
                ' $"LEFT JOIN TBPMO_CONTROL_VALUE T3 ON TRL.RECORD_ID = T3.RECORD_ID AND T3.CONTROL_ID = {OPTION3} " & vbNewLine &
                ' $"LEFT JOIN TBPMO_CONTROL_VALUE T4 ON TRL.RECORD_ID = T4.RECORD_ID AND T4.CONTROL_ID = {OPTION4} " & vbNewLine &
                ' $"WHERE TRL.RECORD_ID = {RECORD_ID} AND TR.FORM_ID  = {ENTITY_ID} AND TRIGHTS.dwUserOrGroupID = {USERID_FK_INT_ECM}"
            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_VALUE] (T.DocID,{0},'DROPDOWN1','{1}',TRL.RECORD_ID) AS DROPDOWN1," & vbNewLine &
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{0},'DROPDOWN2','{1}',TRL.RECORD_ID) AS DROPDOWN2," & vbNewLine &
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{0},'DROPDOWN3','{1}',TRL.RECORD_ID) AS DROPDOWN3," & vbNewLine &
                    "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{0},'DROPDOWN4','{1}',TRL.RECORD_ID) 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_VALUE] (T.DocID,{1},'DROPDOWN1','{7}',TRL.RECORD_ID) AS DROPDOWN1," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN2','{7}',TRL.RECORD_ID) AS DROPDOWN2," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN3','{7}',TRL.RECORD_ID) AS DROPDOWN3," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN4','{7}',TRL.RECORD_ID) AS DROPDOWN4," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC.GUID) AS VALUE1," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC1.GUID) AS VALUE2," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_ID,TRC2.GUID) AS VALUE3," & vbNewLine &
             "[dbo].[FNPMO_DOC_GET_DOC_VALUE] (T.DocID,TRL.RECORD_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_VALUE] (T.DocID,{1},'DROPDOWN1','{0}',TRL.RECORD_ID) AS DROPDOWN1," & vbNewLine &
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN2','{0}',TRL.RECORD_ID) AS DROPDOWN2," & vbNewLine &
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN3','{0}',TRL.RECORD_ID) AS DROPDOWN3," & vbNewLine &
                   "[dbo].[FNPMO_DOC_GET_DROPDOWN_VALUE] (T.DocID,{1},'DROPDOWN4','{0}',TRL.RECORD_ID) 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
            LOGGER.Debug("SQL_DOC_SEARCH: " & SQL_DOC_SEARCH)
            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
            Dim DTFILE_RESULT_ASYNC As DataTable
            Try
                Dim async As New ClassAsyncSQL(SQL_DOC_SEARCH)
                async.bw.RunWorkerAsync()
                While async.bw.IsBusy
                    Application.DoEvents()
                End While
                DTFILE_RESULT_ASYNC = async.dt
            Catch ex As Exception
                ClassHelper.MSGBOX_Handler("ERROR", "Unexpected Error Async DTFILE_RESULT_ASYNC", ex.Message, ex.StackTrace)
            End Try

            Return DTFILE_RESULT_ASYNC
        Catch ex As Exception
            LOGGER.Error("Unexpected Error in GET_DOCSEARCH_DATATABLE: " & ex.Message)
            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_TBPMO_DOCSEARCH_RESULTLIST_CONFIG As DataTable, ENT_ID As Integer, REC_ID As Integer, Optional BG As Boolean = False, Optional Preload As Boolean = False)
        Dim _step As String
        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 SW("Get_DOC_RESULTTABLE")

            Dim DT_FILE_RESULT As DataTable = GET_DOCSEARCH_DATATABLE(BG)
            CURRENT_DOC_RESULTS = DT_FILE_RESULT
            If IsNothing(DT_FILE_RESULT) Then
                '  MsgBox("ATTENTION: GET_DOCSEARCH_DATATABLE IS NOTHING" & vbNewLine & "Please inform Your Sysadmin", MsgBoxStyle.Exclamation, "Attention")
                LOGGER.Warn("ATTENTION: GET_DOCSEARCH_DATATABLE IS NOTHING!!")
                Return Nothing
            End If
            sw.Done()
            _step = "1"
            If Preload = True Then
                Return Nothing
            End If
            sw = New SW("Get_DOC_RESULTTABLE DTGrid-Definition")
            Dim oDTGrid As New DataTable
            Dim ColArrayDEFINITION As New List(Of String)
            Dim oListBoolean As New List(Of String)
            For Each row As DataRow In DT_TBPMO_DOCSEARCH_RESULTLIST_CONFIG.Rows
                Dim oType = row.Item("TYPE_ID")
                Dim COLNAME = row.Item("HEADER_CAPTION")
                If oType = 2 Then
                    oListBoolean.Add(COLNAME)
                End If
            Next
            _step = "2"
            For Each col1 As String In oListBoolean
                Dim collist As DataColumn = DT_FILE_RESULT.Columns(col1)
                If Not IsNothing(collist) Then
                    collist.DataType = GetType(Boolean)
                End If
            Next
            _step = "3"
            Try
                'Die Icon Colum erstellen und konfigurieren
                Dim colIcon As New System.Data.DataColumn()
                colIcon.DataType = GetType(Image)
                colIcon.ColumnName = "ICON"
                colIcon.Caption = ""
                oDTGrid.Columns.Add(colIcon)
                _step = "4"
                Dim colPath As New System.Data.DataColumn()
                colPath.DataType = GetType(String)
                colPath.ColumnName = "FULLPATH"
                colPath.Caption = "Fullpath"
                oDTGrid.Columns.Add(colPath)
                _step = "5"
                Dim colOtype As New System.Data.DataColumn()
                colOtype.DataType = GetType(String)
                colOtype.ColumnName = "OBJECTTYPE"
                colOtype.Caption = "Objecttype"
                oDTGrid.Columns.Add(colOtype)
                _step = "6"
                Dim colDocID As New System.Data.DataColumn()
                colDocID.DataType = GetType(Int32)
                colDocID.ColumnName = "DocID"
                colDocID.Caption = "DocID"
                oDTGrid.Columns.Add(colDocID)
                _step = "7"

                If Not DT_TBPMO_DOCSEARCH_RESULTLIST_CONFIG Is Nothing And DT_TBPMO_DOCSEARCH_RESULTLIST_CONFIG.Rows.Count >= 1 Then
                    Dim ColCount As Integer = 1
                    For Each rw As DataRow In DT_TBPMO_DOCSEARCH_RESULTLIST_CONFIG.Rows
                        Dim ColumnName = rw.Item("HEADER_CAPTION")
                        Dim Width = rw.Item("WIDTH")
                        Dim oColumnType = rw.Item("TYPE_ID")
                        If IsNothing(Width) Or IsDBNull(Width) Then
                            Width = 200
                        End If
                        'Jetzt das Array zusammenbauen um die Spaltenwerte zu erhalten
                        Dim colname = rw.Item("COLUMN_VIEW")
                        ColArrayDEFINITION.Add(rw.Item("COLUMN_VIEW"))
                        _step = "7.2"
                        'Die Spalte definieren
                        Try
                            Dim oColumn As New DataColumn() With {
                                .ColumnName = ColumnName
                            }

                            If oColumnType = 3 Then
                                oColumn.DataType = GetType(DateTime)
                            ElseIf oColumnType = 2 Then
                                oColumn.DataType = GetType(Boolean)
                            ElseIf oColumnType = 4 Then
                                oColumn.DataType = GetType(Int32)
                            End If
                            ''1  String
                            ''2  Integer
                            ''3  DateTime
                            ''4  Integer
                            'Dim oColumn As New DataColumn() With {
                            '    .ColumnName = ColumnName
                            '}
                            'If oColumnType = 2 Then
                            '    oColumn.DataType = GetType(Int16)
                            'ElseIf oColumnType = 3 Then
                            '    oColumn.DataType = GetType(DateTime)
                            'ElseIf oColumnType = 4 Then
                            '    oColumn.DataType = GetType(Boolean)
                            'End If
                            oDTGrid.Columns.Add(oColumn)
                            ColCount += 1
                        Catch ex As Exception
                            LOGGER.Warn(String.Format("Unexpected Error in DTGrid.Columns.Add({0}) - CONSTR_DET_ID: {1} ", ColumnName, CURRENT_CONSTRUCTOR_DETAIL_ID) & ex.Message)
                        End Try
                    Next
                    _step = "7.1"
                End If
            Catch ex As Exception
                LOGGER.Warn("Unexpected Error in Construct Datatable: " & ex.Message)
                MsgBox("Unexpected Error in Construct Datatable for GridResult: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
            sw.Done()

            sw = New SW("Run Get_DOC_RESULTTABLE")
            COL_ARRAY_RESULTLIST = ColArrayDEFINITION
            _step = "8"
            For Each col1 As String In oListBoolean
                Dim collist As DataColumn = oDTGrid.Columns(col1)
                If Not IsNothing(collist) Then
                    collist.DataType = GetType(Boolean)
                End If
            Next
            _step = "9"
            Dim _i As Integer
            For Each FILE_ROW As DataRow In DT_FILE_RESULT.Rows
                _i += 1
                _step = "9 FileCount(" & _i.ToString
                Dim fullpath = FILE_ROW.Item("FULL_FILENAME")
                Dim objecttype = FILE_ROW.Item("OBJECTTYPE")
                Dim DocID = FILE_ROW.Item("DocID")
                LOGGER.Debug($"Creating Row for DocID [{DocID}]")
                'Dim Folderpath = Path.GetDirectoryName(fullpath)
                Dim filename = Path.GetFileName(fullpath)
                Dim extension = Path.GetExtension(fullpath)
                '                Try
                Dim oNewRow As DataRow
                oNewRow = oDTGrid.NewRow()

                'Icon zuweisen
                Select Case extension.ToUpper
                    Case ".csv".ToUpper
                        oNewRow.Item(0) = My.Resources.doc_excel_csv
                    Case ".txt".ToUpper
                        oNewRow.Item(0) = My.Resources.text
                    Case ".pdf".ToUpper
                        oNewRow.Item(0) = My.Resources.pdf
                    Case ".doc".ToUpper
                        oNewRow.Item(0) = My.Resources.doc
                    Case ".docx".ToUpper
                        oNewRow.Item(0) = My.Resources.doc
                    Case ".xls".ToUpper
                        oNewRow.Item(0) = My.Resources.xls
                    Case ".xlsx".ToUpper
                        oNewRow.Item(0) = My.Resources.xls
                    Case ".xlsm".ToUpper
                        oNewRow.Item(0) = My.Resources.xls
                    Case ".ppt".ToUpper
                        oNewRow.Item(0) = My.Resources.ppt
                    Case ".pptx".ToUpper
                        oNewRow.Item(0) = My.Resources.ppt
                    Case ".dwg".ToUpper
                        oNewRow.Item(0) = My.Resources.dwg
                    Case ".dxf".ToUpper
                        oNewRow.Item(0) = My.Resources.dxf
                    Case ".msg".ToUpper
                        oNewRow.Item(0) = My.Resources.email_go
                    Case ".msg".ToUpper
                        oNewRow.Item(0) = My.Resources.email_go
                    Case Else
                        oNewRow.Item(0) = My.Resources._blank
                End Select
                'Den Filepath mitgeben
                oNewRow.Item(1) = fullpath
                oNewRow.Item(2) = objecttype
                oNewRow.Item(3) = DocID
                Dim i = 4 'Fängt bei 4 an, um die definierten Spalten zu überspringen
                For Each Colname As String In ColArrayDEFINITION
                    Dim rowvalue
                    Try
                        rowvalue = FILE_ROW.Item(Colname)
                        If IsDBNull(rowvalue) Then
                            rowvalue = ""
                        End If
                        _step = "9.1  rowvalue for Colname [" & Colname & "]: [" & rowvalue.ToString & "]"
                        LOGGER.Debug("rowvalue for Colname [" & Colname & "]: [" & rowvalue.ToString & "]")
                    Catch ex As Exception
                        If CURRENT_SEARCH_TYPE = "RECORD" Then
                            LOGGER.Warn("Unexpected Error in GetVariableValue for Column [" & Colname & "] - Error: " & ex.Message)
                        End If
                        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 = "#"
                            End If

                        End If
                    End Try
                    _step = "9.2 now: NewRow.Item(i) = rowvalue.ToString..."
                    'LOGGER.Debug("9.2 now: NewRow.Item(i) = rowvalue.ToString...")
                    Try
                        Dim oType = oDTGrid.Columns(i).DataType.ToString
                        If oType = "System.DateTime" And Len(rowvalue) = 0 Then
                            LOGGER.Debug("rowvalue Is empty and Column is Datetime...")

                            rowvalue = "2099-12-31"
                        ElseIf oType = "System.Boolean" Then
                            Try
                                Dim otest = CBool(rowvalue)
                            Catch ex As Exception
                                rowvalue = False
                            End Try


                            oNewRow.Item(i) = CBool(rowvalue)
                        ElseIf oType = "System.Int32" Then
                            If Len(rowvalue) = 0 Then
                                oNewRow.Item(i) = 0
                            Else
                                oNewRow.Item(i) = CInt(rowvalue)
                            End If
                        Else
                                oNewRow.Item(i) = rowvalue.ToString
                        End If

                    Catch ex As Exception
                        LOGGER.Warn("Error in advising rowvalue to rowitem: " + ex.Message)
                        LOGGER.Error("Error in advising rowvalue to rowitem: " + ex.Message)
                        LOGGER.Warn("Type of column: " + oDTGrid.Columns(i).DataType.ToString())
                    End Try

                    i += 1
                Next
                oDTGrid.Rows.Add(oNewRow)
                '                Catch ex As Exception
                '                    Logger.Warn("Unexpected Error in CreateRow for doc '" & filename & "- Error: " & ex.Message)
                '                End Try
            Next
            _step = "10"

            Return oDTGrid

            sw.Done()
        Catch ex As Exception
            MsgBox("Unexpected Error in Get_DOC_RESULTTABLE (Step " & _step & "): " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            LOGGER.Warn("Unexpected Error in Get_DOC_RESULTTABLE (Step " & _step & "): " & ex.Message)
            Return Nothing
        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 MYDB_ECM.ExecuteNonQuery(execute) = True Then
                If LICENSE_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)
                    MYDB_ECM.ExecuteNonQuery(execute)
                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(RESULT_DOCID As Integer, RESULT_DOC_PATH As String, DeleteRightsBefore As Boolean)
        Try
            If CURRENT_RECORD_ID = 0 Then
                MsgBox("No Record selected - Please select one!", MsgBoxStyle.Exclamation)
                Return False
            End If
            LOGGER.Debug("#### SETTING RIGHTS FOR FILE ######")
            If ClassWDRights.Init = True Then
                If ClassWDRights.Doc_Renew_Rights(RESULT_DOCID, RESULT_DOC_PATH, DeleteRightsBefore) Then
                    If ClassWDRights.MSG_RESULT <> "" Then
                        Dim msg = "Achtung: einige Rechte konnten nicht gesetzt werden: " & vbNewLine & ClassWDRights.MSG_RESULT
                        If USER_LANGUAGE <> "de-DE" Then
                            msg = "Attention: some rights could not be set: " & vbNewLine & ClassWDRights.MSG_RESULT
                        End If
                        ClassHelper.MSGBOX_Handler("INFO", "Information:", msg)
                    End If
                    Return True
                Else
                    Return False
                End If
            Else
                MsgBox("Could not initialize right-module - Check the log!", MsgBoxStyle.Exclamation)
                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
    ''' <summary>
    ''' Returns a right-type for file.
    ''' </summary>
    ''' <param name="DOC_ID">DOC_ID of windream-file</param>
    ''' <returns>Returns info about filerights</returns>
    ''' <remarks></remarks>
    Public Shared Function Get_File_Rights(DOC_ID As Integer)
        Try
            If DOC_ID = 0 Then
                Return "R"
            End If
            Dim sql = String.Format("SELECT dwAccessRight FROM VWPMO_WD_OBJECT_RIGHTS WHERE dwObjectID = {0} and dwUserOrGroupID = {1}", DOC_ID, USERID_FK_INT_ECM)
            Dim result = MYDB_ECM.GetScalarValue(sql)
            If IsNothing(result) Or IsDBNull(result) Then
                Return Nothing
            End If
            LOGGER.Debug($"dwAccesRight for DocID [{DOC_ID}] is {result}")
            Select Case result
                Case 1
                    If USER_IS_ADMIN = False Then
                        Return "R"
                    Else
                        LOGGER.Warn("FileRight is R but User is Admin!! - Check the UserKonfig!")
                        Return "RWA"
                    End If

                Case 3
                    Return "RW"
                Case 7
                    Return "RWA"
                Case 141
                    Return "RWA"
                Case Else
                    Return "R"
            End Select

        Catch ex As Exception
            clsLogger.Add("Unexpected Error in Get_File_Rights: " & ex.Message)
            Return Nothing
        End Try
    End Function

End Class
