Skip Navigation Links.

Northwind\Data\CodesManagerBase.cs

D:\NorthwindForWeb\Northwind\CS\DotNet\ADO.Net\Northwind\Data\CodesManagerBase.cs
/*
 * This file was generated by ProCG version 2.0
 *
 * File name:	Northwind\Data\CodesManagerBase.cs
 * Language:	C# - ADO.Net 
 * Database:	My Sql
 *
 * Copyright (c) 2002-2019 iGenXSoft.
 * For more information visit http://www.igenxsoft.com
 */



using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Northwind.General;
using System.Threading.Tasks;

namespace Northwind.Data
{
	/// <summary>
	/// Summary description for NorthwindCodesManager.
	/// </summary>
	public class NorthwindCodesManagerBase 
	{
		/** Map to hold all Codes Tables by their number.  */
		protected System.Collections.Hashtable m_SystemCodesTables = new Hashtable();

		/** Data Object of the CCodes table (data layer)*/
		protected CCodes			m_Codes;

		/** Record object for CCodes table (database layer)*/
		protected CCodesRecord	m_CodesRecord;

		protected DataManager m_DataManager;

		/** static memeber to hold NorthwindCodesManager - we use a singleton pattern */
		protected static NorthwindCodesManagerBase m_CodesManager = null;

		/** database Connection for the CCodes */
		protected static DbConnection m_Connection;

		/** CCodes table name */
		static string m_TableName;

		protected  NorthwindCodesManagerBase()
		{
			InitAll(); 
		}

		protected void InitAll()
		{
			if (!WasInitialize()) 
			{
				SetDefaultInitialParams();
			}
			System.Diagnostics.Debug.Assert(m_Connection != null);
			m_DataManager = new DataManager(m_Connection);
			m_CodesRecord = m_DataManager.CodesRecord;
			m_Codes = m_CodesRecord.Codes;
			LoadAllTablesAsync().Wait();
		}

		public static bool WasInitialize()
		{
			if (m_Connection != null && m_TableName != "")
				return (true);
			else
				return (false);
		}

		public static void SetConnection(DbConnection connection)
		{
			m_Connection = connection;
		}

		/** Destructor    */
		protected  virtual void  Dispose()
		{
			m_SystemCodesTables.Clear();
			m_SystemCodesTables = null;
			m_CodesManager = null;
			m_DataManager.Dispose();
			m_DataManager = null;
		}

		/** Destroy Instance */
		static void DestroyInstance()
		{
			m_CodesManager.Dispose();
		}


		/** Set initial parameters - You must call this before using Codes Manager */
		public static void SetInitialParams(DbConnection connection, string tableName) 
		{ 
			m_Connection = connection;
			m_TableName = tableName; 
		}

		public virtual void SetDefaultInitialParams()
		{
			DbConnection connection = NorthwindGeneral.GetDefaultConnection();
			connection.Open();
			NorthwindCodesManager.SetInitialParams(connection, "Codes");
		}	

		protected CCodesRecord CodesRecord
		{
			get
			{
				if (m_Connection == null || m_Connection.State == ConnectionState.Closed || m_Connection.State == ConnectionState.Broken)
				{
					if (m_Connection != null)
					{
						m_Connection.Dispose();
						m_Connection = null;
					}
					InitAll();
				}
				return (m_CodesRecord);
			}
		}				

		/**
		* Gets Table Codes from memeory
		* @param tableNum
		* @return ArrayList of Code
		* @
		*/
		public ArrayList GetTableCodes(int tableNum)  
		{
			if (m_SystemCodesTables == null)
			{
				throw new Exception("You need to call LoadAllTables before using CodesManager");
			}
			ArrayList tableCodes = (ArrayList)m_SystemCodesTables[tableNum];
			if (tableCodes == null)
				tableCodes = new ArrayList();
			return (tableCodes);
		}

		/**
		 * Get Table Codes as DataTAble
		 * Gets codes for specific table
		 * @param tableNum
		 * @return DataTable of the codes items for the table num provided
		 */
	
		public DataTable GetTableCodesDataTable(int tableNum)  
		{
			ArrayList tableCodes = GetTableCodes(tableNum);
			DataTable dataTable = m_Codes.GetDataTable(tableCodes,m_TableName);
			return (dataTable);
		}

