using System;

using System.Data;

using System.Data.SqlTypes;

using System.Data.SqlClient;

using YellowbridgeSoftwareInc.Info;

 

namespace YellowbridgeSoftwareInc.BusinessBase

{

      /// <summary>

      /// Purpose: Business base class for the table 'Products'.

      /// </summary>

      public class Products

      {

            #region Class Member Declarations

                  string _connectionString;

                  SqlConnection _cn;

                  private SqlInt32 _ProductID;

                  private SqlString _ProductName;

                  private SqlInt32 _SupplierID;

                  private SqlInt32 _CategoryID;

                  private SqlString _QuantityPerUnit;

                  private SqlMoney _UnitPrice;

                  private SqlInt16 _UnitsInStock;

                  private SqlInt16 _UnitsOnOrder;

                  private SqlInt16 _ReorderLevel;

                  private SqlBoolean _Discontinued;

                       

            #endregion

           

            public Products(  string connectionString )

            {

                  _connectionString = connectionString ;

            }

           

            // Create a new BusinessBase.Products object and fill it with data by it's primary key

            public Products(  string connectionString, int ProductID  )

            {

                  _connectionString = connectionString ;

                 

                  // Fill the object with data by primary key

                  SelectOne( ProductID  );

            }

           

            public Products(  SqlConnection cn )

            {

                  _cn = cn;

            }

           

            // Create a new BusinessBase.Products object and fill it with data by it's primary key

            public Products(  SqlConnection cn, int ProductID  )

            {

                  _cn = cn;

                 

                  // Fill the object with data by primary key

                  SelectOne( ProductID  );

            }

           

            public void SelectOne(  int ProductID  )

            {

                  // Get new transport object

                  ProductsInfo info = new ProductsInfo();

                 

                  // Get a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                 

                  // Fill the transport

                  persist.SelectOne( ref info, ProductID  );

                 

                  // Fill the fields

                  FillFromTransport( info );

                 

            }

           

            public DataTable SelectOne_DataTable( int ProductID  )

            {

                  // Get a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  return persist.SelectOne_DataTable( ProductID  );

           

            }

           

            public SqlDataReader SelectOne_DataReader( int ProductID  )

            {

                  // Get a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  return persist.SelectOne_DataReader( ProductID  );

           

            }

           

            public ProductsInfoCollection SelectAll()

            {

                  // Create a collection

                  ProductsInfoCollection infoCollection = new ProductsInfoCollection();

                 

                  // Create a ersist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  // Fill the info collection

                  persist.SelectAll( ref infoCollection );

                 

                  // Return the collection

                  return infoCollection;

            }

           

            public DataTable SelectAll_DataTable()

            {

                  // Create a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  return persist.SelectAll_DataTable( );

            }

           

            public SqlDataReader SelectAll_DataReader()

            {

                  // Create a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  return persist.SelectAll_DataReader( );

            }

 

            public Order_DetailsInfoCollection GetOrder_DetailsByProductID()

            {

                  // Create the where clause

                  string whereClause = "[Order Details].[ProductID] = " + _ProductID.ToString() ;

           

                  // Create a collection

                  Order_DetailsInfoCollection infoCollection = new Order_DetailsInfoCollection();

                 

                  // Create a Persist.Products

                  Persist.Order_Details persist;

                  if ( _cn == null )

                        persist = new Persist.Order_Details( _connectionString );

                  else

                        persist = new Persist.Order_Details( _cn );

                 

                  // Fill the collection 

                  persist.SelectWhere( ref infoCollection, whereClause );

                 

                  // Return the collection

                  return infoCollection;

           

            }

           

            public DataTable GetDataTableByProductID()

            {

                  // Create the where clause

                  string whereClause = "[Order Details].[ProductID] = " + _ProductID.ToString() ;

           

                  // Create a DataTable

                  DataTable dt;

                 

                  // Create a Persist.Products

                  Persist.Order_Details persist;

                  if ( _cn == null )

                        persist = new Persist.Order_Details( _connectionString );

                  else

                        persist = new Persist.Order_Details( _cn );

                 

                  // Get the DataTable   

                  dt = persist.SelectWhere_DataTable( whereClause );

                 

                  // Return the DataTable

                  return dt;

           

            }

           

            public SqlDataReader GetDataReaderByProductID()

            {

                  // Create the where clause

                  string whereClause = "[Order Details].[ProductID] = " + _ProductID.ToString() ;

           

                  // Create a DataReader

                  SqlDataReader dr;

                 

                  // Create a Persist.Products

                  Persist.Order_Details persist;

                  if ( _cn == null )

                        persist = new Persist.Order_Details( _connectionString );

                  else

                        persist = new Persist.Order_Details( _cn );

                 

                  // Get the SqlDataReader

                  dr = persist.SelectWhere_DataReader( whereClause );

                 

                  // Return the DataReader

                  return dr;

           

            }

 

           

           

           

            public ProductsInfoCollection SelectWhere( string whereClause )

            {

                  // Create a collection

                  ProductsInfoCollection infoCollection = new ProductsInfoCollection();

                 

                  // Create a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                 

                  // Fill the collection 

                  persist.SelectWhere( ref infoCollection, whereClause );

                 

                  // Return the collection

                  return infoCollection;

            }

           

            public DataTable SelectWhere_DataTable( string whereClause )

