/*********************************************************************************************
 * 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
	{
		/// 
		/// Required designer variable.
		/// 
		private System.ComponentModel.Container components = null;
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}
		/// 
		/// Clean up any resources being used.
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 
		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
		/// 
		/// The main entry point for the application.
		/// 
		[STAThread]
		static void Main(string[] args) 
		{
			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);
				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());
			}
			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;
			// 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 == "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);
		}
	}
}