
package ibase.webitm.ejb.fin;

import java.io.File;
import java.rmi.RemoteException;
import javax.naming.InitialContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import ibase.system.config.AppConnectParm;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.w3c.dom.*;

import ibase.webitm.ejb.fin.FinCommon;
import ibase.webitm.ejb.sys.UtilMethods;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import ibase.utility.CommonConstants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class MiscVouchDefaultData extends ActionHandlerEJB implements MiscVouchDefaultDataLocal, MiscVouchDefaultDataRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	UtilMethods utilMethods = UtilMethods.getInstance();
	
	public String actionHandler(String actionType, String xmlString1, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		
		Document dom1 = null;
		String  resString = null;
		try
		{
			GenericUtility genericUtility = GenericUtility.getInstance(); 
			/*if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String :"+xmlString);
				dom = genericUtility.parseString(xmlString); 
			}*/
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				System.out.println("XML String1 :"+xmlString1);
				dom1 = genericUtility.parseString(xmlString1); 
			}
			System.out.println("actionType:"+actionType);
			if (actionType.equalsIgnoreCase("DEFDATA"))
			{
				resString = importFromDatabase(dom1,objContext,xtraParams);
			}
			
			
		}
	   	catch(Exception e)
		{
			
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from action actionHandler"+resString);
	    return (resString);
	}
	
	private String importFromDatabase(Document dom1, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		String sql = "";
		String description = "";
		String sundryType = "",sundryCode = "",empCode = "";
		String cctrCodeSal="",grade="",acctCodeExp="",cctrCodeExp="";
		String billNo = "",billDate = "",acctCode="";
		String amnt ="",analCode="",descr = "",applyTax="",claimAmt="",billAmt="",udfNo1="",udfNo2="",analysisDesc="";
		NodeList detailList = null;
		int detailListLength = 0;
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
	
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			sundryType = genericUtility.getColumnValue("sundry_type",dom1);
			sundryCode = genericUtility.getColumnValue("sundry_code",dom1);
			billNo = genericUtility.getColumnValue("bill_no",dom1);
			billDate = genericUtility.getColumnValue("bill_date",dom1);
			DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        Document domxml = db.parse( CommonConstants.JBOSSHOME+File.separator+"resource"+File.separator+"misc_voucher_claim_3_DEFAULT_DATA.xml" );
			if(("E").equalsIgnoreCase(sundryType))
			{
				sql="select cctr_code__sal, grade from employee where emp_code = ?";
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1, sundryCode);
				rs=pstmt.executeQuery();
				if(rs.next())
				{
					cctrCodeSal = rs.getString("cctr_code__sal");
					grade = rs.getString("grade");
				}
				sql = "select acct_code__exp, cctr_code__exp  from grade where grade_code = ? ";
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1, grade);
				rs=pstmt.executeQuery();
				if(rs.next())
				{
					acctCodeExp = rs.getString("acct_code__exp");
					cctrCodeExp = rs.getString("cctr_code__exp");
				}
				if(cctrCodeSal==null||cctrCodeSal.trim().length()==0)
				{
					cctrCodeSal = cctrCodeExp;
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
			}
			else if(("P").equalsIgnoreCase(sundryType))
			{
				sql="select emp_code  from sales_pers where sales_pers = ?";
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1, sundryCode);
				rs=pstmt.executeQuery();
				if(rs.next())
				{
					empCode = rs.getString("emp_code");
				}
				if(empCode!=null&&empCode.trim().length()>0)
				{
					sql="select cctr_code__sal, grade into from employee where emp_code = ?";
					pstmt=conn.prepareStatement(sql);
					pstmt.setString(1, empCode);
					rs=pstmt.executeQuery();
					if(rs.next())
					{
						cctrCodeSal = rs.getString("cctr_code__sal");
						grade = rs.getString("grade");
					}
					sql = "select acct_code__exp, cctr_code__exp  from grade where grade_code = ? ";
					pstmt=conn.prepareStatement(sql);
					pstmt.setString(1, grade);
					rs=pstmt.executeQuery();
					if(rs.next())
					{
						acctCodeExp = rs.getString("acct_code__exp");
						cctrCodeExp = rs.getString("cctr_code__exp");
					}
					if(cctrCodeSal==null||cctrCodeSal.trim().length()==0)
					{
						cctrCodeSal = cctrCodeExp;
					}
					
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
			}
			detailList = domxml.getElementsByTagName("Detail");
			detailListLength = detailList.getLength();
			for (int i = 0;i < detailListLength;i++ )
			{
				acctCode = GenericUtility.getInstance().getColumnValueFromNode("acct_code",detailList.item(i));
				if (acctCode == null||acctCode.trim().length()==0)
				{
					acctCode = acctCodeExp;
				}
				valueXmlString.append("<Detail>\r\n");
				valueXmlString.append("<acct_code isSrvCallOnChg='0'>").append("<![CDATA[" +acctCode+ "]]>").append("</acct_code>");
				amnt = GenericUtility.getInstance().getColumnValueFromNode("amount",detailList.item(i));
				valueXmlString.append("<amount isSrvCallOnChg='0'>").append("<![CDATA[" +amnt+ "]]>").append("</amount>");
				analCode = GenericUtility.getInstance().getColumnValueFromNode("anal_code",detailList.item(i));
				valueXmlString.append("<anal_code isSrvCallOnChg='0'>").append("<![CDATA[" +analCode+ "]]>").append("</anal_code>");
				descr = GenericUtility.getInstance().getColumnValueFromNode("descr",detailList.item(i));
				valueXmlString.append("<descr isSrvCallOnChg='0'>").append("<![CDATA[" +descr+ "]]>").append("</descr>");
				applyTax = GenericUtility.getInstance().getColumnValueFromNode("apply_tax",detailList.item(i));
				valueXmlString.append("<apply_tax isSrvCallOnChg='0'>").append("<![CDATA[" +applyTax+ "]]>").append("</apply_tax>");
				claimAmt = GenericUtility.getInstance().getColumnValueFromNode("claim_amt",detailList.item(i));
				valueXmlString.append("<claim_amt isSrvCallOnChg='0'>").append("<![CDATA[" +claimAmt+ "]]>").append("</claim_amt>");
				billAmt = GenericUtility.getInstance().getColumnValueFromNode("bill_amt",detailList.item(i));
				valueXmlString.append("<bill_amt isSrvCallOnChg='0'>").append("<![CDATA[" +billAmt+ "]]>").append("</bill_amt>");
				udfNo1 = GenericUtility.getInstance().getColumnValueFromNode("udf__num1",detailList.item(i));
				valueXmlString.append("<udf__num1 isSrvCallOnChg='0'>").append("<![CDATA[" +udfNo1+ "]]>").append("</udf__num1>");
				udfNo2 = GenericUtility.getInstance().getColumnValueFromNode("udf__num2",detailList.item(i));
				valueXmlString.append("<udf__num2 isSrvCallOnChg='0'>").append("<![CDATA[" +udfNo2+ "]]>").append("</udf__num2>");
				analysisDesc = GenericUtility.getInstance().getColumnValueFromNode("analysis_descr",detailList.item(i));
				valueXmlString.append("<analysis_descr isSrvCallOnChg='0'>").append("<![CDATA[" +analysisDesc+ "]]>").append("</analysis_descr>");
				valueXmlString.append("<cctr_code isSrvCallOnChg='0'>").append("<![CDATA[" +cctrCodeSal+ "]]>").append("</cctr_code>");
				valueXmlString.append("<bill_no isSrvCallOnChg='0'>").append("<![CDATA[" +billNo+ "]]>").append("</bill_no>");
				valueXmlString.append("<bill_date isSrvCallOnChg='0'>").append("<![CDATA[" +billDate+ "]]>").append("</bill_date>");
				valueXmlString.append("</Detail>\r\n");
			}
			
			valueXmlString.append("</Root>\r\n");			
			
		}
		catch(SQLException e)
		{
			System.out.println("Exception :SQLException in default :getDATA " +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception : in default : getDATA" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(conn!=null)
				{
					System.out.println("Closing Connection........");
					conn.close();
					conn = null;
				}
			}
			catch(SQLException se)
			{
				throw new ITMException(se);
			}
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}
	
}