Imports C1.Win.C1TrueDBGrid
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Imports System.Diagnostics

'
Namespace Utils
    ''' <summary>
    ''' Klasse für das Speichern bzw. Auslesen von Image-Dateien in der Datenbank
    ''' </summary>
    ''' <remarks>
    ''' Es werden folgende Datebanktabellen berücksichtigt:
    ''' <list type="bullet">
    ''' <item>
    ''' <description>Dokument Attribut DocImage</description></item>
    ''' <item>
    ''' <description>Profile Attribut V_Uebersicht (Profillayout des C1TrueDBGrids der  
    ''' Vertragsübersicht|Vertragselemente</description></item></list>
    ''' </remarks>
    ''' <includesource>yes</includesource>
    Public Class MyDocMgmt
        ''' <summary>
        ''' Grid-Layoutfile speichern
        ''' </summary>
        ''' <param name="c1data">C1Truedbgrind, von welchem das Layout gespeichert werden soll</param>
        ''' <param name="GriddNo">Nummer des Grids: 1=Vertragsübersicht...</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Save_LayoutFile(ByRef c1data As C1TrueDBGrid, ByVal GridNo As Integer, ByVal Profilnr As Integer) As Boolean
            Dim filename As String = Globals.TmpFilepath(+Trim(Str(Profilnr)) + Trim(Str(GridNo)) + ".lyt")
            c1data.SaveLayout(filename)

            Dim Connection As New SqlConnection()
            Dim DA As New SqlDataAdapter("select * from profil where profilnr = " & Str(Profilnr), Connection)
            'mitarbeiternr=" + Str(Globals.clsmitarbeiter.iMitarbeiternr.Value)
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA)
            Dim ds As New DataSet()
            Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read)
            Dim mydata(fs.Length) As Byte
            fs.Read(mydata, 0, fs.Length)
            fs.Close()
            Try
                Connection.ConnectionString = Globals.sConnectionString
                Connection.Open()
                DA.Fill(ds, "profil")
                Dim myRow As DataRow
                If ds.Tables(0).Rows.Count = 0 Then
                    ' Neues Profil sepeichern
                    myRow = ds.Tables(0).NewRow
                    myRow.Item(1) = Globals.Mitarbeiternr
                    myRow.Item(2) = ""
                    Select Case GridNo
                        Case 1
                            myRow.Item(3) = mydata
                    End Select
                    ds.Tables(0).Rows.Add(myRow)
                    DA.Update(ds, "profil")
                Else
                    myRow = ds.Tables(0).Rows(0)
                    Select Case GridNo
                        Case 1
                            myRow.Item(3) = mydata
                    End Select
                    DA.Update(ds, "profil")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
            End Try
            fs = Nothing
            cb = Nothing
            ds = Nothing
            DA = Nothing
            Connection.Close()
            Connection = Nothing
            Return True

        End Function

        Private Function Get_Layoutfile_from_db(ByVal filename As String, ByVal GridNo As Integer, ByVal Profilnr As Integer) As Boolean
            'Exit Function
            Dim connection As New SqlConnection()
            Dim da As New SqlDataAdapter("Select * From profil where profilnr=" & Str(Profilnr), connection)
            'mitarbeiternr=" + Str(Globals.clsmitarbeiter.iMitarbeiternr.Value)
            Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim ds As New DataSet()
            Try
                connection.ConnectionString = Globals.sConnectionString
                connection.Open()
                da.Fill(ds, "docs")
                Dim myRow As DataRow
                myRow = ds.Tables(0).Rows(0)
                Dim MyData() As Byte
                Select Case GridNo
                    Case 1
                        MyData = myRow.Item(3)

                End Select

                Dim K As Long
                K = UBound(MyData)
                Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)
                fs.Write(MyData, 0, K)
                fs.Close()
                fs = Nothing
                Return True
            Catch ex As Exception
                Return False
            End Try
            CB = Nothing
            ds = Nothing
            da = Nothing
            connection.Close()
            connection = Nothing
            Return True
        End Function

        Public Function Get_Layout(ByRef c1data As C1TrueDBGrid, ByVal GridNo As Integer, ByVal Profilnr As Integer) As Boolean
            Dim filename As String = Globals.TmpFilepath + Trim(Str(Profilnr)) + Trim(Str(GridNo)) + ".lyt"
            If File.Exists(filename) Then
                c1data.LoadLayout(filename)
                Return True
            End If
            If Get_Layoutfile_from_db(filename, GridNo, Profilnr) Then
                c1data.LoadLayout(filename)
                Return True
            End If
            Return False
        End Function

        ''' <summary>
        ''' Dokument in der Tabelle Dokument speichern
        ''' </summary>
        ''' <param name="Dokumentnr">Nummer des Dokument-Datensatzes</param>
        ''' <param name="Filename">Zu speichender Dateiname</param>
        ''' <returns></returns>
        ''' <remarks></remarks>

        Public Function Save_Document(ByVal Dokumentnr As Integer, ByVal Filename As String) As Boolean
            Dim Connection As New SqlConnection()
            Dim DA As New SqlDataAdapter("select * from dokument where dokumentnr =" + Str(Dokumentnr), Connection)
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA)
            Dim ds As New DataSet()
            Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Read)
            Dim mydata(fs.Length) As Byte
            fs.Read(mydata, 0, fs.Length)
            fs.Close()
            Try
                Connection.ConnectionString = Globals.sConnectionString
                Connection.Open()
                DA.Fill(ds, "Dokument")
                Dim myRow As DataRow
                If ds.Tables(0).Rows.Count = 0 Then
                    Return False
                Else
                    myRow = ds.Tables(0).Rows(0)
                    myRow.Item(16) = mydata
                    DA.Update(ds, "Dokument")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
            End Try
            fs = Nothing
            cb = Nothing
            ds = Nothing
            DA = Nothing
            Connection.Close()
            Connection = Nothing
            Return True
        End Function

        ''' <summary>
        ''' Liest das Dokument aus der DB und speichert dieses unter einem temporären Filenamen ab
        ''' </summary>
        ''' <param name="DokumentNr"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Get_Dokument(ByVal DokumentNr As Integer) As String
            Dim Filename As String = Globals.TmpFilepath
            If Right(Filename, 1) <> "\" Then Filename = Filename + "\"

            Dim connection As New SqlConnection()
            Dim da As New SqlDataAdapter("Select * From Dokument where DokumentNr=" + Str(DokumentNr), connection)
            Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim ds As New DataSet()
            Try
                connection.ConnectionString = Globals.sConnectionString
                connection.Open()
                da.Fill(ds, "Dokument")
                Dim myRow As DataRow
                myRow = ds.Tables(0).Rows(0)
                Select Case myRow.Item("Speichertypnr")
                    Case 1
                        Dim MyData() As Byte
                        MyData = myRow.Item(16)

                        Dim K As Long
                        K = UBound(MyData)
                        Filename = Filename + myRow.Item(6)
                        Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write)
                        fs.Write(MyData, 0, K)
                        fs.Close()
                        fs = Nothing
                    Case 2
                        Filename = myRow.Item("OriginalFilename_incl_Path")
                    Case 3
                        Filename = myRow.Item("OriginalFilename_incl_Path")
                    Case Else
                        Filename = myRow.Item("OriginalFilename_incl_Path")
                End Select
                
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
                Return ""
            Finally
                connection.Close()
                connection = Nothing
            End Try
            CB = Nothing
            ds = Nothing
            da = Nothing
            Return Filename
        End Function

        Public Function Show_Document(ByVal Dokumentnr As Integer) As Boolean
            Dim tmpfilename As String = Me.Get_Dokument(Dokumentnr)
            If tmpfilename <> "" Then
                OpenSystemFile(tmpfilename)
                Return True
            End If
            Return False
        End Function

        Public Function OpenSystemFile(ByVal sFileName As String) As Boolean
            If Len(sFileName) > 0 Then
                System.Diagnostics.Process.Start(sFileName)
                '
                '               ShellExecute(GetDesktopWindow(), vbNullString, sFileName, vbNullString, vbNullString, vbNormalFocus)
                Return True
            End If
        End Function

        Public Function Save_RptDatei(ByVal Auswertungnr As Integer, ByVal AuswertungName As String) As String
            Dim filename As String = AuswertungName
            Dim Connection As New SqlConnection()
            Dim DA As New SqlDataAdapter("select * from AuswertungRptDatei where AuswertungDateiNr = " & Str(Auswertungnr), Connection)
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA)
            Dim ds As New DataSet()
            Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read)
            Dim mydata(fs.Length) As Byte
            fs.Read(mydata, 0, fs.Length)
            fs.Close()
            Try
                Connection.ConnectionString = Globals.sConnectionString
                Connection.Open()
                DA.Fill(ds, "RptFile")
                Dim myRow As DataRow
                If ds.Tables(0).Rows.Count = 0 Then
                    ' Neues Datei speichern
                    myRow = ds.Tables(0).NewRow
                    myRow.Item(0) = Auswertungnr
                    myRow.Item(1) = AuswertungName
                    myRow.Item(2) = RptName(AuswertungName)
                    myRow.Item(3) = mydata
                    myRow.Item(4) = Now
                    myRow.Item(5) = Now
                    myRow.Item(6) = Globals.Mitarbeiternr
                    ds.Tables(0).Rows.Add(myRow)
                    DA.Update(ds, "RptFile")
                Else
                    myRow = ds.Tables(0).Rows(0)
                    myRow.Item(1) = AuswertungName
                    myRow.Item(2) = RptName(AuswertungName)
                    myRow.Item(3) = mydata
                    myRow.Item(5) = Now
                    myRow.Item(6) = Globals.Mitarbeiternr
                    DA.Update(ds, "RptFile")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
            End Try
            fs = Nothing
            cb = Nothing
            ds = Nothing
            DA = Nothing
            Connection.Close()
            Connection = Nothing
            Return RptName(AuswertungName)
        End Function

        Public Function RptName(ByVal path As String) As String
            Dim i As Integer
            Dim file As String = path
            i = InStrRev(file.Trim, "\")
            If i = 0 Then
                Return file.Trim
            Else
                Return Right(file.Trim, Len(file.Trim) - i)
            End If
        End Function

        Public Function Get_RptDatei(ByVal Auswertungnr As String, Optional ByVal fname As String = "") As String
            Dim connection As New SqlConnection()
            Dim DA As New SqlDataAdapter("select * from AuswertungRptDatei where AuswertungDateiNr = " & Str(Auswertungnr), connection)
            Dim CB As SqlCommandBuilder = New SqlCommandBuilder(DA)
            Dim ds As New DataSet()
            Dim Filename As String = ""
            Try
                connection.ConnectionString = Globals.sConnectionString
                connection.Open()
                DA.Fill(ds, "RptFile")
                Dim myRow As DataRow
                myRow = ds.Tables(0).Rows(0)
                Dim MyData() As Byte
                Filename = Globals.TmpFilepath + "\" + myRow.Item(2).ToString
                If fname <> "" Then
                    Filename = fname
                End If
                MyData = myRow.Item(3)
                Dim K As Long
                K = UBound(MyData)
                Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write)
                fs.Write(MyData, 0, K)
                fs.Close()
                fs = Nothing
            Catch ex As Exception
                Return ""
            End Try
            CB = Nothing
            ds = Nothing
            DA = Nothing
            connection.Close()
            connection = Nothing
            Return Filename
        End Function

        Public Function Save_Architekturfile(ByVal Applikationnr As Integer, ByVal iFilename As String)
            Dim filename As String = iFilename
            Dim Connection As New SqlConnection()
            Dim DA As New SqlDataAdapter("select * from ApplikationArchitektur where applikationnr = " & Str(Applikationnr), Connection)
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA)
            Dim ds As New DataSet()
            Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read)
            Dim mydata(fs.Length) As Byte
            fs.Read(mydata, 0, fs.Length)
            fs.Close()
            Try
                Connection.ConnectionString = Globals.sConnectionString
                Connection.Open()
                DA.Fill(ds, "RptFile")
                Dim myRow As DataRow
                If ds.Tables(0).Rows.Count = 0 Then
                    ' Neues Datei speichern
                    myRow = ds.Tables(0).NewRow
                    myRow.Item(0) = Applikationnr
                    myRow.Item(1) = mydata
                    '                    myRow.Item(4) = Now
                    '                   myRow.Item(5) = Now
                    '                  myRow.Item(6) = Globals.clsmitarbeiter.iMitarbeiternr.Value
                    ds.Tables(0).Rows.Add(myRow)
                    DA.Update(ds, "RptFile")
                Else
                    myRow = ds.Tables(0).Rows(0)
                    myRow.Item(1) = mydata
                    '                    myRow.Item(2) = RptName(AuswertungName)
                    '                   myRow.Item(3) = mydata
                    '                  myRow.Item(5) = Now
                    '                 myRow.Item(6) = Globals.clsmitarbeiter.iMitarbeiternr.Value
                    DA.Update(ds, "RptFile")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
            End Try
            fs = Nothing
            cb = Nothing
            ds = Nothing
            DA = Nothing
            Connection.Close()
            Connection = Nothing
        End Function

        Public Function Get_Architekturfile(ByVal Applikationnr As String, Optional ByVal fname As String = "") As String
            Dim connection As New SqlConnection()
            Dim DA As New SqlDataAdapter("select * from ApplikationArchitektur where applikationnr = " & Str(Applikationnr), connection)
            Dim CB As SqlCommandBuilder = New SqlCommandBuilder(DA)
            Dim ds As New DataSet()
            Dim Filename As String = ""
            Try
                connection.ConnectionString = Globals.sConnectionString
                connection.Open()
                DA.Fill(ds, "RptFile")
                Dim myRow As DataRow
                myRow = ds.Tables(0).Rows(0)
                Dim MyData() As Byte
                Filename = Globals.TmpFilepath + "\architekturfile.xml"
                If fname <> "" Then
                    Filename = fname
                End If
                MyData = myRow.Item(1)
                Dim K As Long
                K = UBound(MyData)
                Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write)
                fs.Write(MyData, 0, K)
                fs.Close()
                fs = Nothing
            Catch ex As Exception
                Return ""
            End Try
            CB = Nothing
            ds = Nothing
            DA = Nothing
            connection.Close()
            connection = Nothing
            Return Filename
        End Function

    End Class

End Namespace
