package ibase.webitm.ejb.adm.adv;

import java.rmi.RemoteException;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class AppraisalAct extends ActionHandlerEJB implements AppraisalActLocal,AppraisalActRemote //SessionBean
{
	/*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
	{
		System.out.println("AppraisalActEJB calling.........");
		Document dom = null;
		Document dom1 = null;
		String  resString = null;
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String :"+xmlString);
				dom = GenericUtility.getInstance().parseString(xmlString); 
			}
			System.out.println("objContext :"+objContext+"\n ActionType:"+actionType);
			if (objContext.equals("2"))
			{
				if (actionType.equalsIgnoreCase("Default"))
				{
					resString = actionDefaultDet2(dom,objContext,xtraParams);
				}
			}
			if (objContext.equals("3"))
			{
				if (actionType.equalsIgnoreCase("Default"))
				{
					resString = actionDefaultDet3(dom,objContext,xtraParams);
				}
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :AppraisalActEJB :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from action[Default] actionHandler"+resString);
		return resString;
	}

	private String actionDefaultDet3(Document dom, String objContext, String xtraParams) throws SQLException,Exception
	{
		Statement stmt = null;
		Connection conn = null;
		ResultSet rs = null;
		String sql = "", apprTableNo = "", apprNo = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		ConnDriver connDriver = new ConnDriver();
		
		if (dom != null)
		{
			try
			{		
				conn = connDriver.getConnectDB("DriverITM");
				stmt = conn.createStatement();
				apprTableNo = GenericUtility.getInstance().getColumnValue("appr_tblno",dom);
				apprNo = GenericUtility.getInstance().getColumnValue("appr_no",dom);
				System.out.println("apprTableNo  :"+apprTableNo+" apprNo :"+apprNo);
				sql = "SELECT "
					 +"APPRAISAL_SPEC_TABLE.APPR_SPEC, "
					 +"APPRAISAL_SPEC.SPEC_DETAILS, "
					 +"APPRAISAL_SPEC_TABLE.MIN_RATING, "
					 +"APPRAISAL_SPEC_TABLE.MAX_RATING, "
					 +"APPRAISAL_SPEC_TABLE.OVERALL_PERC "
					 +"FROM APPRAISAL_SPEC_TABLE, APPRAISAL_SPEC "
					 +"WHERE APPRAISAL_SPEC_TABLE.APPR_SPEC = APPRAISAL_SPEC.APPR_SPEC "
					 +"AND APPRAISAL_SPEC_TABLE.APPR_TBLNO = '"+apprTableNo+"'";
				System.out.println("sql :"+sql);
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<appr_no>").append("<![CDATA[").append(apprNo).append("]]>").append("</appr_no>\r\n");
					valueXmlString.append("<appr_spec>").append("<![CDATA[").append((rs.getString(1) == null) ? "":rs.getString(1)).append("]]>").append("</appr_spec>\r\n");
					valueXmlString.append("<spec_details>").append("<![CDATA[").append((rs.getString(2)== null) ? "":rs.getString(2)).append("]]>").append("</spec_details>\r\n");
					valueXmlString.append("<min_rating>").append("<![CDATA[").append((rs.getString(3) == null) ? "":rs.getString(3)).append("]]>").append("</min_rating>\r\n");
					valueXmlString.append("<max_rating>").append("<![CDATA[").append((rs.getString(4) == null) ? "":rs.getString(4)).append("]]>").append("</max_rating>\r\n");
					valueXmlString.append("<overall_perc>").append("<![CDATA[").append((rs.getString(5) == null) ? "":rs.getString(5)).append("]]>").append("</overall_perc>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}				
			}
			catch(SQLException e)
			{
				System.out.println("Exception : AppraisalActEJB : actionStock " +e.getMessage());
				throw(e);
			}
			catch(Exception e)
			{
				System.out.println("Exception : AppraisalActEJB : actionHandler :(Document dom)" +e.getMessage());
				throw(e);
			}
			finally
			{
				try
				{
					if (conn != null)
					{
						conn.close();
						conn = null;
					}
				}catch(Exception e){}
			}
		}
		valueXmlString.append("</Root>\r\n");			
		System.out.println("valueXmlString.toString() "+valueXmlString.toString());
		return valueXmlString.toString();		
	}

	private String actionDefaultDet2(Document dom, String objContext, String xtraParams) throws SQLException ,Exception
	{
		Statement stmt = null;
		Connection conn = null;
		ResultSet rs = null;
		String sql = "", empCode = "", apprNo = "", apprNo1 = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		ConnDriver connDriver = new ConnDriver();
		
		if (dom != null)
		{
			try
			{		
				conn = connDriver.getConnectDB("DriverITM");
				stmt = conn.createStatement();
				empCode = GenericUtility.getInstance().getColumnValue("emp_code",dom);
				apprNo = GenericUtility.getInstance().getColumnValue("appr_no",dom);
				System.out.println("empCode  :"+empCode+" apprNo :"+apprNo);
				
				sql = "SELECT APPR_NO FROM APPRAISAL "
					 +"WHERE EMP_CODE = '"+empCode+"'"+" ORDER BY APPR_DATE DESC";
				System.out.println("sql :"+sql);
				rs = stmt.executeQuery(sql);
				while (rs.next())
				{
					apprNo1 = rs.getString(1);
					if (apprNo.equals(apprNo1))
					{
						apprNo1 = rs.getString(1);					
					}
					else
					{
						break;
					}
				}
				System.out.println("apprNo1 :"+apprNo1);
				sql = "SELECT LINE_NO,KRA_NEXT FROM APPR_KRANEXT "
					 +"WHERE APPR_NO = '"+apprNo1+"'";
				System.out.println("sql :"+sql);
				rs = stmt.executeQuery(sql);
				while (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<appr_no>").append("<![CDATA[").append(apprNo).append("]]>").append("</appr_no>\r\n");
					valueXmlString.append("<line_no>").append("<![CDATA[").append(rs.getString(1)).append("]]>").append("</line_no>\r\n");
					valueXmlString.append("<kra_prev>").append("<![CDATA[").append(rs.getString(2)).append("]]>").append("</kra_prev>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}				
			}
			catch(SQLException e)
			{
				System.out.println("Exception : AppraisalActEJB : actionStock " +e.getMessage());
				throw(e);
			}
			catch(Exception e)
			{
				System.out.println("Exception : AppraisalActEJB : actionHandler :(Document dom)" +e.getMessage());
				throw(e);
			}
			finally
			{
				try
				{
					if (conn != null)
					{
						conn.close();
						conn = null;
					}					
				}catch(Exception e){}
			}
		}
		valueXmlString.append("</Root>\r\n");			
		System.out.println("valueXmlString.toString() "+valueXmlString.toString());
		return valueXmlString.toString();		
	}
}