Imports System.Data.SqlClient
Imports System.Reflection




Public Class cBerechtigungsObjekte_List
     Public LIST As New List(Of cBerechtigungsObjekte)
    Dim SQL As New SQL
    'Dim listTodelete As New List(Of cOfferte)

    Sub New()
        LOAD_LIST()
    End Sub

    Sub New(progID)
        LOAD_LIST(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()
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungsObjekte", conn)
                    '  cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cBerechtigungsObjekte
                        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
    Public Sub LOAD_LIST(bero_progId)
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungsObjekte WHERE bero_progId=@bero_progId", conn)
                    cmd.Parameters.AddWithValue("@bero_progId", bero_progId)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cBerechtigungsObjekte
                        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 cBerechtigungsObjekte
    Property bero_id As Integer
    Property bero_progId As Integer
    Property bero_bezeichnung As String
    Property bero_art As String
  
    Property BER_OBJ_ZUORD As New List(Of cBerechtigungsObjekteZuordnung)

    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("bero_id", bero_id, , True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bero_progId", bero_progId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bero_bezeichnung", bero_bezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("bero_art", bero_art))
        Return list
    End Function

    Public Sub LOAD() ' Wird eig nicht benötigt, da LIST
        Try
            BER_OBJ_ZUORD.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungsObjekte WHERE bero_id=@bero_id ", conn)
                    cmd.Parameters.AddWithValue("@bero_id", Me.bero_id)
                    '   cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        'Dim l As New cBerechtigungsObjekte
                        For Each i In Me.getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(i.Text))
                            End If
                        Next
                        Me.LOAD_ZUORD()
                    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 Sub LOAD_ZUORD()
        '  MsgBox("a")
        Try
            BER_OBJ_ZUORD.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                '  Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungsObjekteZuordnung WHERE beroz_beroId=@beroz_beroId ", conn)
                '  MsgBox(Me.bero_id)
                Using cmd As New SqlCommand(" SELECT *, (CASE  beroz_art " &
                                                            " WHEN 'G' THEN (SELECT [bergr_bezeichnung] FROM [ADMIN].[dbo].[tblBerechtigungenGruppen] WHERE beroz_bergrId=bergr_id )  " &
                                                            " WHEN 'B' THEN  (SELECT [mit_username] FROM [ADMIN].[dbo].tblMitarbeiter WHERE beroz_bergrId=mit_id ) " &
                                                            " WHEN 'A' THEN  (SELECT [abt_kuerzel] FROM [ADMIN].[dbo].tblAbteilungen WHERE beroz_bergrId=abt_id ) " &
                                                            " WHEN 'N' THEN  (SELECT [nl_kuerzel] FROM [ADMIN].[dbo].tblNiederlassungen WHERE beroz_bergrId=nl_id ) " &
                                                        " END) as Bezeichnung   " &
                                            " FROM tblBerechtigungsObjekteZuordnung WHERE beroz_beroId=@beroz_beroId  ORDER BY [beroz_art] desc, Bezeichnung", conn)

                    cmd.Parameters.AddWithValue("@beroz_beroId", Me.bero_id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cBerechtigungsObjekteZuordnung
                        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
                        BER_OBJ_ZUORD.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

    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(bero_id, " INSERT INTO tblBerechtigungsObjekte  (" & 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_beroId]=" & Me.bero_id, "ADMIN") 'Zuerst die Zuordnungen
            SQL.doSQL("DELETE FROM tblBerechtigungsObjekte WHERE bero_id=" & Me.bero_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
