package ibase.webitm.ejb.dis;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ProcessEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.Stateless;

import org.w3c.dom.Document;

@Stateless // added for ejb3 


public class CustSeriesPrcEjb extends ProcessEJB  implements CustSeriesPrcEjbLocal ,CustSeriesPrcEjbRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();


	public String process(String xmlString, String xmlString2, String windowName, String xtraParams) throws RemoteException,ITMException
	{
		String retStr = "";
		Document detailDom = null;
		Document headerDom = null;

		GenericUtility genericUtility = GenericUtility.getInstance();

		try
		{	
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				headerDom = genericUtility.parseString(xmlString); 
				System.out.println("headerDom" + headerDom);
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				detailDom = genericUtility.parseString(xmlString2); 
				System.out.println("detailDom" + detailDom);
			}

			retStr = process(headerDom, detailDom, windowName, xtraParams);

		}
		catch (Exception e)
		{
			System.out.println("Exception :ConsolidationPrcEJB :process(String xmlString, String xmlString2, String windowName, String xtraParams):" + e.getMessage() + ":");
			e.printStackTrace();
			System.out.println("exception :"+e.getMessage());
			retStr = e.getMessage();
		}
		return retStr;

	}
	//END

	public String process(Document headerDom, Document detailDom, String windowName, String xtraParams) throws RemoteException,ITMException
	{

		//variable declaration 
		String sql = ""; 		
		String errString = "";
		String custCode = "";
		String crTerm = "";
		double crLimit = 0.0;
		String nwProdCrTerm = "";
		double businessLimit = 0.0;
		double discPerc = 0.0;
		double commPerc = 0.0;
		double commPerc1 = 0.0;
		double commPerc2 = 0.0;
		int ignoreDays = 0;
		double ignoreCredit = 0.0;
		//end of varible declaration
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null;

		int count = 0;
		boolean isError = false;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();

		try
		{
			System.out.println("*********inside try block*******");
			ConnDriver connDriver = new ConnDriver();
			//get connection
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			//get data from header DOM

			custCode = genericUtility.getColumnValue("cust_code", headerDom)==null?"null":genericUtility.getColumnValue("cust_code", headerDom);
			crTerm = genericUtility.getColumnValue("cr_term", headerDom)==null?"null":genericUtility.getColumnValue("cr_term", headerDom);
			crLimit = Double.valueOf(genericUtility.getColumnValue("credit_lmt", headerDom)==null?"0.0":genericUtility.getColumnValue("credit_lmt", headerDom)) ;
			nwProdCrTerm = genericUtility.getColumnValue("cr_term__np", headerDom)==null?"null":genericUtility.getColumnValue("cr_term__np", headerDom) ;
			businessLimit = Double.valueOf(genericUtility.getColumnValue("busi_limit", headerDom)==null?"0.0":genericUtility.getColumnValue("busi_limit", headerDom)) ;
			discPerc = Double.valueOf(genericUtility.getColumnValue("disc_perc", headerDom)==null?"0.0":genericUtility.getColumnValue("disc_perc", headerDom)) ;
			commPerc = Double.valueOf(genericUtility.getColumnValue("comm_perc", headerDom)==null?"0.0":genericUtility.getColumnValue("comm_perc", headerDom)) ;
			commPerc1 = Double.valueOf(genericUtility.getColumnValue("comm_perc__1", headerDom)==null?"0.0":genericUtility.getColumnValue("comm_perc__1", headerDom));
			commPerc2 = Double.valueOf(genericUtility.getColumnValue("comm_perc__2", headerDom)==null?"0.0":genericUtility.getColumnValue("comm_perc__2", headerDom)) ;//
			ignoreDays = Integer.valueOf(genericUtility.getColumnValue("ignore_days", headerDom) ==null?"0":genericUtility.getColumnValue("ignore_days", headerDom));
			ignoreCredit = Double.valueOf(genericUtility.getColumnValue("ignore_credit", headerDom)==null?"0.0":genericUtility.getColumnValue("ignore_credit", headerDom)) ;
			//IF CUSTOMER CODE IS NOT NULL THEN IT SHOULD BE EXIST IN CUSTOMER TABLE  
			if(custCode!=null)
			{
				sql = "SELECT COUNT(*) AS COUNT FROM CUSTOMER WHERE CUST_CODE = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, custCode );
				rs = pstmt1.executeQuery();
				if( rs.next() )
				{
					count = rs.getInt("COUNT");
				}
				if(count == 0)
				{
					System.out.println("customer code in master ");
					errString = itmDBAccessEJB.getErrorString("","VMCUST1","","",conn);
					return errString;
				}

				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
			}
			//IF CREDIT TERM IS NOT NULL IT SHOULD BE EXIST IN CRTERM TABLE  
			if(crTerm!=null)
			{
				sql = "SELECT COUNT(*) AS COUNT FROM CRTERM WHERE CR_TERM = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, crTerm );
				rs = pstmt1.executeQuery();
				if( rs.next() )
				{
					count = rs.getInt("COUNT");
				}
				if(count == 0)
				{
					System.out.println("CREDIT TERM IN MASTER ");
					errString = itmDBAccessEJB.getErrorString("","VMCRTER1","","",conn);
					return errString;
				}

				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
			}
			//IF CR_TERM__NP IS NOT NULL THEN IT SHOULD BE EXIST IN CR TERM TABLE 
			if(crTerm!=null)
			{
				sql = "SELECT COUNT(*) AS COUNT FROM CRTERM WHERE CR_TERM = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, nwProdCrTerm );
				rs = pstmt1.executeQuery();
				if( rs.next() )
				{
					count = rs.getInt("COUNT");
				}
				if(count == 0)
				{
					System.out.println("CREDIT TERM IN MASTER ");
					errString = itmDBAccessEJB.getErrorString("","VMCRTER1","","",conn);
					return errString;
				}

				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
			}
			//update customer _series table against customer code (cust_code)
			sql="update customer_series set cr_term =  ( case when '"+crTerm+"' = 'null' then cr_term else '"+crTerm+"' end)" +
			", credit_lMT = ( case when "+crLimit+" = 0.0 then credit_lmt else "+crLimit+" end)" +
			",cr_term__np = ( case when '"+nwProdCrTerm+"' = 'null' then cr_term__np else '"+nwProdCrTerm+"' end)"+
			",busi_limit = ( case when "+businessLimit+" = 0.0 then busi_limit else "+businessLimit+" end)"+
			",disc_perc = ( case when "+discPerc+" = 0.0 then disc_perc else "+discPerc+" end) "+
			",comm_perc = ( case when "+commPerc+" = 0.0 then comm_perc else "+commPerc+" end) "+
			",comm_perc__1 = ( case when "+commPerc1+" = 0.0 then comm_perc__1 else "+commPerc1+" end) "+
			",comm_perc__2 = ( case when "+commPerc2+" = 0.0 then comm_perc__2 else "+commPerc2+" end) "+
			",ignore_days = ( case when "+ignoreDays+" = 0 then ignore_days else "+ignoreDays+" end) "+
			",ignore_credit = ( case when "+ignoreCredit+" = 0.0 then ignore_credit else "+ignoreCredit+" end)"+
			" where cust_code =?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, custCode);
			count = pstmt.executeUpdate();
			if(count > 0)
			{
				System.out.println("update row "+count);
				errString = itmDBAccessEJB.getErrorString("","VTPRCOMPLT","","",conn);
				return errString;

			}	
			pstmt.close();

		}		
		catch(Exception se)
		{
			isError = true;
			System.out.println("SQLException :" + se);
			se.printStackTrace();
			System.out.println("error ****"+se.getMessage());
			errString = se.getMessage();
			try
			{
				conn.rollback();
			}
			catch(SQLException e1)
			{
				se = e1;
			}
			return errString;
		}

		finally
		{
			try
			{
				if( !isError  )
				{
					conn.commit(); 
				}

				if(conn != null)
				{	
					if(rs != null)
					{
						rs.close();
						rs = null;
					}
					if(pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				errString = e.getMessage();
				e.printStackTrace();
				System.out.println("error =>"+e.getMessage());
				return errString ;
			}
		}
		return errString;
	}
	@Override
	public void remove() {
		// TODO Auto-generated method stub

	}
}

