package ibase.webitm.ejb.mfg;

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

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;

import javax.ejb.Stateless;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



@Stateless
public class WordCampVal  extends ValidatorEJB implements WordCampValLocal,WordCampValRemote
{

	GenericUtility genericUtility = GenericUtility.getInstance(); 
	public String wfValData(String xmlString, String xmlString1, String xmlString2,String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("------------@ wfvalData method called-----------------");
		System.out.println("Xml String : ["+xmlString+"]");
		String errString = null;
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		System.out.println("below genericUtility--------------->>>>>>>>>");
		try
		{
			
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString); 
				System.out.println("xmlString d" + xmlString);
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genericUtility.parseString(xmlString1); 
				System.out.println("xmlString1 f" + xmlString1);
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genericUtility.parseString(xmlString2); 
				System.out.println("xmlString2 f" + xmlString2);
			}
			
 
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
			
		}
		catch(Exception e)
		{
			System.out.println("Exception : budgetgroup.java : wfValData(String xmlString) : ==>\n"+e.getMessage());
			throw new ITMException(e);
		}
		
		return errString;
	} //end of wfValData 
	
	
	 public String wfValData(Document dom, Document dom1, Document dom2,String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
		{
		
		
		  
		  	NodeList parentNodeList = null;
			NodeList childNodeList = null;
			Node parentNode = null;
			Node childNode = null;
			int ctr=0;
			String childNodeName = null;
			String errString = "";
			Connection conn = null;
			PreparedStatement pstmt = null ;
			ResultSet rs = null;
			
			
			int currentFormNo=0;
			int childNodeListLength;
			ConnDriver connDriver = new ConnDriver();
		    String fromDateStr = null;
			String toDateStr = null;
			Timestamp fromDate = null;
			Timestamp toDate = null;
			String userId="",errCode="",locgrp ="";
			GenericUtility genericUtility = GenericUtility.getInstance();
		   
	        try
			   {
				conn = connDriver.getConnectDB("DriverITM");
				connDriver = null;
				userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
				System.out.println("user ID form XtraParam : "+userId);
				
			
				if(objContext != null && objContext.trim().length()>0)
				{
					currentFormNo = Integer.parseInt(objContext);
				}
				switch(currentFormNo)
				{
					case 1 :
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
					
					 if (childNodeName.equalsIgnoreCase("date_from"))
						{
							 fromDateStr = genericUtility.getColumnValue( "date_from", dom );
							 System.out.println("dfhfhfghfgvfvb hjkddfgrrg"+fromDateStr);
							  toDateStr = genericUtility.getColumnValue( "date_to", dom );
							  System.out.println("dfhfhfghfgvfvb hdssdswererjkddfgrrg"+toDateStr);
							 
							 
							//order_date__fr = genericUtility.getColumnValue("order_date__fr",dom);
							
							System.out.println("dates here"+fromDate+""+toDate);
									System.out.println("in date validation");	
									
									if (fromDateStr == null || fromDateStr.trim().length() == 0 )
									{
										errCode = "VTWOCPRCFR";
										errString = getErrorString("date_from",errCode,userId);
										break;
									}else{
									
										if (fromDateStr != null || fromDateStr.trim().length() > 0 )
										{
											
											if (toDateStr != null)
											{
												
												fromDateStr = genericUtility.getValidDateString( fromDateStr, genericUtility.getApplDateFormat(), genericUtility.getDBDateFormat() );
											     fromDate = java.sql.Timestamp.valueOf( fromDateStr + " 00:00:00.0" );
												
											     
											     toDateStr = genericUtility.getValidDateString( toDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
												 toDate =java.sql.Timestamp.valueOf( toDateStr + " 00:00:00.0");
								        		System.out.println("fromDate is after toDate");
												
										if(fromDate.compareTo(toDate)>0)
										{
											
										    System.out.println("order_date__fr main validation fire");
											errCode = "VTWOCPRCFN";
											errString = getErrorString("date_from",errCode,userId);
											break;
										}
							
									}
									}
						}
						 }
						
						else if (childNodeName.equalsIgnoreCase("date_to"))
						{
							 toDateStr = genericUtility.getColumnValue( "date_to", dom );
							
							 
							 System.out.println("dfhfhfghfgvfvdvfvb hjk"+toDateStr);
							 if (toDateStr == null || toDateStr.trim().length() == 0 )
								{
									errCode = "VTWOCPRCTO";
									errString = getErrorString("date_to",errCode,userId);
									break;
								}
					
						}
						
						} 
					
					
						break;
						default:
						
				} //end switch
			
			} //end try
			catch(Exception e)
			{
				System.out.println("Exception ::"+e);
				e.printStackTrace();
				errString=e.getMessage();
	            throw new ITMException(e);
			}
			finally
			{
				try
				{
					if(conn!=null)
					{
						if(rs != null )
						{
							rs.close();
							rs = null;
						}
						if(pstmt != null )pstmt.close();
						pstmt =null;
						conn.close();
					}
					conn = null;
				}
				catch(Exception d)
				{
				  d.printStackTrace();
				}
			}
			
			System.out.println("ErrString ::[ "+errString+" ]");
			return errString;
		
		
		
		}

	 
}

