package ibase.webitm.ejb.adm;

import java.rmi.RemoteException;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.ejb.*;
import org.w3c.dom.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class WeekOffCreation extends ValidatorEJB implements WeekOffCreationLocal,WeekOffCreationRemote //SessionBean
{
	/*
	public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("Create Method Called...");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String itemChanged() throws RemoteException,ITMException
	{
		System.out.println("itemChanged Called.............");
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{

		Document currentDom = null;
		Document headerDom = null;
		Document detailDom = null;
		String valueXmlString = "";
		System.out.println("itemChanged() Of WeekOffCreationEJB Called .............!");
		try
		{
			currentDom	= parseString(xmlString);
			headerDom	= parseString(xmlString1);
			if (xmlString2.trim().length() > 0 )
			{
				detailDom = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(currentDom,headerDom,detailDom,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [SundrybalOpenEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
			e.printStackTrace();
		}
        return valueXmlString; 
	}
	public String itemChanged(Document currentDom, Document headerDom,Document detailDom, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn = null;
		String fromDate = "", toDate = "", rotate = "";
		int currentYear = 0, currentFormNo = 0;
		java.util.Date  currentDate = null, date1 = null, date2 = null;

		GenericUtility genericUtility = GenericUtility.getInstance();
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>");
		
		try
		{
			conn = getConnection();//Gets a Connection
			System.out.println("[WeekOffCreationEJB] [itemChanged] :objContext ....." +objContext);
			valueXmlString.append("<Root>");
			valueXmlString.append("<header>");
			valueXmlString.append("<editFlag>").append(editFlag).append("</editFlag>");
			valueXmlString.append("</header>");
			valueXmlString.append("<Detail>");		
			if (currentColumn.trim().equals("itm_default"))
			{
				currentDate = new java.util.Date(System.currentTimeMillis());
				Calendar calObj = Calendar.getInstance();
				SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
				calObj.setTime(currentDate);
				currentYear = calObj.get(calObj.YEAR);
				System.out.println("currentYear...:: "+ currentYear);
				calObj.set(currentYear,0,1);
				date1 = calObj.getTime();
				fromDate = sdf.format(date1);
				calObj.set(currentYear,11,31);
				date2 = calObj.getTime();
				toDate = sdf.format(date2);
				System.out.println("fromDate...:: "+ fromDate);
				System.out.println("toDate...:: "+ toDate);
				valueXmlString.append("<from_date>").append(fromDate).append("</from_date>");
				valueXmlString.append("<to_date>").append(toDate).append("</to_date>");
			}
			if (currentColumn.trim().equals("rotate"))
			{
				rotate = genericUtility.getColumnValue("rotate",currentDom);
				if (rotate.equals("Y"))
				{
					valueXmlString.append("<off_for protect = '1'>").append(rotate).append("</off_for>");
				}
				else
				{
					valueXmlString.append("<off_for protect = '0'>").append("</off_for>");
				}
			}
			valueXmlString.append("</Detail>");
			valueXmlString.append("</Root>");
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection...........");
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception in Closing Connection :: " + e);
			}
		}
		return valueXmlString.toString();
	}
}