
package ibase.webitm.ejb.wms;
import ibase.system.config.*;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.ejb.dis.InvAllocTraceBean;
import ibase.webitm.ejb.dis.StockUpdate;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.TransIDGenerator;
import ibase.utility.CommonConstants;

import java.io.*;
import java.rmi.RemoteException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import org.w3c.dom.*;

import javax.ejb.*;
import javax.naming.InitialContext;
@javax.ejb.Stateless
public class ShipmentPos extends ValidatorEJB implements ShipmentPosLocal, ShipmentPosRemote
{
	public String postSave()throws RemoteException,ITMException
	{
		return "";
	}
	public String postSave(String domString, String tranId,String editFlag, String xtraParams, Connection conn ) throws RemoteException,ITMException
	{

		String errString = "";
		String sql = "";
		String transporterCode = "";
		String waveTranCode = "";
		String currPrintStatus = "";
		String despTranCode = "";
		String refId = "";
		String shipmentId = "";
		String waveId = "";
		String PTCN = "";
		int shipupdCnt = 0;
		int desupdCnt = 0;
		int waveupdCnt = 0;
		Document dom = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		java.util.Date currDate1 = new java.util.Date();
		DistCommon distCommon = new DistCommon();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
        //changed by sankara on 03/01/14 for update freight_amt_add in despatch start.
		String freightamount = "";
		NodeList parentNodeList = null;
		Node parentNode = null;
		int parentNodeListLength = 0;
		String refid = "";
		int updatecount = 0;
		//changed by sankara on 03/01/14 for update freight_amt_add in despatch end.
		//changed by sankara on 06/05/14 for referser
		String refSeries = "";
		try
		{
			String applDateFormat = genericUtility.getApplDateFormat();
			String dbDateFormat = genericUtility.getDBDateFormat();
			SimpleDateFormat sdf = new SimpleDateFormat(applDateFormat);
			String currDateStr = sdf.format(currDate1);
			java.sql.Timestamp tranDate = java.sql.Timestamp.valueOf(genericUtility.getValidDateString( currDateStr , applDateFormat, dbDateFormat ) + " 00:00:00.0") ;
			conn.setAutoCommit(false);
			dom = genericUtility.parseString(domString);
			shipmentId = checkNull(genericUtility.getColumnValueFromNode("shipment_id", dom.getElementsByTagName("Detail1").item(0)));
			
		//  Changed by sankara on 16/08/13 for checking transporter code null	
			//transporterCode = genericUtility.getColumnValueFromNode("tran_code", dom.getElementsByTagName("Detail1").item(0));
			transporterCode = checkNull(genericUtility.getColumnValueFromNode("tran_code", dom.getElementsByTagName("Detail1").item(0)));
			//changed by sankara on 07/jun/14 for detail check null
			if( dom.getElementsByTagName("Detail2").item(0) !=null )
			{
			  refSeries = checkNull(genericUtility.getColumnValueFromNode("ref_ser", dom.getElementsByTagName("Detail2").item(0)));
			}
			System.out.println("refSeries:::::::"+refSeries);
			//changed by sankara on 03/01/14 for update freight_amt_add in deepatch start.
			if("S-DSP".equalsIgnoreCase(refSeries.trim()))
			{
				NodeList detail2List = dom.getElementsByTagName("Detail2");
				parentNodeListLength = detail2List.getLength();			
				for(int row = 0; row < parentNodeListLength; row++)
				{				
					refid = GenericUtility.getInstance().getColumnValueFromNode("ref_id", dom.getElementsByTagName("Detail2").item(row));
					freightamount = GenericUtility.getInstance().getColumnValueFromNode("freight_amt_add", dom.getElementsByTagName("Detail2").item(row));

					System.out.println(" refid ["+refid+"] freightamount ["+freightamount+"]");

					sql = 	" UPDATE DESPATCH SET FREIGHT_AMT_ADD = ? WHERE DESP_ID = ? ";
					pstmt = conn.prepareStatement(sql);			
					pstmt.setString(1, freightamount);
					pstmt.setString(2, refid);
					updatecount = pstmt.executeUpdate();
					if( updatecount > 0 )
					{
						System.out.println(" update seccessfully ");
					}
					pstmt.close(); pstmt = null;
				}
				//changed by sankara on 03/01/14 for update freight_amt_add in deepatch end.
				//Changed By Pragyan to get PTCN
				/*sql = " SELECT D.REF_ID AS REF_ID, P.WAVE_ID AS WAVE_ID  FROM SHIP_DOCS D, SHIPMENT S, DESPATCH P WHERE S.SHIPMENT_ID = D.SHIPMENT_ID" +
						" AND P.DESP_ID = D.REF_ID AND  S.SHIPMENT_ID = ?  ";*/
				sql = " SELECT D.REF_ID AS REF_ID, P.WAVE_ID AS WAVE_ID,D.PTCN AS PTCN  FROM SHIP_DOCS D, SHIPMENT S, DESPATCH P WHERE S.SHIPMENT_ID = D.SHIPMENT_ID" +
					  " AND P.DESP_ID = D.REF_ID AND  S.SHIPMENT_ID = ?  ";

				pstmt = conn.prepareStatement(sql);
				pstmt.setString( 1, shipmentId);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					refId = checkNull(rs.getString("REF_ID"));
					waveId = checkNull(rs.getString("WAVE_ID"));
					PTCN = checkNull(rs.getString("PTCN"));
				}
				rs.close(); 
				rs = null;
				pstmt.close();
				pstmt = null;

				//sql = " SELECT TRAN_CODE FROM WAVE_STATUS_ORG WHERE WAVE_ID = ? ";
				//sql = " SELECT TRAN_CODE FROM WAVE_STATUS_ORG WHERE PTCN = ? ";
				sql = " SELECT TRAN_CODE__NEW FROM WAVE_STATUS_ORG WHERE PTCN = ? ";
				pstmt = conn.prepareStatement(sql);
				//pstmt.setString( 1, waveId);
				pstmt.setString( 1, PTCN);
				rs = pstmt.executeQuery();

				if(rs.next())
				{
					waveTranCode = checkNull(rs.getString("TRAN_CODE__NEW"));
					//currPrintStatus = checkNull(rs.getString("PRINT_STATUS")); //PRINT_STATUS
					System.out.println("WAVETRANCODE=="+waveTranCode);
					System.out.println("currPrintStatus=="+currPrintStatus);

				}
				rs.close(); 
				rs = null;
				pstmt.close();
				pstmt = null;

				/*if( ! transporterCode.trim().equalsIgnoreCase( waveTranCode.trim() ) )
				{*/
				//changed by sankara on 07/06/14 commented for this block added in below start.
				/*sql = " UPDATE SHIP_DOCS SET TRAN_CODE = ? WHERE SHIPMENT_ID IN ( SELECT D.SHIPMENT_ID FROM SHIPMENT S, SHIP_DOCS D " +
							" WHERE S.SHIPMENT_ID = D.SHIPMENT_ID AND S.SHIPMENT_ID = ? ) ";		
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, transporterCode);
					pstmt.setString(2, shipmentId);
					shipupdCnt = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;    

					sql = " UPDATE DESPATCH SET TRAN_CODE = ? WHERE DESP_ID = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, transporterCode);
					pstmt.setString(2, refId);
					desupdCnt = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;*/
				//changed by sankara on 07/06/14 commented for this block added in below end.
				//Changed By Pragyan 
				//sql = " UPDATE WAVE_STATUS_ORG SET PRINT_STATUS = 'N' WHERE WAVE_ID = ? ";
				sql = " UPDATE WAVE_STATUS_ORG SET PRINT_STATUS = ? WHERE PTCN = ? ";
				pstmt = conn.prepareStatement(sql);
				//pstmt.setString(1, waveId);
				if( ! transporterCode.trim().equalsIgnoreCase( waveTranCode.trim() )  || isPacketsChanged(PTCN,conn))
				{
					pstmt.setString(1, "N");
				}
				else
				{
					pstmt.setString(1, "Y");
				}
				pstmt.setString(2, PTCN);
				waveupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
				//}

				if( shipupdCnt > 0 && desupdCnt > 0 && waveupdCnt > 0  )
				{
					System.out.println("SHIPUPDCNT=["+shipupdCnt+"] DESUPDCNT=["+desupdCnt+"] WAVEUPDCNT=["+waveupdCnt+"] " );
				}	
				else
				{
					System.out.println("Updated Rows count is zero");
				}
				//changed by sankara on 07/jun/14 for despatch start.
				sql = " UPDATE DESPATCH SET TRAN_CODE = ? WHERE DESP_ID = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, transporterCode);
				pstmt.setString(2, refId);
				desupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
				//changed by sankara on 07/jun/14 for despatch end
			}
			//Changed by Rohan to update gross weight in shipment content from shipmet headere.start
			
			if((!"".equalsIgnoreCase(editFlag) || editFlag != null) )
			{
				sql = " UPDATE SHIPMENT_CONTENT SET GROSS_WEIGHT = (SELECT GROSS_WEIGHT FROM SHIPMENT WHERE SHIPMENT_ID = ? ) WHERE SHIPMENT_ID = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,shipmentId);
				pstmt.setString(2,shipmentId);
				int cnt = pstmt.executeUpdate();
				if(cnt > 0)
				{
					System.out.println("Gross weight update suceesfuly");
				}
				pstmt.close();
				pstmt = null;
						
			}
			//changed by sankara on 06/JUN/14 for changed sql for ref series
			sql = " UPDATE SHIP_DOCS SET TRAN_CODE = ? WHERE SHIPMENT_ID IN ( SELECT D.SHIPMENT_ID FROM SHIPMENT S, SHIP_DOCS D " +
					" WHERE S.SHIPMENT_ID = D.SHIPMENT_ID AND S.SHIPMENT_ID = ? ) ";		
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, transporterCode);
			pstmt.setString(2, shipmentId);
			shipupdCnt = pstmt.executeUpdate();
			pstmt.close();
			pstmt = null;    
			//changed by sankara on 07/jun/14 this code is excuted in side despatch case only 
			/*sql = " UPDATE DESPATCH SET TRAN_CODE = ? WHERE DESP_ID = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, transporterCode);
			pstmt.setString(2, refId);
			desupdCnt = pstmt.executeUpdate();
			pstmt.close();
			pstmt = null;*/
			//changed by sankara on 07/jun/14 this code is excuted in side despatch case only 
			//Changed by Rohan to update gross weight in shipment content from shipment header.end
			
