PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
  1. Imports System.IO
  2. Public Class frmReporting
  3. Public Sub New()
  4. InitializeComponent()
  5. Dim Time As Date = Now
  6. Time = New Date(Time.Year, Time.Month, Time.Day, 0, 0, 0)
  7. dtStartDate.Value = Time.AddMonths(-1)
  8. dtEndDate.Value = Time
  9. If Database.Settings.EnableLogging = False Then
  10. lblStatus.Change(NoticeLabel.IconType.Warning, "Song usage is currently not being recorded.")
  11. 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)
  12. End If
  13. End Sub
  14. Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
  15. ' Hide everything to begin with
  16. txtLog.Visible = False
  17. lvReport.Clear()
  18. lvReport.Visible = False
  19. ' Check what type of report to run
  20. If optRawData.Checked Then
  21. ' Run a raw report
  22. Dim Task As New RawReportTask(dtStartDate.Value, dtEndDate.Value)
  23. Dim Form As New ProgressForm(Task, "Running the report...")
  24. Form.ShowDialog()
  25. ' Show results if the task was not cancelled
  26. If Not Task.CancelRequested Then
  27. txtLog.Text = Task.ReportContents
  28. txtLog.Visible = True
  29. End If
  30. ElseIf optSongUsage.Checked Then
  31. ' Run a song usage report
  32. Dim Task As New SongUsageReportTask(dtStartDate.Value, dtEndDate.Value)
  33. Dim Form As New ProgressForm(Task, "Running the report...")
  34. Form.ShowDialog()
  35. ' Show results if the task was not cancelled
  36. If Not Task.CancelRequested Then
  37. ' Configure the list view
  38. lvReport.Columns.Add("Song Title", 500)
  39. lvReport.Columns.Add("Times Played", 100)
  40. ' Populate the results
  41. For Each Entry As KeyValuePair(Of String, Integer) In Task.ReportContents
  42. Dim Item As ListViewItem = lvReport.Items.Add(Entry.Key)
  43. Item.SubItems.Add(Entry.Value.ToString)
  44. Next
  45. lvReport.Visible = True
  46. End If
  47. End If
  48. End Sub
  49. Private Sub btnSelectFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFileName.Click
  50. dlgSave.FileName = txtFileName.Text
  51. If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
  52. txtFileName.Text = dlgSave.FileName
  53. End If
  54. End Sub
  55. Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
  56. If lvReport.Visible Then
  57. Dim File As StreamWriter = Nothing
  58. Try
  59. ' Ensure a file name has been specified
  60. If txtFileName.Text.Trim = "" Then
  61. MsgBox("Please enter a file name first.", MsgBoxStyle.Exclamation)
  62. Exit Sub
  63. End If
  64. ' Create the file
  65. File = New StreamWriter(New FileStream(txtFileName.Text, FileMode.Create))
  66. ' Export the columns
  67. Dim FirstLine As String = String.Empty
  68. For Each Column As ColumnHeader In lvReport.Columns
  69. If FirstLine <> "" Then FirstLine += ","
  70. FirstLine += """" + Column.Text.Replace("""", "'") + """"
  71. Next
  72. File.WriteLine(FirstLine)
  73. ' Export the data
  74. For Each Row As ListViewItem In lvReport.Items
  75. Dim Line As String = """" + Row.Text.Replace("""", "'") + """"
  76. For SubItemIndex As Integer = 1 To Row.SubItems.Count - 1
  77. Line += ",""" + Row.SubItems(SubItemIndex).Text.Replace("""", "'") + """"
  78. Next
  79. File.WriteLine(Line)
  80. Next
  81. ' Done
  82. MsgBox("The data has been exported.", MsgBoxStyle.Information)
  83. Catch ex As Exception
  84. MsgBox("Export failed:" + Environment.NewLine + ex.Message, MsgBoxStyle.Critical)
  85. Finally
  86. If File IsNot Nothing Then File.Close()
  87. End Try
  88. Else
  89. MsgBox("It is not possible to export the type of data that you are looking at.", MsgBoxStyle.Information)
  90. End If
  91. End Sub
  92. End Class