/*********************************************************************************************
 * Copyright 2002 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: FieldTextFrm.cs $     $Revision: 12 $
 * $Author: Kathy $   $Date: 7/15/04 11:11a $
 *
 * $History: FieldTextFrm.cs $
 * 
 * *****************  Version 12  *****************
 * User: Kathy        Date: 7/15/04    Time: 11:11a
 * Updated in $/EXE/RefObj/ROEditor
 * Fix B2004-015
 * 
 * *****************  Version 11  *****************
 * User: Kathy        Date: 12/09/03   Time: 9:29a
 * Updated in $/EXE/RefObj/ROEditor
 * B2003-066 fix - add new fieldname crash
 * 
 * *****************  Version 10  *****************
 * User: Kathy        Date: 5/21/03    Time: 12:46p
 * Updated in $/EXE/RefObj/ROEditor
 * B2003-041: edit of field name didn't always save
 * 
 * *****************  Version 9  *****************
 * User: Kathy        Date: 12/10/02   Time: 2:25p
 * Updated in $/EXE/RefObj/ROEditor
 * fieldname special chars
 * 
 * *****************  Version 8  *****************
 * User: Kathy        Date: 12/02/02   Time: 8:29a
 * Updated in $/EXE/RefObj/ROEditor
 * fieldname replace chars
 * 
 * *****************  Version 7  *****************
 * User: Kathy        Date: 12/02/02   Time: 6:13a
 * Updated in $/EXE/RefObj/ROEditor
 * added arg on db call
 * 
 * *****************  Version 6  *****************
 * User: Kathy        Date: 11/19/02   Time: 11:31a
 * Updated in $/EXE/RefObj/ROEditor
 * add wait cursor for long op
 * 
 * *****************  Version 5  *****************
 * User: Kathy        Date: 9/27/02    Time: 1:13p
 * Updated in $/EXE/RefObj/ROEditor
 * fix digit as first char in fieldname
 * 
 * *****************  Version 4  *****************
 * User: Kathy        Date: 9/25/02    Time: 9:56a
 * Updated in $/EXE/RefObj/ROEditor
 * dev
 * 
 * *****************  Version 3  *****************
 * User: Kathy        Date: 9/11/02    Time: 1:15p
 * Updated in $/EXE/RefObj/ROEditor
 * vlnxml
 * 
 * *****************  Version 2  *****************
 * User: Kathy        Date: 9/05/02    Time: 12:41p
 * Updated in $/EXE/RefObj/ROEditor
 * dev
 * 
 * *****************  Version 1  *****************
 * User: Jsj          Date: 8/23/02    Time: 3:33p
 * Created in $/EXE/RefObj/ROEditor
 *********************************************************************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Text;
using ROFields;
using RODBInterface;
namespace ROEditor
{
	/// 
	/// Summary description for FieldText.
	/// 
	public class FieldTextFrm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblFName;
		private System.Windows.Forms.Label lblWidth;
		private System.Windows.Forms.TextBox tbFieldName;
		private System.Windows.Forms.TextBox tbWidth;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Label lblPattern;
		private System.Windows.Forms.TextBox tbPattern;
		/// 
		/// Required designer variable.
		/// 
		private System.ComponentModel.Container components = null;
	
		private ROField myrof;
		private RODB myrodb;
		private VlnXmlElement myelem;
		private string schemapiece;
		private string origname;
		private bool exists=false;
		private uint original_type;
		private uint editlevel;
		private ArrayList avail_list;
		private ArrayList inuse_list;
		public FieldTextFrm(ROField rof, RODB rodb, VlnXmlElement elem, uint otype, uint elevel, string oname, ArrayList alist, ArrayList ilist)
		{
			//
			// Required for Windows Form Designer support
			//
			myrodb = rodb;
			myelem = elem;
			myrof = rof;
			original_type = otype;
			editlevel = elevel;
			avail_list = alist;
			inuse_list = ilist;
			uint ftype = rof.GetFieldType;
			// origname is the original name if this is a modification of a fieldname
			// if it's new, it will be null
			if(oname!=null)origname=CvtUserFldToFld(oname);
			InitializeComponent();
			
			if (ftype == (uint)FieldTypes.FrmtSingleTxt) 
			{
				this.tbPattern.Visible = true;
				this.lblPattern.Visible = true;
			}
			else
			{
				this.tbPattern.Visible = false;
				this.lblPattern.Visible = false;
			}
			// put name out. and if it's an existing field get current data to 
			// put out.
			this.tbFieldName.Text = rof.GetFieldname;
			if (rof.GetRecID != null)
			{
				exists = true;
				if (rof.GetMasterRecID != null)
					// get it from the master, else read in the definition from
					// the local table
					schemapiece = myrodb.RODB_GetSchemaPiece(rof.GetMasterRecID,"ROMaster");
				else
					schemapiece = myrodb.RODB_GetSchemaPiece(rof.GetRecID,myelem.GetAttribute("Table"));
				int indxs = schemapiece.IndexOf("maxLength value=");
				int indxe = schemapiece.IndexOf("\"",indxs+17);   // +17, get past maxLength value ="
				this.tbWidth.Text = schemapiece.Substring(indxs+17,indxe-(indxs+17));
				
				// get pattern too.
				if (ftype == (uint)FieldTypes.FrmtSingleTxt) 
				{
					indxs = schemapiece.IndexOf("pattern value=");
					indxe = schemapiece.IndexOf("\"",indxs+15);
					this.tbPattern.Text = schemapiece.Substring(indxs+15,indxe-(indxs+15)); 
				}
			}
		}
		private string CvtUserFldToFld(string fldname)
		{
			if (fldname.Length < 2)
				return fldname;
			// a digit cannot start an xml fieldname, prepend a "__" to it.
			string tmp0;
			if (char.IsDigit(fldname,0))
				tmp0 = "__" + fldname;
			else
				tmp0 = fldname;
			// an xml fieldname cannot have a space, change it to a "__"
			string tmpstr = tmp0.Replace(" ","__");
			int len = tmpstr.Length;
			int cnt = 0;
			// this is also our sequence that tells us the follow 3 digits is the ascii number (base 10)
			// of the character we replaced.
			string OKpunch = "-._"; 
			string outstr = "";
			int decval;
			while (cnt < len)
			{
				char tmpchr = tmpstr[cnt];
				if(!char.IsLetterOrDigit(tmpchr)&& (OKpunch.IndexOf(tmpchr) == -1) )
				{
					decval = tmpchr;
					outstr += OKpunch + decval.ToString("D3");
				}
				else
				{
					outstr += tmpchr.ToString();
				}
				cnt++;
			}
			return outstr;
		}
		private bool CheckValidWidth(string wid)
		{
			long val;
			try {
				// Attempt to convert to long, if it fails, there are problems in the text
				val = System.Convert.ToInt64(wid);
			}
			catch {
				MessageBox.Show("The field width is not a valid number. Please re-enter");
				return false;
			}
			return true;
		}
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string savschemapiece;
			bool success=false;
			// check for field validity
			if (this.tbFieldName.Text == null || this.tbWidth.Text == null)
			{
				MessageBox.Show("Cannot have empty field defintion name or width");
				return;
			}
			success=CheckValidWidth(this.tbWidth.Text);
			if (success == false) return;
			// check that it is not duplicate name.
			ROField rof;
			for (int i=0; i< avail_list.Count; i++)
			{
				rof = (ROField) avail_list[i];
				if (rof.GetFieldname == this.tbFieldName.Text && rof.GetRecID != myrof.GetRecID)
				{
					MessageBox.Show("The field name is not unique. Please re-enter");
					return;
				}
			}
			for (int i=0; i< inuse_list.Count; i++)
			{
				rof = (ROField) inuse_list[i];
				if (rof.GetFieldname == this.tbFieldName.Text && rof.GetRecID != myrof.GetRecID)
				{
					MessageBox.Show("The field name is not unique. Please re-enter");
					return;
				}
			}
			// generate the new schema string based on text box input.
			string fname=CvtUserFldToFld(this.tbFieldName.Text);
			savschemapiece = myrof.MakeSchemaString(fname, this.tbWidth.Text, this.tbPattern.Text);
			myrof.SetFieldname(fname);
			// if this is 'new'. create a new element and do an insert to Master database
			if (exists == false)
				success = myrodb.RODB_NewFieldRecord(myrof, myelem, savschemapiece, editlevel, null);
			else     // modification of existing....
			{
				Cursor.Current = Cursors.WaitCursor;
				success = myrodb.RODB_UpdateFieldRecord(myrof, myelem, savschemapiece, origname, fname, editlevel, false);
				Cursor.Current = Cursors.Default;
			}
			if (success != true)
			{
				this.DialogResult = DialogResult.Cancel;
				return; // DO YET process an error.
			}
			else		 
			{
				// if this is a RRO schema, reload. We're not reloading the group schema
				// because it is rarely used and is only loaded as needed.
				if (editlevel == (uint) RecordType.Schema)
				{
					XmlSchema myschema;
					VlnXmlElement parent;
					parent = myelem;
					while (parent != null)
					{
						if (parent.HasAttribute("Schema") == true) break;
						if (parent.HasAttribute("FieldsInUse") == true) break;
						// If loop to top, fields haven't been defined yet, don't
						// crash on caste - just set to return null (B2004-015)
						if (parent.ParentNode is VlnXmlElement)
							parent = (VlnXmlElement) parent.ParentNode;
						else
							parent = null;
					}
					// if a schema has been read in, delete it and reread it.
					if (parent !=null && parent.HasAttribute("Schema"))
					{
						// delete this attribute because we'll reread it in.
						parent.RemoveAttribute("Schema");
						myschema = myrodb.RODB_GetSchema(myelem);
					}
				}
			}
			this.Close();
		}
		/// 
		/// 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()
		{
			this.lblFName = new System.Windows.Forms.Label();
			this.lblWidth = new System.Windows.Forms.Label();
			this.tbFieldName = new System.Windows.Forms.TextBox();
			this.tbWidth = new System.Windows.Forms.TextBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.lblPattern = new System.Windows.Forms.Label();
			this.tbPattern = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// lblFName
			// 
			this.lblFName.Location = new System.Drawing.Point(16, 16);
			this.lblFName.Name = "lblFName";
			this.lblFName.Size = new System.Drawing.Size(80, 16);
			this.lblFName.TabIndex = 0;
			this.lblFName.Text = "Field Name:";
			// 
			// lblWidth
			// 
			this.lblWidth.Location = new System.Drawing.Point(16, 40);
			this.lblWidth.Name = "lblWidth";
			this.lblWidth.Size = new System.Drawing.Size(72, 16);
			this.lblWidth.TabIndex = 1;
			this.lblWidth.Text = "Width:";
			// 
			// tbFieldName
			// 
			this.tbFieldName.Location = new System.Drawing.Point(104, 8);
			this.tbFieldName.Name = "tbFieldName";
			this.tbFieldName.Size = new System.Drawing.Size(320, 22);
			this.tbFieldName.TabIndex = 2;
			this.tbFieldName.Text = "";
			// 
			// tbWidth
			// 
			this.tbWidth.Location = new System.Drawing.Point(104, 40);
			this.tbWidth.Name = "tbWidth";
			this.tbWidth.Size = new System.Drawing.Size(32, 22);
			this.tbWidth.TabIndex = 3;
			this.tbWidth.Text = "";
			// 
			// btnOK
			// 
			this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOK.Location = new System.Drawing.Point(216, 72);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(88, 24);
			this.btnOK.TabIndex = 4;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(336, 72);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 24);
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "Cancel";
			// 
			// lblPattern
			// 
			this.lblPattern.Location = new System.Drawing.Point(160, 40);
			this.lblPattern.Name = "lblPattern";
			this.lblPattern.Size = new System.Drawing.Size(48, 16);
			this.lblPattern.TabIndex = 6;
			this.lblPattern.Text = "Pattern:";
			this.lblPattern.Visible = false;
			// 
			// tbPattern
			// 
			this.tbPattern.Location = new System.Drawing.Point(224, 40);
			this.tbPattern.Name = "tbPattern";
			this.tbPattern.Size = new System.Drawing.Size(176, 22);
			this.tbPattern.TabIndex = 7;
			this.tbPattern.Text = "";
			this.tbPattern.Visible = false;
			// 
			// FieldTextFrm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(464, 101);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tbPattern,
																		  this.lblPattern,
																		  this.btnCancel,
																		  this.btnOK,
																		  this.tbWidth,
																		  this.tbFieldName,
																		  this.lblWidth,
																		  this.lblFName});
			this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "FieldTextFrm";
			this.Text = "Text Field Definition";
			this.ResumeLayout(false);
		}
		#endregion
	}
}