Imports C1.Win.C1TrueDBGrid
Imports System
Imports System.Data
Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Imports System.Windows.Forms

Namespace TKB.Auswertung
    Public Class clsAuswertung

        Private Auswertungen As New DataSet
        Private Mitarbeiter_Auswerungsparameter As New DB.clsMitarbeiter_Auswertungsparameter
        Public AuswertungParameter As New DataTable
        Public Auswertung As New DB.clsAuswertung
        Public MitarbeiterAuswertungsparameter As New DataTable
        Dim sheader1 As String
        Property TitelZeile1() As String
            Get
                Return sheader1
            End Get
            Set(ByVal value As String)
                sheader1 = value
            End Set
        End Property
        Dim sheader2 As String
        Property TitelZeile2() As String
            Get
                Return sheader2
            End Get
            Set(ByVal value As String)
                sheader2 = value
            End Set
        End Property
        Dim scparamcollection As New Collection
        Property ParamCollection() As Collection
            Get
                Return scparamcollection
            End Get
            Set(ByVal value As Collection)
                scparamcollection = value
            End Set
        End Property
        Dim mFullparam As String
        Property FullParam() As String
            Get
                Return mFullparam
            End Get
            Set(ByVal value As String)
                mFullparam = value
            End Set
        End Property

        Public Sub Get_Auswertungen(ByRef tree As TreeView)
            Read_Auswertungen()
            Load_Treeview(Auswertungen, tree)
        End Sub



        ''' <summary>
        ''' Auswertungen, für welche der User berechtigt ist, auslesen
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function Read_Auswertungen() As DataTable
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            Dim dtToReturn As DataTable = New DataTable()
            Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute)
            scmCmdToExecute.CommandText = "dbo.sp_Auswertung_Get_Auswertungen"
            scmCmdToExecute.CommandType = CommandType.StoredProcedure
            scmCmdToExecute.Connection = Globals.conn.scoDBConnection
            Try
                scmCmdToExecute.Parameters.Add(New SqlParameter("@mitarbeiternr", SqlDbType.Int, 4, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, Globals.Mitarbeiternr))
                sdaAdapter.Fill(dtToReturn)
                Auswertungen.Tables.Clear()
                Auswertungen.Tables.Add(dtToReturn)
            Catch ex As Exception
                Throw New Exception("clsAuswertung::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex)
            Finally
                scmCmdToExecute.Dispose()
                sdaAdapter.Dispose()
            End Try
        End Function

        ''' <summary>
        ''' Interne Relation aufbauen und Tree aufbauen
        ''' </summary>
        ''' <param name="oSourceData"></param>
        ''' <param name="tree"></param>
        ''' <remarks></remarks>
        Private Sub Load_Treeview(ByVal oSourceData As DataSet, ByRef tree As TreeView)
            If Not (oSourceData Is Nothing) Then
                Dim oView As DataView = oSourceData.Tables(0).DefaultView
                Dim oTable As DataTable = oView.Table
                Dim oDS As DataSet = New DataSet()
                oDS.Tables.Add(oTable.Copy())

                If oDS.Relations.Contains("SelfRefenceRelation") = False Then
                    oDS.Relations.Add("SelfRefenceRelation", _
                        oDS.Tables(0).Columns("id"), _
                        oDS.Tables(0).Columns("Parentid"))
                End If
                oTable.Dispose()
                oTable = Nothing
                LoadTreeView(oDS, tree)
                oDS.Dispose()
                oDS = Nothing
            End If
        End Sub

        ''' <summary>
        ''' Tree aufbauen
        ''' </summary>
        ''' <param name="oDS"></param>
        ''' <param name="oTreeview"></param>
        ''' <remarks></remarks>
        Private Sub LoadTreeView(ByVal oDS As DataSet, ByRef oTreeview As TreeView)
            'Dim oTreeView As TreeView = New TreeView()
            Dim oDataRow As DataRow
            For Each oDataRow In oDS.Tables(0).Rows
                If Not oDataRow.IsNull("Parentid") Then
                    If oDataRow.Item("Parentid") = 0 Then
                        Dim oNode As New TreeNode()
                        oNode.Text = oDataRow("Bezeichnung").ToString()
                        oNode.Tag = oDataRow("auswertungnr").ToString
                        oNode.ToolTipText = oDataRow("Beschreibung").ToString
                        oNode.ImageIndex = 0
                        oNode.SelectedImageIndex = 0
                        oNode.StateImageIndex = 0
                        oTreeview.Nodes.Add(oNode)
                        RecursivelyLoadTree(oDataRow, oNode)
                    End If
                End If
            Next oDataRow
            oDS.Dispose()
            oDS = Nothing
        End Sub

        ''' <summary>
        ''' Child-Nodes hinzufügen
        ''' </summary>
        ''' <param name="oDataRow"></param>
        ''' <param name="oNode"></param>
        ''' <remarks></remarks>
        Private Sub RecursivelyLoadTree(ByVal oDataRow As DataRow, ByRef oNode As TreeNode)
            Dim oChildRow As DataRow
            For Each oChildRow In oDataRow.GetChildRows("SelfRefenceRelation")
                Dim oChildNode As New TreeNode()
                oChildNode.Text = oChildRow("Bezeichnung").ToString()
                oChildNode.Tag = oChildRow("Auswertungnr").ToString()
                If oChildRow("Auswertungnr") = 0 Then
                    oChildNode.ImageIndex = 0
                    oChildNode.SelectedImageIndex = 0
                    oChildNode.StateImageIndex = 0
                    oChildNode.ToolTipText = oChildRow("Beschreibung").ToString
                Else
                    oChildNode.ImageIndex = 1
                    oChildNode.SelectedImageIndex = 1
                    oChildNode.StateImageIndex = 1
                    oChildNode.ToolTipText = oChildRow("Beschreibung").ToString
                End If
                oNode.Nodes.Add(oChildNode)
                RecursivelyLoadTree(oChildRow, oChildNode)
            Next oChildRow
        End Sub

        Public Function Get_Auswertung(ByVal Auswertungnr As Integer) As Boolean
            Try
                Me.Auswertung.iAuswertungNr = New SqlInt32(CType(Auswertungnr, Int32))
                Me.Auswertung.cpMainConnectionProvider = Globals.conn
                Me.Auswertung.SelectOne()
                Me.AuswertungParameter = Get_Auswertungsparameter(Me.Auswertung.iAuswertungNr.Value)
                Me.MitarbeiterAuswertungsparameter = Me.Get_MAParameter(Me.Auswertung.iAuswertungNr.Value)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Function Get_Auswertungsparameter(ByVal Auswertungnr As Integer) As DataTable
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            Dim dtToReturn As DataTable = New DataTable()
            Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute)
            scmCmdToExecute.CommandText = "dbo.sp_rpt_get_auswertungparameter"
            scmCmdToExecute.CommandType = CommandType.StoredProcedure
            scmCmdToExecute.Connection = conn.scoDBConnection
            Try
                scmCmdToExecute.Parameters.Add(New SqlParameter("@Auswertungnr", SqlDbType.VarChar, 50, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, Auswertungnr))
                sdaAdapter.Fill(dtToReturn)
                Return dtToReturn
            Catch ex As Exception
                ' // some error occured. Bubble it to caller and encapsulate Exception object
                Throw New Exception("frmAuswertung::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex)
            Finally
                scmCmdToExecute.Dispose()
                sdaAdapter.Dispose()
            End Try

        End Function

        Public Function get_rptparam_values(ByVal sp As String) As DataTable
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            Dim dtToReturn As DataTable = New DataTable()
            Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute)
            If UCase(sp.ToString.Substring(0, 6)) = "SELECT" Then
                scmCmdToExecute.CommandType = CommandType.Text
            Else
                scmCmdToExecute.CommandType = CommandType.StoredProcedure
            End If
            scmCmdToExecute.CommandText = sp
            scmCmdToExecute.Connection = conn.scoDBConnection
            Try
                sdaAdapter.Fill(dtToReturn)
                Return dtToReturn
            Catch ex As Exception
                ' // some error occured. Bubble it to caller and encapsulate Exception object
                Throw New Exception("frmAuswertung::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex)
            Finally
                scmCmdToExecute.Dispose()
                sdaAdapter.Dispose()
            End Try

        End Function

        Public Function Get_Auswertungsdaten(ByVal sql As String, ByVal sqlwhere As String, ByVal sqltype As String) As DataSet
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            Dim dtToReturn As DataSet = New DataSet()
            Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute)
            scmCmdToExecute.CommandText = sql
            Select Case UCase(sqltype)
                Case "SQL"
                    If sqlwhere <> "" Then sql = sql + " where " + sqlwhere
                    scmCmdToExecute.CommandText = sql
                    scmCmdToExecute.CommandType = CommandType.Text
                Case "VIEW"
                    sql = sql + "Select * from  " + sql
                    scmCmdToExecute.CommandText = sql
                    If sqlwhere <> "" Then sql = sql + " where " + sqlwhere
                    scmCmdToExecute.CommandType = CommandType.Text
                Case "SP"
                    scmCmdToExecute.CommandType = CommandType.StoredProcedure
                    scmCmdToExecute.Parameters.Add(New SqlParameter("@sqlwhere", SqlDbType.VarChar, 1024, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, sqlwhere))
            End Select
            scmCmdToExecute.Connection = conn.scoDBConnection
            Try
                'scmCmdToExecute.Parameters.Add(New SqlParameter("@Rolle", SqlDbType.VarChar, 50, ParameterDirection.Output, True, 0, 0, "", DataRowVersion.Proposed, ""))
                sdaAdapter.Fill(dtToReturn)
                Return dtToReturn
            Catch ex As Exception
                ' // some error occured. Bubble it to caller and encapsulate Exception object
                Throw New Exception("frmAuswertung::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex)
            Finally
                scmCmdToExecute.Dispose()
                sdaAdapter.Dispose()
            End Try

        End Function
        Public Function Get_Auswertungsdaten(ByVal sp As String, ByVal sqlwhere As String, ByVal txp As Boolean) As DataTable

            Dim con As New SqlConnection(Get_TXP_Connection)
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            Dim dtToReturn As DataTable = New DataTable()
            Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute)
            scmCmdToExecute.CommandText = sp
            scmCmdToExecute.CommandType = CommandType.StoredProcedure
            scmCmdToExecute.Connection = con
            scmCmdToExecute.CommandTimeout = 30000

            Try
                scmCmdToExecute.Parameters.Add(New SqlParameter("@sqlwhere", SqlDbType.VarChar, 1024, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, sqlwhere))
                sdaAdapter.Fill(dtToReturn)
                Return dtToReturn
            Catch ex As Exception
                ' // some error occured. Bubble it to caller and encapsulate Exception object
                Throw New Exception("frmAuswertung::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex)
            Finally
                con.Close()
                scmCmdToExecute.Dispose()
                sdaAdapter.Dispose()
                con.Dispose()
            End Try

        End Function

        Private Function Get_TXP_Connection() As String
            Dim sCstr As String = ""
            Dim txpfile As System.IO.File
            Dim txpread As System.IO.StreamReader
            txpread = txpfile.OpenText(Application.StartupPath + "\TXPConn.cfg")
            sCstr = txpread.ReadLine
            sCstr = Crypto.DecryptText(sCstr, "HutterundMueller")
            sCstr = Left(sCstr, Len(sCstr) - 1)
            txpread.Close()

            Return sCstr
        End Function
        Public Function Save_MAParameter(ByVal auswertungnr As Integer, ByVal Bezeichnung As String, ByVal Parameter As String, ByVal Titel1 As String, ByVal Titel2 As String, ByVal PrintParam As Boolean, ByVal sqlwhere As String)
            Me.Mitarbeiter_Auswerungsparameter.cpMainConnectionProvider = Globals.conn
            Me.Mitarbeiter_Auswerungsparameter.iAuswertungnr = New SqlInt32(CType(auswertungnr, Int32))
            Me.Mitarbeiter_Auswerungsparameter.iMitarbeiternr = New SqlInt32(CType(Globals.Mitarbeiternr, Int32))
            Me.Mitarbeiter_Auswerungsparameter.sBeschreibung = New SqlString(CType(Bezeichnung, String))
            Me.Mitarbeiter_Auswerungsparameter.sParameterdaten = New SqlString(CType(Parameter, String))
            Me.Mitarbeiter_Auswerungsparameter.sTitelzeile1 = New SqlString(CType(Titel1, String))
            Me.Mitarbeiter_Auswerungsparameter.sTitelzeile2 = New SqlString(CType(Titel2, String))
            Me.Mitarbeiter_Auswerungsparameter.sSQLWhere = New SqlString(CType(sqlwhere, String))
            If PrintParam = True Then
                Me.Mitarbeiter_Auswerungsparameter.bParamPrint = New SqlBoolean(CType(True, Boolean))
            Else
                Me.Mitarbeiter_Auswerungsparameter.bParamPrint = New SqlBoolean(CType(False, Boolean))
            End If
            Globals.conn.OpenConnection()
            Me.Mitarbeiter_Auswerungsparameter.Insert()
            Globals.conn.CloseConnection(True)
            Me.MitarbeiterAuswertungsparameter = Me.Get_MAParameter(auswertungnr)
        End Function


        Public Function Get_MAParameter(ByVal Auswertungnr As Integer) As DataTable
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            Dim dtToReturn As DataTable = New DataTable()
            Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute)
            scmCmdToExecute.CommandText = "dbo.sp_mitarbeiter_auswertungparamter_selectall"
            scmCmdToExecute.CommandType = CommandType.StoredProcedure
            scmCmdToExecute.Connection = conn.scoDBConnection
            Try
                scmCmdToExecute.Parameters.Add(New SqlParameter("@mitarbeiternr", SqlDbType.Int, 4, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, Globals.Mitarbeiternr))
                scmCmdToExecute.Parameters.Add(New SqlParameter("@Auswertungnr", SqlDbType.Int, 4, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, Auswertungnr))
                sdaAdapter.Fill(dtToReturn)
                Return dtToReturn
            Catch ex As Exception
                ' // some error occured. Bubble it to caller and encapsulate Exception object
                Throw New Exception("frmAuswertung::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex)
            Finally
                scmCmdToExecute.Dispose()
                sdaAdapter.Dispose()
            End Try
        End Function

        Public Function Delete_Parameter(ByVal AuswertungParameterNr As Integer)
            Me.Mitarbeiter_Auswerungsparameter.iMitarbeiter_AuswertungsparameterNr = New SqlInt32(CType(AuswertungParameterNr, Int32))
            Me.Mitarbeiter_Auswerungsparameter.cpMainConnectionProvider = Globals.conn
            Globals.conn.OpenConnection()
            Me.Mitarbeiter_Auswerungsparameter.Delete()
            Globals.conn.CloseConnection(True)
            Me.MitarbeiterAuswertungsparameter = Me.Get_MAParameter(Me.Auswertung.iAuswertungNr.Value)
        End Function


    End Class

End Namespace
