PageRenderTime 16ms CodeModel.GetById 6ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/Source Code/SongDatabase/Licenses.vb

#
Visual Basic | 100 lines | 59 code | 30 blank | 11 comment | 0 complexity | c9af90d83a39077d198df74a1e1d1193 MD5 | raw file
  1Public Class Licenses
  2
  3    Friend FDatabase As Database = Nothing
  4    Friend FLicenseData As Data.LicensesDataTable = Nothing
  5
  6    Friend Sub New(ByVal database As Database)
  7        FDatabase = database
  8        FLicenseData = database.Data.Licenses
  9    End Sub
 10
 11    Public Function GetAllLicenses() As List(Of License)
 12
 13        ' Get all of the available licenses
 14        Dim Result As New List(Of License)
 15        For Each Row As Data.LicensesRow In FLicenseData
 16            Result.Add(CreateLicense(Row))
 17        Next
 18
 19        ' Return the list of licenses
 20        Return Result
 21
 22    End Function
 23
 24    Public Function FindLicense(ByVal licenseName As String) As License
 25
 26        ' Find the license
 27        licenseName = licenseName.ToUpper.Trim
 28        For Each Row As Data.LicensesRow In FLicenseData
 29            If Row.Name.ToUpper.Trim = licenseName Then
 30                Return CreateLicense(Row)
 31            End If
 32        Next
 33
 34        ' The license could not be found
 35        Return Nothing
 36
 37    End Function
 38
 39    Public Function DoesLicenseExist(ByVal licenseID As Guid) As Boolean
 40        Return FLicenseData.FindByLicenseID(licenseID) IsNot Nothing
 41    End Function
 42
 43    Public Function GetLicense(ByVal licenseID As Guid) As License
 44        Return CreateLicense(FLicenseData.FindByLicenseID(licenseID))
 45    End Function
 46
 47    Public Sub AddLicense(ByVal license As License)
 48
 49        ' Create the row for the license in the XML database
 50        Dim LicenseID As Guid = Guid.NewGuid
 51        license.LicenseID = LicenseID
 52        FLicenseData.AddLicensesRow(LicenseID, license.Name, license.Details, license.Number)
 53
 54        ' Save the database
 55        FDatabase.Save()
 56
 57    End Sub
 58
 59    Public Sub DeleteLicense(ByVal license As License)
 60        DeleteLicense(license.LicenseID)
 61    End Sub
 62
 63    Public Sub DeleteLicense(ByVal licenseID As Guid)
 64
 65        ' Remove the license row
 66        FLicenseData.RemoveLicensesRow(FLicenseData.FindByLicenseID(licenseID))
 67
 68        ' Save changes
 69        FDatabase.Save()
 70
 71    End Sub
 72
 73    Public Sub EditLicense(ByVal licenseID As Guid, ByVal license As License)
 74
 75        ' Internal check
 76        If license.LicenseID <> licenseID Then Throw New ApplicationException("A license is being edited but an invalid license ID was provided.")
 77
 78        ' Change the main license row
 79        Dim LicenseRow As Data.LicensesRow = FLicenseData.FindByLicenseID(licenseID)
 80        LicenseRow.LicenseID = license.LicenseID
 81        LicenseRow.Name = license.Name
 82        LicenseRow.Details = license.Details
 83        LicenseRow.Number = license.Number
 84
 85        ' Save changes
 86        FDatabase.Save()
 87
 88    End Sub
 89
 90    Private Function CreateLicense(ByVal licenseRow As Data.LicensesRow) As License
 91
 92        Dim Result As New License(licenseRow.Name)
 93        Result.LicenseID = licenseRow.LicenseID
 94        Result.Details = licenseRow.Details
 95        Result.Number = licenseRow.Number
 96        Return Result
 97
 98    End Function
 99
100End Class