package ibase.webitm.ejb.dis;

import ibase.webitm.ejb.ValidatorEJB;
import ibase.system.config.ConnDriver;
import ibase.utility.BaseLogger;
import ibase.webitm.ejb.fin.*;
import ibase.webitm.ejb.mfg.MfgCommon;
import ibase.utility.E12GenericUtility;
import ibase.utility.UserInfoBean;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;




public class ReqForQuotationWiz extends ValidatorEJB{

	@Override
	public String itemChanged() throws RemoteException, ITMException
	{
		BaseLogger.log("3", getUserInfo(), null,"In ReqForQuotationWiz itemChanged1");
		

		return "";
	}


	@Override
	public String itemChanged(String xmlCurrFrmString, String xmlHdrFrmString, String xmlAllFrmString, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException, ITMException
	{
		BaseLogger.log("3", getUserInfo(), null,"In ReqForQuotationWiz itemChanged2");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn RAW = [" + currentColumn + "]");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn trimmed = [" + 
		    (currentColumn == null ? "null" : currentColumn.trim()) + "]");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn length = " + 
		    (currentColumn == null ? 0 : currentColumn.length()));
		Document currDom = null;
		Document hdrDom = null;
		Document allDom = null;

		String errString = null;

		E12GenericUtility genericUtility = new E12GenericUtility();

