/*******************************************    
	Title : CheckSheetPostSave
    Date  : 16/06/12
    Author: Kunal Mandhre

 ********************************************************/
package ibase.webitm.ejb.wms;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.w3c.dom.*;

@javax.ejb.Stateless
public class CheckSheetPrintPos extends ValidatorEJB implements CheckSheetPrintPosLocal, CheckSheetPrintPosRemote
{
	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 ptcn = "";
		String palletID = "";
		boolean isError = false;
		Document dom = null;
		String printer = "";
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();	
		String errorString = "";
		
		try
		{
			conn.setAutoCommit(false);
			GenericUtility genericUtility = GenericUtility.getInstance();
			dom = genericUtility.parseString(domString);
			System.out.println("Dom String ="+domString);
			ptcn = genericUtility.getColumnValue("ptcn", dom, "1", "1");
			palletID = genericUtility.getColumnValue("pallet_id", dom, "1", "1");
			printer = checkNull(genericUtility.getColumnValue("printer_name", dom, "1", "1"));
			System.out.println(" printer ->["+printer+"]ptcn["+ptcn+"]["+palletID+"]");
			
			/*if(ptcn != null && ptcn.trim().length() > 0)
			{
				errString = generateCheckSheetReport(ptcn, palletID, printer, conn, xtraParams);
			}*/
			errString = generateCheckSheetReport(ptcn, palletID, printer, conn, xtraParams);
			
			if(errString == null || !(errString.trim().length() > 0))
			{
				return itmDBAccessEJB.getErrorString("","RPTPRTSUCF","","",conn);
			}
			
		}
		catch(Exception e)
		{
			isError = true;
			throw new ITMException(e);
		}
		finally
		{
			try
			{

			}
			catch(Exception e)
			{
				System.err.println("Exception : calling Checksheet Print report :\n"+e.getMessage());
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return errString;
	}
	
	
	private String generateCheckSheetReport(String ptcn, String palletID, String printer, Connection conn, String xtraParams) throws ITMException
	{
		String sql = "";
		String reportObjName = "";
		String errString = "";
		String loginCode = "";
		
		
		try
		{
			if(palletID != null && !(palletID.trim().length() > 0))
			{
				System.out.println("inside if pallet::::::"+palletID);
				palletID = null;
			}		
			if(ptcn != null && !(ptcn.trim().length() > 0))
			{
				ptcn = null;
			}
			//Changed by sankara on 28/07/14 for report issue by pallet no start.
			if(ptcn == null || ptcn.trim().length() <= 0)
			{
				ptcn = "";
				System.out.println("inside blank ptcn["+ptcn+"]");
			}
			if(palletID == null || palletID.trim().length() <= 0)
			{
				palletID = "";
				System.out.println("inside blank pallet["+palletID+"]");
			}
			//Changed by sankara on 28/07/14 for report issue by pallet no end.
			
			System.out.println("palletID:::::::::::"+palletID);
			System.out.println("ptcn:::::::::::"+ptcn);
			GenericUtility genericUtility = GenericUtility.getInstance();
			System.out.println(" calling Checksheet Print report -------------------");
			loginCode = checkNull(genericUtility.getValueFromXTRA_PARAMS( xtraParams, "loginCode" ));
			DistCommon discommon = new DistCommon();	
			reportObjName = "d_quality_audit_carton";//discommon.getDisparams("999999","LOADING_REP_OBJNAME",conn);				
			ibase.webitm.reports.utility.JasperReportGenerator jrGenerator = new ibase.webitm.reports.utility.JasperReportGenerator();
			String xsdString ="<Arguments>" +
						"<Argument id=\"PALLET_ID\" name=\"PALLET_ID\">" +
						"<argType>java.lang.String</argType>" +						
						"<value>"+palletID+"</value>" +
						"</Argument>" +
			   			"<Argument id=\"PTCN\" name=\"PTCN\">" +
						"<argType>java.lang.String</argType>" +						
						"<value>"+ptcn+"</value>" +
						"</Argument>" +
						"<Argument id=\"x_path\" name=\"x_path\">" +
						"<argType>java.lang.String</argType>" +
						"<value>/DocumentRoot/d_quality_audit_carton</value>" +
						"</Argument>" +
						"<Argument id=\"design_source\" name=\"design_source\">" +
						"<argType>java.lang.String</argType>" +
						"<value>d_quality_audit_carton.jrxml</value>" +
						"</Argument>" +
						"<Argument id=\"data_source\" name=\"data_source\">" +
						"<argType>java.lang.String</argType>" +
						"<value>SQL</value>" +
						"</Argument>" +
						"<Argument id=\"bind_type\" name=\"bind_type\">" +
						"<argType>java.lang.Integer</argType>" +
						"<value>0</value>" +
						"</Argument>" +
						"<Argument id=\"report_type\" name=\"report_type\">" +
						"<argType>java.lang.String</argType>" +
						"<value>JASPER</value>" +
						"</Argument>" +
						"<Argument id=\"report_save_type\" name=\"report_save_type\">" +
						"<argType>java.lang.String</argType>" +								
						"<value>NATIVE</value>" +
						"</Argument>" +
						"</Arguments>";
			System.out.println(" XSD parser **->["+xsdString+"]");			
            //changed by sankara on 22/07/14 not reuired as per pragyan sir start.
			/*if(isRemotePrintSrvCofigured("IS_REMOTE_PRNTSRV_CONFIGRD",conn))
			{
				 errString = jrGenerator.callRemotePrintServlet(reportObjName, xsdString, loginCode, printer.trim(), 1, conn);
			}
			else
			{
				errString = jrGenerator.printReport(reportObjName, xsdString, loginCode, printer.trim(), 1, conn);
			}*/
			errString = jrGenerator.callRemotePrintServlet(reportObjName, xsdString, loginCode, printer.trim(), 1, conn);
			//changed by sankara on 22/07/14 not reuired as per pragyan sir end.
			//
			 
			
			 
			
		}
		catch(Exception e)
		{
			System.err.println("Exception : calling Checksheet Print report ------------------- :\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		return null;
	}
	private String checkNull(String str)
	{
		if( str == null)
		{
			str = "";
		}
		return str;
	}
	
	private boolean isRemotePrintSrvCofigured(String propertyName , Connection conn) throws ITMException
	{
		String sql = "";
		String flagRemoteRptSrvltCofigured = "N";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean isConfigured = false;
		DistCommon discommon = new DistCommon();	
		try
		{
			flagRemoteRptSrvltCofigured  = discommon.getDisparams("999999",propertyName,conn);       
			
			if("Y".equalsIgnoreCase(flagRemoteRptSrvltCofigured))
			{
				return isConfigured = true;
			}
			else
			{
				return isConfigured;
			}
				
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{	
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
				throw new ITMException(ex);
			}
		}
	}
}

