Public Class ClassFunctionCommands
    'Public Shared Function InsertFunction(ControlId As Integer)
    '    Try
    '        Dim SQL As String = "INSERT INTO TBPMO_FUNCTION_ADD_APPOINTMENT(CONTROL_ID) VALUES (" & ControlId & ")"
    '        ClassDatabase.Execute_non_Query(SQL)
    '        Return True
    '    Catch ex As Exception
    '        MsgBox("Fehler beim Anlegen der KalenderFunktion:" & vbNewLine & ex.Message)
    '        Return False
    '    End Try
    'End Function

    Public Shared Function InsertFunction(ControlId As Integer, Method As String)
        Try
            Dim SQL As String = "INSERT INTO TBPMO_FUNCTION_GENERAL (CONTROL_ID, METHOD) VALUES (" & ControlId & ", '" & Method & "')"
            ClassDatabase.Execute_non_Query(SQL)
            Return True
        Catch ex As Exception
            MsgBox("Fehler beim Anlegen der Funktion " & Method & ":" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    'Public Shared Function UpdateFunction(ControlId As Integer, FromDateId As Integer, ToDateId As Integer, SubjectId As Integer)
    '    Try
    '        Dim SQL As String = "UPDATE TBPMO_FUNCTION_ADD_APPOINTMENT SET FROM_DATE = " & FromDateId & ", TO_DATE = " & ToDateId & ", SUBJECT = " & SubjectId & " WHERE CONTROL_ID = " & ControlId
    '        ClassDatabase.Execute_non_Query(SQL)
    '        Return True
    '    Catch ex As Exception
    '        MsgBox("Fehler beim Aktalisieren der KalenderFunktion:" & vbNewLine & ex.Message)
    '        Return False
    '    End Try
    'End Function

    Public Shared Function UpdateAddAppointmentFunction(ControlId As Integer, FromDateId As Integer, ToDateId As Integer, SubjectString As String, Subject2String As String, PlaceString As String, DescriptionString As String)
        Try
            Dim SQL As String = "UPDATE TBPMO_FUNCTION_GENERAL SET INTEGER1 = " & FromDateId & ", INTEGER2 = " & ToDateId & ", STRING1 = '" & SubjectString & "', STRING2 = '" & Subject2String & "', STRING3 = '" & PlaceString & "', STRING4 = '" & DescriptionString & "' WHERE CONTROL_ID = " & ControlId
            If ClassDatabase.Execute_non_Query(SQL) = False Then
                Throw New Exception("Datenbankfehler.")
            End If
            Return True
        Catch ex As Exception
            MsgBox("Error in UpdateAddAppointmentFunction:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    Public Shared Function UpdateAddFormDataFunction(ControlId As Integer, FormId As Integer, ScreenId As Integer)
        Try
            Dim SQL As String = "UPDATE TBPMO_FUNCTION_GENERAL SET INTEGER1 = " & FormId & ", INTEGER2 = " & ScreenId & " WHERE CONTROL_ID = " & ControlId
            If ClassDatabase.Execute_non_Query(SQL) = False Then
                Throw New Exception("Datenbankfehler.")
            End If
            Return True
        Catch ex As Exception
            MsgBox("Error in UpdateAddFormDataFunction:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    Public Shared Function LoadFunction(ControlId As Integer) As DataRow
        Try
            Dim SQL As String = "SELECT * FROM TBPMO_FUNCTION_GENERAL WHERE CONTROL_ID = " & ControlId
            Dim dt As DataTable = ClassDatabase.Return_Datatable(SQL)

            If dt.Rows.Count = 1 Then
                Return dt.Rows.Item(0)
            Else
                Throw New Exception("Funktion für " & ControlId & " wurde nicht gefunden")
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Empfangen der KalenderFunktion:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Shared Function DeleteFunction(ControlId As Integer)
        Try
            Dim SQL As String = "DELETE FROM TBPMO_FUNCTION_GENERAL WHERE CONTROL_ID = " & ControlId
            ClassDatabase.Execute_non_Query(SQL)
            Return True
        Catch ex As Exception
            MsgBox("Fehler beim Löschen der KalenderFunktion:" & vbNewLine & ex.Message)
        End Try
    End Function

    Public Shared Function InsertValue(ControlID As Integer, RecordID As Integer, Value As Object)
        Try
            Dim SQL As String = String.Format("INSERT INTO TBPMO_CONTROL_VALUE (CONTROL_ID, RECORD_ID, VALUE) VALUES({0}, {1}, '{2}')", ControlID, RecordID, Value)
            If ClassDatabase.Execute_non_Query(SQL) = True Then
                Return True
            Else : Return False
            End If
        Catch ex As Exception
            MsgBox("Fehler bei InsertValue:" & vbNewLine & ex.Message)
            Return False
        End Try
    End Function

    Public Shared Function UpdateValue(ControlID As Integer, RecordID As Integer, Value As Object)
        Try
            Dim SQL As String = String.Format("SELECT COUNT(*) FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = {0} AND RECORD_ID  = {1}", ControlID, RecordID)
            Dim ValueExists = ClassDatabase.Execute_Scalar(SQL)

            If ValueExists = 0 Then
                Return InsertValue(ControlID, RecordID, Value)
            Else
                SQL = String.Format("UPDATE TBPMO_CONTROL_VALUE SET VALUE = '{0}' WHERE CONTROL_ID = {1} AND RECORD_ID = {2}", Value, ControlID, RecordID)
                If ClassDatabase.Execute_non_Query(SQL) = True Then
                    Return True
                Else : Return False
                End If
            End If
        Catch ex As Exception
            MsgBox("Fehler bei UpdateValue:" & vbNewLine & ex.Message)
            Return False
        End Try
    End Function

    Public Shared Function GETCONTROL_ID_FOR_COL_NAME(FORM_ID As Integer, Column_Name As String)
        Try
            Dim SQL As String = "SELECT GUID FROM TBPMO_CONTROL WHERE FORM_ID = " & FORM_ID & " AND COL_NAME = '" & Column_Name & "'"
            Dim result = ClassDatabase.Execute_Scalar(SQL)
            Return result
        Catch ex As Exception
            MsgBox("Error in GETCONTROL_ID_FOR_COL_NAME:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

End Class
