package ibase.webitm.ejb.fin;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import org.w3c.dom.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import ibase.system.config.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import javax.ejb.Stateless;
@Stateless // added for ejb3
public class DefaultAssetRegisterIC extends ActionHandlerEJB implements DefaultAssetRegisterICRemote,DefaultAssetRegisterICLocal
{	
	
	/* public void ejbCreate() throws RemoteException, CreateException {}
	public void ejbRemove(){}
	public void ejbActivate(){}
	public void ejbPassivate(){} */
    public String actionHandler() throws RemoteException,ITMException{return "";}
	public String actionHandler(String actionType, String xmlString, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		String retString = "";
		Document dom = null;
		try
		{
			GenericUtility genericUtility = GenericUtility.getInstance();
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String :"+xmlString);
				dom = genericUtility.parseString(xmlString);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :DefaultAssetRegisterICEJB :actionHandler(String xmlString):" + e.getMessage() + ":");
			throw new ITMException(e);
		}
		return retString;
	}

	public String actionHandler(String actionType, String xmlString, String xmlString1, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn=null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		Document dom = null;
		Document dom1 = null;
		String  retString = null;
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String:::"+xmlString);
				dom = GenericUtility.getInstance().parseString(xmlString); 
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				System.out.println("XML String1:::"+xmlString1);
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
			}
			