		try
		{
			if (xmlCurrFrmString != null && xmlCurrFrmString.trim().length()!=0)
			{
				currDom = genericUtility.parseString(xmlCurrFrmString); 
			}
			if (xmlHdrFrmString != null && xmlHdrFrmString.trim().length()!=0)
			{
				hdrDom = genericUtility.parseString(xmlHdrFrmString); 
			}
			if (xmlAllFrmString != null && xmlAllFrmString.trim().length()!=0)
			{
				allDom = genericUtility.parseString(xmlAllFrmString); 
			}
			System.out.println("xmlCurrFrmString--------"+xmlCurrFrmString);
			System.out.println("xmlHdrFrmString--------"+xmlHdrFrmString);
			System.out.println("xmlAllFrmString--------"+xmlAllFrmString);
			errString = itemChanged( currDom, hdrDom, allDom, objContext, currentColumn, editFlag, xtraParams );
			System.out.println ( "ErrString :" + errString);
		}
		catch (Exception e)
		{
			System.out.println ( "Exception : QCResultEntryWizIC:default_ItemChanged(String,String):" + e.getMessage() + ":" );
			errString = genericUtility.createErrorString(e);
			throw new ITMException(e);
		}
		System.out.println ( "returning from QCResultEntryWizIC default_Itemchanged" );
		return errString;
	}	




	@Override
	public String itemChanged( Document currDom, Document hdrDom, Document allDom, String objContext, String currentColumn, String editFlag, String xtraParams ) throws RemoteException,ITMException
	{
		BaseLogger.log("3", getUserInfo(), null,"In ReqForQuotationWiz itemChanged3");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn RAW = [" + currentColumn + "]");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn trimmed = [" + 
		    (currentColumn == null ? "null" : currentColumn.trim()) + "]");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn length = " + 
		    (currentColumn == null ? 0 : currentColumn.length()));
		String retValue = "";
		try
		{
			retValue = default_ItemChanged( currDom, hdrDom, allDom,  objContext, currentColumn, editFlag, xtraParams );
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		return retValue;

	}
	public String default_ItemChanged( Document currDom, Document hdrDom, Document allDom, String objContext, String currentColumn, String editFlag, String xtraParams ) throws RemoteException,ITMException
	{
		BaseLogger.log("3", getUserInfo(), null,"In ReqForQuotationWiz default_ItemChangeddddddddd2612");
		
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn RAW = [" + currentColumn + "]");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn trimmed = [" + 
		    (currentColumn == null ? "null" : currentColumn.trim()) + "]");
		BaseLogger.log("3", getUserInfo(), null,"DEBUG currentColumn length = " + 
		    (currentColumn == null ? 0 : currentColumn.length()));

		String columnValue = "";

		int cnt = 0;
		int domID = 0;

		ResultSet rs1 = null,rs2=null;
		Connection conn = null;
		PreparedStatement pstmt1 = null;
		int line =0,domId=0,curdomID=0;
		E12GenericUtility genericUtility = new E12GenericUtility();
		FinCommon finCommon = new FinCommon();
		MfgCommon mfgCommon = new MfgCommon();
		DistCommon distComm = new DistCommon();
		NodeList parentNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		NodeList childNodeList = null;
		int childNodeLength = 0,childNodeListLength;
		String childNodeName = "";
		int ctr = 0;
		String loginEmpCode="";
		String operationStr="",specRemarks="",status="",jobId="",jobCardFrom="",jobCardTo="",specCode="",jobCard="",specCodeDescr="";
		String itemDescr="",procCode="",routeCode="",workCtr="",operStatus="",chgUser="",chgTerm="",operStatushdr="";
		Timestamp chgDate=null,todayT=null;
		int operation=0, lineNo=0,operationT=0;
		String mfgLine="", mfgLayout="", mfgLayoutDescr="",remarks="",today="",mcCode="",empNameQc="";
		int  seqNumber=0,setTime=0,runTime=0;
		String machinCode="",operationStatus="",parentTranId="",revTranId="",confirmed="",empCodeAprv="";
		String custCode="",empNameAprv="",empCodeQc="";
		double quantity=0.0;
		double quantityPassed=0.0;
		double quantityRejected=0.0;
		String actualResult="";
		String qcResult="";
		String exceptedResult="";
		String specRepresent="";
		String qcResultDescr="";
		String domIDStr="";
		List<String> itemCodeList = new ArrayList<>();
		String custName = "";
		String itemSerDescr= "";
		int pos = 0;

		//Chnages start
		
		StringBuffer valueXmlString = null;
		int currentFormNo = 0;
		String userId = "";
		String sql = "";
		try {
			conn = getConnection();
			
		//changes end

		
		
		
			UserInfoBean userInfo = getUserInfo(); 
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			loginEmpCode = checkNull(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode"));
			String loginSite = checkNull(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode"));
			 userId = checkNull(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginCode"));
			chgTerm = checkNull(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "chgTerm"));
			String itemSer = checkNull(genericUtility.getColumnValue("item_ser",hdrDom,"1"));

			
			todayT = new Timestamp(System.currentTimeMillis());
			today = sdf.format(todayT);

			System.out.println( "todayT[" + todayT + "]" );
			System.out.println( "today[" + today + "]" );

			if (chgTerm.trim().length() == 0)
			{
				chgTerm = "SYSTEM";
			}
			if (userId.trim().length() == 0)
			{
				userId = "BASE";
			}

			columnValue = genericUtility.getColumnValue( currentColumn, currDom );

			conn = getConnection();

			if( objContext != null && objContext.trim().length() > 0 )
			{
				currentFormNo = Integer.parseInt( objContext );
			}
			
			
			
			userId = getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode"); // To get user details after login
			if (objContext != null && objContext.trim().length() > 0) {
				currentFormNo = Integer.parseInt(objContext);
			}

			// start creating xml
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			switch (currentFormNo) {
			case 1:
				valueXmlString.append("<Detail1>");
				if ("itm_default".equalsIgnoreCase(currentColumn.trim())) {
					BaseLogger.log("0", null,userId,"case 1---------itemDefault------");
					valueXmlString.append( "<tran_id/>" );
					valueXmlString.append("<tran_date protect = '1'>").append("<![CDATA[" + today + "]]>").append("</tran_date>");

				}
				
				if ("item_ser".equalsIgnoreCase(currentColumn.trim())) {

					String itemSerForIC = checkNull(genericUtility.getColumnValue("item_ser",currDom,"1"));
					System.out.println("case 1---------itemSerForIC------[" +itemSerForIC + "]");

					String itemSerSql = "select descr from itemser where item_ser = ?";
					PreparedStatement itemSerPs = conn.prepareStatement(itemSerSql);
					itemSerPs.setString(1, itemSerForIC);
					ResultSet itemSerRs = itemSerPs.executeQuery();
					while(itemSerRs.next()) {
						itemSerDescr = itemSerRs.getString("descr");
					}
					
					valueXmlString.append("<descr>").append("<![CDATA[" + itemSerDescr + "]]>").append("</descr>");

					if (itemSerRs != null)
					{
						itemSerRs.close();
						itemSerRs = null;
					}
					if (itemSerPs != null)
					{
						itemSerPs.close();
						itemSerPs = null;
					}
				}
				if ("cust_code".equalsIgnoreCase(currentColumn.trim())) {

					
					BaseLogger.log("0", null,userId,"case 1---------custCodeForIC::------]");

					String custCodeForIC = checkNull(genericUtility.getColumnValue("cust_code",currDom,"1"));

					BaseLogger.log("0", null,userId,"case 1---------custCodeForIC------[" +custCodeForIC + "]");



					String custNameSql = "select cust_name from customer where cust_code = ?";
					PreparedStatement custNamePs = conn.prepareStatement(custNameSql);
					custNamePs.setString(1, custCodeForIC);
					ResultSet custNameRs = custNamePs.executeQuery();
					while(custNameRs.next()) {
						custName = custNameRs.getString("cust_name");
					}
					valueXmlString.append("<cust_name>").append("<![CDATA[" + custName + "]]>").append("</cust_name>");

					if (custNameRs != null)
					{
						custNameRs.close();
						custNameRs = null;
					}
					if (custNamePs != null)
					{
						custNamePs.close();
						custNamePs = null;
					}
				
				}
				 
				valueXmlString.append("</Detail1>\r\n");
				break;
			}
			valueXmlString.append("</Root>\r\n");
		} catch (Exception e) {
			System.out.println("Exception :AssetAssign :itemChanged(Document,String):" + e.getMessage() + ":");
			e.printStackTrace();
		} 
		BaseLogger.log("3", getUserInfo(), null, "asset_assign valueXmlString::"+valueXmlString.toString());
		return valueXmlString.toString().trim();
		}
		
	
	private String checkNull( String inputVal )
	{
		if ( inputVal == null )
		{
			inputVal = "";
		}
		return inputVal;
	}	
}