			//changed by sankara on 08/10/14 for update tran_code in C-ISS AND D-ISS if tran_code is changed in shipment start.		
			if("C-ISS".equalsIgnoreCase(refSeries.trim()) || "D-ISS".equalsIgnoreCase(refSeries.trim())  )
			{
				NodeList detail2List = dom.getElementsByTagName("Detail2");
				parentNodeListLength = detail2List.getLength();			
				for(int row = 0; row < parentNodeListLength; row++)
				{				
					refid = GenericUtility.getInstance().getColumnValueFromNode("ref_id", dom.getElementsByTagName("Detail2").item(row));
				    System.out.println("refid:::::::"+refid);
				}
			}		
			if("C-ISS".equalsIgnoreCase(refSeries.trim()) )
			{
				sql = " UPDATE CONSUME_ISS SET TRAN_CODE = ? WHERE CONS_ISSUE = ? ";  //C-ISS
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, transporterCode);
				pstmt.setString(2, refid);
				desupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
			}	
			if("D-ISS".equalsIgnoreCase(refSeries.trim()) )
			{
				sql = " UPDATE DISTORD_ISS SET TRAN_CODE = ? WHERE TRAN_ID = ? ";  //D-ISS
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, transporterCode);
				pstmt.setString(2, refid);
				desupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;	
			}	
			//changed by sankara on 08/10/14 for update tran_code in C-ISS AND D-ISS if tran_code is changed in shipment end.		
		}
		catch(Exception e)
		{
			System.out.println("Exception "+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if( rs != null )
				{
					rs.close();
					rs = null;
				}
				if( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch(Exception e)
			{
				System.err.println("Exception :DespatchTranPos :\n"+e.getMessage());
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return errString;
	}
	//Changed by sankara on 16/08/13 for checking null start.
	private String checkNull( String input )	
	{
	  if ( input == null )
	  {
			input = "";
	  }
	   return input;
	}
	//Changed by sankara on 16/08/13 for checking null end.
	
	private boolean isPacketsChanged(String PTCN, Connection conn) throws ITMException
	{
		String sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		double count = 0;
		boolean isPacketDataChanged = false;
		
		try
		{
			sql = "SELECT COUNT(*) FROM CARTON_MASTER WHERE PTCN = ? AND CHANGE_STATUS  = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, PTCN);
			pstmt.setString(2, "Y");
			
			rs = pstmt.executeQuery();
			if( rs.next())
			{
				count = rs.getDouble(1);
			}
			rs.close(); rs = null;
			pstmt.close(); pstmt = null;
			System.out.println(" item count ["+count+"]");
			
			if(count > 0)
			{
				isPacketDataChanged = true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if( rs != null)
				{
					rs.close(); rs = null;
				}
				if( pstmt != null )
				{
					pstmt.close(); pstmt = null;
				}
			}
			catch(Exception es)
			{
				es.printStackTrace();
				throw new ITMException(es);
			}
		}
		
		return isPacketDataChanged;
	}
}

