package ibase.webitm.ejb.mfg.adv;
import ibase.webitm.utility.*;
import ibase.utility.CommonConstants;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import java.rmi.RemoteException;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.ejb.*;
import java.util.Calendar;
import java.util.Date;
import javax.naming.InitialContext;
import ibase.system.config.AppConnectParm;
import ibase.webitm.ejb.MasterStatefulEJB;
//import ibase.webitm.ejb.MasterStatefulHome;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class ConfirmWOAmendment extends ActionHandlerEJB implements  ConfirmWOAmendmentLocal , ConfirmWOAmendmentRemote //SessionBean
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	String errorString = null;
	/*public void ejbCreate() throws RemoteException, CreateException
	{
		System.out.println("Create Method Called....");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate()
	{
	}
	public void ejbPassivate()
	{
	}*/
	public String actionHandler() throws RemoteException,ITMException
	{
		System.out.println("actionHandler() Method Called....");
		return "";
	}
	public String actionHandler(String xmlString, String xtraParams, String objContext) throws RemoteException,ITMException
	{
		String  retString = null;
		System.out.println("Xtra Params : " + xtraParams);
		//genericUtility = GenericUtility.getInstance();
		try
		{
			retString = actionConfirm(xmlString, xtraParams);
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from  actionHandler"+retString);
	    return (retString);
	}
	private String actionConfirm(String tranID, String xtraParams) throws RemoteException,ITMException, Exception
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		String userId ="",loginSite ="",empCode="";
		userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
		loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
		empCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
		System.out.println("empCode.......... !"+empCode);
		String sql = "";
        String retString = "";
		int count =0,i=0,upd=0;
		System.out.println("tranID ...."+tranID );
		ArrayList arrayList =new ArrayList();
		ArrayList itemList =new ArrayList();
		ArrayList arrayListItem =new ArrayList();
		HashMap hm =new HashMap();
		HashMap currentMap =new HashMap();
		HashMap itemLiscontracmap =new HashMap();
		String lsConf = "Y",confirmed = "";
		java.sql.Timestamp currDate = null;
		String  itemCode ="",siteCode ="",unit ="",phyAttrib1="",phyAttrib2 ="",phyAttrib3 ="",phyAttrib4 ="",phyAttrib5 ="",phyAttrib6 ="";
		String suppSour="",itemCodenew="",returnString="",item_type="",item_ser="",tranIdRef = "";
		String contract="",errorCode ="",noOfHour = "",empCodeAmd = "",projCode = "";
		double quantity =0;
		Timestamp mfgDateStart = null,expDate = null,planDueDate = null;
		Timestamp mfgDateStartOld = null,expDateOld = null,planDueDateOld = null;
		try
		{
			System.out.println("In new workorder amd >>>>>>>>>>>>>> ");
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			sql = "select mfg_date__start mfg_date,exp_date,plan__due_date plan_date,"
						+ " site_code,work_order__ref work_order_ref,confirmed"
						+ " from worder_amd "
						+ " where tran_id = '"+tranID+"'";
			System.out.println("select  sql from worder_amd>>>>>>>>>>>>>> " + sql);
			pstmt= conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			 {
				mfgDateStart = rs.getTimestamp("mfg_date");
				expDate = rs.getTimestamp("exp_date");
				planDueDate = rs.getTimestamp("plan_date");
				siteCode = rs.getString("site_code") == null ? "" : rs.getString("site_code");
				tranIdRef = rs.getString("work_order_ref") == null ? "" : rs.getString("work_order_ref");
				confirmed = rs.getString("confirmed") == null ? "" : rs.getString("confirmed");
			 }
			pstmt.close();
			rs.close();
			pstmt = null;
			rs = null;
			// changed by pankaj on 17.02.09 to update old date when new is null
			sql = "select mfg_date__start mfg_date,exp_date,plan_due_date plan_date"
					+ " from workorder where work_order = '" + tranIdRef + "'";
					System.out.println("select  sql from worder_amd>>>>>>>>>>>>>> " + sql);
			pstmt= conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			 {
				mfgDateStartOld = rs.getTimestamp("mfg_date");
				expDateOld = rs.getTimestamp("exp_date");
				planDueDateOld = rs.getTimestamp("plan_date");
			 }
			pstmt.close();
			rs.close();
			pstmt = null;
			rs = null;
			if( !confirmed.equalsIgnoreCase("Y"))
			{
				sql = " Update workorder set "
							+ " mfg_date__start =?,"
							+ " exp_date = ?,"
							+ " site_code = '" + siteCode +"',"
							+ " plan_due_date = ?"
							+ " where tran_id = '" + tranIdRef + "'" ;
				System.out.println("sql workorder>>>>>> ....."+sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setTimestamp(1, mfgDateStart = mfgDateStart == null ? mfgDateStartOld : mfgDateStart );
				pstmt.setTimestamp(2, expDate = expDate == null ? expDateOld : expDate);
				pstmt.setTimestamp(3, planDueDate = planDueDate == null ? planDueDateOld : planDueDate);
				System.out.println("seting mfgDateStart>>>>>> ....."+mfgDateStart);
				System.out.println("seting expDate>>>>>> ....."+expDate);
				System.out.println("seting planDueDate>>>>>> ....."+planDueDate);
				pstmt.executeUpdate();
				sql = " Update worder_amd set "
							+ " confirmed = 'Y',"
							+ " conf_date = ? ,"
							+ " emp_code__aprv = '" + empCode + "'" 
							+ " where tran_id = '" + tranID + "'" ;
				System.out.println("sql to update worder_amd>>....."+sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setTimestamp(1, getCurrdateAppFormat() );
				pstmt.executeUpdate();
				returnString = itmDBAccessEJB.getErrorString("","VTWOASC",userId);
				// end changed by pankaj on 17.02.09 to update old date when new is null
				//returnString = returnString.substring(0,returnString.indexOf("Amended") ) + " for tran_id = " + tranID + returnString.substring(returnString.indexOf("</description>") );
				System.out.println("new returnString changed....."+returnString);
			}
			else
			{
				returnString = itmDBAccessEJB.getErrorString("","VTWODASC",userId);
			}
		}
		catch(Exception e)
		{
			System.out.println("ConfirmWOAmendmentEJB..."+e.getMessage());
			e.printStackTrace();
			try
			{
				conn.rollback();
			}
			catch(Exception e1)
			{
			   System.out.println("ConfirmWOAmendmentEJB..."+e1.getMessage());
			   e1.printStackTrace();
			}
		}
		finally
		{
			try
			{
				conn.commit();
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception ConfirmWOAmendmentEJB....... :\n"+e.getMessage());
			}
		}
		System.out.println("retString ::"+returnString);
		return returnString;
	}
	 private Timestamp getCurrdateAppFormat()
    {
        String s = "";	
		 Timestamp timestamp = null;		
        GenericUtility genericUtility = GenericUtility.getInstance();
        try
        {
            java.util.Date date = null;
            timestamp = new Timestamp(System.currentTimeMillis());
            System.out.println(genericUtility.getDBDateFormat());
            SimpleDateFormat simpledateformat = new SimpleDateFormat(genericUtility.getDBDateFormat());
            date = simpledateformat.parse(timestamp.toString());
            timestamp = Timestamp.valueOf(simpledateformat.format(date).toString() + " 00:00:00.0");
           // s = (new SimpleDateFormat(genericUtility.getApplDateFormat())).format(timestamp).toString();
        }
        catch(Exception exception)
        {
            System.out.println("Exception in  getCurrdateAppFormat " + exception.getMessage());
        }
        return timestamp;
    }
}
