package ibase.webitm.ejb.dis;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

@javax.ejb.Stateless
public class ChgUserAllocPostSave extends ValidatorEJB implements ChgUserAllocPostSaveLocal,ChgUserAllocPostSaveRemote{
	
	boolean isLocalConn = false;
	
	public String postSave(String xmlString,String editFlag,String tranId,  String xtraParams,Connection conn) throws RemoteException,ITMException
	{

		System.out.println(">>>>>>>>>>>>>>CONNECTION"+conn);
		System.out.println("------------ DisIssDelWizPostSave postSave method called-----------------");		
		Document dom = null;
		String errString="";
        
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString); 
				System.out.println("xmlString  *===> " + xmlString);
			}
			
			//conn = null;
			if(conn==null){
				
				ConnDriver connDriver = new ConnDriver();
				conn = connDriver.getConnectDB("DriverITM");
				//connDriver= null;
				conn.setAutoCommit(false);
				isLocalConn = true;
			}
			errString = postSave(dom,tranId,editFlag,xtraParams,conn);


		}
		catch(Exception e)
		{
			System.out.println("Exception : DisIssDelWizPostSave : postSave : ==>\n"+e.getMessage());
			throw new ITMException(e);
		}		
		return errString;
	}

	public String postSave(Document dom,String tranId,String editflag,String xtraParams,Connection conn)
	{
		System.out.println("post save dom arg  --------------- > "+dom);
		String sql = "", error = "";
		String distOrder="", chgTerm = "", chgUserNew = "";
		int cnt = 0;
		
		NodeList parentNodeList = null;
		int ctr=0;
		int detailListLength = 0;

		System.out.println("tranId in postSave dom ----> "+tranId);
		
		PreparedStatement pstmt = null;
		ResultSet rs=null;
		try
		{
			chgTerm = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams, "chgTerm");
			//chgUser = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams, "loginCode");
			
			parentNodeList = dom.getElementsByTagName("Detail2");
			System.out.println("parentNodeList length >>>>>>>>>>>>>>>>>>>>> "+parentNodeList.getLength());
			detailListLength = parentNodeList.getLength();
			for(ctr = 0; ctr < detailListLength; ctr++)
			{
				distOrder = GenericUtility.getInstance().getColumnValue("dist_order",dom);
				chgUserNew = GenericUtility.getInstance().getColumnValue("code",dom);
				
				System.out.println("dist_order in DOM --->>" +distOrder);
				System.out.println("chgUserNew in DOM --->>" +chgUserNew);
						
					sql = "update distord_iss set chg_user = ?, chg_term = ?, chg_date = sysdate where dist_order = ? and confirmed = 'N'";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, chgUserNew);
					pstmt.setString(2, chgTerm);
					pstmt.setString(3, distOrder);
					cnt = pstmt.executeUpdate();
					
					if(pstmt != null){
						pstmt.close();
						pstmt = null;
					}
					
					sql = "update despatch set chg_user = ?, chg_term = ?, chg_date = sysdate where sord_no = ? and confirmed = 'N'";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, chgUserNew);
					pstmt.setString(2, chgTerm);
					pstmt.setString(3, distOrder);
					cnt = pstmt.executeUpdate();
					
					if(pstmt != null){
						pstmt.close();
						pstmt = null;
					}
					
					System.out.println(">>>>>>>successfully deleted record ChgUserAllocPostSave cnt = :" + cnt);
			}	 
			
			conn.commit();
			System.out.println(">>>>>>> commit executed ");

		}catch(Exception e)
		{
			try {
				System.out.println(">>>>>>>>>>>>In catch Before rollback>>>");
				conn.rollback();
				System.out.println(">>>>>>>>>>>>rollback  issued >>>");
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				error=e1.getMessage();
			}
			System.out.println("Exception ::"+e);
			e.printStackTrace();
			error=e.getMessage();

		}	
		finally
		{
			try {
				System.out.println(">>>>>>>>>>>>In finally Before Commit>>>");
				
				if(pstmt != null){
					pstmt.close();
					pstmt = null;
				}

				if(rs != null){
					rs.close();
					rs = null;
				}
				
				if(isLocalConn){
					
					if(conn != null)
					{
						conn.close();	
						conn = null;
						isLocalConn = false;
					}
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				error=e.getMessage();
			}		
		}

		return error;

	}
}
