/*
 * Created on Feb 5, 2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ibase.webitm.ejb.sys;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import org.w3c.dom.Document;

/**
 * @author ashah
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

 import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class GenValidate extends ValidatorEJB implements  GenValidateLocal , GenValidateRemote // SessionBean{
{

	
	//Connection conn = null;
	//String sql = null;
	//PreparedStatement pstmt = null;
	//ResultSet rs = null;
	
		
  /*	public void ejbCreate() throws RemoteException, CreateException 
	{
		//System.out.println("GenValidateEJB Created...");
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

	public void setSessionContext(SessionContext arg0) 
	{
	}
	
	public String wfValData(String xmlString, String xmlString1, String xmlString2, String objContext, String winName, String xtraParams) throws RemoteException
	{
		String finalStr = ""; 
		String errFldName="";
		Document dom = null;
		String userId="", errCode = "",resultString = "";
		String errString = "",errorType="";
		HashMap<String , String > errorMap= new HashMap<String, String>();
		StringBuffer errStringXml = new StringBuffer("<?xml version = \"1.0\"?> \r\n <Root> <Errors>");
		//		finalStr = mergeXml(xmlString, xmlString1, xmlString2);
		try
		{
			//***ConnDriver connDriver = new ConnDriver();
			//***conn = connDriver.getConnectDB("DriverITM");
			//**connDriver = null;
			System.out.println("Calling wfValData**********************************");
			System.out.println("xmlString2 : " + xmlString2);
			dom = parseString(xmlString2);
			userId = getValueFromXTRA_PARAMS(xtraParams, "loginCode");
			//changed by msalam on 27/09/07 start
			//for passing current focused Row from xtraParams
			String focusedRow = null;
			focusedRow = getValueFromXTRA_PARAMS(xtraParams, "focusRow");
			System.out.println( "winName :: " + winName );
			System.out.println( "objContext :: " + objContext );
			System.out.println( "focusedRow :: " + focusedRow );
			//System.out.println( "xmlString2 :: " + xmlString2 ); comment by kunal on 17/07/13 ,for remove Out Of Memory Error   
			//errCode = genVal.genVal(winName,objContext,dom,conn);
			//****errCode = genVal.genVal(winName,objContext,dom, focusedRow, conn);
			GenVal genVal = new GenVal();
			//errCode = genVal.genVal(winName,objContext,dom, focusedRow);//comment added by sagar on 25/12/14
			errorMap = genVal.genVal(winName,objContext,dom, focusedRow);//code added by sagar on 25/12/14
		 	genVal = null;
			//changed by msalam on 27/09/07 end
			/*if (errCode != null && errCode.trim().length() > 0) 
			{
				resultString = getErrorString("", errCode, userId);
			}*/
		 	//code added by sagar on 25/12/14 Start....
			System.out.println(">>>>>>>>Return errorMap in GenValidate:"+errorMap);
			if(!errorMap.isEmpty())
			{
				System.out.println(">>>>>>>>Return Non empty errorMap in GenValidate:"+errorMap.size());
				Iterator iterator = errorMap.entrySet().iterator();
				while (iterator.hasNext()) 
				{
					Map.Entry mapEntry = (Map.Entry) iterator.next();
					//errFldName= (String) mapEntry.getKey();
					//errCode= (String) mapEntry.getValue();
					errCode= (String) mapEntry.getKey();
					errFldName= (String) mapEntry.getValue();
					System.out.println(">>>>>>>>errFldName:"+errFldName);
					System.out.println(">>>>>>>>errCode:"+errCode);
					errString = getErrorString(errFldName, errCode, userId);
					//errorType = errorType(conn, errCode);
					if (errString.length() > 0)
					{
						String bifurErrString = errString.substring(errString.indexOf("<Errors>") + 8, errString.indexOf("<trace>"));
						bifurErrString = bifurErrString + errString.substring(errString.indexOf("</trace>") + 8, errString.indexOf("</Errors>"));
						errStringXml.append(bifurErrString);
						errString = "";
					}
				}
				errStringXml.append("</Errors> </Root> \r\n");
			}
			else
			{
				errStringXml = new StringBuffer("");
			}
			//code added by sagar on 25/12/14 end.
		}
		catch (RemoteException e1) {
			e1.printStackTrace();
		} 
		catch (ITMException e1) {
			e1.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		/*finally
		{
			try
			{
				if (conn != null)
				{	conn.close();
					conn = null;
				}
			}
			catch(Exception ex)
			{ex.printStackTrace();}
			
		 }*/

		//return resultString;//comment added by sagar on 25/12/14
		errString = errStringXml.toString(); //code added by sagar on 25/12/14
		return errString;
	}
	
	
	//added by priyanka on 31/12/14 to execute genValidation based o form no on accept button 
	public String wfValData1(String xmlString, String xmlString1, String xmlString2, String objContext, String winName, String xtraParams) throws RemoteException
	{
		String finalStr = ""; 
		String errFldName="";
		Document dom = null;
		String userId="", errCode = "",resultString = "";
		String errString = "",errorType="";
		HashMap<String , String > errorMap= new HashMap<String, String>();
		StringBuffer errStringXml = new StringBuffer("<?xml version = \"1.0\"?> \r\n <Root> <Errors>");
		//		finalStr = mergeXml(xmlString, xmlString1, xmlString2);
		try
		{
			//***ConnDriver connDriver = new ConnDriver();
			//***conn = connDriver.getConnectDB("DriverITM");
			//**connDriver = null;
			System.out.println("Calling wfValData**********************************");
			System.out.println("xmlString2 : " + xmlString2);
			dom = parseString(xmlString2);
			userId = getValueFromXTRA_PARAMS(xtraParams, "loginCode");
			//changed by msalam on 27/09/07 start
			//for passing current focused Row from xtraParams
			String focusedRow = null;
			//focusedRow = getValueFromXTRA_PARAMS(xtraParams, "focusRow");
			focusedRow=objContext;//added by priyanka
			System.out.println( "winName :: " + winName );
			System.out.println( "objContext :: " + objContext );
			System.out.println( "focusedRow :: " + focusedRow );
			//System.out.println( "xmlString2 :: " + xmlString2 ); comment by kunal on 17/07/13 ,for remove Out Of Memory Error   
			//errCode = genVal.genVal(winName,objContext,dom,conn);
			//****errCode = genVal.genVal(winName,objContext,dom, focusedRow, conn);
			GenVal genVal = new GenVal();
			//errCode = genVal.genVal(winName,objContext,dom, focusedRow);//comment added by sagar on 25/12/14
			errorMap = genVal.genVal(winName,objContext,dom, focusedRow,true);//code added by sagar on 25/12/14
		 	genVal = null;
			//changed by msalam on 27/09/07 end
			/*if (errCode != null && errCode.trim().length() > 0) 
			{
				resultString = getErrorString("", errCode, userId);
			}*/
		 	//code added by sagar on 25/12/14 Start....
			System.out.println(">>>>>>>>Return errorMap in GenValidate:"+errorMap);
			if(!errorMap.isEmpty())
			{
				System.out.println(">>>>>>>>Return Non empty errorMap in GenValidate:"+errorMap.size());
				Iterator iterator = errorMap.entrySet().iterator();
				while (iterator.hasNext()) 
				{
					Map.Entry mapEntry = (Map.Entry) iterator.next();
					//errFldName= (String) mapEntry.getKey();
					//errCode= (String) mapEntry.getValue();
					errCode= (String) mapEntry.getKey();
					errFldName= (String) mapEntry.getValue();
					System.out.println(">>>>>>>>errFldName:"+errFldName);
					System.out.println(">>>>>>>>errCode:"+errCode);
					errString = getErrorString(errFldName, errCode, userId);
					//errorType = errorType(conn, errCode);
					if (errString.length() > 0)
					{
						String bifurErrString = errString.substring(errString.indexOf("<Errors>") + 8, errString.indexOf("<trace>"));
						bifurErrString = bifurErrString + errString.substring(errString.indexOf("</trace>") + 8, errString.indexOf("</Errors>"));
						errStringXml.append(bifurErrString);
						errString = "";
					}
				}
				errStringXml.append("</Errors> </Root> \r\n");
			}
			else
			{
				errStringXml = new StringBuffer("");
			}
			//code added by sagar on 25/12/14 end.
		}
		catch (RemoteException e1) {
			e1.printStackTrace();
		} 
		catch (ITMException e1) {
			e1.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		/*finally
		{
			try
			{
				if (conn != null)
				{	conn.close();
					conn = null;
				}
			}
			catch(Exception ex)
			{ex.printStackTrace();}
			
		 }*/

		//return resultString;//comment added by sagar on 25/12/14
		errString = errStringXml.toString(); //code added by sagar on 25/12/14
		return errString;
	}
	

	private String mergeXml(String xmlString, String xmlString1, String xmlString2)
	{
		StringBuffer data = new StringBuffer();
		StringBuffer compData = new StringBuffer();
		String temp = "";
		
//		System.out.println("xmlString : " + xmlString);
//		System.out.println("xmlString1 : " + xmlString1);
//		System.out.println("xmlString2 : " + xmlString2);
			try{
				compData.append("<DocumentRoot><description>Datawindow Root</description>");
				compData.append("<group0><description>Group0 description</description>");
				compData.append("<Header0><description>Header0 members</description>");
				compData.append(xmlString);
				data.append(xmlString1);
//				if (data.indexOf("<Detail2") > 0)
//				{
					temp = data.substring(data.indexOf("<Detail"),data.indexOf("</Header0>"));
					compData.append(temp);
//				}
				data.replace(0,data.length(),xmlString2);
//				if (data.indexOf("<Detail3") > 0)
//				{
					temp = data.substring(data.indexOf("<Detail"),data.indexOf("</Header0>"));
					compData.append(temp);
//				}
				compData.append("</Header0></group0></DocumentRoot>");
			}
			catch(Exception e)
			{
				//System.out.println("In mergeXml ::: " + e.getMessage());
				e.printStackTrace();
			}

		//System.out.println("compData ::::" + compData.toString());
		return compData.toString();
	}
}
