/*********************************************************************************************
 * Copyright 2002 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: ROField.cs $     $Revision: 11 $
 * $Author: Kathy $   $Date: 4/04/03 9:41a $
 *
 * $History: ROField.cs $
 * 
 * *****************  Version 11  *****************
 * User: Kathy        Date: 4/04/03    Time: 9:41a
 * Updated in $/LibSource/ROFields
 * B2003-030: get correct field type for new image
 * 
 * *****************  Version 10  *****************
 * User: Kathy        Date: 12/10/02   Time: 2:27p
 * Updated in $/LibSource/ROFields
 * fieldname special chars 
 * 
 * *****************  Version 9  *****************
 * User: Kathy        Date: 12/02/02   Time: 8:30a
 * Updated in $/LibSource/ROFields
 * fieldname replace chars
 * 
 * *****************  Version 8  *****************
 * User: Kathy        Date: 11/11/02   Time: 7:14a
 * Updated in $/LibSource/ROFields
 * image field type should be 32 not 20 
 * 
 * *****************  Version 7  *****************
 * User: Kathy        Date: 10/15/02   Time: 2:17p
 * Updated in $/LibSource/ROFields
 * combo field number
 * 
 * *****************  Version 6  *****************
 * User: Kathy        Date: 9/27/02    Time: 1:11p
 * Updated in $/LibSource/ROFields
 * fix digit as first char in fieldname
 * 
 * *****************  Version 5  *****************
 * User: Kathy        Date: 9/25/02    Time: 2:31p
 * Updated in $/LibSource/ROFields
 * multitxt->variable
 * 
 * *****************  Version 4  *****************
 * User: Kathy        Date: 9/03/02    Time: 2:53p
 * Updated in $/LibSource/ROFields
 * missing " on image field def.
 * 
 * *****************  Version 3  *****************
 * User: Kathy        Date: 8/30/02    Time: 11:56a
 * Updated in $/LibSource/ROFields
 * image
 * 
 * *****************  Version 2  *****************
 * User: Kathy        Date: 8/28/02    Time: 10:59a
 * Updated in $/LibSource/ROFields
 * development
 *********************************************************************************************/

using System;
using System.Text;

namespace ROFields
{
	public enum FieldTypes: uint
	{
		Nil=0, SingleTxt=1, VariableTxt=2, FrmtSingleTxt=4, XYPlot=8, Table=10, Image=32, 
		MultiTxt=40, Combination=128, MultiFld=100
	}

	/// <summary>
	/// Summary description for ROField: This class contains RO Field information. Each ROField
	/// contains:
	///   string fieldname - the name of the field
	///   string recid -  its database record id (used to interface to the database - null if
	///      not created yet.
	///   string masterrecID - its record id in the master if appropriate.
	///   uint fieldtype - the field type as defined in the enum FieldTypes (listed above)
	/// </summary>
	public class ROField
	{
		string fieldname;
		string recID;
		string masterrecID;
		uint fldtype;

		// constuctor
		public ROField(string fname, string rID, string mrID, uint fieldtype)
		{
			fieldname = fname;
			recID = rID;
			masterrecID = mrID;
			fldtype = fieldtype;
		}
		public string GetFieldname { get {return fieldname;}}
		public string GetRecID { get {return recID;}}
		public string GetMasterRecID { get {return masterrecID;}}
		public uint GetFieldType{ get {return fldtype;}}
		public void SetFieldname(string name) {fieldname = name;}
		public void SetRecID(string recid) {recID = recid;}
		public void SetMasterRecID(string mrecid) {masterrecID = mrecid;}
		public void SetFieldType(uint ftype) {fldtype = ftype;}
		
