/*********************************************************************************************
 * Copyright 2002 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: CmpRpt.cs $     $Revision: 3 $
 * $Author: Jsj $   $Date: 6/08/05 4:31p $
 *
 * $History: CmpRpt.cs $
 * 
 * *****************  Version 3  *****************
 * User: Jsj          Date: 6/08/05    Time: 4:31p
 * Updated in $/EXE/RefObj/CmpRpt
 * cleanup
 * 
 * *****************  Version 2  *****************
 * User: Jsj          Date: 1/08/04    Time: 2:51p
 * Updated in $/EXE/RefObj/CmpRpt
 * constructor of  RODB() class changed
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 11/11/02   Time: 7:17a
 * Created in $/EXE/RefObj/CmpRpt
 *********************************************************************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Data;
using RODBInterface;


// CmpRpt reads data for the RO database and puts the data into the
// temporary file, 'print.tmp'. After this, it is sorted and printed.
namespace CmpRpt
{
	// Form1 just acts as the container for this app.
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args) 
		{
			if (args.Length <= 0) return; // nothing to do
			bool success;
			VlnXmlElement rootXml;
			VlnXmlDocument myroXmlDoc;
			string recid=null;
			string tbl=null;
			RODB myrodb=null;

			// the next two lists contain group headers for the reports.
			ArrayList headers = new ArrayList();
			ArrayList reversehdrs = new ArrayList();

			//if (args.Length <= 0) return;
			//// if an RO directory path was passed in, then change the
			//// current working directory to it.
			//// this path will also be used to generate a connection string
			//// for the Access database.
			//if (args.Length == 2)
			//{
			//    //				MessageBox.Show(args[1],"args[1]");
			//    string path = args[0];
			//    Directory.SetCurrentDirectory(path);
			//    tbl = args[1].Substring(0, 4);
			//    //tbl = args[1].Substring(13,4);
			//    //    recid = args[1].Substring(17,8);
			//    if (args[1].Length > 4)
			//        recid = args[1].Substring(4, 8);
			//    //				myrodb = new RODB(path);
			//}
			//else
			//{
			//    tbl = args[0].Substring(0, 4);
			//    recid = args[0].Substring(4, 8);
			//    //				myrodb = new RODB(Directory.GetCurrentDirectory());
			//}
/******/
			// if an RO directory path was passed in, then change the
			// current working directory to it.
			// this path will also be used to generate a connection string
			// for the Access database.
			string roIdArg = BuildROList(args);
			string[] roIDList = roIdArg.Split(',');
			if (args.Length == 2)
			{
				string path = args[0];
				Directory.SetCurrentDirectory(path);
			}
			myrodb = new RODB(Directory.GetCurrentDirectory());
			VlnXmlElement rptele=null;

			// Add the root to the tree
			myroXmlDoc = myrodb.RODB_GetRoot();

			rootXml = (VlnXmlElement) myroXmlDoc.FirstChild;
			success = myrodb.RODB_GetRootGroups(rootXml);
			if (success == false) return;

			foreach (string roid in roIDList)
			{
				Application.DoEvents();
			    tbl = roid.Substring(0, 4);
			    recid = (roid.Length > 4) ? roid.Substring(4, 8) : null;

				// if the table & recid = all zeros, we have a request
				// for all ROs, otherwise it's a request for specific RO(s)
				if (tbl == "0000" && recid != null && recid == "00000000")
				{
					// the report element becomes the root.
					rptele = rootXml;
				}
				else
				{
					// from the input tbl string, get a table name.
					int itbl = System.Convert.ToInt32(tbl, 16);
					string stbl = System.Convert.ToString(itbl, 10);
					string pstbl = stbl.PadLeft(6, '0');
					string tbname = "RO" + pstbl;

					// read in this element from the table.
					rptele = myrodb.RODB_ReadRO(tbname, recid);

					VlnXmlElement parent;
					string parentid;
					parentid = rptele.GetAttribute("ParentID");
					string rpteleParentid = parentid;

					// walk up tree to get the path through the tree.
					VlnXmlElement child = rptele;
					while (parentid != null && parentid != "00000000")
					{
						parent = myrodb.RODB_ReadRO(tbname, parentid);
						if (parent != null)
						{
							parentid = parent.GetAttribute("ParentID");
							if (parentid == "00000000")
							{
								reversehdrs.Add(parent.InnerText);
								break;
							}
							parent.AppendChild(child);
							child = parent;
							reversehdrs.Add(parent.GetAttribute("MenuTitle"));
						}
						else
							parentid = null;
					}

					// Now hook this into the top part of tree by looking at the table
					// names.  
					VlnXmlElement group = (VlnXmlElement)rootXml.FirstChild;
					while (group != null)
					{
						string curtbname = group.GetAttribute("Table");
						// hook it in here.
						if (curtbname == tbname)
						{
							if (rpteleParentid != "00000000")
								group.AppendChild(child);
							else    // if this is a top group, just reset rept ele
								rptele = group;
							break;
						}
						group = (VlnXmlElement)group.NextSibling;
					}
					// Set up the headers array, it's in reverse order since we walked
					// up the tree, not down.
					int cnt = reversehdrs.Count;
					for (int i = cnt - 1; i >= 0; i--)
						headers.Add(reversehdrs[i]);
				}
			rptele.Show(myrodb,headers);
			headers.Clear();
			reversehdrs.Clear();
		}// end foreach roid

		}

		private static string BuildROList(string[] args)
		{
			string parm2 = args[args.Length - 1];
			string roIdArg = null;
			if (parm2.StartsWith("/f="))
			{
				FileInfo fi = new FileInfo(parm2.Substring(3));
				using (StreamReader sr = fi.OpenText())
				{
					roIdArg = sr.ReadToEnd();
					sr.Close();
				}
			}
			else
				roIdArg = parm2;
			return roIdArg;
		}
	}
}
