Public Class ClassFormCommands

    Public Shared Function LoadForm(guid) As DataTable
        Try
            Dim SQL As String = "SELECT NAME, LEVEL FROM TBPMO_FORM WHERE GUID = " & guid
            Dim DT As DataTable = ClassDatabase.Return_Datatable(SQL)

            If DT.Rows.Count = 1 Then
                Return DT
            Else
                Throw New Exception()
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Laden der Anzeigeeigenschaften der Form: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Shared Function LoadFormView(guid) As DataTable
        Try
            Dim SQL As String = "SELECT FORM_TITLE FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & guid
            Dim DT As DataTable = ClassDatabase.Return_Datatable(SQL)

            If DT.Rows.Count = 1 Then
                Return DT
            Else
                Throw New Exception()
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Laden der Formeigenschaften: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Shared Function SaveForm(guid As Integer, form_name As String, form_level As Integer, form_title As String, doc_view As Integer)
        Try
            Dim SQL As String
            Dim NAME As String = form_name
            Dim LEVEL As String = form_level

            SQL = "UPDATE TBPMO_FORM SET NAME = '" & NAME & "', LEVEL = " & LEVEL & "WHERE GUID = " & guid

            If ClassDatabase.Execute_non_Query(SQL) = True Then
                Return SaveFormView(guid, form_title, doc_view)
            Else
                Throw New Exception()
            End If

        Catch ex As Exception
            MsgBox("Fehler beim Speichern der Formeigenschaften: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    Public Shared Function SaveFormView(guid As Integer, form_title As String, doc_view As Integer)
        Try
            Dim SQL As String
            Dim TITLE As String = form_title
            Dim DOCUMENT_VIEW As Integer = doc_view

            SQL = "UPDATE TBPMO_FORM_VIEW SET FORM_TITLE = '" & TITLE & "', DOCUMENT_VIEW = " & DOCUMENT_VIEW & " WHERE FORM_ID = " & guid

            If ClassDatabase.Execute_non_Query(SQL) = True Then
                Return True
            Else
                Throw New Exception()
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Speichern der Anzeigeeigenschaften der Form: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try

    End Function

    Public Shared Function DeleteFormView(guid)
        Try
            Dim SQL As String = "DELETE FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE FORMVIEW_ID in (SELECT GUID FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & guid & ")"
            If ClassDatabase.Execute_non_Query(SQL) = False Then Return False

            SQL = "DELETE FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & guid
            If ClassDatabase.Execute_non_Query(SQL) = False Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Löschen der Anzeigeeigenschaften der Form: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    Public Shared Function DeleteFormControls(guid)
        Try
            Dim SQL As String = "SELECT GUID FROM TBPMO_CONTROL WHERE FORM_ID = " & guid

            Dim dt As DataTable = ClassDatabase.Return_Datatable(SQL)
            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    Dim controlid As Integer = row.Item(0)
                    CURRENT_CONTROL_ID = controlid
                    ClassControlCommands.DeleteControl()

                Next
            End If
            Return True
        Catch ex As Exception
            MsgBox("Fehler beim Löschen der FormControls: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
    
End Class
