/*
 * 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 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 = ""; 
		Document dom = null;
		String userId="", errCode = "",resultString = "";
		//		finalStr = mergeXml(xmlString, xmlString1, xmlString2);
		try
		{
			//***ConnDriver connDriver = new ConnDriver();
			//***conn = connDriver.getConnectDB("DriverITM");
			//**connDriver = null;
			//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 );
			//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);
		 	genVal = null;
			//changed by msalam on 27/09/07 end
			if (errCode != null && errCode.trim().length() > 0) 
			{
				resultString = getErrorString("", errCode, userId);
			}
		}
		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;
	}
	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();
	}
}
