Imports System.Data Imports System.Text Imports System.Windows.Forms Imports System.Xml Public Class clsBook Implements ICloneable Private mguidBookID As Guid Private m_objComposer As clsComposer Private m_objInstrumentation As clsInstrumentation Private m_strBookTitle As String Private m_objPublisher As clsPublisher Private m_blnObjectAlreadyPersisted As Boolean Public Sub New() mguidBookID = Guid.NewGuid m_objComposer = Nothing m_objInstrumentation = Nothing m_strBookTitle = "" m_objPublisher = Nothing m_blnObjectAlreadyPersisted = False End Sub Public Sub New(ByVal objComposer As clsComposer, ByVal strBookTitle As String, ByVal objInstrumentation As clsInstrumentation, ByVal objPublisher As clsPublisher) mguidBookID = Guid.NewGuid m_objComposer = objComposer m_objInstrumentation = objInstrumentation m_strBookTitle = strBookTitle m_objPublisher = objPublisher m_blnObjectAlreadyPersisted = False End Sub Public Sub New(ByVal guidBookID As Guid) mguidBookID = guidBookID Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "sel_book" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmBookID As New SqlClient.SqlParameter prmBookID.ParameterName = "@book_id" prmBookID.SqlDbType = SqlDbType.UniqueIdentifier 'prmBookID.Size = 5 prmBookID.Value = guidBookID ldbcmdCommand.Parameters.Add(prmBookID) Dim ldbdsBooks As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsBooks.Tables(0).Rows.Count > 0 Then Dim ldbrwRow As DataRow ldbrwRow = ldbdsBooks.Tables(0).Rows(0) PopulateFromDBRow(ldbrwRow) m_blnObjectAlreadyPersisted = True MyGlobals.AddCachedObject(Me, Me.BookID.ToString) End If ldbdsBooks.Clear() End Sub Public Sub New(ByVal ldbrwRow As DataRow) PopulateFromDBRow(ldbrwRow) m_blnObjectAlreadyPersisted = True MyGlobals.AddCachedObject(Me, Me.BookID.ToString) End Sub Private Sub PopulateFromDBRow(ByVal ldbrwRow As DataRow) mguidBookID = CType(ldbrwRow.Item("book_id"), Guid) If ldbrwRow.IsNull("composer_id") Then m_objComposer = Nothing Else Dim guidComposerID As Guid = CType(ldbrwRow.Item("composer_id"), Guid) If MyGlobals.CachedObjectExists(guidComposerID.ToString) Then m_objComposer = CType(MyGlobals.CachedObjectRetrieve(guidComposerID.ToString), clsComposer) Else m_objComposer = New clsComposer(guidComposerID) End If End If If ldbrwRow.IsNull("instrumentation_id") Then m_objInstrumentation = Nothing Else Dim guidInstrumentationID As Guid = CType(ldbrwRow.Item("instrumentation_id"), Guid) If MyGlobals.CachedObjectExists(guidInstrumentationID.ToString) Then m_objInstrumentation = CType(MyGlobals.CachedObjectRetrieve(guidInstrumentationID.ToString), clsInstrumentation) Else m_objInstrumentation = New clsInstrumentation(guidInstrumentationID) End If End If m_strBookTitle = CType(ldbrwRow.Item("book_title"), String) If ldbrwRow.IsNull("publisher_id") Then m_objPublisher = Nothing Else Dim guidPublisherID As Guid = CType(ldbrwRow.Item("publisher_id"), Guid) If MyGlobals.CachedObjectExists(guidPublisherID.ToString) Then m_objPublisher = CType(MyGlobals.CachedObjectRetrieve(guidPublisherID.ToString), clsPublisher) Else m_objPublisher = New clsPublisher(guidPublisherID) End If End If End Sub Public Function Delete() As Boolean Try MyGlobals.RemoveCachedObject(Me.BookID.ToString) Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "del_book" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmBookID As New SqlClient.SqlParameter prmBookID.ParameterName = "@book_id" prmBookID.SqlDbType = SqlDbType.UniqueIdentifier 'prmBookID.Size = 5 prmBookID.Value = mguidBookID ldbcmdCommand.Parameters.Add(prmBookID) Dim lintRowsAffected As Integer = MyGlobals.g_objDatabaseLayer.ExecuteNonQuery(ldbcmdCommand) If lintRowsAffected > 0 Then Return True Else Return False End If Catch Return False End Try End Function Public Function Save() As Boolean Try If Not m_objComposer Is Nothing Then If Not m_objComposer.ItemAlreadyPersisted Then m_objComposer.Save() End If End If If Not m_objInstrumentation Is Nothing Then If Not m_objInstrumentation.ItemAlreadyPersisted Then m_objInstrumentation.Save() End If End If If Not m_objPublisher Is Nothing Then If Not m_objPublisher.ItemAlreadyPersisted Then m_objPublisher.Save() End If End If MyGlobals.AddCachedObject(Me, Me.BookID.ToString) If m_blnObjectAlreadyPersisted Then Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "upd_book" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmBookID As New SqlClient.SqlParameter prmBookID.ParameterName = "@book_id" prmBookID.SqlDbType = SqlDbType.UniqueIdentifier 'prmComposerID.Size = 5 prmBookID.Value = mguidBookID ldbcmdCommand.Parameters.Add(prmBookID) ' Set up parameter for stored procedure Dim prmComposerID As New SqlClient.SqlParameter prmComposerID.ParameterName = "@composer_id" prmComposerID.SqlDbType = SqlDbType.UniqueIdentifier 'prmComposerID.Size = 5 If m_objComposer Is Nothing Then prmComposerID.Value = DBNull.Value Else prmComposerID.Value = m_objComposer.ComposerID End If ldbcmdCommand.Parameters.Add(prmComposerID) ' Set up parameter for stored procedure Dim prmInstrumentationID As New SqlClient.SqlParameter prmInstrumentationID.ParameterName = "@instrumentation_id" prmInstrumentationID.SqlDbType = SqlDbType.UniqueIdentifier 'prmInstrumentationID.Size = 5 If m_objInstrumentation Is Nothing Then prmInstrumentationID.Value = DBNull.Value Else prmInstrumentationID.Value = m_objInstrumentation.InstrumentationID End If ldbcmdCommand.Parameters.Add(prmInstrumentationID) ' Set up parameter for stored procedure Dim prmBookTitle As New SqlClient.SqlParameter prmBookTitle.ParameterName = "@book_title" prmBookTitle.SqlDbType = SqlDbType.VarChar prmBookTitle.Size = MyGlobals.BOOK_TITLE_SIZE prmBookTitle.Value = m_strBookTitle ldbcmdCommand.Parameters.Add(prmBookTitle) ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 If m_objPublisher Is Nothing Then prmPublisherID.Value = DBNull.Value Else prmPublisherID.Value = m_objPublisher.PublisherID End If ldbcmdCommand.Parameters.Add(prmPublisherID) Dim lintRowsAffected As Integer = MyGlobals.g_objDatabaseLayer.ExecuteNonQuery(ldbcmdCommand) If lintRowsAffected > 0 Then Return True Else Return False End If Else Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "ins_book" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmBookID As New SqlClient.SqlParameter prmBookID.ParameterName = "@book_id" prmBookID.SqlDbType = SqlDbType.UniqueIdentifier 'prmComposerID.Size = 5 prmBookID.Value = mguidBookID ldbcmdCommand.Parameters.Add(prmBookID) ' Set up parameter for stored procedure Dim prmComposerID As New SqlClient.SqlParameter prmComposerID.ParameterName = "@composer_id" prmComposerID.SqlDbType = SqlDbType.UniqueIdentifier 'prmComposerID.Size = 5 If m_objComposer Is Nothing Then prmComposerID.Value = DBNull.Value Else prmComposerID.Value = m_objComposer.ComposerID End If ldbcmdCommand.Parameters.Add(prmComposerID) ' Set up parameter for stored procedure Dim prmInstrumentationID As New SqlClient.SqlParameter prmInstrumentationID.ParameterName = "@instrumentation_id" prmInstrumentationID.SqlDbType = SqlDbType.UniqueIdentifier 'prmInstrumentationID.Size = 5 If m_objInstrumentation Is Nothing Then prmInstrumentationID.Value = DBNull.Value Else prmInstrumentationID.Value = m_objInstrumentation.InstrumentationID End If ldbcmdCommand.Parameters.Add(prmInstrumentationID) ' Set up parameter for stored procedure Dim prmBookTitle As New SqlClient.SqlParameter prmBookTitle.ParameterName = "@book_title" prmBookTitle.SqlDbType = SqlDbType.VarChar prmBookTitle.Size = MyGlobals.BOOK_TITLE_SIZE prmBookTitle.Value = m_strBookTitle ldbcmdCommand.Parameters.Add(prmBookTitle) ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 If m_objPublisher Is Nothing Then prmPublisherID.Value = DBNull.Value Else prmPublisherID.Value = m_objPublisher.PublisherID End If ldbcmdCommand.Parameters.Add(prmPublisherID) Dim lintRowsAffected As Integer = MyGlobals.g_objDatabaseLayer.ExecuteNonQuery(ldbcmdCommand) If lintRowsAffected > 0 Then m_blnObjectAlreadyPersisted = True Return True Else Return False End If End If Catch e As SqlClient.SqlException MessageBox.Show(e.Message) Return False Catch Return False End Try End Function Public Property BookID() As Guid Get Return mguidBookID End Get Set(ByVal Value As Guid) mguidBookID = Value End Set End Property Public Property Composer() As clsComposer Get Return m_objComposer End Get Set(ByVal Value As clsComposer) m_objComposer = Value End Set End Property Public Property Instrumentation() As clsInstrumentation Get Return m_objInstrumentation End Get Set(ByVal Value As clsInstrumentation) m_objInstrumentation = Value End Set End Property Public Property BookTitle() As String Get Return m_strBookTitle End Get Set(ByVal Value As String) m_strBookTitle = Value.Trim End Set End Property Public Property Publisher() As clsPublisher Get Return m_objPublisher End Get Set(ByVal Value As clsPublisher) m_objPublisher = Value End Set End Property Public Property ItemAlreadyPersisted() As Boolean Get Return m_blnObjectAlreadyPersisted End Get Set(ByVal Value As Boolean) m_blnObjectAlreadyPersisted = Value End Set End Property Public Function Clone() As Object Implements ICloneable.Clone Dim lobjReturnValue As New clsBook lobjReturnValue.BookID = Me.BookID lobjReturnValue.Composer = Me.Composer lobjReturnValue.Instrumentation = Me.Instrumentation lobjReturnValue.BookTitle = Me.BookTitle lobjReturnValue.Publisher = Me.Publisher lobjReturnValue.ItemAlreadyPersisted = Me.ItemAlreadyPersisted Return lobjReturnValue End Function Public Function StateIsIdentical(ByVal lobjOtherBook As clsBook) As Boolean Dim blnResult As Boolean If Me.BookID.Equals(lobjOtherBook.BookID) _ AndAlso Me.BookTitle = lobjOtherBook.BookTitle Then blnResult = True If Me.Composer Is Nothing Then If lobjOtherBook.Composer Is Nothing Then 'OK Else Return False End If Else If lobjOtherBook.Composer Is Nothing Then Return False Else blnResult = Me.Composer.ComposerID.Equals(lobjOtherBook.Composer.ComposerID) End If End If If Not blnResult Then Return False End If If Me.Instrumentation Is Nothing Then If lobjOtherBook.Instrumentation Is Nothing Then 'OK Else Return False End If Else If lobjOtherBook.Instrumentation Is Nothing Then Return False Else blnResult = Me.Instrumentation.InstrumentationID.Equals(lobjOtherBook.Instrumentation.InstrumentationID) End If End If If Not blnResult Then Return False End If If Me.Publisher Is Nothing Then If lobjOtherBook.Publisher Is Nothing Then 'OK Else Return False End If Else If lobjOtherBook.Publisher Is Nothing Then Return False Else blnResult = Me.Publisher.PublisherID.Equals(lobjOtherBook.Publisher.PublisherID) End If End If Else Return False End If Return blnResult End Function Public Function Validate(ByRef lstrErrorString As String) As Boolean lstrErrorString = "" If Me.BookTitle.Length = 0 Then lstrErrorString = "Book Title is required" Return False End If If Me.Publisher Is Nothing Then lstrErrorString = "Publisher is required" Return False End If Return True End Function Public Function OKToDelete(ByRef lstrReferencedByString As String) As Boolean Dim lblnReturnValue As Boolean lblnReturnValue = True lstrReferencedByString = "" Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "sel_book_isreferenced" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmBookID As New SqlClient.SqlParameter prmBookID.ParameterName = "@book_id" prmBookID.SqlDbType = SqlDbType.UniqueIdentifier 'prmBookID.Size = 5 prmBookID.Value = mguidBookID ldbcmdCommand.Parameters.Add(prmBookID) Dim ldbdsBooks As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsBooks.Tables(0).Rows.Count > 0 Then lblnReturnValue = False Dim ldbrwRow As DataRow For Each ldbrwRow In ldbdsBooks.Tables(0).Rows If Not ldbrwRow.IsNull("table_referencing_instance") Then If lstrReferencedByString.Length = 0 Then lstrReferencedByString = CType(ldbrwRow.Item("table_referencing_instance"), String) + "(s)" Else lstrReferencedByString = lstrReferencedByString + " and " + CType(ldbrwRow.Item("table_referencing_instance"), String) + "(s)" End If End If Next End If ldbdsBooks.Clear() Return lblnReturnValue End Function Public Overrides Function ToString() As String Dim strReturnString As New StringBuilder If m_objComposer Is Nothing Then strReturnString.Append(MyGlobals.COLLECTION_STRING) Else strReturnString.Append(m_objComposer.ComposerName) End If strReturnString.Append(" - ") strReturnString.Append(m_strBookTitle) Return strReturnString.ToString End Function Public Sub WriteXML(ByRef objXMLWriter As XmlTextWriter) objXMLWriter.WriteStartElement("Book") objXMLWriter.WriteElementString("BookID", Me.BookID.ToString) objXMLWriter.WriteElementString("BookTitle", Me.BookTitle) If Me.Composer Is Nothing Then objXMLWriter.WriteElementString("ComposerID", "") Else objXMLWriter.WriteElementString("ComposerID", Me.Composer.ComposerID.ToString) Me.Composer.WriteXML(objXMLWriter) End If If Me.Instrumentation Is Nothing Then objXMLWriter.WriteElementString("InstrumentationID", "") Else objXMLWriter.WriteElementString("InstrumentationID", Me.Instrumentation.InstrumentationID.ToString) Me.Instrumentation.WriteXML(objXMLWriter) End If objXMLWriter.WriteElementString("BookPublisherID", Me.Publisher.PublisherID.ToString) Me.Publisher.WriteXML(objXMLWriter) objXMLWriter.WriteEndElement() End Sub Public Shared Function GetAllBooks() As System.Collections.Generic.List(Of clsBook) 'Preload to cache clsComposer.AddAllComposersToCache() clsInstrumentation.AddAllInstrumentationsToCache() clsPublisher.AddAllPublishersToCache() Dim lguidBookID As Guid Dim ldbcmdCommand As New SqlClient.SqlCommand Dim lobjBook As clsBook Dim lcolBooks As New System.Collections.Generic.List(Of clsBook) ldbcmdCommand.CommandText = "sel_book_all" ldbcmdCommand.CommandType = CommandType.StoredProcedure Dim ldbdsBooks As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsBooks.Tables(0).Rows.Count > 0 Then Dim ldbrwRow As DataRow For Each ldbrwRow In ldbdsBooks.Tables(0).Rows lguidBookID = CType(ldbrwRow.Item("book_id"), Guid) If MyGlobals.CachedObjectExists(lguidBookID.ToString) Then lobjBook = CType(MyGlobals.CachedObjectRetrieve(lguidBookID.ToString), clsBook) Else 'lobjBook = New clsBook(lguidBookID) lobjBook = New clsBook(ldbrwRow) End If lcolBooks.Add(lobjBook) Next End If ldbdsBooks.Clear() Return lcolBooks End Function Public Shared Sub AddAllBooksToCache() If MyGlobals.g_objObjectTypesCached.Contains("Books") = False Then GetAllBooks() MyGlobals.g_objObjectTypesCached.Add("Books") End If End Sub End Class