		// Given a schema string, determine which RO Field type it is.
		public uint ParseFieldType(string info)
		{
			int indx;
			// formatted single line text has 'pattern' in schema definition)
			indx = info.IndexOf("xsd:pattern");
			if (indx >= 0) return (uint) FieldTypes.FrmtSingleTxt;

			// Combination field has 'choice' in schema definition
			indx = info.IndexOf("xsd:choice");
			if (indx >= 0) return (uint) FieldTypes.Combination;
		
			// image  - search for 'Image_Height', in case Image is in the string from some other mechanism
			indx = info.IndexOf("Image_Height");    
			if (indx >=0) return (uint) FieldTypes.Image;

			// single line text has 'normalizedString' (pattern does too, but
			// it would be eliminated above.
			indx = info.IndexOf("xsd:normalizedString");
			if (indx >=0) return (uint) FieldTypes.SingleTxt;

			// now look for all of the multi-line fields: multi text, table, & XY Plot
			indx = info.IndexOf("xsd:string");
			if (indx <0) indx = info.IndexOf("xsd:String");
			if (indx >=0) 
			{
				int indxspecific;
				indxspecific = info.IndexOf("Variable");
				if (indxspecific >= 0) return (uint) FieldTypes.VariableTxt;
				indxspecific = info.IndexOf("Table");
				if (indxspecific >= 0) return (uint) FieldTypes.Table;
				indxspecific = info.IndexOf("XYPlot");
				if (indxspecific >= 0) return (uint) FieldTypes.XYPlot;
			}

		//	indx = info.IndexOf("Image");  
		//	if (indx >=0) return (uint) FieldTypes.Image;

			return 1000;
		}

		// Creates a string representing the schema for this field. note that the input
		// fieldname will override that which is stored for this ROField (for editting
		// purposes).
		public string MakeFieldName(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;
		}

		public string MakeSchemaString(string fieldname, string width, string pattern)
		{
			uint ftype = this.GetFieldType;
			StringBuilder strbld = new StringBuilder();
			strbld.Append("<xsd:element name=\"");
			strbld.Append(MakeFieldName(fieldname));
			strbld.Append("\" minOccurs=\"0\">\n");
			if (ftype == (uint)FieldTypes.VariableTxt || ftype == (uint)FieldTypes.Table || ftype == (uint)FieldTypes.XYPlot)
			{
				strbld.Append("<xsd:annotation>\n<xsd:documentation xml:lang=\"en\">");
				if (ftype == (uint)FieldTypes.VariableTxt)
					strbld.Append("Variable");
				else if (ftype == (uint)FieldTypes.Table)
					strbld.Append("Table");
				else
					strbld.Append("XYPlot");
				strbld.Append("</xsd:documentation>\n</xsd:annotation>");
			}	
			strbld.Append("<xsd:simpleType>\n<xsd:restriction base=\"xsd:");
			if (ftype == (uint)FieldTypes.FrmtSingleTxt || ftype == (uint)FieldTypes.SingleTxt)
				strbld.Append("normalizedString\">\n<xsd:maxLength value=\"");
			else
				strbld.Append("String\">\n<xsd:maxLength value=\"");
			strbld.Append(width);	
			strbld.Append("\" />\n");
			if (ftype == (uint)FieldTypes.FrmtSingleTxt)
			{
				strbld.Append("<xsd:pattern value=\"");
				strbld.Append(pattern);
				strbld.Append("\" />\n");
			}
			strbld.Append("</xsd:restriction>\n</xsd:simpleType>\n</xsd:element>");
			return (strbld.ToString());
		}

