//Changed by Pawan on 7/2/2011 [SY01SUN042].
//package ibase.webitm.bean.sys;
package ibase.webitm.ejb.sys;

import ibase.webitm.ejb.sys.ColumnProperty;
//Changed by Pawan on 7/2/2011 [SY01SUN042] because it is not being used.start
//import ibase.webitm.ejb.sys.ColumnPropertyHome;
import ibase.webitm.ejb.sys.ColumnPropertyRemote;
//Changed by Pawan on 7/2/2011 [SY01SUN042] because it is not being used.end

import ibase.utility.CommonConstants;
import ibase.webitm.utility.ITMException;
import ibase.system.config.AppConnectParm;
import ibase.webitm.utility.GenericUtility;

import java.util.ArrayList;
import java.util.TreeMap;

import java.io.File;
import java.io.FileOutputStream;

import java.sql.Timestamp;
import java.sql.SQLException;


import javax.naming.InitialContext;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMException;

public class ColumnPropertyBean
{
	String xmlString = "";
	String objName = "";
	int childNodeListLength = 0;

	TreeMap tMap = null;

	GenericUtility genericUtility = null;

	public void setObjName(String objName)
	{
		this.objName = objName;
	}
	public String getObjName()
	{
		return this.objName;
	}
	
	private InitialContext getInitialContext()throws ITMException
	{
		InitialContext ctx = null;
		try
		{
			AppConnectParm appConnect = new AppConnectParm();
			ctx = new InitialContext(appConnect.getProperty());			
		}
		catch(ITMException itme)
		{
			throw itme;
		}
		catch(Exception e)
		{
			System.out.println("Exception :ColumnPropertyBean :getInitialContext :");
			throw new ITMException(e);
		}
		return ctx;
	}
	
