/* 
	Developed by : Kasturi
	Company : Base Information Management Pvt. Ltd
	Version : 1.0
	Date : 27/10/2005
*/
package ibase.webitm.ejb.dis.adv;

import java.rmi.RemoteException;
import java.sql.*;

import javax.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.system.config.*;

import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class ConsumeOrderCnc extends ActionHandlerEJB implements ConsumeOrderCncLocal, ConsumeOrderCncRemote
{
	/*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 tranID, String xtraParams, String forcedFlasg) throws RemoteException,ITMException
	{
		String  retString = null;
		try
		{
			retString = actionCancel(tranID,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :ConsumeOrdCanc :actionHandler:" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
	    return retString;
	}

	private String actionCancel(String consOrd ,String xtraParams)throws ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt =null;
		ResultSet rs = null;
		String sql = "";
		String updSql = "";
		String errCode="";
		String errString="";
		String status = "";
		int rows=0;
		ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		try
		{	
		   	conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			sql = "SELECT STATUS FROM CONSUME_ORD WHERE CONS_ORDER ='"+consOrd+"'";
			System.out.println("ConsumeOrdCanc:actionCancel:sql:"+sql);
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				status = rs.getString("STATUS");
			}
			rs.close();
			rs = null;
			stmt.close();
			stmt = null;
			
			System.out.println("consOrd:"+consOrd+":status:"+status+":");
			if(status != null && status.trim().length() > 0 )
			{
				if(status.equalsIgnoreCase("D"))
				{
					errCode ="VTCANC5";
				}
				if(status.equalsIgnoreCase("C"))
				{
                  errCode ="VTCANCEL2";
				}
				else
			    {
                    updSql="UPDATE CONSUME_ORD  SET STATUS = 'C',STATUS_DATE = ?"+
							" WHERE CONS_ORDER = ?";
					System.out.println("ConsumeOrdCanc:Cancel:updSql:"+updSql+":");
					pstmt = conn.prepareStatement(updSql);
					pstmt.setDate(1,new java.sql.Date(System.currentTimeMillis()));
					pstmt.setString(2,consOrd);
					rows = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
					System.out.println("*ConsumeOrdCanc:rows:"+rows);
					if(rows <= 0)
					{
			         errCode="DS000NR";
					}
					else if(rows > 0)
					{
						conn.commit();
						errCode="VTCANC1";
						System.out.println("\n <==== Table Updated Successfully ====>");
					}
				}//end of else
			}	//end of if
			if (errCode != null  && errCode.trim().length() > 0)
			{
				System.out.println("ConsOrdCanc:errCode:"+errCode);
				errString = itmDBAccess.getErrorString("",errCode,"","",conn);
				System.out.println("errString:"+errString+":");				
			}		  
		}//end try
		catch(Exception e)
		{
			System.out.println("Exception :ConseOrdCanc :" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if (conn != null)
				{
					conn.close();
					conn = null;
				}
			}catch(Exception e){}
		}
		return errString;
	}
}