		public string MakeComboSchemaString(string fieldname, bool fixd, string fixdwid, bool multi, string multiwid, 
			bool table, string tablewid, bool xyplot, string xyplotwid)
		{
			StringBuilder strbld = new StringBuilder();
			strbld.Append("<xsd:choice>\n");
			if (fixd == true)
			{
				strbld.Append("<xsd:element name=\"");
				strbld.Append(MakeFieldName(fieldname)+"a");
				strbld.Append("\" type=\"Fixed\" minOccurs=\"0\">\n");
				strbld.Append("<xsd:simpleType>\n<xsd:restriction base=\"xsd:normalizedString\">\n<xsd:maxLength value=\"");
				strbld.Append(fixdwid);
				strbld.Append("\" />\n</xsd:restriction>\n</xsd:simpleType>\n</xsd:element>\n");
			}
			if (multi == true)
			{
				strbld.Append("<xsd:element name=\"");
				strbld.Append(MakeFieldName(fieldname)+"b");
				strbld.Append("\" type=\"Variable\" minOccurs=\"0\">\n");
				strbld.Append("<xsd:annotation>\n<xsd:documentation xml:lang=\"en\">\nVariable\n");
				strbld.Append("</xsd:documentation>\n</xsd:annotation>\n<xsd:simpleType name=\"Variable\">\n");
				strbld.Append("<xsd:restriction base=\"xsd:string\">\n<xsd:maxLength value=\"");
				strbld.Append(multiwid);
				strbld.Append("\" />\n</xsd:restriction>\n</xsd:simpleType>\n</xsd:element>\n");
			}
			if (table == true)
			{
				strbld.Append("<xsd:element name=\"");
				strbld.Append(MakeFieldName(fieldname)+"c");
				strbld.Append("\" type=\"Table\" minOccurs=\"0\">\n");
				strbld.Append("<xsd:annotation>\n<xsd:documentation xml:lang=\"en\">\nTable\n");
				strbld.Append("</xsd:documentation>\n</xsd:annotation>\n<xsd:simpleType name=\"Variable\">\n");
				strbld.Append("<xsd:restriction base=\"xsd:string\">\n<xsd:maxLength value=\"");
				strbld.Append(tablewid);
				strbld.Append("\" />\n</xsd:restriction>\n</xsd:simpleType>\n</xsd:element>");
			}
			if (xyplot == true)
			{
				strbld.Append("<xsd:element name=\"");
				strbld.Append(MakeFieldName(fieldname)+"d");
				strbld.Append("\" type=\"XY_Plot\" minOccurs=\"0\">\n");
				strbld.Append("<xsd:annotation>\n<xsd:documentation xml:lang=\"en\">\nXYPlot\n");
				strbld.Append("</xsd:documentation>\n</xsd:annotation>\n<xsd:simpleType name=\"Variable\">\n");
				strbld.Append("<xsd:restriction base=\"xsd:string\">\n<xsd:maxLength value=\"");
				strbld.Append(xyplotwid);
				strbld.Append("\" />\n</xsd:restriction>\n</xsd:simpleType>\n</xsd:element>");
			}
			strbld.Append("</xsd:choice>");
			return (strbld.ToString());
		}

		public string MakeImageSchemaString(string fieldname)
		{
			StringBuilder strbld = new StringBuilder();
			strbld.Append("<xsd:element name=\"");
			strbld.Append(MakeFieldName(fieldname));
			strbld.Append("\">\n<xsd:complexType>\n<xsd:sequence>");
			strbld.Append("<xsd:element name=\"Image_Filename\" minOccurs=\"0\">\n");
			strbld.Append("<xsd:simpleType>\n<xsd:annotation>\n");
			strbld.Append("<xsd:documentation xml:lang=\"en\">VLN_FINDFILE</xsd:documentation>\n");
			strbld.Append("</xsd:annotation>\n<xsd:restriction base=\"xsd:normalizedString\">\n");
			strbld.Append("<xsd:maxLength value=\"70\" />\n</xsd:restriction>\n</xsd:simpleType>");
			strbld.Append("</xsd:element>\n<xsd:element name=\"Image_Height\" minOccurs=\"0\">\n");
			strbld.Append("<xsd:simpleType>\n<xsd:annotation>\n");
			strbld.Append("<xsd:documentation xml:lang=\"en\">Lines (6 lines per inch)</xsd:documentation>\n");
			strbld.Append("</xsd:annotation>\n<xsd:restriction base=\"xsd:normalizedString\">\n");
			strbld.Append("<xsd:maxLength value=\"10\" />\n</xsd:restriction>\n</xsd:simpleType>\n");
			strbld.Append("</xsd:element>\n<xsd:element name=\"Image_Width\" minOccurs=\"0\">\n");
			strbld.Append("<xsd:simpleType>\n<xsd:annotation>\n");
			strbld.Append("<xsd:documentation xml:lang=\"en\">Characters (12 chars. Per inch)</xsd:documentation>\n");
			strbld.Append("</xsd:annotation>\n<xsd:restriction base=\"xsd:normalizedString\">\n");
			strbld.Append("<xsd:maxLength value=\"10\" />\n</xsd:restriction>\n</xsd:simpleType>\n");
			strbld.Append("</xsd:element>\n</xsd:sequence>\n</xsd:complexType>\n</xsd:element>");
			return(strbld.ToString());
		}

	}

}