		/**
		 * Get Table Codes as a CCodesCollection
		 * Gets codes for specific table
		 * @param tableNum
		 * @return CCodesCollection of the codes items for the table num provided
		 * @
		 */
		public CCodesCollection GetTableCodesCollection(int tableNum)  
		{
			ArrayList tableCodes = GetTableCodes(tableNum);
			CCodesCollection collection = new CCodesCollection(tableCodes);
			return (collection);
		}
		

		/**
		* Get Code
		* Gets one codes item by tableNum and codeNum - numeric code
		* @param tableNum
		* @param codeNum
		* @return CCodes
		*/
		public CCodes GetCode(int tableNum,int codeNum)  
		{
			ArrayList tableCodes = GetTableCodes(tableNum);

			return GetCode(tableCodes,codeNum);
		}


		/**
		* Get Code
		* Gets one codes item by tableNum and strCode - string code
		* @param tableNum
		* @param strCode sting code
		* @return CCodes
		*/
		public CCodes GetCode(int tableNum,string strCode)  
		{
			ArrayList tableCodes = GetTableCodes(tableNum);

			return GetCode(tableCodes,strCode);
		}

		/**
		* Get Code By Description
		* Gets CCodes data object according to the description.
		* We use this function in JComboBox when the user choose one option and we have only the deescription
		* of the selected item.
		* @param tableNum
		* @param description
		* @return CCodes
		* @
		*/
		public CCodes GetCodeByDescription(int tableNum,string description)  
		{
			ArrayList tableCodes = GetTableCodes(tableNum);

			return GetCodeByDescription(tableCodes,description);
		}

		/**
		* Get Code Description
		* Gets the description of specific CCodes item by table num and code num
		* @param tableNum
		* @param codeNum
		* @return description (string)
		* @
		*/
		public string GetCodeDescription(int tableNum,int codeNum)  
		{
			CCodes codes = GetCode(tableNum,codeNum);
			if (codes != null)
				return (codes.Description);
			else
				return (null);
		}

		/**
		* Get Code Name
		* Gets the name of specific CCodes item by table num and code num
		* @param tableNum
		* @param codeNum
		* @return Name (string)
		* @
		*/
		public string GetCodeName(int tableNum,int codeNum)  
		{
			CCodes codes = GetCode(tableNum,codeNum);
			if (codes != null)
				return (codes.Name);
			else
				return (null);
		}

		/**
		* Get Code Description
		* Gets the description of specific CCodes item by table num and string code
		* @param tableNum
		* @param strCode
		* @return description (string)
		* @
		*/
		public string GetCodeDescription(int tableNum,string strCode)  
		{
			CCodes codes = GetCode(tableNum,strCode);
			if (codes != null)
				return (codes.Description);
			else
				return (null);
		}

		/**
		* Get Code Name
		* Gets the Name of specific CCodes item by table num and string code
		* @param tableNum
		* @param strCode
		* @return Name (string)
		* @
		*/
		public string GetCodeName(int tableNum,string strCode)  
		{
			CCodes codes = GetCode(tableNum,strCode);
			if (codes != null)
				return (codes.Name);
			else
				return (null);
		}


		/**
		* Get Description Code
		* gets the code num for an item by table num and it's description
		* @param tableNum
		* @param description
		* @return int code num
		* @
		*/
		public int GetDescriptionCode(int tableNum,string description) 
		{
			CCodes codes = GetCodeByDescription(tableNum,description);
			if (codes != null)
				return (codes.CodeNum);
			else
				return (-1);
		}
		/**
		* Load all Tables
		* Loads all tables from the database, refreshes the codes tables in the memory if they were
		* already loaded.
		*/
		public ArrayList LoadAllTables()  
		{         
			ArrayList arrayList = new ArrayList();
			CodesRecord.Load(CCodesRecord.AccessMethodsEnum.ALL_CODES, arrayList, 0);

			SetAllTables(arrayList);

			return(arrayList);
		}

		/**
		* Load all Tables
		* Loads all tables from the database, refreshes the codes tables in the memory if they were
		* already loaded.
		*/
		public async Task<ArrayList> LoadAllTablesAsync()  
		{         
			ArrayList arrayList = new ArrayList();
			await CodesRecord.LoadAsync(CCodesRecord.AccessMethodsEnum.ALL_CODES, arrayList, 0);

			SetAllTables(arrayList);

			return(arrayList);
		}

