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 javax.annotation.*;
import ibase.webitm.utility.GenericUtility;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class InventoryAct extends ActionHandlerEJB implements InventoryActLocal,InventoryActRemote // SessionBean
{
	GenericUtility genericUtility= GenericUtility.getInstance();
	Connection conn=null;
	Vector routeVec  = null;
	String tranId = null;
	int lineNo = 0;
	/*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 :InventoryActEJB :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
	{
		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)
							{
								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.getApplDateFormat());
								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);
		}
		finally
		{
			routeVec = null;
		}
		System.out.println("returning String from Despatch : actionHandler"+retString);
	    return retString;
	}
	private String actionDefault(Document dom, Document dom1, String objContext, String xtraParams) throws RemoteException, ITMException
	{
		StringBuffer valueXmlString = null;
		routeVec  = new Vector();
		valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		int parentNodeListLength=0;
		int childNodeListLength = 0;
		Node parentNode =null;
		Node childNode =null;
		String childNodeName = null;
		
		
		PreparedStatement pstmt = null;
		ResultSet rs =null;
		String sql = null;
		String workOrder =  null;
		String operation =  null;
		String routeCode = null;
		String 	itemCode = null;
		String 	QtyStr = null;
		String unit = null;
		String descr1 = null;
			
					 
		
		try 
		{
		//	*******************************
			parentNodeList = dom1.getElementsByTagName("Detail2");
			parentNodeListLength = parentNodeList.getLength(); 
			System.out.println("parentNodeListLength:::::::::"+parentNodeListLength);
			for (int row = 0; row < parentNodeListLength; row++)
			{
				workOrder = null;
				operation = null;
				parentNode = parentNodeList.item(row);
				childNodeList = parentNode.getChildNodes();
				childNodeListLength = childNodeList.getLength();
				System.out.println("childNodeListLength::: "+ childNodeListLength+"\n");
				for (int childRow = 0; childRow < childNodeListLength; childRow++)
				{
					
					childNode = childNodeList.item(childRow);
					childNodeName = childNode.getNodeName();

					/*if (childNodeName.equals("route_code"))
					{
						if(childNode.getFirstChild()!=null)
						{
							routeCode = childNode.getFirstChild().getNodeValue();
							//System.out.println("routeCode ::"+ routeCode ) ;
						}
					}*/
					if (childNodeName.equals("work_order"))
					{
						if(childNode.getFirstChild()!=null)
						{
							workOrder = childNode.getFirstChild().getNodeValue();
						}
					}
					
				
		 
				}//end for
				sql = "SELECT ROUTE_CODE FROM WORKORDER WHERE WORK_ORDER='"+workOrder+"'";
				System.out.println("Sql:::["+sql+"]");
				pstmt = conn.prepareStatement(sql);
				rs= pstmt.executeQuery();
				if(rs.next())
				{
					routeCode = ((rs.getString(1)==null)?" ":rs.getString(1));
				
				}
				rs.close();
				pstmt.close();
				
				System.out.println("WorkOrder :::["+workOrder+"]  RouteCode["+routeCode+"]");
				if(!routeVec.contains(routeCode))
				{ 
					routeVec.add(routeCode);
					
					sql = "SELECT PROCROUTE_WIP.ITEM_CODE__WIP,PROCROUTE_WIP.PRC_OUTPUT_SIZE,ITEM.UNIT,ITEM.DESCR FROM PROCROUTE_WIP,ITEM WHERE ITEM.ITEM_CODE= PROCROUTE_WIP.ITEM_CODE__WIP AND  PROCROUTE_WIP.ROUTE_CODE='"+routeCode+"'";
					System.out.println("Sql:::["+sql+"]");
					pstmt = conn.prepareStatement(sql);
					rs= pstmt.executeQuery();
					while(rs.next())
					{ 
						itemCode = (rs.getString(1)==null?" ":rs.getString(1));
						QtyStr = (rs.getString(2)==null?" ":rs.getString(2));
						unit = (rs.getString(3)==null?" ":rs.getString(3));
						descr1 = (rs.getString(4)==null?" ":rs.getString(4));
						
						valueXmlString.append("<Detail>\r\n");
						
						valueXmlString.append("<item_code>").append("<![CDATA[").append(itemCode).append("]]>").append("</item_code>\r\n");
						valueXmlString.append("<descr>").append("<![CDATA[").append(descr1).append("]]>").append("</descr>\r\n");
						valueXmlString.append("<operation>").append("<![CDATA[").append("0").append("]]>").append("</operation>\r\n");
						valueXmlString.append("<quantity>").append("<![CDATA[").append(QtyStr).append("]]>").append("</quantity>\r\n");
						valueXmlString.append("<unit>").append("<![CDATA[").append(unit).append("]]>").append("</unit>\r\n");
						valueXmlString.append("<reas_code>").append("<![CDATA[").append("").append("]]>").append("</reas_code>\r\n");
						valueXmlString.append("<det_remarks>").append("<![CDATA[").append(" ").append("]]>").append("</det_remarks>\r\n");
						valueXmlString.append("<loc_code>").append("<![CDATA[").append(" ").append("]]>").append("</loc_code>\r\n");
						valueXmlString.append("<lot_no>").append("<![CDATA[").append(" ").append("]]>").append("</lot_no>\r\n");
						valueXmlString.append("<lot_sl>").append("<![CDATA[").append(" ").append("]]>").append("</lot_sl>\r\n");
						valueXmlString.append("</Detail>\r\n");
					}
				}//end if
				
			}
			valueXmlString.append("</Root>\r\n");
	}
		catch(Exception e)
		{
			
			e.printStackTrace();
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}

}//end of Ejb