/********************************************************
	Title : HoldRelAct
	Date  : 7 Nov 2011
	Developer: Dipak Chattar

********************************************************/


package ibase.webitm.ejb.wms;

import java.rmi.RemoteException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import org.w3c.dom.*;
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 javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class HoldRelAct extends ActionHandlerEJB implements HoldRelActLocal, HoldRelActRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	UtilMethods utilMethods = UtilMethods.getInstance();
	
    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}

	public String actionHandler(String actionType, String xmlString, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		String  resString = null;
		try
		{
//excutes when default button press..
			if (actionType.equalsIgnoreCase("IMP_FROM_DBS"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					dom = genericUtility.parseString(xmlString); 
				}
				System.out.println("inside default IMP_FROM_DBS");
				resString = importFromDatabase(dom,objContext,xtraParams);
				System.out.println(" default ends IMP_FROM_DBS");
			}

		}
	   	catch(Exception e)
		{
			System.out.println("Exception :in default :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from actionHandler default "+resString);
		return resString;
	}
	

//import values from database when default press	
	private String importFromDatabase(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		String sql = "";
		String Status = "H";
		String sqlAppend = "";
		String tranIdHold = "";
		String lineNoHold = "";
		String remarks = "";
		
		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");
			
			sqlAppend = "tran_id, "
				  + "line_no, "
				  + "remarks ";
				
			sql ="select "+ sqlAppend + "from inv_hold_det where hold_status = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,Status);
			rs = pstmt.executeQuery();
			int lineNo = 1;
			while(rs.next())
			{
            	tranIdHold = rs.getString(1) == null ? "" : rs.getString(1).trim();
            	System.out.println("----------- TranId Hold----------------------" + tranIdHold +"---- Line No------" + lineNo);
				lineNoHold = rs.getString(2) == null ? "" : rs.getString(2).trim();		
				System.out.println("----------- Line No Hold----------------------" + lineNoHold +"---- Line No------" + lineNo);
				remarks = rs.getString(3) == null ? "" : rs.getString(3).trim();										
				valueXmlString.append("<Detail objContext =\"1\"").append(" objName=\"inv_hold_rel\" domID=\""+ lineNo +"\"	dbID=\"\">");
				valueXmlString.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				valueXmlString.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
				valueXmlString.append("<tran_id__hold><![CDATA[").append(tranIdHold).append("]]></tran_id__hold>");
				valueXmlString.append("<line_no__hold><![CDATA[").append(lineNoHold).append("]]></line_no__hold>");
				valueXmlString.append("<remarks><![CDATA[").append(remarks).append("]]></remarks>");
				lineNo = lineNo + 1;
				valueXmlString.append("</Detail>\r\n");
			}
			

				System.out.println("appending sucessfull completed....... ");
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;	
				valueXmlString.append("</Root>\r\n");			
			
		}
		catch(SQLException e)
		{
			System.out.println("Exception :SQLException in default : importFromDatabase " +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception : in default : importFromDatabase" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection........");
				conn.close();
				conn = null;
			}catch(SQLException se){}
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}
	

}
