
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cProgrammeUpdate
    Property pud_progId As Object = Nothing
    Property pud_codename As String
    Property pud_version As String
    Property pud_fileName As String
    Property pud_filePathFolder As Object = Nothing
    Property pud_datum As Date = Now

    Property pud_file_Verion As String
    Property pud_file_Date As Date
    Property pud_file_Size As Integer

    Dim SQL As New SQL


    Shared Function getParameterList(UD As cProgrammeUpdate) As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_progId", UD.pud_progId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_codename", UD.pud_codename))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_version", UD.pud_version))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_fileName", UD.pud_fileName))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_filePathFolder", UD.pud_filePathFolder))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_datum", UD.pud_datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_file_Verion", UD.pud_file_Verion))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_file_Date", UD.pud_file_Date))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("pud_file_Size", UD.pud_file_Size))
        Return list
    End Function



    Public Function SAVE() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList(Me)
        Return SQL.doSQLVarList(getInsertCmd, "ADMIN", , list)
    End Function

    Public Shared Function LOAD_AKT_LIST(pud_progId) As List(Of cProgrammeUpdate)
        Dim LIST As New List(Of cProgrammeUpdate)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("SELECT * FROM tblProgrammeUpdate  WHERE pud_progId=@pud_progId  AND pud_version=(SELECT TOP (1) pud_version   FROM tblProgrammeUpdate   WHERE pud_progId=@pud_progId  order by pud_version DESC)   ", conn)
                    cmd.Parameters.AddWithValue("@pud_progId", pud_progId)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim UD As New cProgrammeUpdate
                        For Each li In cProgrammeUpdate.getParameterList(UD)
                            Dim propInfo As PropertyInfo = UD.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(UD, Nothing)
                            Else
                                propInfo.SetValue(UD, dr.Item(li.Text))
                            End If

                        Next
                        LIST.Add(UD)
                    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
        Return LIST
    End Function

    Public Shared Function SAVE_LIST(pud_progId As Integer, pud_version As String, pud_codename As String, LIST As List(Of cProgrammeUpdate)) As Boolean
        Dim SQL As New SQL
        SQL.doSQL("DELETE FROM tblProgrammeUpdate WHERE pud_version='" & pud_version & "'", "ADMIN")
        SAVE_LIST = True
        For Each UD In LIST
            UD.pud_progId = pud_progId
            UD.pud_version = pud_version
            UD.pud_codename = pud_codename
            If Not UD.SAVE() Then SAVE_LIST = False
        Next
    End Function


    Public Function getUpdateCmd() As String
        Try
            Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList(Me)

            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 [tblProgrammeUpdate] SET " & str & " WHERE gb_nr=@gb_nr ")

        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(Me)
            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 tblProgrammeUpdate  (" & 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


    Public Shared Function isNewFile(ByRef file As VERAG_PROG_ALLGEMEIN.cProgrammeUpdate, LIST_DEST As List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)) As Boolean
        For Each li In LIST_DEST
            If li.pud_filePathFolder = file.pud_filePathFolder And li.pud_fileName = file.pud_fileName And li.pud_file_Size = file.pud_file_Size And li.pud_file_Date = file.pud_file_Date Then
                Return False
            End If
        Next
        Return True
    End Function

    Public Shared Function isOldFile(ByRef file As VERAG_PROG_ALLGEMEIN.cProgrammeUpdate, LIST_SOURCE As List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)) As Boolean
        For Each li In LIST_SOURCE
            If li.pud_filePathFolder = file.pud_filePathFolder And li.pud_fileName = file.pud_fileName And li.pud_file_Size = file.pud_file_Size And li.pud_file_Date = file.pud_file_Date Then
                Return False
            End If
        Next
        Return True
    End Function


    Public Shared Function copyProgramLIST(ByRef sourcePfad As String, ByVal destPfad As String, ignoreFilesBeginWith As String()) As Boolean
        Try

            If destPfad.Trim = "" Then Return False
            If sourcePfad.Trim = "" Then Return False

            If Not My.Computer.FileSystem.DirectoryExists(sourcePfad) Then
                MsgBox("ERROR_COPY_01: Quell-Ordner existiert nicht.", MsgBoxStyle.Critical, "ERROR") : Return False
            End If
            ' MsgBox(destPfad)
            If Not My.Computer.FileSystem.DirectoryExists(destPfad) Then
                MsgBox("ERROR_COPY_02: Ziel-Ordner existiert nicht.", MsgBoxStyle.Critical, "ERROR") : Return False
            End If

            Dim LIST_SOURCE As New List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)
            Dim dirNameReplace_SOURCE = ""
            Dim DI_SOURCE As New System.IO.DirectoryInfo(sourcePfad)
            RekursivGetFiles(DI_SOURCE, dirNameReplace_SOURCE, LIST_SOURCE, {})

            Dim LIST_DEST As New List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate)
            Dim dirNameReplace_DEST = ""
            Dim DI_DEST As New System.IO.DirectoryInfo(destPfad)
            RekursivGetFiles(DI_DEST, dirNameReplace_DEST, LIST_DEST, ignoreFilesBeginWith)

            If LIST_SOURCE.Count = 0 Then
                MsgBox("ERROR_COPY_01: Quell-Daten existiert nicht.", MsgBoxStyle.Critical, "ERROR") : Return False
            End If
            'If LIST_DEST.Count = 0 Then
            '    MsgBox("ERROR_COPY_02: Ziel-Daten existiert nicht.", MsgBoxStyle.Critical, "ERROR") : Return False
            'End If

            'ORDNER erstellen:
            For Each f In LIST_SOURCE
                If If(f.pud_filePathFolder, "") <> "" Then
                    If Not System.IO.Directory.Exists(dirNameReplace_DEST & "\" & f.pud_filePathFolder) Then '.Replace("\\", "\")
                        System.IO.Directory.CreateDirectory(dirNameReplace_DEST & "\" & f.pud_filePathFolder) '.Replace("\\", "\")
                    End If
                End If
            Next

            'Löschen der unbenötigten Files:
            For Each f In LIST_DEST
                If isOldFile(f, LIST_SOURCE) Then
                    System.IO.File.Delete(dirNameReplace_DEST & "\" & If(f.pud_filePathFolder, "") & "\" & f.pud_fileName) '.Replace("\\", "\")
                End If
            Next

            If dirNameReplace_DEST = "" Then dirNameReplace_DEST = destPfad
            'Kopieren/Überschreiben der neuen Files:
            For Each f In LIST_SOURCE
                If isNewFile(f, LIST_DEST) Then
                    Dim s_path = dirNameReplace_SOURCE & "\" & If(f.pud_filePathFolder, "") & "\" & f.pud_fileName '.Replace("\\", "\")
                    Dim d_path = dirNameReplace_DEST & "\" & If(f.pud_filePathFolder, "") & "\" & f.pud_fileName '.Replace("\\", "\")
                    d_path = d_path '.replace("\\", "\")
                    System.IO.File.Copy(s_path, d_path)
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
            Return False
        End Try
        '  If isNewFile(destPfad) Then Return FileCopier(sourcePfad, destPfad)
        Return True
    End Function

    Public Shared Function fileStartsWith(fileName As String, ignoreFilesBeginWith As String())
        For Each s In ignoreFilesBeginWith
            If fileName.StartsWith(s) Then Return True
        Next
        Return False
    End Function
    Public Shared Sub RekursivGetFiles(ByVal oDir As System.IO.DirectoryInfo, ByRef dirNameReplace As String, ByRef LIST As List(Of VERAG_PROG_ALLGEMEIN.cProgrammeUpdate), ignoreFilesBeginWith As String())
        Dim oSubDir As System.IO.DirectoryInfo
        Dim FI As System.IO.FileInfo

        ' zunächst alle Dateien des Ordners aufspüren
        For Each FI In oDir.GetFiles()
            If Not fileStartsWith(FI.Name, ignoreFilesBeginWith) Then 'DATEIEN beginnen mit betimmten Namen, z.B. UPDATER


                ' Dim FI As New IO.FileInfo(oFile)
                If dirNameReplace = "" Then
                    dirNameReplace = FI.DirectoryName
                End If

                Dim UD As New VERAG_PROG_ALLGEMEIN.cProgrammeUpdate
                UD.pud_fileName = FI.Name
                UD.pud_filePathFolder = FI.DirectoryName.Replace(dirNameReplace, "")
                If UD.pud_filePathFolder = "" Then UD.pud_filePathFolder = Nothing
                UD.pud_file_Size = FI.Length
                UD.pud_file_Date = FI.LastWriteTime
                UD.pud_file_Verion = "" 'FI.
                LIST.Add(UD)

            End If
            'With oFile
            '    ListBox1.Items.Add(.DirectoryName & "\" & .Name & vbTab &
            '      .Length & vbTab & .LastWriteTime)
            'End With
        Next

        ' Jetzt alle Unterverzeichnis durchlaufen
        ' und die Prozedur rekursiv selbst aufrufen
        For Each oSubDir In oDir.GetDirectories()
            RekursivGetFiles(oSubDir, dirNameReplace, LIST, {})
        Next
    End Sub

End Class

