PageRenderTime 43ms CodeModel.GetById 35ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/Source Code/SongDatabase/Items/SongItem.vb

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