PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/SongDatabase/Items/SongItem.vb

#
Visual Basic | 142 lines | 67 code | 32 blank | 43 comment | 2 complexity | 18955f0784674488710a116730af377f MD5 | raw file
  1. Imports System.IO
  2. Namespace Items
  3. ''' <summary>
  4. ''' Represents a type of item that is a song.
  5. ''' </summary>
  6. Public Class SongItem
  7. Inherits BaseItem
  8. Private FSongID As Guid = Nothing
  9. ''' <summary>
  10. ''' Gets or sets the song ID.
  11. ''' </summary>
  12. ''' <value>The unique ID of the song.</value>
  13. Public Property SongID() As Guid
  14. Get
  15. If FSongID = Nothing Then Throw New ApplicationException("No ID has been set for this song.")
  16. Return FSongID
  17. End Get
  18. Set(ByVal value As Guid)
  19. If FSongID <> Nothing Then Throw New ApplicationException("An ID has already been set for this song.")
  20. FSongID = value
  21. End Set
  22. End Property
  23. ''' <summary>
  24. ''' Gets or sets the authors of the song.
  25. ''' </summary>
  26. ''' <value>The song's authors.</value>
  27. Public Property Authors() As List(Of String) = New List(Of String)
  28. Private FCategories As New List(Of Guid)
  29. ''' <summary>
  30. ''' Gets the categories the song is assigned to.
  31. ''' </summary>
  32. ''' <value>The list of categories the song is assigned to.</value>
  33. Public ReadOnly Property Categories() As List(Of Guid)
  34. Get
  35. Return FCategories
  36. End Get
  37. End Property
  38. ''' <summary>
  39. ''' Initializes a new instance of the <see cref="SongItem" /> class.
  40. ''' </summary>
  41. Public Sub New()
  42. End Sub
  43. ''' <summary>
  44. ''' Initializes a new instance of the <see cref="SongItem" /> class.
  45. ''' </summary>
  46. ''' <param name="title">The title of the song.</param>
  47. ''' <param name="parts">The parts / verses that make up the song.</param>
  48. Public Sub New(ByVal title As String, ByVal ParamArray parts() As String)
  49. MyBase.Title = title
  50. For Each Part As String In parts
  51. Me.Parts.Add(Part)
  52. Next
  53. End Sub
  54. ''' <summary>
  55. ''' Loads the song from the specified file name.
  56. ''' </summary>
  57. ''' <param name="fileName">Name of the file to load.</param>
  58. ''' <returns>A song item, whose contents are defined in the specified file.</returns>
  59. Public Shared Function Load(ByVal fileName As String) As SongItem
  60. ' Open the file
  61. Dim File As New BinaryReader(New FileStream(fileName, FileMode.Open))
  62. ' Get the ID of the song
  63. Dim Result As New SongItem
  64. Result.SongID = New Guid(IO.Path.GetFileNameWithoutExtension(fileName))
  65. ' Keep reading the file
  66. While File.BaseStream.Position < File.BaseStream.Length
  67. ' Read the name of the next datum
  68. Select Case File.ReadString
  69. Case "TITLE"
  70. Result.Title = File.ReadString
  71. Case "AUTHOR"
  72. Result.Authors.Add(File.ReadString)
  73. Case "COPYRIGHTLINE"
  74. Result.CopyrightLine = File.ReadString
  75. Case "PART"
  76. Result.Parts.Add(File.ReadString)
  77. End Select
  78. End While
  79. ' Return the song
  80. File.Close()
  81. Return Result
  82. End Function
  83. ''' <summary>
  84. ''' Saves the song to the specified file name.
  85. ''' </summary>
  86. ''' <param name="fileName">Name of the file to write the song to.</param>
  87. Public Sub Save(ByVal fileName As String)
  88. ' Create a new file
  89. Dim File As New BinaryWriter(New FileStream(fileName, FileMode.Create))
  90. ' Write title
  91. File.Write("TITLE")
  92. File.Write(MyBase.Title)
  93. ' Write list of authors
  94. For Each Author As String In Me.Authors
  95. File.Write("AUTHOR")
  96. File.Write(Author)
  97. Next
  98. ' Write parts
  99. For Each Part As String In MyBase.Parts
  100. File.Write("PART")
  101. File.Write(Part)
  102. Next
  103. ' Write copyright details
  104. File.Write("COPYRIGHTLINE")
  105. File.Write(MyBase.CopyrightLine)
  106. ' Close the file
  107. File.Close()
  108. End Sub
  109. End Class
  110. End Namespace