package ibase.webitm.ejb.adm;
//Created by Suraj [19/3/15] [To create a payarr_to_proc transaction][Starts]
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import javax.ejb.Stateless;
import org.w3c.dom.Document;
@Stateless
public class PayarrToProc extends ValidatorEJB implements PayarrToProcLocal,PayarrToProcRemote 
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	public String wfValData() throws RemoteException, ITMException 
	{
		return "";
	}

	public String itemChanged() throws RemoteException, ITMException
	{
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn,String editFlag, String xtraParams) throws RemoteException,ITMException 
	{
		System.out.println("Came Inside 1 itemChanged:::");
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String errFields = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		try 
		{
			System.out.println("xmlString [" + xmlString + "]");
			System.out.println("xmlString1 [" + xmlString1 + "]");
			System.out.println("xmlString2 [" + xmlString2 + "]");
			if (xmlString != null && xmlString.trim().length() != 0) 
			{
				dom = genericUtility.parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length() != 0)
			{
				dom1 = genericUtility.parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length() != 0) 
			{
				dom2 = genericUtility.parseString(xmlString2);
			}
			errFields = itemChanged(dom, dom1, dom2, objContext, currentColumn,editFlag, xtraParams);
			System.out.println("errFields :" + errFields);
		} 
		catch (Exception e) 
		{
			System.out.println("Exception :PayarrToProc :" + e.getMessage()+ ":");
			errFields = genericUtility.createErrorString(e);
		}		
		return errFields;
	}
	public String itemChanged(Document dom, Document dom1, Document dom2,String objContext, String currentColumn, String editFlag,String xtraParams) throws RemoteException, ITMException
	{
		String empCode="";
		String prdCode="";
		String status="";
		String prdCodeAcc="";
		String procDate="";
		String chgDate="";
		String chgUser="";
		String chgTerm="";
		String site="";
		String descr="";
		String procType="";
		String tranId="";		
		StringBuffer xmlString = new StringBuffer();		
		try
		{
			chgUser = checkNull(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "userId"));
			chgTerm = getValueFromXTRA_PARAMS(xtraParams, "termId");
			java.util.Date currDate = new java.util.Date();
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			chgDate = sdf.format(currDate);		
			int currentFormNo = 0;
			if (objContext != null && objContext.trim().length() > 0) 
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			xmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			xmlString.append(editFlag).append("</editFlag></header>");
			System.out.println("In Detail1");
			System.out.println("Current Form No [" + currentFormNo + "]");			
			System.out.println("xtraparam is [" + xtraParams + "]");						
			switch ( currentFormNo )
			{					
				case 1:
				{
				xmlString.append("<Detail1 domID='1' dbID=''>\r\n");					
				if (currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
				{
					empCode=checkNull(genericUtility.getColumnValue("emp_code", dom1));
					System.out.println("Emp_Code: ["+empCode+"]");
					site=checkNull(genericUtility.getColumnValue("site", dom1));
					System.out.println("Site_Code: ["+site+"]");
					descr=checkNull(genericUtility.getColumnValue("descr", dom1));
					System.out.println("Description: ["+descr+"]");
					prdCode=checkNull(genericUtility.getColumnValue("prd_code", dom1));
					System.out.println("Prd_Code: ["+prdCode+"]");
					status=checkNull(genericUtility.getColumnValue("status", dom1));
					System.out.println("Status: ["+status+"]");
					prdCodeAcc=checkNull(genericUtility.getColumnValue("prd_code__acc", dom1));
					System.out.println("prd_code__acc: ["+prdCodeAcc+"]");
					procDate=checkNull(genericUtility.getColumnValue("proc_date", dom1));
					System.out.println("proc_Date: ["+procDate+"]");
					procType=checkNull(genericUtility.getColumnValue("proc_type", dom1));
					System.out.println("proc_type: ["+procType+"]");
					tranId=checkNull(genericUtility.getColumnValue("tran_id", dom1));
					System.out.println("tran_id: ["+tranId+"]");
					
					System.out.println("Current column is[" + currentColumn + "]");
					
					xmlString.append("<chg_date><![CDATA[").append(checkNull(chgDate)).append("]]></chg_date>\r\n");
					xmlString.append("<chg_user><![CDATA[").append(checkNull(chgUser)).append("]]></chg_user>\r\n");
					xmlString.append("<chg_term><![CDATA[").append(checkNull(chgTerm)).append("]]></chg_term>\r\n");
					if((prdCodeAcc != null && prdCodeAcc.trim().length()>0 ) && status.trim().equalsIgnoreCase("P") )
					{
						System.out.println("IN IF CONDITION::::::::::");
						xmlString.append("<status protect=\"1\"><![CDATA[").append(checkNull(status)).append("]]></status>\r\n");
					}
					else
					{
						xmlString.append("<status protect=\"0\"><![CDATA[").append(checkNull(status)).append("]]></status>\r\n");
						System.out.println("IN ELSE CONDITION::::::::::");
					}
				}				
				xmlString.append("</Detail1>\r\n");
				}
			}
		}
		catch (Exception ex) 
		{
			System.out.println("1st catch Exception::" + ex.getMessage());
			ex.printStackTrace();
		} 		
		System.out.println("value of xmlString::::" + xmlString);
		xmlString.append("</Root>\r\n");
		System.out.println("xmlString....[" + xmlString.toString());
		return xmlString.toString();
	}

		
	private String checkNull(String input) 
	{
		if (input == null)
		{
			input = "";
		} else 
		{
			input = input.trim();
		}
		return input;
	}
	}
//Created by Suraj [20/3/15] [To create a payarr_to_proc transaction][Ends]				

			
