
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


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




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

    Public Shared Function isOldFile(ByRef file As cProgrammeUpdate, LIST_SOURCE As List(Of cProgrammeUpdate)) As Boolean
        For Each l In LIST_SOURCE
            If l.pud_filePathFolder = file.pud_filePathFolder And l.pud_fileName = file.pud_fileName And l.pud_file_Size = file.pud_file_Size And l.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
            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 cProgrammeUpdate)
            Dim dirNameReplace_SOURCE = ""
            Dim DI_SOURCE As New IO.DirectoryInfo(sourcePfad)
            RekursivGetFiles(DI_SOURCE, dirNameReplace_SOURCE, LIST_SOURCE, {})

            Dim LIST_DEST As New List(Of cProgrammeUpdate)
            Dim dirNameReplace_DEST = ""
            Dim DI_DEST As New 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 IO.Directory.Exists(dirNameReplace_DEST & "\" & f.pud_filePathFolder.Replace("\\", "\")) Then
                        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
                    IO.File.Delete(dirNameReplace_DEST & "\" & If(f.pud_filePathFolder, "") & "\" & f.pud_fileName.Replace("\\", "\"))
                End If
            Next

            '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("\\", "\")
                    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 IO.DirectoryInfo, ByRef dirNameReplace As String, ByRef LIST As List(Of cProgrammeUpdate), ignoreFilesBeginWith As String())
        Dim oSubDir As IO.DirectoryInfo
        Dim FI As 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 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

Public Class SQLVariable
    Private TextSQLName As String
    Private ValueSQLVALUE As Object
    Private Scalarvariablename As String
    Private primaryParam As Boolean
    Private onlyForLoad As Boolean

    Public Sub New(ByVal Text As String, ByVal Value As Object, Optional Scalarvariablename As String = "", Optional primaryParam As Boolean = False, Optional onlyForLoad As Boolean = False)
        Me.TextSQLName = Text
        Me.ValueSQLVALUE = Value
        Me.primaryParam = primaryParam
        Me.onlyForLoad = onlyForLoad

        If Scalarvariablename <> "" Then Me.Scalarvariablename = Scalarvariablename Else Me.Scalarvariablename = Text
    End Sub

    Public ReadOnly Property Text() As String
        Get
            Return TextSQLName
        End Get
    End Property

    Public ReadOnly Property Value() As Object
        Get
            Return ValueSQLVALUE
        End Get
    End Property

    Public ReadOnly Property Scalarvariable() As Object
        Get
            Return Scalarvariablename
        End Get
    End Property

    Public ReadOnly Property isPrimaryParam() As Boolean
        Get
            Return primaryParam
        End Get
    End Property

    Public ReadOnly Property isonlyForLoad() As Boolean
        Get
            Return onlyForLoad
        End Get
    End Property


    '  Public Overrides Function ToString() As Object
    '      Return mText
    '  End Function



End Class
