
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data


'#####################################################
'charter.com support article survey reports
'KEYWORDS. Issue: 4644
'NAQIB 10/14/08    
'#####################################################

Partial Class Reports_ContentRatingReport
    Inherits System.Web.UI.Page


    Private _cmd As DbCommand
    Private _db As Database

    Private Const ASCENDING As String = " ASC"
    Private Const DESCENDING As String = " DESC"


    Private Property DB() As Database
        Get
            Return _db
        End Get
        Set(ByVal value As Database)
            _db = value
        End Set
    End Property
    Private Property CMD() As DbCommand
        Get
            Return _cmd
        End Get
        Set(ByVal value As DbCommand)
            _cmd = value
        End Set
    End Property


    Public Property ReportType() As String
        Get
            If ViewState("reportType") Is Nothing Then
                ViewState("reportType") = String.Empty
            End If
            Return DirectCast(ViewState("reportType"), String)
        End Get
        Set(ByVal value As String)
            ViewState("reportType") = value
        End Set
    End Property

    Protected Function GenerateReport(ByVal reportType As String) As Boolean
        Try
            Dim retCode As Data.DataSet
            If txtStartDate.Text.Length > 0 And txtEndDate.Text.Length > 0 And chkUseDates.Checked Then
                retCode = DB.ExecuteDataSet(reportType, CDate(txtStartDate.Text), CDate(txtEndDate.Text))
            Else
                retCode = DB.ExecuteDataSet(reportType, DBNull.Value, DBNull.Value)
            End If
            GridView1.DataSource = retCode
            GridView1.DataBind()

            If GridView1.Rows.Count > 0 Then
                Label1.Visible = True
                GridViewSortDirection = SortDirection.Ascending
                AddSortImage(0, GridView1.HeaderRow)
            Else
                Label1.Visible = False
            End If

        Catch ex As Exception
            Throw ex
            Return False
        Finally
        End Try
        Return True
    End Function

    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
        Dim sortExpression As String = e.SortExpression
        If GridViewSortDirection = SortDirection.Ascending Then
            GridViewSortDirection = SortDirection.Descending
            SortGridView(sortExpression, DESCENDING)
        Else
            GridViewSortDirection = SortDirection.Ascending
            SortGridView(sortExpression, ASCENDING)
        End If
    End Sub

    Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
        Try
            Dim retCode As Data.DataSet
            retCode = DB.ExecuteDataSet(ReportType(), DBNull.Value, DBNull.Value)
            Dim dv As New DataView()
            dv = retCode.Tables(0).DefaultView
            dv.Sort = sortExpression & " " & direction
            If GridView1.Rows.Count > 0 Then
                GridView1.DataSource = dv
                GridView1.DataBind()
                AddSortImage(GetSortColumnIndex(sortExpression), GridView1.HeaderRow)
            Else
                Label1.Visible = False
            End If
        Catch ex As Exception
            Throw ex
        Finally
        End Try
    End Sub

    Private Function GetSortColumnIndex(ByVal sortExp As String) As Integer
        For Each field As DataControlField In GridView1.Columns
            If field.SortExpression = DirectCast(sortExp, String) Then
                Return GridView1.Columns.IndexOf(field)
            End If
        Next
        Return -1
    End Function

    Private Sub AddSortImage(ByVal columnIndex As Integer, ByVal headerRow As GridViewRow)
        Dim sortImage As New Image()
        If GridViewSortDirection = SortDirection.Ascending Then
            sortImage.ImageUrl = "~/Reports/images/uparrow.gif"
            sortImage.AlternateText = "Ascending Order"
        Else
            sortImage.ImageUrl = "~/Reports/images/downarrow.gif"
            sortImage.AlternateText = "Descending Order"
        End If
        GridView1.HeaderRow.Cells(columnIndex).Controls.Add(sortImage)
    End Sub

    Public Property GridViewSortDirection() As SortDirection
        Get
            If ViewState("sortDirection") Is Nothing Then
                ViewState("sortDirection") = SortDirection.Ascending
            End If
            Return DirectCast(ViewState("sortDirection"), SortDirection)
        End Get
        Set(ByVal value As SortDirection)
            ViewState("sortDirection") = value
        End Set
    End Property

    Protected Sub Avg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Avg.Click
        ReportType = "support.GET_AVG_RATING"
        txtStartDate.Text = ""
        txtEndDate.Text = ""
        chkUseDates.Checked = False
        GenerateReport(ReportType)
    End Sub

    Protected Sub Max_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Max.Click
        ReportType = "support.GET_MAX_RATING"
        txtStartDate.Text = ""
        txtEndDate.Text = ""
        chkUseDates.Checked = False
        GenerateReport(ReportType)
    End Sub

    Protected Sub Min_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Min.Click
        ReportType() = "support.GET_MIN_RATING"
        txtStartDate.Text = ""
        txtEndDate.Text = ""
        chkUseDates.Checked = False
        GenerateReport(ReportType())
    End Sub

    Protected Sub Top20_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Top20.Click
        ReportType = "support.GET_TOP20_RATING"
        txtStartDate.Text = ""
        txtEndDate.Text = ""
        chkUseDates.Checked = False
        GenerateReport(ReportType)
    End Sub

    Protected Sub btnDateRangeReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDateRangeReport.Click
        ReportType = "support.GET_AVG_RATING"
        chkUseDates.Checked = True

        If Not (txtStartDate.Text.Contains("/")) And (txtStartDate.Text.Length = 4) Then
            ' only start year supplied
            txtStartDate.Text = "1/1/" & txtStartDate.Text
        End If

        If Not (txtEndDate.Text.Contains("/")) And (txtEndDate.Text.Length = 4) And (Not txtEndDate.Text = String.Empty) Then
            ' only start year supplied
            txtEndDate.Text = "1/1/" & txtEndDate.Text
            If (CDate(txtStartDate.Text) > CDate(txtEndDate.Text)) Then
                txtEndDate.Text = txtStartDate.Text
            End If
        End If

        GenerateReport(ReportType)
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Response.Redirect("ContentRatingReport.aspx")
    End Sub

    Protected Sub Yearly_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Yearly.Click
        ReportType = "support.GET_AVG_RATING"
        txtStartDate.Text = System.DateTime.Now().AddYears(-1)
        txtEndDate.Text = System.DateTime.Now()
        chkUseDates.Checked = True
        GenerateReport(ReportType)
    End Sub

    Protected Sub Monthly_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Monthly.Click
        ReportType = "support.GET_AVG_RATING"
        txtStartDate.Text = System.DateTime.Now().AddMonths(-1)
        txtEndDate.Text = System.DateTime.Now()
        chkUseDates.Checked = True
        GenerateReport(ReportType)
    End Sub

    Protected Sub Weekly_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Weekly.Click
        ReportType = "support.GET_AVG_RATING"
        txtStartDate.Text = System.DateTime.Now().AddDays(-7)
        txtEndDate.Text = System.DateTime.Now()
        chkUseDates.Checked = True
        GenerateReport(ReportType)
    End Sub

    Protected Sub Daily_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Daily.Click
        ReportType = "support.GET_AVG_RATING"
        txtStartDate.Text = System.DateTime.Now().AddHours(-24)
        txtEndDate.Text = System.DateTime.Now()
        chkUseDates.Checked = True
        GenerateReport(ReportType)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.DB = DatabaseFactory.CreateDatabase("Charter.Website.WS.CommerceEngineConnectionString")
        wucHeader1.Heading = "Content Rating Report"
    End Sub

End Class