	private ColumnPropertyRemote getRemoteObject() throws ITMException
	{
		ColumnPropertyRemote columnPropertyRemote = null;
		try
		{
			//Changed by Pawan 04/02/2011 [SY01SUN042] to look up ColumnProperty.
			//ColumnPropertyHome columnPropertyHome = (ColumnPropertyHome)getInitialContext().lookup("ColumnProperty");
			//columnProperty = columnPropertyHome.create();
			columnPropertyRemote = (ColumnPropertyRemote)getInitialContext().lookup("ibase/ColumnProperty/remote");	
			System.out.println("Lookup ColumnProperty Successfully");			
		}
		catch (Exception e)
		{
			System.out.println("Exception :ColumnPropertyBean :getXmlString() :==>\n"+e.getMessage());
			throw new ITMException(e);
		}		
		//Changed by Pawan 04/02/2011 [SY01SUN042] to migrate from ejb2 to ejb3 Syntax.end
		//return columnProperty;
		return columnPropertyRemote;
	}
	public void setXmlString( String objectName, String tomcatPath ) throws ITMException
	{
		try
		{
			System.out.println("objectName -> "+objectName);
			this.xmlString = getRemoteObject().getColumnProperty( objectName, tomcatPath );
		}
		catch (Exception e)
		{
			System.out.println("Exception :ColumnPropertyBean :getXmlString() :==>\n"+e.getMessage());
			throw new ITMException(e);
		}
	}
	public String getXmlString()
	{
		return this.xmlString;
	}
	public int getNoOfForms() throws ITMException
	{
		String retString = null;
		NodeList nodeList = null;
		NodeList childNodeList = null;
		try
		{
			genericUtility = new ibase.webitm.utility.GenericUtility();
			Document dom = genericUtility.parseString( xmlString );
			nodeList = dom.getElementsByTagName("Details");
			childNodeList = nodeList.item(0).getChildNodes();
			this.childNodeListLength = childNodeList.getLength();
		}
		catch (Exception e)
		{
			System.out.println("Exception :ColumnPropertyBean :getNoOfForms() :==>\n"+e.getMessage());
			throw new ITMException(e);
		}
		System.out.println("this.childNodeListLength :: "+ this.childNodeListLength);
		return this.childNodeListLength;
	}
	public TreeMap getDetail(int i) throws ITMException
	{
		String retString = null;
		String dispName = "";
		String internalName = "";
		String tabSeq = "";
		String limit = "";
		String update = "";
		String dbName = "";
		String type = "";
		String dbSize = "";
		//Changed by Pawan on 7/2/2011 [SY01SUN042] to get the value of columnDescr.
		String columnDescr = "";
		String childNodeName = null;

		NodeList detailNodeList = null;
		NodeList detailChildNodeList = null;
		NodeList columnNodeList = null;
		NodeList columnChildNodeList = null;
		Node columnChildNode = null;

		ArrayList arrList = null;
		tMap = new TreeMap();
		
		try
		{
			genericUtility = new ibase.webitm.utility.GenericUtility();
			Document dom = genericUtility.parseString( xmlString );
			detailNodeList = dom.getElementsByTagName("Detail"+i);
			for (int d = 0; d < detailNodeList.getLength(); d++)
			{
				detailChildNodeList = detailNodeList.item(d).getChildNodes();
				for (int c = 0; c < detailChildNodeList.getLength(); c++)
				{
					dispName = "";
					internalName = "";
					tabSeq = "";
					limit = "";
					update = "";
					dbName = "";
					type = "";
					dbSize = "";
					arrList = new ArrayList();
					columnChildNodeList = detailChildNodeList.item(c).getChildNodes();
					for (int cp = 0; cp < columnChildNodeList.getLength(); cp++ )
					{
						columnChildNode = columnChildNodeList.item(cp);
						childNodeName = columnChildNode.getNodeName();
						if (columnChildNode.getFirstChild()!= null && columnChildNode.getFirstChild().getNodeValue() != null)
						{
							if(childNodeName.equals("display_name"))
							{
								dispName = columnChildNode.getFirstChild().getNodeValue();
							}
							else if (childNodeName.equals("internal_name"))
							{
								internalName = columnChildNode.getFirstChild().getNodeValue();
							}
							//Changed by Pawan on 7/2/2011 [SY01SUN042] to retrieve the value of columnDescr.start
							else if (childNodeName.equals("column_descr"))
							{
								columnDescr = columnChildNode.getFirstChild().getNodeValue();
							}
							//Changed by Pawan on 7/2/2011 [SY01SUN042] to retrieve the value of columnDescr.end
							else if (childNodeName.equals("tabsequence"))
							{
								tabSeq = columnChildNode.getFirstChild().getNodeValue();
							}
							else if (childNodeName.equals("limit"))
							{
								limit = columnChildNode.getFirstChild().getNodeValue();
							}
							else if (childNodeName.equals("update"))
							{
								update = columnChildNode.getFirstChild().getNodeValue();
							}
							else if (childNodeName.equals("db_name"))
							{
								dbName = columnChildNode.getFirstChild().getNodeValue();
							}
							else if (childNodeName.equals("type"))
							{
								type = columnChildNode.getFirstChild().getNodeValue();
							}
							else if (childNodeName.equals("db_size"))
							{
								dbSize = columnChildNode.getFirstChild().getNodeValue();
							}
						}//End Of if Block
					}//End OF Inner for Loop (cp)
					arrList.add(dispName);
					arrList.add(internalName);
					//Changed by Pawan on 7/2/2011 [SY01SUN042] to add into arrayList.
					arrList.add(columnDescr);
					arrList.add(tabSeq);
					arrList.add(limit);
					arrList.add(dbSize);
					arrList.add(update);
					arrList.add(dbName);
					arrList.add(type);
					tMap.put(new Integer(c), arrList);
				}//End Of For Middle For Loop (c)
			}//End OF Outer For Loop (d)
		}
		catch (Exception e)
		{
			System.out.println("Exception :ColumnPropertyBean :getNoOfForms() :==>\n"+e.getMessage());
			throw new ITMException(e);
		}
		return this.tMap;
	}
}