package ibase.webitm.ejb.mfg;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
//import ibase.utility.GenericUtility;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
import org.w3c.dom.*;
import javax.ejb.EJBException;


/**
 *
 * @author cpandey
 */
@Stateless
public class WIPScanPvSolar extends ValidatorEJB implements WIPScanPvSolarRemote,WIPScanPvSolarLocal
{
	GenericUtility genericUtility=GenericUtility.getInstance();
	public String wfValData(String xmlString, String xmlString1, String objContext, String editFlag, String xtraParams) throws ITMException
	{
		String errString="";
		Document dom=null;
		Document dom1=null;

		try {
			System.out.println("xmlString---------"+xmlString);
			System.out.println("xmlString1---------"+xmlString1);
			dom = parseString(xmlString);
			dom1=parseString(xmlString1);
			errString=wfValData(dom, dom1, objContext, editFlag, xtraParams);

		} catch (RemoteException ex)
		{
			throw new ITMException(ex);
		}
		return errString;
	}
	public String wfValData(Document dom,Document dom1,String objContext,String editFlag,String xtraParams) throws RemoteException,ITMException
	{

		NodeList parentNodeList=null;
		NodeList childNodeList=null;
		Node parentNode=null;
		Node childNode=null;
		String childNodeName=null;
		int childNodeListLength=0;
		String errString="";
		String errCode="";
		String errFieldName="";
		String errorType="";
		Connection conn=null;
		GenericUtility genutility=null;
		//int currOperation = 0,maxOperation = 0;
		String lotNo= "",lotSl= "",siteCode = "",tranId = "",userId="",currOperation = "",maxOperation = "",reasonCode = "",prevLotNo = "",prvLotSl = "",remarks = "",opreration = "", qcApprovalReject = "",mcCode = "",workOrd = "",itemCode = "",procCode = "",conf = "",waitTime = "";     //LOT NO FOR BAR CODE READING FIELD  FROM WORKORDER TABLE 
		ResultSet rs=null;
		int cont = 0;
		int currentFormNo=0;
		String sql = "";
		PreparedStatement pstmt = null;
		ArrayList <String> errList=new ArrayList<String>();
		ArrayList<String> errFields = new ArrayList<String>();
		ITMDBAccessEJB itmdbAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver=new ConnDriver();
		StringBuffer errStringXml = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root><Errors>");
		System.out.println("EDIT FLAG "+editFlag);

		try
		{
			genutility=GenericUtility.getInstance();
			conn=connDriver.getConnectDB("DriverITM");
			connDriver=null;
			userId=getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			System.out.println("user id "+userId);
			System.out.println("1---currentFormNo-------"+currentFormNo);
			if(objContext!=null && objContext.trim().length()>0)
			{
				currentFormNo=Integer.parseInt(objContext);
			}
			System.out.println("2----currentFormNo-------"+currentFormNo);
			switch(currentFormNo)
			{
			case 1:
				parentNodeList=dom.getElementsByTagName("Detail1");
				parentNode=parentNodeList.item(0);
				childNodeList=parentNode.getChildNodes();
				childNodeListLength=childNodeList.getLength();
				System.out.println("CHILD NODE LIST LENTH"+childNodeListLength);

				for(int ctr=0;ctr<childNodeListLength;ctr++)
				{
					childNode=childNodeList.item(ctr);
					childNodeName=childNode.getNodeName();

					//VALIDATION FOR DUPLICATE TRAN_ID

					if (childNodeName.equalsIgnoreCase("tran_id"))
					{
						System.out.println("EDIT FLAG ["+editFlag+"]");
						System.out.println(" >>>>>>>..edit flag length ["+editFlag.length()+")");
						if(!("E".equalsIgnoreCase(editFlag)))
						{	
							System.out.println(" +++++++++ In Save Method 1 +++++++++++++++++++");
							tranId = genericUtility.getColumnValue("tran_id", dom);
							System.out.println("========tran id ["+tranId+"]");
							if(tranId == null || tranId.trim().length()==0)
							{
								errList.add("VTWIPTI1"); 
								errFields.add("tran_id");
								break ;
							}

							sql = "SELECT COUNT(1) FROM WIP_SCANNING_PS_HDR WHERE TRAN_ID = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,tranId);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								if(rs.getInt(1)==1)
								{
									//errString = itmdbAccessEJB.getErrorString("tran_id"," VTWIPTI", userId);
									errList.add("VTWIPTI"); 
									errFields.add("tran_id");
									System.out.println("EXTING TRAN ID : \t"+tranId);
									break ;
								}
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
						}
					}

					if (childNodeName.equalsIgnoreCase("lot_no"))
					{
						lotNo = getColumnValue("lot_no", dom);
						System.out.println(" +++++++++ In Save Method 2 +++++++++++++++++++");
						System.out.println("This is in wfValdata Case1 & LOT_NO is \t"+lotNo);
					}
					else if (childNodeName.equalsIgnoreCase("lot_sl"))
					{
						lotNo = getColumnValue("lot_no", dom);	
						lotSl = getColumnValue("lot_sl", dom);
						System.out.println("LOT NO "+lotNo);
						System.out.println("LOT SL "+lotSl);

						//VALIDATIN FOR EXISTING DATA IN WORKORDER TABLE AGAINST LOT NO AND LOT SL. 
						sql = 	"SELECT COUNT(1) FROM WORKORDER  WHERE  lot_no = ? and lot_sl = ?" ;
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,lotNo);
						pstmt.setString(2,lotSl);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							if(rs.getInt(1)==0)
							{
								errList.add("VTWIPLN1");
								errFields.add("lot_sl");
								System.out.println("WORK WORKORDER FEILD ");
							}
							else
							{
								sql = "SELECT WORK_ORDER,ITEM_CODE  FROM WORKORDER WHERE LOT_NO = ? AND LOT_SL = ? ";
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,lotNo);
								pstmt.setString(2,lotSl);
								rs = pstmt.executeQuery();
								System.out.println(" +++++++++ In Save Method 3 +++++++++++++++++++");
								if(rs.next())
								{
									System.out.println("All column value ");
									workOrd=rs.getString("WORK_ORDER");
									itemCode = rs.getString("ITEM_CODE");
								}								
							}
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						//
						sql = " SELECT MAX(OPERATION) FROM WIP_SCANNING_PS_HDR WHERE WORK_ORDER = ?";
						pstmt=conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							currOperation = rs.getString(1);
							System.out.println("current operation in wip scanning ps hdr table"+currOperation);
							System.out.println("current operation in wip scanning ps hdr table"+itemCode);
						}
						rs.close();
						pstmt.close();
						rs = null;
						pstmt = null;
						//GET MAX(OPERATION) FROM  WORDER_ROUTE
						sql = " SELECT MAX(OPERATION) FROM WORDER_ROUTE WR ,WORKORDER W1 WHERE WR.WORK_ORDER= W1.WORK_ORDER AND W1.WORK_ORDER = ?" ;
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							maxOperation = rs.getString(1);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						//VALIDATION FOR DUPLICATE BAR CODE AND COMPARE WITH  
						if(currOperation == maxOperation)
						{
						if(!("E".equalsIgnoreCase(editFlag)))
						{ 
							sql = "SELECT count(1) FROM WIP_SCANNING_PS_HDR  WHERE  LOT_NO = ? AND LOT_SL = ? ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,lotNo);
							pstmt.setString(2,lotSl);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								if(rs.getInt(1)>0)
								{
									errList.add("VTWIPSBC");
									errFields.add("lot_no");
									System.out.println("WORK WORKORDER FEILD ");
								}

							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;						
						}
					}
				
						//VALIDATION FOR CONFIRM WORK ORDER
						sql = "SELECT CONFIRMED FROM WORKORDER_ISS WHERE WORK_ORDER = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						System.out.println(" +++++++++ In Save Method 4 +++++++++++++++++++");
						while(rs.next())
						{
							conf=rs.getString("confirmed");
						}
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						if(conf =="Y")
						{ 
							errList.add("VTWIPCONF");
							errFields.add("confirmed");
							System.out.println("CONFIRM FIELD ");
						}
					}

					//VALIDATION FOR EXISTING DATA IN WORKORDER_ROUTE TABLE
					if (childNodeName.equalsIgnoreCase("mc_code")  )
					{
						mcCode = genericUtility.getColumnValue("mc_code", dom);
						System.out.println(" +++++++++ In Save Method 5 +++++++++++++++++++");
						sql = "SELECT COUNT(*) FROM WORDER_ROUTE WHERE WORK_ORDER = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							if(rs.getInt(1)==0)
							{
								errList.add("VTWIPMC"); 
								errFields.add("MC_CODE");
								System.out.println("INVALID MACHINE CODE : \t"+mcCode);
								break ;
							}
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
					}

					//VALIDATION FOR EXISTING DATA IN ITEM TABLE 
					if(childNodeName.equalsIgnoreCase("ITEM_CODE") )
					{
						System.out.println(" +++++++++ In Save Method 6 +++++++++++++++++++");
						sql = "SELECT COUNT(1) FROM ITEM WHERE ITEM_CODE = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,itemCode);
						rs = pstmt.executeQuery();
						if(rs.next())
						{	
							if(rs.getInt(1)==0)
							{
								errList.add("VTWIITM");
								errFields.add("ITEM_CODE");
								System.out.println("ITEM TABLE FIELD ");
								break ;
							}
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
					}				

					//VALIDATION FOR EXISTING DATA IN PROCESS TABLE
					if(childNodeName.equalsIgnoreCase("PROC_CODE") )
					{
						System.out.println("+++++++++++++=  IN SAVE METHOD  7 ++++++++++++++++=");
						procCode = genericUtility.getColumnValue("proc_code", dom);
						System.out.println("proc code"+procCode);
						sql = 	" SELECT COUNT(1) FROM PROCESS WHERE PROC_CODE = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,procCode);
						rs = pstmt.executeQuery();
						if(rs.next())
						{	
							if(rs.getInt(1)==0)
							{
								errList.add("VTWIPPC");
								errFields.add("PROC_CODE");
								System.out.println("PROCESS CODE----- >>> ");
								break ;
							}
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
					}				

					//VALIDATION FOR EXISTING DATA IN WORKORDER_FEEDBACK TABLE
					if(childNodeName.equalsIgnoreCase("WAIT_TIME") )
					{
						System.out.println("++++++++=IN SAVE METHOD   8 +++++++++++++=");
						 waitTime = genericUtility.getColumnValue("wait_time", dom);
						sql ="SELECT COUNT(1) FROM WORKORDER_FEEDBACK WF,WORKORDER W1 WHERE WF.WORK_ORDER = W1.WORK_ORDER AND W1.WORK_ORDER = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next()) 
						{	
							if(rs.getInt(1)==0)
							{
								errList.add("VTWIPWT");
								errFields.add("WAIT_TIME");
								System.out.println("WORK ORDER FEEDBACK TABLE FEILD ");

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

					//VALIDATION FOR QC APPROVAL REJECT
					if (childNodeName.equalsIgnoreCase("qc_approval_reject"))
					{
						System.out.println(" +++++++++ In Save Method 9 +++++++++++++++++++");
						qcApprovalReject = genericUtility.getColumnValue("qc_approval_reject", dom);
						System.out.println("This is in wfValdata Case1 & Emp_id is \t"+qcApprovalReject);
						if ( qcApprovalReject == null || qcApprovalReject.trim().length() == 0 )
						{
							errList.add("VTWIPQCAR1"); 
							errFields.add("qc_approval_reject");
							System.out.println("Qc approval validation : \t"+qcApprovalReject);
							break ;
						}							 
					}

					//VALIDATION FOR REASON CODE
					if (childNodeName.equalsIgnoreCase("reason_code"))
					{
						// qcApprovalReject = genericUtility.getColumnValue("qc_approval_reject", dom);

						if(!("A".equalsIgnoreCase(qcApprovalReject)))
						{
							System.out.println(" +++++++++ In Save Method 10 +++++++++++++++++++");
							reasonCode = genericUtility.getColumnValue("reason_code", dom);
							System.out.println("______ This is in wfValdata Case1 &qcApprovalReject is \t"+reasonCode);
							if ( reasonCode == null || reasonCode.trim().length() == 0 )
							{
								errList.add("VTWIPRC1"); 
								errFields.add("reason_code");
								System.out.println("reson code validation: \t"+reasonCode);
								break ;
							}	
						}
					}
					if (childNodeName.equalsIgnoreCase("remarks"))
					{
						System.out.println(" +++++++++ In Save Method 11 +++++++++++++++++++");
						remarks = genericUtility.getColumnValue("remarks", dom);
						System.out.println("This is in wfValdata Case1 & Emp_id is \t"+remarks);
						if ( remarks == null || remarks.trim().length() == 0 )
						{
							//errString = itmdbAccessEJB.getErrorString(" "," VTWIPRM1", "");
							errList.add("VTWIPRM1"); 
							errFields.add("remarks");
							System.out.println("Remarks null : \t"+remarks);
							break ;
						}							
					}			
				}

				int errListSize=errList.size();
				if(errListSize>0)
				{
					for(int cnt=0;cnt<errListSize;cnt++)
					{
						errCode=errList.get(cnt);
						errFieldName=errFields.get(cnt);
						errString=getErrorString(errFieldName, errCode, userId);
						errorType =  errorType( conn , errCode );

						if ( errString.length() > 0)
						{
							String bifurErrString = errString.substring( errString.indexOf("<Errors>") + 8,errString.indexOf("<trace>"));
							bifurErrString = bifurErrString;
							bifurErrString = bifurErrString+errString.substring( errString.indexOf("</trace>") + 8,errString.indexOf("</Errors>"));
							errStringXml.append(bifurErrString);
							errString = "";
						}

						if ( errorType.equalsIgnoreCase("E"))
						{
							break;
						}
					}
					errList.clear();
					errList = null;
					errFields.clear();
					errFields = null;
					errStringXml.append("</Errors></Root>\r\n");
				}else
				{
					errStringXml = new StringBuffer( "" );

				}

			}
		}
		catch(Exception e)
		{
			System.out.println(e);
			e.printStackTrace();
		}
		finally
		{

			try
			{
				if(conn!=null)
				{
					conn.close();
				}
				conn=null;
			} catch (Exception ex) {
				Logger.getLogger(WIPScanPvSolar.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		errString = errStringXml.toString();
		return errString;
	}

	public String itemChanged(String xmlString, String xmlString1,String objContext,String currentColumn,String editFlag,String xtraParams)
	{
		System.out.println(" $$ xmlString ::"+xmlString);
		System.out.println(" $$ xmlString2 ::"+xmlString1);

		Document dom=null;
		Document dom1=null;
		String valueXmlString="";
		try
		{
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1);
			valueXmlString=itemChanged(dom, dom1, objContext, currentColumn, editFlag, xtraParams);
		}catch(Exception e)
		{
			System.out.println(e);
		}
		return valueXmlString;
	}

	public String itemChanged(Document dom,Document dom1,String objContext,String currentColumn,String editFlag,String xtraParams)
	{
		NodeList parentNodeList=null;
		NodeList childNodeList=null;
		Node parentNode=null;
		Node childNode=null;
		String childNodeName=null;
		int childNodeListLength=0;
		ConnDriver connDriver=new ConnDriver();
		Connection conn=null;
		StringBuffer valueXmlString=null;
		PreparedStatement pstmt=null,pstmtInner = null;
		ResultSet rs=null,rsInner = null;
		int currentFormNo=0,prevOperation = 0, currOperation=0;
		String lotNo= "",lotSl= "",procCode = "",procDescr = "",prodDescr = "",prevOperation1 = "",qcApprovalReject = "",reasonDescr = "",userId = "",reasonCode = "",prevOperationStr = "",workOrd = "",itemCode = "",mcCode= "",siteCode = "";                //LOT NO FOR SCANING BAR CODE
		int ctr;
		String columnValue = "";
		GenericUtility genutility=null;
		String runStartTime = "" ;       //SCANNING TIME
		Date runEndTime  = null;         //RUN END TIME FROM WORKORDER_FEEDBACK TABLE  
		Double waitTime = 0.0;           //FROM WORKORDER_FEEDBACK TABLE
		double totalRunTime = 0.0 ;        //DEFRENCE BETWEEN START TIME AND END TIME
		Double cummulRunTime  = 0.0;        //TOTAL RUN TIME
		String errString="";
		String sql = "";//FLD VALUE FROM REASON CODE
		boolean flag = true;//FROM WIP_SCANNING_HDR  
		ArrayList <String> errList=new ArrayList<String>();
		ArrayList<String> errFields = new ArrayList<String>();
		try
		{
			genutility=GenericUtility.getInstance();
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver=null;
			siteCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");	
			userId=getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			System.out.println("^^^^^^^^^SITE CODE^^^^^^^^^^^^^"+siteCode);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat(genericUtility.getApplDateTimeFormat());
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			switch(currentFormNo)
			{
			case 1 :
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				ITMDBAccessEJB itmdbAccessEJB = new ITMDBAccessEJB();
				ctr = 0;
				valueXmlString.append("<Detail>");
				System.out.println("+++++++++++++current form no++++++++++"+currentFormNo);
				childNodeListLength = childNodeList.getLength();
				do
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();
					if(childNodeName.equals(currentColumn))
					{
						if (childNode.getFirstChild() != null)
						{
							columnValue = childNode.getFirstChild().getNodeValue().trim();
						}
						System.out.println("in do while loop "+columnValue);
					}
					ctr++;
				}while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
				System.out.println("[" + currentColumn + "] ==> '" + columnValue + "'");
				if(currentColumn.trim().equalsIgnoreCase("itm_default"))	
				{
					qcApprovalReject = genericUtility.getColumnValue("qc_approval_reject",dom);
					valueXmlString.append("<tran_date>").append("<![CDATA["+simpleDateFormat.format(new Date())+"]]>").append("</tran_date>");
					valueXmlString.append("<confirmed><![CDATA[").append("N").append("]]></confirmed >");
					valueXmlString.append("<reason_code protect=\"1\">").append("").append("</reason_code>");
				}
				if(currentColumn.trim().equalsIgnoreCase("lot_no") || currentColumn.trim().equalsIgnoreCase("lot_SL"))
				{
					lotNo = genericUtility.getColumnValue("lot_no",dom);
					lotSl = genericUtility.getColumnValue("lot_sl",dom) ;
					System.out.println("Lot sl "+lotSl);
					sql = "select count(1) from workorder where lot_no = ? and lot_sl = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,lotNo);
					pstmt.setString(2,lotSl);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						if(rs.getInt(1)==0)
						{
							flag=false;
						}
					}
				}
				if(flag)
				{					
					if(currentColumn.trim().equalsIgnoreCase("lot_no"))
					{
						System.out.println("Current column["+currentColumn+"]");
						lotNo = genericUtility.getColumnValue("lot_no",dom);
						if(lotNo !=null && lotNo.trim().length() >0 )
						{
							lotNo = lotNo.toUpperCase();							
						}
						System.out.println("lot no  "+lotNo);
						//valueXmlString.append("<lot_no>").append("<![CDATA["+lotNo+"]]>").append("</lot_no>");
					}
					else if(currentColumn.trim().equalsIgnoreCase("lot_sl"))
					{
						System.out.println("Current column["+currentColumn+"]");
						lotSl = genericUtility.getColumnValue("lot_sl",dom) ;
						lotNo = genericUtility.getColumnValue("lot_no",dom) ;
						System.out.println("lot no "+lotNo);
						System.out.println("lot sl"+lotSl);
						//GET WORK_ORDER FROM WORKORDER TABLE
						sql = "SELECT WORK_ORDER,ITEM_CODE FROM WORKORDER  WHERE LOT_NO = ? AND LOT_SL = ? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,lotNo);
						pstmt.setString(2,lotSl);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							workOrd=rs.getString("WORK_ORDER")==null?"":rs.getString("WORK_ORDER");
							itemCode=rs.getString("ITEM_CODE")==null?"":rs.getString("ITEM_CODE");
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						//GET MAX OPERATION FROM WORKORDER ROUTE TABLE DEPENDING ON THE WORK ORDER NUMBER
						sql = 	" SELECT MAX(OPERATION) FROM WIP_SCANNING_PS_HDR  WHERE WORK_ORDER = ?" ;
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next()) 
						{
							prevOperation= Integer.parseInt(rs.getString(1)==null?"0":rs.getString(1)) ;
							System.out.println("previous operation["+prevOperation+" ]");
							System.out.println("get string ["+rs.getString(1)+"]");
							currOperation = prevOperation + 1;
							System.out.println("current operation ["+currOperation+"]");
													
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						//GET WAIT_TIME PROC_CODE FROM  WORDER_ROUTE TABLE
						sql = "SELECT WAIT_TIME, MC_CODE,PROC_CODE FROM WORDER_ROUTE WR ,WORKORDER W1 WHERE WR.WORK_ORDER= W1.WORK_ORDER AND W1.WORK_ORDER = ? ";
						pstmt = conn.prepareStatement(sql);
						prevOperationStr = Integer.toString(prevOperation);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next()) 
						{
							waitTime= (Double)rs.getDouble("wait_time")== null ?0.0:(Double)rs.getDouble("wait_time");
							procCode=rs.getString("proc_code")==null?" ":rs.getString("proc_code");
							mcCode=rs.getString("mc_code")==null?" ":rs.getString("mc_code");
							System.out.println("process code"+procCode);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						//GET PROCESS DESCRIPTION FROM PPROCESS TABLE
						sql = 	" SELECT DESCR FROM PROCESS WHERE PROC_CODE = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,procCode);
						rs = pstmt.executeQuery();
						if(rs.next()) 
						{						
							procDescr=rs.getString("descr")==null?" ":rs.getString("descr");

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

						//GET ITEM DESCR FROM ITEM TABLE								
						sql = "SELECT DESCR FROM ITEM WHERE ITEM_CODE = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,itemCode);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							prodDescr=rs.getString("DESCR")==null?" ":rs.getString("DESCR");
						}										
						rs.close();
						rs = null; 
						pstmt.close();
						pstmt = null;				
						//GET RUN_TIME_START,RUN_TIME_END FROM WORKORDER_FEEDBACK 
						sql ="SELECT RUN_TIME_START,RUN_TIME_END FROM WORKORDER_FEEDBACK WF,WORKORDER W1 WHERE WF.WORK_ORDER = W1.WORK_ORDER AND W1.WORK_ORDER = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							runEndTime=rs.getTimestamp("RUN_TIME_END");
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						//CALCULATING CUMULATIVE TIME FROM WORDER_ROUTE TABLE
						sql = "SELECT SUM(RUN_TIME) AS CUMULATIVE_TIME FROM WORDER_ROUTE WR,WORKORDER W1 WHERE WR.WORK_ORDER = W1.WORK_ORDER AND W1.WORK_ORDER = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,workOrd);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							cummulRunTime=(Double)rs.getDouble("cumulative_time")==null?0.0:(Double)rs.getDouble("cumulative_time");
							System.out.println("CUMULATIVE RUN TIME"+cummulRunTime);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						valueXmlString.append("<work_order>").append("<![CDATA[" +workOrd+ "]]>").append("</work_order>");
						valueXmlString.append("<lot_no>").append("<![CDATA[" + lotNo + "]]>").append("</lot_no>");
						valueXmlString.append("<lot_sl>").append("<![CDATA[" + lotSl + "]]>").append("</lot_sl>");
						valueXmlString.append("<product_code>").append("<![CDATA[" + itemCode+ "]]>").append("</product_code>");
						valueXmlString.append("<descr>").append("<![CDATA[" + prodDescr+ "]]>").append("</descr>");
						valueXmlString.append("<operation>").append("<![CDATA[" + currOperation  + "]]>").append("</operation>");
						valueXmlString.append("<proc_code>").append("<![CDATA[" +procCode + "]]>").append("</proc_code>");
						valueXmlString.append("<site_code>").append("<![CDATA[" + siteCode+ "]]>").append("</site_code>");
						valueXmlString.append("<process_descr>").append("<![CDATA[" + procDescr + "]]>").append("</process_descr>");
						valueXmlString.append("<wait_time>").append("<![CDATA[" + waitTime + "]]>").append("</wait_time>");
						valueXmlString.append("<mc_code>").append("<![CDATA[" + mcCode + "]]>").append("</mc_code>");
						valueXmlString.append("<run_start_time>").append("<![CDATA["+simpleDateTimeFormat.format(new Timestamp(System.currentTimeMillis()))+"]]>").append("</run_start_time>");
						if(runEndTime != null)
						{
							valueXmlString.append("<run_end_time>").append("<![CDATA["+ simpleDateTimeFormat.format(runEndTime) + "]]>").append("</run_end_time>");
						}
						else
						{
							valueXmlString.append("<run_end_time/>");
						}
						if(runEndTime != null)
						{
							double runTime = ((System.currentTimeMillis())-(runEndTime.getTime()))/3600000 ;
							totalRunTime = runTime ;
							valueXmlString.append("<total_run_time>").append("<![CDATA[" + totalRunTime+ "]]>").append("</total_run_time>");
						} 
						else
						{	
							valueXmlString.append("<total_run_time/>");
						}
						valueXmlString.append("<cumulativ_run_time>").append("<![CDATA[" + cummulRunTime + "]]>").append("</cumulativ_run_time>");
						valueXmlString.append("<gencodes_descr>").append("<![CDATA["+reasonDescr+"]]>").append("</gencodes_descr>");
					}

					if(currentColumn.trim().equalsIgnoreCase("qc_approval_reject"))
					{
						qcApprovalReject = genericUtility.getColumnValue("qc_approval_reject",dom);
						if("A".equals(qcApprovalReject) )
						{
							valueXmlString.append("<reason_code protect=\"1\">").append("").append("</reason_code>");
						}
						else
						{
							valueXmlString.append("<reason_code protect=\"0\">").append("").append("</reason_code>");
						}
					}

					if(currentColumn.trim().equalsIgnoreCase("reason_code"))
					{
						reasonCode = genericUtility.getColumnValue("reason_code",dom);
						if(reasonCode != null && reasonCode.length()== 0)
						{
							System.out.println("reason_code comming null in itemChanged event of case 1");
						}
						pstmt= conn.prepareStatement("SELECT DESCR FROM GENCODES WHERE fld_value = ? ");
						pstmt.setString(1,reasonCode);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							reasonDescr = rs.getString(1)==null?" ":rs.getString("DESCR");
						}
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						valueXmlString.append("<gencodes_descr>").append("<![CDATA["+reasonDescr+"]]>").append("</gencodes_descr>");					
					}
				}

				valueXmlString.append("</Detail>");
				break ;
			}//switch case end
			valueXmlString.append("</Root>");
		}//try end
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(conn!=null)
				{
					if(pstmt != null)
						pstmt.close();
					if(rs != null)
						rs.close();
					rs = null;
					pstmt = null;
					conn.close();
					conn = null;
				}

			}catch(Exception d)
			{
				d.printStackTrace();
			}
		}
		System.out.println("VALUE STRING"+valueXmlString.toString());
		return valueXmlString.toString();
	}
	private String errorType( Connection conn , String errorCode )
	{
		String msgType = "";
		System.out.println("IN ERROR METHOD");
		PreparedStatement pstmt = null ;
		ResultSet rs = null;
		try
		{
			String  sql = " SELECT MSG_TYPE FROM MESSAGES WHERE MSG_NO =   ? ";
			System.out.println("SQL IS EXECUTING");
			pstmt = conn.prepareStatement( sql );
			pstmt.setString( 1, errorCode );
			rs = pstmt.executeQuery();
			while( rs.next() )
			{
				msgType = rs.getString("MSG_TYPE");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try
			{
				if ( rs != null )
				{
					rs.close();
					rs = null;
				}
				if ( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
		}
		return msgType;
	}

}

