Imports System.Data Imports System.Xml Public Class clsPublisher Implements ICloneable Private mguidPublisherID As Guid Private mstrPublisherName As String Private mstrPublisherStreamingAllowed As String Private m_blnObjectAlreadyPersisted As Boolean Public Sub New() mguidPublisherID = Guid.NewGuid mstrPublisherName = String.Empty mstrPublisherStreamingAllowed = "Unknown" m_blnObjectAlreadyPersisted = False End Sub Public Sub New(ByVal lstrPublisherName As String, ByVal lstrPublisherStreamingAllowed As String) Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "sel_publisher_by_name" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherName As New SqlClient.SqlParameter prmPublisherName.ParameterName = "@publisher_name" prmPublisherName.SqlDbType = SqlDbType.VarChar prmPublisherName.Size = MyGlobals.PUBLISHER_NAME_SIZE prmPublisherName.Value = lstrPublisherName ldbcmdCommand.Parameters.Add(prmPublisherName) Dim ldbdsPublishers As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsPublishers.Tables(0).Rows.Count > 0 Then Dim ldbrwRow As DataRow ldbrwRow = ldbdsPublishers.Tables(0).Rows(0) PopulateFromDBRow(ldbrwRow) ldbdsPublishers.Clear() m_blnObjectAlreadyPersisted = True Else ldbdsPublishers.Clear() mguidPublisherID = Guid.NewGuid mstrPublisherName = lstrPublisherName mstrPublisherStreamingAllowed = lstrPublisherStreamingAllowed m_blnObjectAlreadyPersisted = False End If End Sub Public Sub New(ByVal lguidPublisherID As Guid) mguidPublisherID = lguidPublisherID Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "sel_publisher" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 prmPublisherID.Value = lguidPublisherID ldbcmdCommand.Parameters.Add(prmPublisherID) Dim ldbdsPublishers As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsPublishers.Tables(0).Rows.Count > 0 Then Dim ldbrwRow As DataRow ldbrwRow = ldbdsPublishers.Tables(0).Rows(0) PopulateFromDBRow(ldbrwRow) m_blnObjectAlreadyPersisted = True MyGlobals.AddCachedObject(Me, Me.PublisherID.ToString) End If ldbdsPublishers.Clear() End Sub Public Sub New(ByVal ldbrwRow As DataRow) PopulateFromDBRow(ldbrwRow) m_blnObjectAlreadyPersisted = True MyGlobals.AddCachedObject(Me, Me.PublisherID.ToString) End Sub Private Sub PopulateFromDBRow(ByVal ldbrwRow As DataRow) mguidPublisherID = CType(ldbrwRow.Item("publisher_id"), Guid) mstrPublisherName = CType(ldbrwRow.Item("publisher_name"), String) mstrPublisherStreamingAllowed = CType(ldbrwRow.Item("publisher_streaming_allowed"), String) End Sub Public Function Delete() As Boolean Try MyGlobals.RemoveCachedObject(Me.PublisherID.ToString) Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "del_publisher" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 prmPublisherID.Value = mguidPublisherID ldbcmdCommand.Parameters.Add(prmPublisherID) 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 MyGlobals.AddCachedObject(Me, Me.PublisherID.ToString) If m_blnObjectAlreadyPersisted Then Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "upd_publisher" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 prmPublisherID.Value = mguidPublisherID ldbcmdCommand.Parameters.Add(prmPublisherID) ' Set up parameter for stored procedure Dim prmPublisherName As New SqlClient.SqlParameter prmPublisherName.ParameterName = "@publisher_name" prmPublisherName.SqlDbType = SqlDbType.VarChar prmPublisherName.Size = MyGlobals.PUBLISHER_NAME_SIZE prmPublisherName.Value = mstrPublisherName ldbcmdCommand.Parameters.Add(prmPublisherName) Dim prmPublisherStreamingAllowed As New SqlClient.SqlParameter prmPublisherStreamingAllowed.ParameterName = "@publisher_streaming_allowed" prmPublisherStreamingAllowed.SqlDbType = SqlDbType.VarChar prmPublisherStreamingAllowed.Size = MyGlobals.PUBLISHER_STREAMING_ALLOWED_SIZE prmPublisherStreamingAllowed.Value = mstrPublisherStreamingAllowed ldbcmdCommand.Parameters.Add(prmPublisherStreamingAllowed) 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_publisher" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 prmPublisherID.Value = mguidPublisherID ldbcmdCommand.Parameters.Add(prmPublisherID) ' Set up parameter for stored procedure Dim prmPublisherName As New SqlClient.SqlParameter prmPublisherName.ParameterName = "@publisher_name" prmPublisherName.SqlDbType = SqlDbType.VarChar prmPublisherName.Size = MyGlobals.PUBLISHER_NAME_SIZE prmPublisherName.Value = mstrPublisherName ldbcmdCommand.Parameters.Add(prmPublisherName) Dim prmPublisherStreamingAllowed As New SqlClient.SqlParameter prmPublisherStreamingAllowed.ParameterName = "@publisher_streaming_allowed" prmPublisherStreamingAllowed.SqlDbType = SqlDbType.VarChar prmPublisherStreamingAllowed.Size = MyGlobals.PUBLISHER_STREAMING_ALLOWED_SIZE prmPublisherStreamingAllowed.Value = mstrPublisherStreamingAllowed ldbcmdCommand.Parameters.Add(prmPublisherStreamingAllowed) 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 Return False End Try End Function Public Function DuplicateExists(ByVal lstrPublisherNameToCheck As String) As Boolean Try Dim ldbcmdCommand As New SqlClient.SqlCommand ldbcmdCommand.CommandText = "sel_publisher_by_name" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherName As New SqlClient.SqlParameter prmPublisherName.ParameterName = "@publisher_name" prmPublisherName.SqlDbType = SqlDbType.VarChar prmPublisherName.Size = MyGlobals.PUBLISHER_NAME_SIZE prmPublisherName.Value = lstrPublisherNameToCheck ldbcmdCommand.Parameters.Add(prmPublisherName) Dim ldbdsPublishers As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsPublishers.Tables(0).Rows.Count > 0 Then Dim ldbrwRow As DataRow For Each ldbrwRow In ldbdsPublishers.Tables(0).Rows If mguidPublisherID.Equals(CType(ldbrwRow.Item("publisher_id"), Guid)) Then 'OK Else ldbdsPublishers.Clear() Return True End If Next End If ldbdsPublishers.Clear() Return False Catch Return False End Try End Function Public Property PublisherID() As Guid Get Return mguidPublisherID End Get Set(ByVal lguidValue As Guid) mguidPublisherID = lguidValue End Set End Property Public Property PublisherName() As String Get Return mstrPublisherName End Get Set(ByVal sValue As String) mstrPublisherName = sValue.Trim End Set End Property Public Property PublisherStreamingAllowed() As String Get Return mstrPublisherStreamingAllowed End Get Set(ByVal sValue As String) mstrPublisherStreamingAllowed = sValue.Trim 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 clsPublisher lobjReturnValue.PublisherID = Me.PublisherID lobjReturnValue.PublisherName = Me.PublisherName lobjReturnValue.PublisherStreamingAllowed = Me.PublisherStreamingAllowed lobjReturnValue.ItemAlreadyPersisted = Me.ItemAlreadyPersisted Return lobjReturnValue End Function Public Function StateIsIdentical(ByVal lobjOtherPublisher As clsPublisher) As Boolean If Me.PublisherID.Equals(lobjOtherPublisher.PublisherID) _ AndAlso Me.PublisherName = lobjOtherPublisher.PublisherName _ AndAlso Me.PublisherStreamingAllowed = lobjOtherPublisher.PublisherStreamingAllowed Then Return True Else Return False End If End Function Public Function Validate(ByRef lstrErrorString As String) As Boolean lstrErrorString = "" If Me.PublisherName.Length = 0 Then lstrErrorString = "Publisher Name is required" Return False End If If Me.DuplicateExists(Me.PublisherName) Then lstrErrorString = "Given Publisher Name already exists" 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_publisher_isreferenced" ldbcmdCommand.CommandType = CommandType.StoredProcedure ' Set up parameter for stored procedure Dim prmPublisherID As New SqlClient.SqlParameter prmPublisherID.ParameterName = "@publisher_id" prmPublisherID.SqlDbType = SqlDbType.UniqueIdentifier 'prmPublisherID.Size = 5 prmPublisherID.Value = mguidPublisherID ldbcmdCommand.Parameters.Add(prmPublisherID) Dim ldbdsPublishers As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsPublishers.Tables(0).Rows.Count > 0 Then lblnReturnValue = False Dim ldbrwRow As DataRow For Each ldbrwRow In ldbdsPublishers.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 ldbdsPublishers.Clear() Return lblnReturnValue End Function Public Overrides Function ToString() As String Return mstrPublisherName End Function Public Sub WriteXML(ByRef objXMLWriter As XmlTextWriter) objXMLWriter.WriteStartElement("Publisher") objXMLWriter.WriteElementString("PublisherID", Me.PublisherID.ToString) objXMLWriter.WriteElementString("PublisherName", Me.PublisherName) objXMLWriter.WriteElementString("PublisherStreamingAllowed", Me.PublisherStreamingAllowed) objXMLWriter.WriteEndElement() End Sub Public Shared Function GetAllPublishers() As System.Collections.Generic.List(Of clsPublisher) Dim lguidPublisherID As Guid Dim ldbcmdCommand As New SqlClient.SqlCommand Dim lobjPublisher As clsPublisher Dim lcolPublishers As New System.Collections.Generic.List(Of clsPublisher) ldbcmdCommand.CommandText = "sel_publisher_all" ldbcmdCommand.CommandType = CommandType.StoredProcedure Dim ldbdsPublishers As DataSet = MyGlobals.g_objDatabaseLayer.ExecuteDataSet(ldbcmdCommand) If ldbdsPublishers.Tables(0).Rows.Count > 0 Then Dim ldbrwRow As DataRow For Each ldbrwRow In ldbdsPublishers.Tables(0).Rows lguidPublisherID = CType(ldbrwRow.Item("publisher_id"), Guid) If MyGlobals.CachedObjectExists(lguidPublisherID.ToString) Then lobjPublisher = CType(MyGlobals.CachedObjectRetrieve(lguidPublisherID.ToString), clsPublisher) Else 'lobjPublisher = New clsPublisher(lguidPublisherID) lobjPublisher = New clsPublisher(ldbrwRow) End If lcolPublishers.Add(lobjPublisher) Next End If ldbdsPublishers.Clear() Return lcolPublishers End Function Public Shared Sub AddAllPublishersToCache() If MyGlobals.g_objObjectTypesCached.Contains("Publishers") = False Then GetAllPublishers() MyGlobals.g_objObjectTypesCached.Add("Publishers") End If End Sub End Class