			if (actionType.equalsIgnoreCase("Default"))
			{
					/*try
					{
						if(conn==null)
						{
							ConnDriver connDriver = new ConnDriver();
							conn = connDriver.getConnectDB("DriverITM");
							conn.setAutoCommit(false);
							DatabaseMetaData dbmd = conn.getMetaData();
							System.out.println("DriverName["+dbmd.getDriverName() );
							System.out.println("DriverURI["+dbmd.getURL()  );
							System.out.println("DriverUSER["+dbmd.getUserName());
							//System.out.println("ApplDateFormat["+genericUtility. ());
							System.out.println("DBDateFormat["+genericUtility.getDBDateFormat() );
							connDriver = null;
						}
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}*/
					retString = actionDefault(dom, dom1, objContext, xtraParams);
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :Dispatch :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from DefaultAssetRegisterICEJB : actionHandler"+retString);
	    return retString;
	}
	private String actionDefault(Document dom, Document dom1, String objContext, String xtraParams) throws RemoteException, ITMException
	{
		GenericUtility genericUtility = GenericUtility.getInstance();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		StringBuffer valueXmlString = null;
		valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");	
		int currentFormNo = 0;
		String sql1 = "";
		String itemserhdr = null;
		String sitecodehdr = null;
		String assetcode = null;
		String assetdescr = null;
		String itemcode = null;
		String itemdescr = null;
		
		try
		{	
			//if(conn==null)
			//{
				ConnDriver connDriver = new ConnDriver();
				conn = connDriver.getConnectDB("DriverITM");
				connDriver=null;
			//}
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			itemserhdr = genericUtility.getColumnValue( "item_grp", dom1 );
			sitecodehdr = genericUtility.getColumnValue( "site_code", dom1 );
			sql1 =  "select ar.asset_code, ar.descr , ar.item_code, m.descr "
					+" from asset_register ar, item m"
					+" where ar.item_code = m.item_code"
					+" and ar.grp_code = '"+itemserhdr+"'"
					+" and ar.site_code = '"+sitecodehdr+"'"; 
			System.out.println("sql1 is :"+sql1);
		    pstmt = conn.prepareStatement(sql1);	
		    rs = pstmt.executeQuery();
			while (rs.next())
			{
				assetcode	=	rs.getString(1) == null ? "" : rs.getString(1);				
				assetdescr	=	rs.getString(2) == null ? "" : rs.getString(2);
				itemcode	=	rs.getString(3) == null ? "" : rs.getString(3);
				itemdescr	=	rs.getString(4) == null ? "" : rs.getString(4);					
				
				valueXmlString.append("<Detail>\r\n");
				valueXmlString.append("<asset_code>").append("<![CDATA[").append(assetcode).append("]]>").append("</asset_code>\r\n");
				valueXmlString.append("<descr>").append("<![CDATA[").append(assetdescr).append("]]>").append("</descr>\r\n");
				valueXmlString.append("<item_code>").append("<![CDATA[").append(itemcode).append("]]>").append("</item_code>\r\n");
				valueXmlString.append("<descr_item>").append("<![CDATA[").append(itemdescr).append("]]>").append("</descr_item>\r\n");
				valueXmlString.append("</Detail>\r\n");	
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;		
			valueXmlString.append("</Root>\r\n");					
		}
		catch(SQLException sqx)
		{
			System.out.println("The Exception caught from DefaultAssetRegisterICEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch(Exception e)
		{
			System.out.println("The Exception caught from DefaultAssetRegisterICEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				conn.close();
				conn = null;
				
			}catch(Exception e){}
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}
	public String actionHandlerTransform(String actionType, String xmlString,
			String xmlString1, String objContext, String xtraParams,
			String selDataStr) throws RemoteException, ITMException 
	{
		Document dom = null;
		Document dom1 = null;
		Document selDataDom = null;
		String retString = null;
		try 
		{
			if (xmlString != null && xmlString.trim().length() != 0) {
				dom = GenericUtility.getInstance().parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length() != 0) 
			{
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
			}
			if(selDataStr != null && selDataStr.length() > 0)
			{
				selDataDom = GenericUtility.getInstance().parseString(selDataStr);
			}
			
			if (actionType.equalsIgnoreCase("Default"))
			{
				retString = makeTransform(dom, dom1, objContext, xtraParams,selDataDom);
			}
		} 
		catch (Exception e)
		{
			System.out.println("Exception :DefaultAssetRegisterICEJB :actionHandlerTransform(String xmlString):"
							+ e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from DefaultAssetRegisterICEJB : actionHandlerTransform"
						+ retString);
		return retString;
	}
	private String makeTransform(Document dom, Document dom1, String objContext, String xtraParams, Document selDataDom)throws ITMException 
	{
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		
		//Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Node currDetail = null, currDetail1 = null;
		//ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		
		String astcode_trans = null;
		String astdescr_trans = null;
		String itmcode_trans = null;
		String itmdescr_trans = null;
		try 
		{
			//conn = connDriver.getConnectDB("DriverITM");
			//connDriver = null;
			
			NodeList detailList = selDataDom.getElementsByTagName("Detail");
			int noOfDetails = detailList.getLength();
			for (int ctr = 0; ctr < noOfDetails; ctr++) 
			{
				valueXmlString.append("<Detail>\r\n");
				currDetail1 = detailList.item(ctr);
				astcode_trans = genericUtility.getColumnValueFromNode("asset_code", currDetail1);
				astdescr_trans = genericUtility.getColumnValueFromNode("descr", currDetail1);
				itmcode_trans = genericUtility.getColumnValueFromNode("item_code", currDetail1);
				itmdescr_trans = genericUtility.getColumnValueFromNode("descr_item", currDetail1);
				if (astcode_trans != null && astcode_trans.trim().length() > 0)
				{
					valueXmlString.append("<asset_code>").append("<![CDATA[").append(astcode_trans).append("]]>").append("</asset_code>\r\n");
				}
				else
				{
					valueXmlString.append("<asset_code>").append("<![CDATA[]]>").append("</asset_code>\r\n");
				}
				if (astdescr_trans != null && astdescr_trans.trim().length() > 0)
				{
					valueXmlString.append("<descr>").append("<![CDATA[").append(astdescr_trans).append("]]>").append("</descr>\r\n");
				}
				else
				{
					valueXmlString.append("<descr>").append("<![CDATA[]]>").append("</descr>\r\n");
				}
				
				valueXmlString.append("</Detail>");
				//valueXmlString.append("<item_code>").append("<![CDATA[").append(itmcode_trans).append("]]>").append("</item_code>\r\n");
				//valueXmlString.append("<descr>").append("<![CDATA[").append(itmdescr_trans).append("]]>").append("</descr>\r\n");
			}// for end
			valueXmlString.append("</Root>");
		}// end try
		catch (ITMException itme) {
			itme.printStackTrace();
			throw itme;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ITMException(e);
		} /*finally {
			try {
				conn.close();
				conn = null;
			} catch (Exception e) {
			}
		}*/
		System.out.println("valueXmlString from :" + valueXmlString.toString());
		return valueXmlString.toString();
	}	
}//end of Ejb