Public Class ClassFileResult
    Public Shared DocID As Integer
    Public Shared InWork As Boolean = False
    Public Shared Function Set_InWork(state As Integer, comment As String)
        Try
            Dim upd As String
            InWork = False
            If comment = "" Then
                upd = String.Format("UPDATE TBPMO_DOCRESULT_LIST SET IN_WORK = {0} WHERE DocID = {1}", state, DocID)
            Else
                upd = String.Format("UPDATE TBPMO_DOCRESULT_LIST SET IN_WORK = {0}, IN_WORK_USER = '{1}', IN_WORK_COMMENT = '{2}' WHERE DocID = {3}", state, USER_USERNAME, comment, DocID)
            End If
            If ClassDatabase.Execute_non_Query(upd) = True Then
                If LICENSE_SITE_PROXY = True And DD_LIB_Standards.clsDatabase.DB_PROXY_INITIALIZED Then
                    ClassDatabase.Execute_non_Query(upd, True)
                End If
                InWork = True
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in Set_InWork: " & ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Set_Displayname(Displayname As String)
        Try
            Dim upd As String
            upd = String.Format("UPDATE TBPMO_DOCRESULT_LIST SET DISPLAY_NAME = '{0}' WHERE DocID = {1}", Displayname, DocID)
            If ClassDatabase.Execute_non_Query(upd) = True Then
                If LICENSE_SITE_PROXY = True And DD_LIB_Standards.clsDatabase.DB_PROXY_INITIALIZED Then
                    ClassDatabase.Execute_non_Query(upd, True)
                End If
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in Set_Displayname: " & ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function GET_DATATABLE_INDICES_PMO()
        Try
            Dim sql = String.Format("SELECT * FROM TBPMO_INDEX_MAN WHERE ENTITY_ID = (SELECT CASE REDUNDANT_ENTITY WHEN 0 THEN GUID ELSE REDUNDANT_ENTITY END AS ENTITY_ID  FROM TBPMO_FORM WHERE GUID = {0}) AND DOCTYPE_ID = {1}", CURRENT_ENTITY_ID, CURRENT_DOKARTID)
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql, True)
            If LogErrorsOnly = False Then ClassLogger.Add(" >> GET_DATATABLE_INDICES_PMO: " & sql, False)
            CURRENT_TBPMO_INDEX_MAN = DT
            Return True
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in GET_DATATABLE_INDICES_PMO: " & ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function SET_DOCID_INDICES()
        Try
            Dim DT As DataTable = CURRENT_TBPMO_INDEX_MAN
            If DT.Rows.Count > 0 Then
                For Each row As DataRow In DT.Rows
                    If row.Item("MAN_VALUE") <> String.Empty Then
                        Dim MAN_INDEX_ID = row.Item("GUID")
                        Dim insert As String = String.Format("INSERT INTO TBPMO_DOC_INDICES (DocID,INDEX_ID,VALUE,ADDED_WHO) VALUES ({0},{1},'{2}','{3}')", DocID, MAN_INDEX_ID, row.Item("MAN_VALUE"), USER_USERNAME)
                        ClassDatabase.Execute_non_Query(insert)
                    End If
                 
                Next
            End If
            Return True
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in SET_DOCID_INDICES: " & ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Delete_Doc_Indices()
        Try
            Dim delete As String = String.Format("DELETE FROM TBPMO_DOC_INDICES WHERE DocID = {0}", DocID)
            Return ClassDatabase.Execute_non_Query(delete)
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in Update_Doc_Indices: " & ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Delete_ResultFile(RESULT_DOC_ID, RECORD_ID, DELETE_FILE)
        Try
            Dim proc = String.Format("EXEC PRPMO_DELETE_RESULTFILE {0},{1},{2}", RESULT_DOC_ID, RECORD_ID, DELETE_FILE)
            If ClassDatabase.Execute_non_Query(proc) = True Then
                If LICENSE_SITE_PROXY = True And DD_LIB_Standards.clsDatabase.DB_PROXY_INITIALIZED Then
                    proc = String.Format("EXEC PRPROXY_DOC_CHECK_DELETE {0}", RESULT_DOC_ID)
                    ClassDatabase.Execute_non_Query(proc, True)
                End If
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            ClassLogger.Add(String.Format("Unexpected Error in Delete_ResultFile: ({0},{1},{2})", RESULT_DOC_ID, RECORD_ID, DELETE_FILE) & ex.Message)
            MsgBox("Unexpected Error in Delete ResultFile:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
    Public Shared Function CheckWrite_IndexeMan(grbxControls As GroupBox)
        '#### Zuerst manuelle Werte indexieren ####
        Try
            If LogErrorsOnly = False Then ClassLogger.Add(" >>In CheckWrite_IndexeMan", False)
            Dim result As Boolean = False
            For Each ctrl As Control In grbxControls.Controls
                '            ' MsgBox(ctrl.Name)
                If ctrl.Name.StartsWith("txt") Then
                    Dim box As TextBox = ctrl
                    If box.Text = "" Then
                        Dim optional_index = ClassMoreMetadata.Return_CURRENT_TBPMO_INDEX_MAN_VALUE(box.Tag, "OPTIONAL") 'ClassDatabase.Execute_Scalar("SELECT OPTIONAL FROM TBPMO_INDEX_MAN WHERE GUID = " & box.Tag, True)
                        If Not IsNothing(optional_index) Then
                            If CBool(optional_index) = False Then
                                MsgBox("Please insert a value!", MsgBoxStyle.Exclamation, "Missing input:")
                                box.Focus()
                                Return False
                            Else
                                SET_CURRENT_TBPMO_INDEX_MAN_VALUE(box.Tag, "")
                                'Indexwert_Postprocessing(Replace(box.Name, "txt", ""), "")
                                result = True
                            End If
                        Else
                            Return False
                        End If
                    Else
                        'If Indexwert_checkValueDB(Replace(box.Name, "txt", ""), box.Text) = False Then
                        '    ClassLogger.Add(" - Der eingegebene Wert wurde nicht in der Datenbank gefunden", False)
                        '    MsgBox("Der eingegebene Wert wurde nicht in der Datenbank gefunden!", MsgBoxStyle.Exclamation, "Fehlerhafte Indexierung:")
                        '    box.Focus()
                        '    Return False
                        'Else
                        '    'Indexwert_Postprocessing(Replace(box.Name, "txt", ""), box.Text)
                        SET_CURRENT_TBPMO_INDEX_MAN_VALUE(box.Tag, "")

                        result = True
                        'End If
                    End If
                End If
                If ctrl.Name.StartsWith("cmb") Then
                    Dim cmb As ComboBox = ctrl
                    If cmb.Text = "" Then
                        Dim optional_index = ClassMoreMetadata.Return_CURRENT_TBPMO_INDEX_MAN_VALUE(cmb.Tag, "OPTIONAL") 'ClassDatabase.Execute_Scalar("SELECT OPTIONAL FROM TBPMO_INDEX_MAN WHERE GUID = " & box.Tag, True)
                        If Not IsNothing(optional_index) Then
                            If CBool(optional_index) = False Then
                                MsgBox("Please choose a value!", MsgBoxStyle.Exclamation, "Missing input:")
                                cmb.Focus()
                                Return False
                            Else
                                SET_CURRENT_TBPMO_INDEX_MAN_VALUE(cmb.Tag, "")
                                'Indexwert_Postprocessing(Replace(box.Name, "txt", ""), "")
                                result = True
                            End If
                        End If
                    Else
                        SET_CURRENT_TBPMO_INDEX_MAN_VALUE(cmb.Tag, cmb.Text)
                        ' Indexwert_Postprocessing(Replace(cmb.Name, "cmb", ""), cmb.Text)
                        result = True
                    End If
                End If
                If ctrl.Name.StartsWith("dtp") Then
                    Dim dtp As DateTimePicker = ctrl
                    SET_CURRENT_TBPMO_INDEX_MAN_VALUE(dtp.Tag, dtp.Value)
                    'Indexwert_Postprocessing(Replace(dtp.Name, "dtp", ""), dtp.Text)
                    result = True
                End If
                If ctrl.Name.StartsWith("chk") Then
                    Dim chk As CheckBox = ctrl
                    SET_CURRENT_TBPMO_INDEX_MAN_VALUE(chk.Tag, chk.Checked)
                    'Indexwert_Postprocessing(Replace(chk.Name, "chk", ""), chk.Checked)
                    result = True
                End If
                If ctrl.Name.StartsWith("lbl") = False And result = False Then
                    ClassLogger.Add("Die Überprüfung der manuellen Indices ist fehlerhaft. Bitte informieren Sie den Systembetreuer", True)
                    Return False
                End If
            Next


            Return True

        Catch ex As Exception
            ClassLogger.Add(" - Unvorhergesehener Fehler in CheckWrite_IndexeMan - Fehler: " & vbNewLine & ex.Message)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Unerwarteter Unexpected error in CheckWrite_IndexeMan:")
            Return False
        End Try
    End Function

    Private Shared Function SET_CURRENT_TBPMO_INDEX_MAN_VALUE(CONTROL_GUID As Integer, VALUE As String)
        Try
            For Each row As DataRow In CURRENT_TBPMO_INDEX_MAN.Rows
                If row.Item("GUID") = CONTROL_GUID Then
                    row.Item("MAN_VALUE") = VALUE
                    Return True
                End If
            Next
        Catch ex As Exception
            ClassLogger.Add(" - Unvorhergesehener Fehler in SET_CURRENT_TBPMO_INDEX_MAN_VALUE - Fehler: " & vbNewLine & ex.Message)
            Return Nothing
        End Try
    End Function
End Class
