package ibase.webitm.ejb.mfg;
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; // added for ejb3

@Stateless // added for ejb3

public class DefaultLotIssued extends ActionHandlerEJB implements DefaultLotIssuedLocal,DefaultLotIssuedRemote // 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
	{
		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 :DefaultLotIssuedEJB :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
		{
			System.out.println("XML String1:::"+xmlString1);
			System.out.println("XML String:::"+xmlString);
			System.out.println("Default function executing in progress...................");
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String:::"+xmlString);
				dom = GenericUtility.getInstance().parseString(xmlString); 
				System.out.println("dom :"+dom);
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				System.out.println("XML String1:::"+xmlString1);
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
				System.out.println("dom1 :"+dom1);
			}
			System.out.println("actionType:::["+actionType+"]");
						
			if (actionType.equalsIgnoreCase("Default"))
			{
					try
					{
						if(conn==null)
						{
							System.out.println("Getting Connection From Database......");
							ConnDriver connDriver = new ConnDriver();
							conn = connDriver.getConnectDB("DriverITM");
							conn.setAutoCommit(false);
							System.out.println("Connection Established......");
							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 DefaultLotIssuedEJB : actionHandler"+retString);
	    return retString;
	}
	private String actionDefault(Document dom, Document dom1, String objContext, String xtraParams) throws RemoteException, ITMException
	{
		System.out.println("Calling the Default function.........");
		GenericUtility genericUtility = GenericUtility.getInstance();
		Connection conn = null;
		PreparedStatement pstmt = null,pstmt1 = null;
		ResultSet rs = null,rs1 = null;
		StringBuffer valueXmlString = null;
		valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		String worder =null;
		String operation = null;
		int operation1 = 0;
		String sql1 = null;
		String sql2 = null;	
		int currentFormNo = 0;
		String workorder = "";
		int operation2 = 0;
		double quantity = 0;
		String lotNo ="",lotSl ="";
		try
		{	
			String detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt");
			System.out.println("detCnt ...... :"+detCnt);
			if(conn==null)
			{
				ConnDriver connDriver = new ConnDriver();
				conn = connDriver.getConnectDB("DriverITM");
				connDriver=null;
			}
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("[] :currentFormNo ....." +currentFormNo);
			if (dom != null)
			{
				worder = genericUtility.getColumnValue("work_order",dom);
				String 	worder1 =genericUtility.getColumnValue("work_order",dom1);
				System.out.println("worder1 is :"+worder1);
				operation = genericUtility.getColumnValue("operation",dom1); //changes by rajendra
			}
			if(operation == null || operation.trim().length()==0)operation="0";	 
			operation1 = Integer.parseInt(operation);
			System.out.println("operation1 is :"+operation1);
			System.out.println("work order & operations are"+worder+" , "+operation);
			sql1 = "SELECT b.LOT_NO,b.LOT_SL,b.QUANTITY  from workorder_iss a,workorder_issdet b  "
				   +" where a.tran_id =b.tran_id and a.work_order = '"+worder+"'   ";
				  System.out.println("sql1 is :"+sql1);
		   pstmt = conn.prepareStatement(sql1);	
		   rs = pstmt.executeQuery();
			while (rs.next())
			{
				lotNo = rs.getString(1) == null ? "" :rs.getString(1);
				lotSl = rs.getString(2) == null ? "" :rs.getString(2);
				quantity =rs.getDouble(3);
				System.out.println("lotNo is :"+lotNo);
				System.out.println("lotSl is :"+lotSl);
				System.out.println("quantity is :"+quantity);
				valueXmlString.append("<Detail>\r\n");
				valueXmlString.append("<work_order>").append("<![CDATA[").append(worder.trim()).append("]]>").append("</work_order>\r\n");
				valueXmlString.append("<lot_no>").append("<![CDATA[").append(lotNo).append("]]>").append("</lot_no>\r\n");
				valueXmlString.append("<lot_sl>").append("<![CDATA[").append(lotSl).append("]]>").append("</lot_sl>\r\n");
				valueXmlString.append("<iss_qty>").append("<![CDATA[").append(quantity).append("]]>").append("</iss_qty>\r\n");
				valueXmlString.append("<no_art>").append("<![CDATA[").append("1").append("]]>").append("</no_art>\r\n");
				valueXmlString.append("</Detail>\r\n");	
			}
			pstmt.close();
			valueXmlString.append("</Root>");
			pstmt = null;
		}//tryBase1 greyd			
		catch(SQLException sqx)
		{
			System.out.println("The Exception caught from DefaultLotIssuedEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch(Exception e)
		{
			System.out.println("The Exception caught from DefaultLotIssuedEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				conn.close();
				conn = null;
				
			}catch(Exception e){}
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}
}//end of Ejb