 /*Copyright GBASE. 2009  All rights reserved.                     */
 /*                                                                   */
 /*This sample program is owned by International Business Machines    */
 /*Corporation or one of its subsidiaries ("IBM") and is copyrighted  */
 /*and licensed, not sold.                                            */
 /*                                                                   */
 /*You may copy, modify, and distribute this sample program in any    */
 /*form without payment to IBM,  for any purpose including developing,*/
 /*using, marketing or distributing programs that include or are      */
 /*derivative works of the sample program. Licenses to IBM patents    */
 /*are expressly excluded from this license.                          */
 /*The sample program is provided to you on an "AS IS" basis, without */
 /*warranty of any kind.  IBM HEREBY  EXPRESSLY DISCLAIMS ALL         */
 /*WARRANTIES EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO*/
 /*THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTIC-*/
 /*ULAR PURPOSE. Some jurisdictions do not allow for the exclusion or */
 /*limitation of implied warranties, so the above limitations or      */
 /*exclusions may not apply to you.  IBM shall not be liable for any  */
 /*damages you suffer as a result of using, modifying or distributing */
 /*the sample program or its derivatives.                             */
 /*                                                                   */
 /*Each copy of any portion of this sample program or any derivative  */
 /*work,  must include the above copyright notice and disclaimer of   */
 /*warranty.                                                          */
 /*                                                                   */

namespace IFXDOTNETDEMO
{
	using System;
	using System.Text;
	using System.Collections;
	using DOTNETDEMO;

	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class CLDemoApp
	{
		public static ArrayList ValidArgs = new ArrayList();
		public static ArrayList RunDemos = new ArrayList();
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//1. Create connection to Demo Database	
			//2. Create Database and schema
			//3. Create Demo tables and data
			//4. DEMONSTRATE EXAMPLES FOR QUERY, STORED PROCEDURE EXECUTION 
			//    RETRIEVING SCHEMA INFORMATION
			
			//Usage : DemoApp [create, query, catalog, all]
			//DemoApp create - runs CreateDemo
			//DemoApp query - runs QueryDemo afer running CreateDemo if required
			//DemoApp catalog - runs CatalogDemo to retrieve catlog info for display
			//DemoApp all - runs RunAllDemo - runs all the examples listed above
			ValidArgs.Add("Create");
			ValidArgs.Add("Query");
			ValidArgs.Add("Insert");
			ValidArgs.Add("Catalog");
			bool IsValid = ValidateCommandLine(args);
			if(IsValid == false)
			{
				Console.WriteLine("DOTNET Demo Usage: DemoApp {[create] [query] [insert] [catalog] | [all]}");
				Console.WriteLine("Press Enter to Continue");
				Console.Read();
				return;
			}
			CLDemoApp MyDemo = new CLDemoApp();
			MyDemo.Run(RunDemos);
		}

		public void Run(ArrayList DemoList)
		{
			foreach(string Demo in DemoList)
			{
				switch(Demo)
				{
					case "QueryDemo":
                QueryDemo();
						break;
					case "CreateDemo":
						CreateDemo();
						break;
					case "CatalogDemo" :
						CatalogDemo();
						break;
					case "InsertDemo" :
						InsertDemo();
						break;
					case "RunAllDemo" : 
						RunAllDemos();
						break;
					default :
						break;
				}
			}
		}

		private void RunAllDemos()
		{
			CreateDemo();
			QueryDemo();
			InsertDemo();
			CatalogDemo();
		}

		private void CreateDemo()
		{
			//delegate all sql operations to utility class
			IfxDemo Demo = IfxDemo.GetInstance();
		}
		
		private void InsertDemo()
		{
			IfxDemo Demo = IfxDemo.GetInstance();
			Demo.InsertWithCommand();
		}

		private void QueryDemo()
		{
       IfxDemo Demo = IfxDemo.GetInstance();
       Demo.ExecuteQuery();
		}
		private void CatalogDemo()
		{
       IfxDemo Demo = IfxDemo.GetInstance();
       Demo.CatalogInfo();
		}
		
		private static bool ValidateCommandLine(string[] args)
		{
			bool Found = false;
			//If there are no arguments, show usage
			if(args.Length == 0 || args.Length > 6)
			{
				return false;
			}
			//so we have arguments
			//check to see if we one or more of the following keywords:
			//create, query, catalog, all
			StringBuilder ArgsString = new StringBuilder();
			for(int i = 0; i < args.Length; i++)
			{
				String Arg = args[i];
				if(String.Compare(Arg, "all", true) == 0)
				{
					RunDemos.Clear();
					RunDemos.Add("RunAllDemo");
					Found = true; 
					break;
				}
				foreach(object s in ValidArgs)
				{
					int Res = String.Compare(Arg,(string)s,true);
					if(Res == 0)
					{
						
						ArgsString.Append(s).Append("Demo");
						RunDemos.Add(ArgsString.ToString());
						ArgsString = new StringBuilder();
						Found = true;
						break;
					}
				}
				
			}
			return Found;
		}
	}
}
