PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/PowerSong/Reporting/frmReporting.vb

#
Visual Basic | 126 lines | 79 code | 34 blank | 13 comment | 0 complexity | 302a83b0f667f8743aba64a6cbadc97a MD5 | raw file
  1Imports System.IO
  2
  3Public Class frmReporting
  4
  5    Public Sub New()
  6
  7        InitializeComponent()
  8
  9        Dim Time As Date = Now
 10        Time = New Date(Time.Year, Time.Month, Time.Day, 0, 0, 0)
 11        dtStartDate.Value = Time.AddMonths(-1)
 12        dtEndDate.Value = Time
 13
 14        If Database.Settings.EnableLogging = False Then
 15            lblStatus.Change(NoticeLabel.IconType.Warning, "Song usage is currently not being recorded.")
 16            MsgBox("Song usage is currently not being recorded, therefore any reports generated will be inaccurate. To report on future song usage, please enable logging via the PowerSong Configuration dialog box, accessible via the Tools menu of the main form.", MsgBoxStyle.Exclamation)
 17        End If
 18
 19    End Sub
 20
 21    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
 22
 23        ' Hide everything to begin with
 24        txtLog.Visible = False
 25        lvReport.Clear()
 26        lvReport.Visible = False
 27
 28        ' Check what type of report to run
 29        If optRawData.Checked Then
 30
 31            ' Run a raw report
 32            Dim Task As New RawReportTask(dtStartDate.Value, dtEndDate.Value)
 33            Dim Form As New ProgressForm(Task, "Running the report...")
 34            Form.ShowDialog()
 35
 36            ' Show results if the task was not cancelled
 37            If Not Task.CancelRequested Then
 38                txtLog.Text = Task.ReportContents
 39                txtLog.Visible = True
 40            End If
 41
 42        ElseIf optSongUsage.Checked Then
 43
 44            ' Run a song usage report
 45            Dim Task As New SongUsageReportTask(dtStartDate.Value, dtEndDate.Value)
 46            Dim Form As New ProgressForm(Task, "Running the report...")
 47            Form.ShowDialog()
 48
 49            ' Show results if the task was not cancelled
 50            If Not Task.CancelRequested Then
 51
 52                ' Configure the list view
 53                lvReport.Columns.Add("Song Title", 500)
 54                lvReport.Columns.Add("Times Played", 100)
 55
 56                ' Populate the results
 57                For Each Entry As KeyValuePair(Of String, Integer) In Task.ReportContents
 58                    Dim Item As ListViewItem = lvReport.Items.Add(Entry.Key)
 59                    Item.SubItems.Add(Entry.Value.ToString)
 60                Next
 61                lvReport.Visible = True
 62
 63            End If
 64
 65        End If
 66
 67    End Sub
 68
 69    Private Sub btnSelectFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFileName.Click
 70
 71        dlgSave.FileName = txtFileName.Text
 72        If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
 73            txtFileName.Text = dlgSave.FileName
 74        End If
 75
 76    End Sub
 77
 78    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
 79
 80        If lvReport.Visible Then
 81
 82            Dim File As StreamWriter = Nothing
 83            Try
 84
 85                ' Ensure a file name has been specified
 86                If txtFileName.Text.Trim = "" Then
 87                    MsgBox("Please enter a file name first.", MsgBoxStyle.Exclamation)
 88                    Exit Sub
 89                End If
 90
 91                ' Create the file
 92                File = New StreamWriter(New FileStream(txtFileName.Text, FileMode.Create))
 93
 94                ' Export the columns
 95                Dim FirstLine As String = String.Empty
 96                For Each Column As ColumnHeader In lvReport.Columns
 97                    If FirstLine <> "" Then FirstLine += ","
 98                    FirstLine += """" + Column.Text.Replace("""", "'") + """"
 99                Next
100                File.WriteLine(FirstLine)
101
102                ' Export the data
103                For Each Row As ListViewItem In lvReport.Items
104                    Dim Line As String = """" + Row.Text.Replace("""", "'") + """"
105                    For SubItemIndex As Integer = 1 To Row.SubItems.Count - 1
106                        Line += ",""" + Row.SubItems(SubItemIndex).Text.Replace("""", "'") + """"
107                    Next
108                    File.WriteLine(Line)
109                Next
110
111                ' Done
112                MsgBox("The data has been exported.", MsgBoxStyle.Information)
113
114            Catch ex As Exception
115                MsgBox("Export failed:" + Environment.NewLine + ex.Message, MsgBoxStyle.Critical)
116            Finally
117                If File IsNot Nothing Then File.Close()
118            End Try
119
120        Else
121            MsgBox("It is not possible to export the type of data that you are looking at.", MsgBoxStyle.Information)
122        End If
123
124    End Sub
125
126End Class