/*
* PURPOSE : presave InvFreeze
* AUTHOR : Changed By Sumit On 22/09/2011 
*/

package ibase.webitm.ejb.wms;
import java.rmi.RemoteException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.w3c.dom.*;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;


@javax.ejb.Stateless
public class PhyScanPreSave extends ValidatorEJB implements PhyScanPreSaveLocal, PhyScanPreSaveRemote
{
	public String preSave()throws RemoteException,ITMException
	{
		return "";
	}

	public String preSave( String domString, String editFlag, String xtraParams, Connection conn ) throws RemoteException,ITMException
	{
		String retString = "";
		PreparedStatement pStmt = null;
		Document dom = null;		
		java.sql.Timestamp effDate = null;		
		boolean isError = false;
		
		try
		{
			conn.setAutoCommit( false );
			
			GenericUtility genericUtility = GenericUtility.getInstance();
			dom = genericUtility.parseString(domString);
			
			System.out.println("--- domString InvFreezePreSave ["+domString+"]");
			String tranId = genericUtility.getColumnValue("tran_id",dom);
			System.out.println("--- tranId ["+tranId+"]");
			//String effDateStr = genericUtility.getColumnValue("eff_date",dom);
			DateFormat dateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			java.util.Date currDate = new java.util.Date();
			SimpleDateFormat sdf = sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			String effDateStr  = sdf.format(currDate);
			
			effDateStr = genericUtility.getValidDateString(effDateStr, genericUtility.getApplDateFormat(), genericUtility.getDBDateFormat());
			effDate = java.sql.Timestamp.valueOf(effDateStr + " 00:00:00");
			System.out.println("--- effDate--> [" + effDate + "]");
			String sql="";
			int updateCnt = 0;

			
			
			sql = "DELETE  FROM PHYSCANDET WHERE TRAN_ID IN( SELECT TRAN_ID FROM PHYSCAN WHERE TRAN_DATE=?) ";
			System.out.println("--- Delete if record exist in table ["+sql+"]");
			pStmt = conn.prepareStatement( sql );
			pStmt.setTimestamp(1, effDate);
			updateCnt = pStmt.executeUpdate();
			System.out.println("******** Query Executed @ **********");
			System.out.println("[ InvFreezePreSave ] Update Count for deleting :"+updateCnt);
			if(pStmt !=null)
			{
				pStmt.close();
				pStmt = null;
			}			
			
			
			sql = "DELETE FROM PHYSCAN WHERE TRAN_DATE=?";
			System.out.println("--- Delete if record exist  ["+sql+"]");
			pStmt = conn.prepareStatement( sql );
			pStmt.setTimestamp(1, effDate);
			updateCnt = pStmt.executeUpdate();
			System.out.println("******** Query Executed  **********");
			System.out.println("[ DespPackPreSave ] Update Count for deleting  :"+updateCnt);

			if(pStmt !=null)
			{
				pStmt.close();
				pStmt = null;
			}
            
		}
		catch(Exception e)
		{
			isError = true;
			System.out.println("Exception :PhyScanPreSave : :==>\n"+e.getMessage());
			retString = e.getMessage();
			throw new ITMException(e);
			
		}
		finally
		{
			try
			{
				System.out.println("isError ["+isError+"]");
							
				    if( isError )
					{
						conn.rollback();
					}
					if(pStmt != null)
					{
						pStmt.close();
						pStmt = null;
					}
					
					//conn.close();	
			}
			catch(Exception e)
			{
				System.out.println("Exception :InvFreezePreSave : :==>\n"+e.getMessage());
				try
				{
					System.out.println("Before rollback");
					conn.rollback();
				}
			
				catch(SQLException sqle)
				{
                    System.out.println(sqle);
				}
				throw new ITMException(e);
			
			}
		}
		System.out.println("Return string :"+retString);
		return retString;		
	}
}
