Imports System.Data.SqlClient
Imports System.Reflection

Public Class cBerechtigungsObjekteZuordnung

    Property beroz_id As Integer = -1
    Property beroz_beroId As Integer
    Property beroz_bergrId As Integer
    Property Bezeichnung As Object = Nothing
    'Property BenutzerBezeichnung As Object = Nothing

    Property beroz_art As String
    Property beroz_berechtigungsstufe As Integer = -1
    Dim SQL As New SQL


    Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("beroz_id", beroz_id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("beroz_beroId", beroz_beroId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("beroz_bergrId", beroz_bergrId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("beroz_art", beroz_art))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("beroz_berechtigungsstufe", beroz_berechtigungsstufe))

        ' list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("GruppenBezeichnung", GruppenBezeichnung, , , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Bezeichnung", Bezeichnung, , , True))
        Return list
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungsObjekteZuordnung WHERE beroz_id=@beroz_id  ", conn)
                    cmd.Parameters.AddWithValue("@beroz_id", beroz_id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(li.Text))
                            End If
                        Next
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Public Function INSERT() As String
        Try
            Dim list As List(Of SQLVariable) = getParameterList()
            Dim str As String = ""
            Dim values As String = ""
            For Each i In list
                If Not (i.isPrimaryParam Or i.isonlyForLoad) Then
                    str &= "[" & i.Text & "],"
                    values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            values = values.Substring(0, values.Length - 1) 'wg. ','
            '  MsgBox(" INSERT INTO tblBerechtigungsObjekteZuordnung  (" & str & ") VALUES(" & values & ")  ")
            SQL.doSQLVarListID(beroz_id, " INSERT INTO tblBerechtigungsObjekteZuordnung  (" & str & ") VALUES(" & values & ")  ", "ADMIN", , list)
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function


    Public Function DELETE() As String
        Try
            SQL.doSQL("DELETE FROM tblBerechtigungsObjekteZuordnung  WHERE beroz_id=" & Me.beroz_id, "ADMIN")
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function

End Class