            {

                  // Create a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

 

                  // Return the DataTable

                  return persist.SelectWhere_DataTable( whereClause );

                 

            }

           

            public SqlDataReader SelectWhere_DataReader( string whereClause )

            {

                  // Create a Persist.Products

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

 

                  // Return the DataTable

                  return persist.SelectWhere_DataReader( whereClause );

                 

            }

           

           

           

            public bool Insert ()

            {

                  ProductsInfo info = PackageTransport();

                 

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  bool insertSuccess = persist.Insert( ref info );

                 

                  // Fill the fields

                  FillFromTransport( info );

                 

                  return insertSuccess;

            }

           

            public bool Update ()

            {

                  ProductsInfo info = PackageTransport();

                 

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  bool updateSuccess = persist.Update( ref info );

                 

                  // Fill the fields

                  FillFromTransport( info );

                 

                  return updateSuccess;

            }

           

            public bool Delete()

            {

                  Persist.Products persist;

                  if ( _cn == null )

                        persist = new Persist.Products( _connectionString );

                  else

                        persist = new Persist.Products( _cn );

                       

                  bool deleteSuccess = persist.Delete( ProductID   );

                 

                  return deleteSuccess;

            }

                 

           

            public void SetSupplierIDNull()

            {

                  _SupplierID = SqlInt32.Null;

                  }

           

                  public bool IsSupplierIDNull()

              {

                  return _SupplierID.IsNull;

              }

                 

            public void SetCategoryIDNull()

            {

                  _CategoryID = SqlInt32.Null;

                  }

           

                  public bool IsCategoryIDNull()

              {

                  return _CategoryID.IsNull;

              }

                 

            public void SetQuantityPerUnitNull()

            {

                  _QuantityPerUnit = SqlString.Null;

                  }

           

                  public bool IsQuantityPerUnitNull()

              {

                  return _QuantityPerUnit.IsNull;

              }

                 

            public void SetUnitPriceNull()

            {

                  _UnitPrice = SqlMoney.Null;

                  }

           

                  public bool IsUnitPriceNull()

              {

                  return _UnitPrice.IsNull;

              }

                 

            public void SetUnitsInStockNull()

            {

                  _UnitsInStock = SqlInt16.Null;

                  }

           

                  public bool IsUnitsInStockNull()

              {

                  return _UnitsInStock.IsNull;

              }

                 

            public void SetUnitsOnOrderNull()

            {

                  _UnitsOnOrder = SqlInt16.Null;

                  }

           

                  public bool IsUnitsOnOrderNull()

              {

                  return _UnitsOnOrder.IsNull;

              }

                 

            public void SetReorderLevelNull()

            {

                  _ReorderLevel = SqlInt16.Null;

                  }

           

                  public bool IsReorderLevelNull()

              {

                  return _ReorderLevel.IsNull;

              }

                 

                 

                 

            // Packages fields into ProductsTransport

            private ProductsInfo PackageTransport()

            {

                  // Get a new transport

                  ProductsInfo transport = new ProductsInfo();

                 

                  // Fill transport

                  transport.ProductID = _ProductID;

                  transport.ProductName = _ProductName;

                  transport.SupplierID = _SupplierID;

                  transport.CategoryID = _CategoryID;

                  transport.QuantityPerUnit = _QuantityPerUnit;

                  transport.UnitPrice = _UnitPrice;

                  transport.UnitsInStock = _UnitsInStock;

                  transport.UnitsOnOrder = _UnitsOnOrder;

                  transport.ReorderLevel = _ReorderLevel;

                  transport.Discontinued = _Discontinued;

                 

                  return transport;

            }

           

           

            // Fills fields with data from a ProductsTransport

            private void FillFromTransport( ProductsInfo transport )

            {

                  // Fill fields

                  _ProductID = transport.ProductID;

                  _ProductName = transport.ProductName;

                  _SupplierID = transport.SupplierID;

                  _CategoryID = transport.CategoryID;

                  _QuantityPerUnit = transport.QuantityPerUnit;

                  _UnitPrice = transport.UnitPrice;

                  _UnitsInStock = transport.UnitsInStock;

                  _UnitsOnOrder = transport.UnitsOnOrder;

                  _ReorderLevel = transport.ReorderLevel;

                  _Discontinued = transport.Discontinued;

                  }

           

           

            #region Class Property Declarations

            public SqlInt32 ProductID

            {    

                  get{ return _ProductID; }

                  set{ _ProductID = value; }

            }

            public SqlString ProductName

            {    

                  get{ return _ProductName; }

                  set{ _ProductName = value; }

            }

            public SqlInt32 SupplierID

            {    

                  get{ return _SupplierID; }

                  set{ _SupplierID = value; }

            }

            public SqlInt32 CategoryID

            {    

                  get{ return _CategoryID; }

                  set{ _CategoryID = value; }

            }

            public SqlString QuantityPerUnit

            {    

                  get{ return _QuantityPerUnit; }

                  set{ _QuantityPerUnit = value; }

            }

            public SqlMoney UnitPrice

            {    

                  get{ return _UnitPrice; }

                  set{ _UnitPrice = value; }

            }

            public SqlInt16 UnitsInStock

            {    

                  get{ return _UnitsInStock; }

                  set{ _UnitsInStock = value; }

            }

            public SqlInt16 UnitsOnOrder

            {    

                  get{ return _UnitsOnOrder; }