package ibase.webitm.ejb.adm;

import java.rmi.RemoteException;
import java.util.*;
import org.w3c.dom.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import java.sql.*;
// Changed by Dhiraj 07/10/10 [AD01SUN008]
import java.text.SimpleDateFormat;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class ShiftPattern extends ValidatorEJB implements ShiftPatternLocal,ShiftPatternRemote //SessionBean
{
	String  returnValue12 = "";
	
public String wfValData() throws RemoteException,ITMException
	{
		return "";
	}
    
	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;		
		String errString = "";
		System.out.println("xmlString : "  + xmlString);
		System.out.println("xmlString1 : "  + xmlString1);
		System.out.println("xmlString2 : "  + xmlString2);
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			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); 
			}
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
			System.out.println("ErrString :" + errString);
		}
		catch(Exception e)
		{
			System.out.println("Exception :ShiftPatternEJB :wfValData(String xmlString):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("returning from ShiftPatternEJB wfValData");
		return (errString);
	}
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		boolean checkNextCol = true;
		//String count = "";
		String dayslist2 = "";
		ArrayList dayslist = new ArrayList();
		String columnName = "";
		String columnValue = "";
		String columnValue1 = "";
		String userId = "";
		String sql = "";
		String sql2 = "";
		String errString = "";
		Connection connectionObject = null;
		Statement stmt = null;
		Statement pstmt = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String returnValue = "";
		String returnValue2 = "";
		java.util.Date returnDate = null;
		//Date columnDate = null;
		//SimpleDateFormat columnDate1 = null;
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		String shiftPattern = null;
		//String dayslist = null;
		Statement st = null;
		Connection conn = null;
		String site_code = "";
		int ctr ;
		//int ctr1 ;
		int count = 0;
		int noOfChilds = 0;
		int currentFormNo = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
	    ConnDriver connDriver = new ConnDriver();
		ITMDBAccessLocal itmDBAccessLocal = null;
		//ITMDBAccess itmDBAccess = null;//commented for ejb3
		ITMDBAccessEJB itmDBAccess = null; // added for ejb3
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			// Changed by Dhiraj 07/10/10 [AD01SUN008]. start
			/*AppConnectParm appConnect = new AppConnectParm();
			Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			itmDBAccessLocal = (ITMDBAccessLocal)ctx.lookup("ITMDBAccess");
			*/
			// Changed by Dhiraj 07/10/10 [AD01SUN008]. end
			//itmDBAccess = itmDBAccessHome.create();

			// Changed by Dhiraj 07/10/10 [AD01SUN008] start
			/*connectionObject = itmDBAccessLocal.getConnection(); //added for ejb3
			  stmt = connectionObject.createStatement();
			*/
			stmt = conn.createStatement();
			// Changed by Dhiraj 07/10/10 [AD01SUN008] end
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			parentList = dom.getElementsByTagName("Detail" + currentFormNo);
			parentNode = parentList.item(0);
			childList = parentNode.getChildNodes();
			noOfChilds = childList.getLength();
			for(ctr = 0; ctr < noOfChilds; ctr++)
			{
				childNode = childList.item(ctr);
				childNodeName = childNode.getNodeName();
				if (childNodeName.equals("shift_pattern"))
				{
					if(childNode.getFirstChild() == null)
					{
						//Changed by Dhiraj 07/10/10 [AD01SUN008]
						//errString = itmDBAccess.getErrorString("shift_pattern","VTSPCANT",userId,errString,connectionObject); 
						errString = getErrorString("shift_pattern","VTSPCANT",userId);
					} 
					else
					{
						shiftPattern = childNode.getFirstChild().getNodeValue();
						System.out.println("shiftPattern :---------------------"+shiftPattern);
						
						dayslist = GenericUtility.getInstance().getTokenList(shiftPattern, ";");
						System.out.println("--------------------dayslist="+dayslist.size());
						for(int ctr1 = 0; ctr1 < dayslist.size()-1; ctr1++)
						{	
							String strDaysList =dayslist.get(ctr1).toString();
							int index = strDaysList.indexOf('-');
							dayslist2 = strDaysList.substring(index+1,strDaysList.length());
							System.out.println(dayslist2);
							site_code = getColumnValue("site_code",dom);
							System.out.println(site_code);
							sql =	"select count(*) from workshft "
									+"where shift = '"+dayslist2+"' "
									+"And site_code = '"+site_code+"' " ;
							System.out.println("sql :"+sql);
							st = conn.createStatement();
							rs = st.executeQuery(sql);
							if(rs.next())
							{
								 count = rs.getInt(1);
							}
							System.out.println("count :---------------------"+count);
							if (count == 0)
							{
								//Changed by Dhiraj 07/10/10 [AD01SUN008]
								//errString = itmDBAccess.getErrorString("shift_pattern","VTWSNOTM",userId,errString,connectionObject);
								errString = getErrorString("shift_pattern","VTWSNOTM",userId);


							}

						}

					}
				}
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :ShiftPatternEJB :wfValData(Document dom):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("error :"+errString);
		return (errString);
	}
//item change logic
	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 
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;

		String valueXmlString = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			System.out.println("xmlString :"+xmlString+ "\n xmlString1 :"+xmlString1+ " \nxmlString2 :"+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);
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :ShiftPatternItmChngEJB :itemChanged(String,String) :==>\n"+e.getMessage());
			valueXmlString = genericUtility.createErrorString(e);			
		}
		return valueXmlString.toString(); 
	}
	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		char days ;
		int shift_no = 0;
		String dayslist1 = "";
		int total_days = 0;
		ArrayList dayslist = new ArrayList();
		String columnValue = "";
		String site_code = "";
		String sql = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		// Changed by Dhiraj 07/10/10 [AD01SUN008].start
		String loginSite = "";
		String siteCodeDescr = "";
		
		// Changed by Dhiraj 07/10/10 [AD01SUN008] .end
		StringBuffer valueXmlString = new StringBuffer();
	    ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			columnValue = genericUtility.getColumnValue(currentColumn,dom);
			System.out.println("columnValue :"+columnValue);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><Header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></Header>");
			valueXmlString.append("<Detail1>");
			
			if (currentColumn.trim().equals("itm_default"))
			{
				System.out.println("xtraParamsitemchange:---------------"+xtraParams);
				site_code = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"site_code");
				valueXmlString.append("<site_code>").append(site_code).append("</site_code>\r\n");
							
				//Changed by Dhiraj 07/10/10 [AD01SUN008]. start
				//java.sql.Date toDay =new java.sql.Date(System.currentTimeMillis());
				java.util.Date toDay = new java.util.Date();
				SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
				String dbDtStr = sdf.format(toDay);
				// Changed by Dhiraj 07/10/10 [AD01SUN008]. end

				
				//valueXmlString.append("<start_date>").append(toDay).append("</start_date>\r\n");
				 valueXmlString.append("<start_date>").append(dbDtStr).append("</start_date>\r\n");
				System.out.println("Start_date:-------------------------"+toDay);
			
			}	
			
			else if (currentColumn.trim().equals("shift_pattern"))
			{
				dayslist = GenericUtility.getInstance().getTokenList(columnValue, ";");
				System.out.println("--------------------dayslist="+dayslist.size());
				for(int ctr = 0; ctr < dayslist.size()-1; ctr++)
				{	
					String strDaysList =dayslist.get(ctr).toString();
					int index = strDaysList.indexOf('-');
					dayslist1 = strDaysList.substring(0,index);
					System.out.println("dayslist:-------------------------"+dayslist1);
					shift_no = Integer.parseInt(dayslist1.toString());

					//days = dayslist.get(ctr).toString().charAt(0);
					//shift_no = Integer.parseInt((new Character(days)).toString());
					//shift_no = Integer.parseInt((new Character(days));
					System.out.println("--------------------shift_no="+shift_no);
					total_days = total_days + shift_no;
					System.out.println("--------------------dayslist="+total_days);
				}
				System.out.println("-----------------------------total_days ="+total_days);
				valueXmlString.append("<no_of_days>").append(total_days).append("</no_of_days>\r\n");				
				//GenericUtility.getInstance();
				
				
			}
			
			
			valueXmlString.append("</Detail1>\r\n");
			valueXmlString.append("</Root>\r\n");			
		}
		catch(Exception e)
		{
			System.out.println("Exception in ShiftPatternItmChngEJB.java occured");
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.......");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		System.out.println("Return XML" + valueXmlString.toString() );
		return valueXmlString.toString(); 

	}
}