		/**
		* Set all Tables
		* Set all tables from an array list (contains all project codes order by TableNum, refreshes the codes tables in the memory if they were
		* already loaded.
		*/
		public 	void SetAllTables(ArrayList allCodesArrayList)  
		{         
			CCodes codes;
			ArrayList   tableCodes=null;
			int lastTableNum = -5;
			int i;

			RemoveAllTables();

			for(i = 0; i < allCodesArrayList.Count; i++) 
			{
				codes = (CCodes)allCodesArrayList[i];

				if (codes != null) 
				{
					if (codes.TableNum != lastTableNum) 
					{
						if (lastTableNum != -5) 
						{
							m_SystemCodesTables.Add(lastTableNum,tableCodes);
						}
						tableCodes = new ArrayList();
					}
					tableCodes.Add(codes);
					lastTableNum = codes.TableNum;
				}
			}
			if (lastTableNum != -5) 
			{
				m_SystemCodesTables.Add(lastTableNum,tableCodes);
			}
		}

		/**
		* Get Code
		* Gets CCodes from a ArrayList of CCodes accoring to code num
		* @param tableCodes - ArrayList
		* @param codeNum
		* @return
		*/
		protected CCodes GetCode(ArrayList tableCodes,int codeNum) 
		{
			CCodes codes = null;
			bool found = false;
			int i;
			
			for(i=0; i < tableCodes.Count; i++)
			{
				codes = (CCodes)tableCodes[i];

				if (codes != null) 
				{
					if (codes.CodeNum == codeNum) 
					{
						found = true;
						break;
					}
				}
			}
			if (found)
				return(codes);
			else
				return(null);
		}

		/**
		* Get Code
		* Gets CCodes from a ArrayList of CCodes accoring to string code
		* @param tableCodes - ArrayList
		* @param strCode
		* @return
		*/
		protected CCodes GetCode(ArrayList tableCodes,string strCode) 
		{
			bool found = false;
			CCodes codes = null;

			int i;
			
			for(i=0; i< tableCodes.Count; i++)
			{
				codes = (CCodes)tableCodes[i];

				if (codes != null) 
				{
					if (codes.StrCode.Equals(strCode)) 
					{
						found = true;
						break;
					}
				}
			}
			if (found)
				return(codes);
			else
				return(null);
		}

		/**
		* Get Code by Description
		* Gets CCodes from a ArrayList of CCodes accoring to description
		* @param tableCodes - ArrayList
		* @param description
		* @return
		*/
		protected CCodes GetCodeByDescription(ArrayList tableCodes,string description) 
		{
			bool found = false;

			CCodes codes = null;

			int i;
			
			for(i=0; i< tableCodes.Count; i++)
			{
				codes = (CCodes)tableCodes[i];

				if (codes != null) 
				{
					if (codes.Description.Equals(description) ) 
					{
						found = true;
						break;
					}
				}
			}
			if (found)
				return(codes);
			else
				return(null);
		}

		/**
		* Remove Table
		* Removes specific table from the memory
		* @param tableNum
		*/
		public void	RemoveTable(int tableNum)
		{
			m_SystemCodesTables.Remove(tableNum);
		}

		/**
		* Remove All tables
		* Removes all tables from the memory
		*/
		public void	RemoveAllTables() 
		{
			m_SystemCodesTables.Clear();
		}

		/**
		* finalize
		* keeps one instance of Codes Manager always. Prevent the GC to remove the
		* Codes Manager.
		*/
		public void Finalize ()
		{
			new Resourator(this);
		}

		public int NumTablesInMemory
		{
			get
			{
				return (m_SystemCodesTables.Keys.Count);
			}
		}

		/**
		* Class to hold one instance of CodesManager.
		*/
		public class Resourator 
		{
			NorthwindCodesManagerBase m_CodesManager;
			public Resourator (NorthwindCodesManagerBase codesManager)
			{
				this.m_CodesManager = codesManager;
			}
		}
	}
}


  //     11943 ProCG uses this line - don't edit it