Imports System.Data.SqlClient
Imports System.Reflection

Public Class cLanguageText_LIST
    Dim LIST As New List(Of cLanguageText)

    Sub New(tlan_kategorie, tlan_ukategorie1, tlan_ukategorie2, tlan_ukategorie3)
        LOAD_LIST(tlan_kategorie, tlan_ukategorie1, tlan_ukategorie2, tlan_ukategorie3)
    End Sub
    Public Sub LOAD_LIST(tlan_kategorie, tlan_ukategorie1, tlan_ukategorie2, tlan_ukategorie3)
        Try
            List.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionWEB()
                Using cmd As New SqlCommand("SELECT * FROM tblLanguageText WHERE tlan_kategorie=@tlan_kategorie AND tlan_ukategorie1=@tlan_ukategorie1  AND tlan_ukategorie2=@tlan_ukategorie2  AND tlan_ukategorie3=@tlan_ukategorie3  ", conn)
                    cmd.Parameters.AddWithValue("@tlan_kategorie", tlan_kategorie)
                    cmd.Parameters.AddWithValue("@tlan_ukategorie1", tlan_ukategorie1)
                    cmd.Parameters.AddWithValue("@tlan_ukategorie2", tlan_ukategorie2)
                    cmd.Parameters.AddWithValue("@tlan_ukategorie3", tlan_ukategorie3)

                    '  cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cLanguageText
                        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
                        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 Function getTxt(tlan_Id, tlan_language, defaultTXT) As String
        For Each li In LIST
            If li.tlan_Id = tlan_Id And li.tlan_language = tlan_language Then
                Return li.tlan_TEXT
            End If
        Next
        Return defaultTXT
    End Function

End Class


Public Class cLanguageText
    Property tlan_Id As Integer
    Property tlan_kategorie As String = ""
    Property tlan_ukategorie1 As String = ""
    Property tlan_ukategorie2 As String = ""
    Property tlan_ukategorie3 As String = ""
    Property tlan_language As String = ""
    Property tlan_TEXT As String = ""

    Dim SQL As New SQL

    Sub New()

    End Sub
    Sub New(tlan_Id, tlan_language)
        Me.tlan_Id = tlan_Id
        Me.tlan_language = tlan_language
        LOAD()
    End Sub
    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("tlan_Id", tlan_Id))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("tlan_kategorie", tlan_kategorie))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("tlan_ukategorie1", tlan_ukategorie1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("tlan_ukategorie2", tlan_ukategorie2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("tlan_ukategorie3", tlan_ukategorie3))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("tlan_language", tlan_language))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("tlan_TEXT", tlan_TEXT))

        Return list
    End Function



    Public Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblLanguageText  WHERE tlan_Id=@tlan_Id AND tlan_language=@tlan_language) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        Return SQL.doSQLVarList(sqlstr, "WEB", , list)
    End Function

    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionWEB()
                Using cmd As New SqlCommand("SELECT * FROM tblLanguageText WHERE tlan_Id=@tlan_Id AND tlan_language=@tlan_language  ", conn)
                    cmd.Parameters.AddWithValue("@tlan_Id", tlan_Id)
                    cmd.Parameters.AddWithValue("@tlan_language", tlan_language)
                    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 getUpdateCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

            Dim str As String = ""
            For Each i In list
                If Not i.isPrimaryParam Then
                    str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
                End If
            Next
            str = str.Substring(0, str.Length - 1) 'wg. ','
            Return (" UPDATE [tblLanguageText] SET " & str & " WHERE tlan_Id=@tlan_Id AND tlan_language=@tlan_language ")

        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 getInsertCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
            Dim str As String = ""
            Dim values As String = ""
            For Each i In list
                If Not i.isPrimaryParam 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. ','
            Return (" INSERT INTO tblLanguageText  (" & str & ") VALUES(" & values & ")  ")
        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
