Imports System.Collections.Specialized
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Configuration
Imports GZTW.Data
'''
''' This is a base class from which readonly name/value
''' business classes can be quickly and easily created.
'''
'''
''' As discussed in Chapter 5, inherit from this class to
''' quickly and easily create name/value list objects for
''' population of ListBox or ComboBox controls and for
''' validation of list-based data items in your business
''' objects.
'''
_
Public MustInherit Class NameValueList
Inherits NameObjectCollectionBase
Implements ICloneable
#Region " Clone "
'''
''' Creates a clone of the object.
'''
''' A new object containing the exact data of the original object.
Public Function Clone() As Object Implements ICloneable.Clone
Dim buffer As New MemoryStream()
Dim formatter As New BinaryFormatter()
formatter.Serialize(buffer, Me)
buffer.Position = 0
Return formatter.Deserialize(buffer)
End Function
#End Region
#Region " Collection methods "
'''
''' Returns a value from the list.
'''
''' The positional index of the value in the collection.
''' The specified value.
Default Public ReadOnly Property Item(ByVal Index As Integer) As String
Get
Return CStr(MyBase.BaseGet(Index))
End Get
End Property
'''
''' Returns a value from the list.
'''
''' The name of the value.
''' The specified value.
Default Public ReadOnly Property Item(ByVal Name As String) As String
Get
Return CStr(MyBase.BaseGet(Name))
End Get
End Property
'''
''' Adds a name/value pair to the list.
'''
''' The name of the item.
''' The value to be added.
Protected Sub Add(ByVal Name As String, ByVal Value As String)
MyBase.BaseAdd(Name, Value)
End Sub
'''
''' Returns the first name found in the list with the specified
''' value.
'''
'''
''' This method throws an exception if no matching value is found
''' in the list.
'''
''' The value to search for in the list.
''' The name of the item found.
Public ReadOnly Property Key(ByVal Item As String) As String
Get
Dim keyName As String
For Each keyName In Me
If Me.Item(keyName) = Item Then
Return keyName
End If
Next
' we didn't find a match - throw an exception
Throw New ApplicationException("No matching item in collection")
End Get
End Property
#End Region
#Region " Create and Load "
'''
''' Creates a new NameValueList.
'''
Protected Sub New()
' prevent public creation
End Sub
'''
''' Creates a new NameValueList.
'''
Protected Sub New(ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
MyBase.New(info, context)
End Sub
#End Region
#Region " Data Access "
Private Sub DataPortal_Create(ByVal Criteria As Object)
Throw New NotSupportedException("Invalid operation - create not allowed")
End Sub
'''
''' Override this method to allow retrieval of an existing business
''' object based on data in the database.
'''
'''
''' In many cases you can call the SimpleFetch method to
''' retrieve simple name/value data from a single table in
''' a database. In more complex cases you may need to implement
''' your own data retrieval code using ADO.NET.
'''
''' An object containing criteria values to identify the object.
Protected Overridable Sub DataPortal_Fetch(ByVal Criteria As Object)
Throw New NotSupportedException("Invalid operation - fetch not allowed")
End Sub
Private Sub DataPortal_Update()
Throw New NotSupportedException("Invalid operation - update not allowed")
End Sub
Private Sub DataPortal_Delete(ByVal Criteria As Object)
Throw New NotSupportedException("Invalid operation - delete not allowed")
End Sub
'''
''' Returns the specified database connection string from the application
''' configuration file.
'''
'''
''' The database connection string must be in the appSettings section
''' of the application configuration file. The database name should be
''' prefixed with 'DB:'. For instance, DB:mydatabase.
'''
''' Name of the database.
''' A database connection string.
Protected Function DB(ByVal DatabaseName As String) As String
Return System.Configuration.ConfigurationManager.AppSettings("DB:" & DatabaseName)
End Function
'''
''' Provides default/simple loading for most lists.
''' It is called to load data from the database
'''
'''
''' The DAL database to read
'''
''' The name of the table to read.
''' The name of the column containing name or key values.
''' The name of the column containing data values.
Protected Sub SimpleFetch(ByVal dbase As GZTWDatabase, ByVal TableName As String, ByVal NameColumn As String, ByVal ValueColumn As String)
Dim dbcw As DBCommandWrapper
dbcw = dbase.GetSqlStringCommandWrapper("SELECT " & NameColumn & "," & ValueColumn & " FROM " & TableName)
Dim dr As New Data.SafeDataReader(dbase.ExecuteReader(dbcw))
Try
While dr.Read()
Add(CStr(dr.GetValue(0)), CStr(dr.GetValue(1)))
End While
Finally
dr.Close()
End Try
End Sub
'''
''' Provides default/simple loading for most lists.
''' It is called to load data from the database
''' Customized by JOHN - Modified version to deal with GUID as id column
'''
'''
''' The DAL database to read
'''
''' The name of the table to read.
''' The name of the column containing name or key values.
''' The name of the column containing data values.
Protected Sub SimpleFetchG(ByVal dbase As GZTWDatabase, ByVal TableName As String, ByVal NameColumn As String, ByVal ValueColumn As String)
Dim dbcw As DBCommandWrapper
dbcw = dbase.GetSqlStringCommandWrapper("SELECT " & NameColumn & "," & ValueColumn & " FROM " & TableName)
Dim dr As New Data.SafeDataReader(dbase.ExecuteReader(dbcw))
Try
While dr.Read()
'Modified to accept a Guid as the id value
'NVCHANGED
Add(dr.GetGuid(1).ToString(), CStr(dr.GetValue(0)))
End While
Finally
dr.Close()
End Try
End Sub
'''
''' Same as SimpleFetchG, but only loads records with Active=true
'''
''' Provides default/simple loading for most lists.
''' It is called to load data from the database
''' Customized by JOHN - Modified version to deal with GUID as id column
'''
'''
''' The DAL database to read
'''
''' The name of the table to read.
''' The name of the column containing name or key values.
''' The name of the column containing data values.
Protected Sub SimpleFetchGActiveOnly(ByVal dbase As GZTWDatabase, ByVal TableName As String, ByVal NameColumn As String, ByVal ValueColumn As String)
Dim dbcw As DBCommandWrapper
dbcw = dbase.GetSqlStringCommandWrapper("SELECT " & NameColumn & "," & ValueColumn & " FROM " & TableName & " WHERE AACTIVE=@aTrue")
dbcw.AddInParameter("@aTrue", DbType.Boolean, True)
Dim dr As New Data.SafeDataReader(dbase.ExecuteReader(dbcw))
Try
While dr.Read()
'Modified to accept a Guid as the id value
'NVCHANGED
Add(dr.GetGuid(1).ToString(), CStr(dr.GetValue(0)))
End While
Finally
dr.Close()
End Try
End Sub
'''
''' Loads name value list based on passed sql query text
''' Customized by JOHN - Modified version to deal with GUID as id column
''' and to allow passing in a string to specify sql
'''
'''
''' The DAL database to read
'''
''' The sql query to run. **MUST return the name as the first parameter
''' And a GUID value as the second parameter, anything else is indeterminate
Protected Sub SQLFetchG(ByVal dbase As GZTWDatabase, ByVal SQLText As String)
Dim dbcw As DBCommandWrapper
dbcw = dbase.GetSqlStringCommandWrapper(SQLText)
Dim dr As New Data.SafeDataReader(dbase.ExecuteReader(dbcw))
Try
While dr.Read()
'Modified to accept a Guid as the id value
'NVCHANGED
Add(dr.GetGuid(0).ToString(), CStr(dr.GetValue(1)))
End While
Finally
dr.Close()
End Try
End Sub
'''
''' Loads name value list based on passed sql stored procedure name
''' Customized by JOHN - Modified version to deal with GUID as id column
''' and to allow passing in a string to specify sql
'''
'''
''' The name of the database to read. This database name
''' must correspond to a database entry in the application
''' configuration file.
'''
''' The sql stored procedure query to run. **MUST return the name as the first parameter
''' And a GUID value as the second parameter, anything else is indeterminate
'Protected Sub SPFetchG(ByVal DataBaseName As String, ByVal StoredProcedure As String)
' Dim cn As New SqlConnection(DB(DataBaseName))
' Dim cm As New SqlCommand
' cn.Open()
' Try
' With cm
' .Connection = cn
' .CommandType = CommandType.StoredProcedure
' .CommandText = StoredProcedure
' Dim dr As New Data.SafeDataReader(.ExecuteReader)
' Try
' While dr.Read()
' 'Modified to accept a Guid as the id value
' Add(CStr(dr.GetValue(0)), dr.GetGuid(1).ToString())
' End While
' Finally
' dr.Close()
' End Try
' End With
' Finally
' cn.Close()
' End Try
'End Sub
'''
''' Loads name value list based on passed sql query text
''' Customized by JOHN - to allow passing in a string to specify sql
'''
'''
''' The DAL database to read
'''
''' The sql query to run. **MUST return the name as the first parameter
''' And a string value as the second parameter, anything else is indeterminate
Protected Sub SQLFetch(ByVal dbase As GZTWDatabase, ByVal SQLText As String)
Dim dbcw As DBCommandWrapper
dbcw = dbase.GetSqlStringCommandWrapper(SQLText)
Dim dr As New Data.SafeDataReader(dbase.ExecuteReader(dbcw))
Try
While dr.Read()
Add(CStr(dr.GetValue(0)), CStr(dr.GetValue(1)))
End While
Finally
dr.Close()
End Try
'Before DAL WAS:
'Dim cn As New SqlConnection(DB(DataBaseName))
'Dim cm As New SqlCommand
'cn.Open()
'Try
' With cm
' .Connection = cn
' .CommandText = SQLText
' Dim dr As New Data.SafeDataReader(.ExecuteReader)
' Try
' While dr.Read()
' 'Modified to accept a Guid as the id value
' Add(CStr(dr.GetValue(0)), CStr(dr.GetValue(1)))
' End While
' Finally
' dr.Close()
' End Try
' End With
'Finally
' cn.Close()
'End Try
End Sub
#End Region
#Region " Binding "
'Customized by John for combo list
Private FBindableList As ArrayList
ReadOnly Property BindableList() As ArrayList
Get
If FBindableList Is Nothing Then
FBindableList = New ArrayList
For Each cKey As String In Me.Keys
FBindableList.Add(New DictionaryEntry(cKey, Me.Item(cKey)))
Next
End If
Return FBindableList
End Get
End Property
#End Region
End Class