Imports System Imports System.Data Imports NorthwindVB.DataLayer Imports NorthwindVB.BusinessObject Imports NorthwindVB.Models Imports System.Web.Script.Serialization Imports System.Runtime.InteropServices Namespace BusinessObject.Base ''' <summary> ''' Base class for Products. Do not make changes to this class, ''' instead, put additional code in the Products class ''' </summary> Public Class ProductsBase Inherits Models.ProductsModel ''' <summary> ''' Gets or sets the Related Suppliers. Related to column SupplierID ''' </summary> <ScriptIgnore()> _ Public ReadOnly Property Suppliers() As Lazy(Of Suppliers) Get Dim value As Integer Dim hasValue As Boolean = Int32.TryParse(SupplierID.ToString(), value) If hasValue Then Return New Lazy(Of Suppliers)(Function() BusinessObject.Suppliers.SelectByPrimaryKey(value)) Else Return Nothing End If End Get End Property ''' <summary> ''' Gets or sets the Related Categories. Related to column CategoryID ''' </summary> <ScriptIgnore()> _ Public ReadOnly Property Categories() As Lazy(Of Categories) Get Dim value As Integer Dim hasValue As Boolean = Int32.TryParse(CategoryID.ToString(), value) If hasValue Then Return New Lazy(Of Categories)(Function() BusinessObject.Categories.SelectByPrimaryKey(value)) Else Return Nothing End If End Get End Property ''' <summary> ''' Constructor ''' </summary> Public Sub New() End Sub ''' <summary> ''' Selects a record by primary key(s) ''' </summary> Public Shared Function SelectByPrimaryKey(ByVal productID As Integer) As Products Return ProductsDataLayer.SelectByPrimaryKey(productID) End Function ''' <summary> ''' Gets the total number of records in the Products table ''' </summary> Public Shared Function GetRecordCount() As Integer Return ProductsDataLayer.GetRecordCount() End Function ''' <summary> ''' Gets the total number of records in the Products table by SupplierID ''' </summary> Public Shared Function GetRecordCountBySupplierID(supplierID As Integer) As Integer Return ProductsDataLayer.GetRecordCountBySupplierID(supplierID) End Function ''' <summary> ''' Gets the total number of records in the Products table by CategoryID ''' </summary> Public Shared Function GetRecordCountByCategoryID(categoryID As Integer) As Integer Return ProductsDataLayer.GetRecordCountByCategoryID(categoryID) End Function ''' <summary> ''' Gets the total number of records in the Products table based on search parameters ''' </summary> Public Shared Function GetRecordCountDynamicWhere(productID As Integer?, productName As String, supplierID As Integer?, categoryID As Integer?, quantityPerUnit As String, unitPrice As Decimal?, unitsInStock As Short?, unitsOnOrder As Short?, reorderLevel As Short?, discontinued As Boolean?) As Integer Return ProductsDataLayer.GetRecordCountDynamicWhere(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued) End Function ''' <summary> ''' Selects records as a collection (List) of Products sorted by the sortByExpression and returns the maximumRows (# of records) starting from the startRowIndex ''' </summary> Public Shared Function SelectSkipAndTake(maximumRows As Integer, startRowIndex As Integer, <Out()> ByRef totalRowCount As Integer, sortByExpression As String) As ProductsCollection totalRowCount = GetRecordCount() Dim ending As Integer = startRowIndex + maximumRows sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTake(sortByExpression, startRowIndex, ending) End Function ''' <summary> ''' Selects records as a collection (List) of Products sorted by the sortByExpression and returns the maximumRows (# of records) starting from the startRowIndex. Use for non model-binded gridview control. Does not return total row count. ''' </summary> Public Shared Function SelectSkipAndTake(maximumRows As Integer, startRowIndex As Integer, sortByExpression As String) As ProductsCollection sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTake(sortByExpression, startRowIndex, maximumRows) End Function ''' <summary> ''' Selects records by SupplierID as a collection (List) of Products sorted by the sortByExpression and returns the maximumRows (# of records) starting from the startRowIndex ''' </summary> Public Shared Function SelectSkipAndTakeBySupplierID(maximumRows As Integer, startRowIndex As Integer, <Out()> ByRef totalRowCount As Integer, sortByExpression As String, supplierID As Integer) As ProductsCollection totalRowCount = ProductsDataLayer.GetRecordCountBySupplierID(supplierID) Dim ending As Integer = startRowIndex + maximumRows sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTakeBySupplierID(sortByExpression, startRowIndex, ending, supplierID) End Function ''' <summary> ''' Selects records by SupplierID as a collection (List) of Products sorted by the sortByExpression starting from the startRowIndex ''' </summary> Public Shared Function SelectSkipAndTakeBySupplierID(maximumRows As Integer, startRowIndex As Integer, sortByExpression As String, supplierID As Integer) As ProductsCollection Dim ending As Integer = startRowIndex + maximumRows sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTakeBySupplierID(sortByExpression, startRowIndex, ending, supplierID) End Function ''' <summary> ''' Selects records by CategoryID as a collection (List) of Products sorted by the sortByExpression and returns the maximumRows (# of records) starting from the startRowIndex ''' </summary> Public Shared Function SelectSkipAndTakeByCategoryID(maximumRows As Integer, startRowIndex As Integer, <Out()> ByRef totalRowCount As Integer, sortByExpression As String, categoryID As Integer) As ProductsCollection totalRowCount = ProductsDataLayer.GetRecordCountByCategoryID(categoryID) Dim ending As Integer = startRowIndex + maximumRows sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTakeByCategoryID(sortByExpression, startRowIndex, ending, categoryID) End Function ''' <summary> ''' Selects records by CategoryID as a collection (List) of Products sorted by the sortByExpression starting from the startRowIndex ''' </summary> Public Shared Function SelectSkipAndTakeByCategoryID(maximumRows As Integer, startRowIndex As Integer, sortByExpression As String, categoryID As Integer) As ProductsCollection Dim ending As Integer = startRowIndex + maximumRows sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTakeByCategoryID(sortByExpression, startRowIndex, ending, categoryID) End Function ''' <summary> ''' Selects records as a collection (List) of Products sorted by the sortByExpression and returns the maximumRows (# of records) starting from the startRowIndex, based on the search parameters ''' </summary> Public Shared Function SelectSkipAndTakeDynamicWhere(productID As Integer?, productName As String, supplierID As Integer?, categoryID As Integer?, quantityPerUnit As String, unitPrice As Decimal?, unitsInStock As Short?, unitsOnOrder As Short?, reorderLevel As Short?, discontinued As Boolean?, maximumRows As Integer, startRowIndex As Integer, <Out()> ByRef totalRowCount As Integer, sortByExpression As String) As ProductsCollection totalRowCount = GetRecordCountDynamicWhere(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued) sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTakeDynamicWhere(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued, sortByExpression, startRowIndex, maximumRows) End Function ''' <summary> ''' Selects records as a collection (List) of Products sorted by the sortByExpression starting from the startRowIndex, based on the search parameters ''' </summary> Public Shared Function SelectSkipAndTakeDynamicWhere(productID As Integer?, productName As String, supplierID As Integer?, categoryID As Integer?, quantityPerUnit As String, unitPrice As Decimal?, unitsInStock As Short?, unitsOnOrder As Short?, reorderLevel As Short?, discontinued As Boolean?, maximumRows As Integer, startRowIndex As Integer, sortByExpression As String) As ProductsCollection sortByExpression = GetSortExpression(sortByExpression) Return ProductsDataLayer.SelectSkipAndTakeDynamicWhere(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued, sortByExpression, startRowIndex, maximumRows) End Function ''' <summary> ''' Gets the grand total or sum of fields with a money or decimal data type. E.g. UnitPriceTotal ''' </summary> Public Shared Function SelectTotals() As Products Return ProductsDataLayer.SelectTotals() End Function ''' <summary> ''' Selects all records as a collection (List) of Products ''' </summary> Public Shared Function SelectAll() As ProductsCollection Return ProductsDataLayer.SelectAll() End Function ''' <summary> ''' Selects all records as a collection (List) of Products sorted by the sort expression ''' </summary> Public Shared Function SelectAll(sortExpression As String) As ProductsCollection Dim objProductsCol As ProductsCollection = ProductsDataLayer.SelectAll() Return SortByExpression(objProductsCol, sortExpression) End Function ''' <summary> ''' Selects records based on the passed filters as a collection (List) of Products. ''' </summary> Public Shared Function SelectAllDynamicWhere(productID As Integer?, productName As String, supplierID As Integer?, categoryID As Integer?, quantityPerUnit As String, unitPrice As Decimal?, unitsInStock As Short?, unitsOnOrder As Short?, reorderLevel As Short?, discontinued As Boolean?) As ProductsCollection Return ProductsDataLayer.SelectAllDynamicWhere(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued) End Function ''' <summary> ''' Selects records based on the passed filters as a collection (List) of Products sorted by the sort expression. ''' </summary> Public Shared Function SelectAllDynamicWhere(productID As System.Nullable(Of Integer), productName As String, supplierID As System.Nullable(Of Integer), categoryID As System.Nullable(Of Integer), quantityPerUnit As String, unitPrice As System.Nullable(Of Decimal), unitsInStock As System.Nullable(Of Short), unitsOnOrder As System.Nullable(Of Short), reorderLevel As System.Nullable(Of Short), discontinued As System.Nullable(Of Boolean), sortExpression As String) As ProductsCollection Dim objProductsCol As ProductsCollection = ProductsDataLayer.SelectAllDynamicWhere(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued) Return SortByExpression(objProductsCol, sortExpression) End Function ''' <summary> ''' Selects all Products by Suppliers, related to column SupplierID ''' </summary> Public Shared Function SelectProductsCollectionBySupplierID(supplierID As Integer) As ProductsCollection Return ProductsDataLayer.SelectProductsCollectionBySupplierID(supplierID) End Function ''' <summary> ''' Selects all Products by Suppliers, related to column SupplierID, sorted by the sort expression ''' </summary> Public Shared Function SelectProductsCollectionBySupplierID(supplierID As Integer, sortExpression As String) As ProductsCollection Dim objProductsCol As ProductsCollection = ProductsDataLayer.SelectProductsCollectionBySupplierID(supplierID) Return SortByExpression(objProductsCol, sortExpression) End Function ''' <summary> ''' Selects all Products by Categories, related to column CategoryID ''' </summary> Public Shared Function SelectProductsCollectionByCategoryID(categoryID As Integer) As ProductsCollection Return ProductsDataLayer.SelectProductsCollectionByCategoryID(categoryID) End Function ''' <summary> ''' Selects all Products by Categories, related to column CategoryID, sorted by the sort expression ''' </summary> Public Shared Function SelectProductsCollectionByCategoryID(categoryID As Integer, sortExpression As String) As ProductsCollection Dim objProductsCol As ProductsCollection = ProductsDataLayer.SelectProductsCollectionByCategoryID(categoryID) Return SortByExpression(objProductsCol, sortExpression) End Function ''' <summary> ''' Selects ProductID and ProductName columns for use with a DropDownList web control, ComboBox, CheckedBoxList, ListView, ListBox, etc ''' </summary> Public Shared Function SelectProductsDropDownListData() As ProductsCollection Return ProductsDataLayer.SelectProductsDropDownListData() End Function ''' <summary> ''' Sorts the ProductsCollection by sort expression ''' </summary> Public Shared Function SortByExpression(objProductsCol As ProductsCollection, sortExpression As String) As ProductsCollection Dim isSortDescending As Boolean = sortExpression.ToLower().Contains(" desc") If isSortDescending Then sortExpression = sortExpression.Replace(" DESC", "") sortExpression = sortExpression.Replace(" desc", "") Else sortExpression = sortExpression.Replace(" ASC", "") sortExpression = sortExpression.Replace(" asc", "") End If Select Case sortExpression Case "ProductID" objProductsCol.Sort(Products.ByProductID) Exit Select Case "ProductName" objProductsCol.Sort(Products.ByProductName) Exit Select Case "SupplierID" objProductsCol.Sort(Products.BySupplierID) Exit Select Case "CategoryID" objProductsCol.Sort(Products.ByCategoryID) Exit Select Case "QuantityPerUnit" objProductsCol.Sort(Products.ByQuantityPerUnit) Exit Select Case "UnitPrice" objProductsCol.Sort(Products.ByUnitPrice) Exit Select Case "UnitsInStock" objProductsCol.Sort(Products.ByUnitsInStock) Exit Select Case "UnitsOnOrder" objProductsCol.Sort(Products.ByUnitsOnOrder) Exit Select Case "ReorderLevel" objProductsCol.Sort(Products.ByReorderLevel) Exit Select Case "Discontinued" objProductsCol.Sort(Products.ByDiscontinued) Exit Select Case Else Exit Select End Select If isSortDescending Then objProductsCol.Reverse() End If Return objProductsCol End Function ''' <summary> ''' Inserts a record ''' </summary> Public Function Insert() As Integer Dim objProducts As Products = DirectCast(Me, Products) Return ProductsDataLayer.Insert(objProducts) End Function ''' <summary> ''' Updates a record ''' </summary> Public Sub Update() Dim objProducts As Products = DirectCast(Me, Products) ProductsDataLayer.Update(objProducts) End Sub ''' <summary> ''' Deletes a record based on primary key(s) ''' </summary> Public Shared Sub Delete(ByVal productID As Integer) ProductsDataLayer.Delete(productID) End Sub Private Shared Function GetSortExpression(sortByExpression As String) As String If [String].IsNullOrEmpty(sortByExpression) Then sortByExpression = "ProductID" Else If sortByExpression.Contains(" asc") Then sortByExpression = sortByExpression.Replace(" asc", "") End If Return sortByExpression End Function ''' <summary> ''' Compares ProductID used for sorting ''' </summary> Public Shared ByProductID As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return x.ProductID.CompareTo(y.ProductID) End Function ''' <summary> ''' Compares ProductName used for sorting ''' </summary> Public Shared ByProductName As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Dim value1 As String = If(x.ProductName, String.Empty) Dim value2 As String = If(y.ProductName, String.Empty) Return value1.CompareTo(value2) End Function ''' <summary> ''' Compares SupplierID used for sorting ''' </summary> Public Shared BySupplierID As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return Nullable.Compare(x.SupplierID, y.SupplierID) End Function ''' <summary> ''' Compares CategoryID used for sorting ''' </summary> Public Shared ByCategoryID As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return Nullable.Compare(x.CategoryID, y.CategoryID) End Function ''' <summary> ''' Compares QuantityPerUnit used for sorting ''' </summary> Public Shared ByQuantityPerUnit As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Dim value1 As String = If(x.QuantityPerUnit, String.Empty) Dim value2 As String = If(y.QuantityPerUnit, String.Empty) Return value1.CompareTo(value2) End Function ''' <summary> ''' Compares UnitPrice used for sorting ''' </summary> Public Shared ByUnitPrice As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return Nullable.Compare(x.UnitPrice, y.UnitPrice) End Function ''' <summary> ''' Compares UnitsInStock used for sorting ''' </summary> Public Shared ByUnitsInStock As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return Nullable.Compare(x.UnitsInStock, y.UnitsInStock) End Function ''' <summary> ''' Compares UnitsOnOrder used for sorting ''' </summary> Public Shared ByUnitsOnOrder As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return Nullable.Compare(x.UnitsOnOrder, y.UnitsOnOrder) End Function ''' <summary> ''' Compares ReorderLevel used for sorting ''' </summary> Public Shared ByReorderLevel As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return Nullable.Compare(x.ReorderLevel, y.ReorderLevel) End Function ''' <summary> ''' Compares Discontinued used for sorting ''' </summary> Public Shared ByDiscontinued As Comparison(Of Products) = Function(ByVal x As Products, ByVal y As Products) Return x.Discontinued.CompareTo(y.Discontinued) End Function End Class End Namespace