Imports System.Data.SqlClient
Imports System.Reflection



Public Class cBerechtigungenGruppen_List
    Property KundenNr As Object = Nothing
    Public LIST As New List(Of cBerechtigungenGruppen)
    Dim SQL As New SQL
    Dim bergr_progId

    Sub New(bergr_progId)
        LOAD_LIST(bergr_progId)
    End Sub

    Public Sub CLEAR()
        LIST.Clear()
    End Sub
    Public Sub ADD(eb_EMail As String, Optional cc As Boolean = False, Optional bcc As Boolean = False)
        ' Dim l As New cOfferte
        'l.eb_bcc = bcc
        'LIST.Add(l)
    End Sub

    Public Sub LOAD_LIST(bergr_progId)
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungenGruppen WHERE bergr_progId=@bergr_progId ORDER BY bergr_bezeichnung", conn)
                    cmd.Parameters.AddWithValue("@bergr_progId", bergr_progId)
                     Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cBerechtigungenGruppen
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next
                        ' l.LOAD_ZUORD()
                        LIST.Add(l)

                    End While
                    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

End Class

Public Class cBerechtigungenGruppen
    Property bergr_id As Integer
    Property bergr_progId As Integer
    Property bergr_bezeichnung As String

    Dim SQL As New SQL
    '  Property BER_BENGR_ZUORD As List(Of cBerechtigungenBenutzerGruppenZuordnung)

    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("bergr_id", bergr_id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bergr_progId", bergr_progId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bergr_bezeichnung", bergr_bezeichnung))
        Return list
    End Function

    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(bergr_id, " INSERT INTO tblBerechtigungenGruppen  (" & 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 tblBerechtigungenBenutzerGruppenZuordnung  WHERE [berbgz_bergrId]=" & Me.bergr_id, "ADMIN") 'Zuerst die Zuordnungen
            SQL.doSQL("DELETE FROM tblBerechtigungsObjekteZuordnung  WHERE [beroz_art]='G' AND [beroz_bergrId]=" & Me.bergr_id, "ADMIN") 'Zuerst die Zuordnungen
            SQL.doSQL("DELETE FROM tblBerechtigungenGruppen  WHERE bergr_id=" & Me.bergr_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
