/********************************************************
	Title : CopyHolParamPrcLocal
 ********************************************************/


package ibase.webitm.ejb.adm;

import ibase.webitm.utility.*;
//import ibase.utility.CommonConstants;
import ibase.system.config.*;
//import java.text.*;
import java.util.*;
//import javax.xml.parsers.*;
import javax.ejb.*;
//import javax.naming.InitialContext;
import java.io.*;

import java.util.Calendar;
import java.util.Date;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.rmi.RemoteException;
import org.w3c.dom.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.ejb.*;

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


//public class SReturnPrsEJB extends ValidatorEJB implements SessionBean
@Stateless // added for ejb3
public class CopyHolParamPrs extends ValidatorEJB implements CopyHolParamPrsLocal, CopyHolParamPrsRemote
{
	/*
	public void ejbCreate() throws RemoteException, CreateException
	{
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate()
	{
	}
	public void ejbPassivate()
	{
	}*/
	
	public String preSaveRec() throws RemoteException, ITMException
	{
		return "";
	}
	
	public String preSaveRec(String xmlString1, String domId, String objContext, String editFlag, String xtraParams, Connection conn) throws RemoteException, ITMException
	{
		System.out.println("@@@@@@@@********* CopyPayParamPrs Called *******@@@@@@@@");
		Document dom = null;
		String errString = "";
		//ITMDBAccessEJB itmDBAccessLocal = new ITMDBAccessEJB();

		
		try
		{
			System.out.println("CopyPayParamPrs xmlString1 [" + xmlString1 + "]");
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom = GenericUtility.getInstance().parseString(xmlString1);
				errString = executepreSaveRec(dom,domId,objContext,editFlag,xtraParams,conn);
				if (errString == null || errString.trim().length() == 0)
				{
					System.out.println("@@@@@ CopyPayParamPrs transaction insert succesfully........");
					//errString = itmDBAccessLocal.getErrorString("","VTMCONF2","");
				}
				else
				{
					conn.rollback();
					System.out.println("@@@@@ insertion failed........");
					//errString = itmDBAccessLocal.getErrorString("","VMPROCUSG","");
				}
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :CopyPayParamPrs :preSaveRec(): " + e.getMessage()+ ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		return errString;
	}
	private String executepreSaveRec(Document dom, String domID, String ObjContext, String editFlag, String xtraParams, Connection conn) throws RemoteException, ITMException
	{
		

		//Connection conn = null;
		PreparedStatement pstmt = null,pstmt1 = null,pstmtInser = null;
		ResultSet rs = null ,rs1 = null;
		String hol_tblno = "",newhol_tblno="";
		String sql = "",sql1 = "",sqlInser;
		//String varName = "",varType = "",varValue = "",chgUser = "",chgTerm = "",descr = "";
		
		String  holType = "",chgUser = "",chgTerm = "",descr = "";
		int noDays=0;
		int cnt = 0,updCnt = 0;
		String  holDate="";
		Date chgdate = null; 
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		//ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();		
		ITMDBAccessEJB itmDBAccessLocal = new ITMDBAccessEJB();
		String errString = "";
		
		try
		{	
			ConnDriver connDriver = new ConnDriver();			
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			hol_tblno = genericUtility.getColumnValue("hol_tblno",dom);
			//varName = genericUtility.getColumnValue("var_name",dom);
			newhol_tblno = genericUtility.getColumnValue("new_hol_tblno",dom);
			String currAppdate ="";
			java.sql.Timestamp currDate = null;
			Object date = null;
			currDate =new java.sql.Timestamp(System.currentTimeMillis()) ;
			System.out.println(genericUtility.getDBDateFormat());
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getDBDateFormat());
			date = sdf.parse(currDate.toString());
			currDate =	java.sql.Timestamp.valueOf(sdf.format(date).toString() + " 00:00:00.0");
			System.out.println("@@@@ currDate [[[[[["+currDate+"]]]]]]]");
			currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate).toString();
			
			chgUser = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"chg_user");
			
			chgTerm = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"chg_term");
		
			
			if( hol_tblno == null || hol_tblno.trim().length() ==0 )
			{
				//errString = itmDBAccessEJB.getErrorString("","PRDCODNULL","");
				//return errString;
			}
			else if( newhol_tblno == null || newhol_tblno.trim().length() == 0 )
			{
				errString = itmDBAccessEJB.getErrorString("","NEWTBLNUL","");
				return errString;
			}
			else if( newhol_tblno != null && newhol_tblno.trim().length() > 0 )
			{
				if( newhol_tblno.trim().length() > 5 )
				{
					
					errString = itmDBAccessEJB.getErrorString("","NEWTBLINV","");
            		return errString;
					
				}
				
			}
			
			if( hol_tblno != null && hol_tblno != null && newhol_tblno.trim().length() > 0 && hol_tblno.trim().length() > 0)
			{	
			//sql = " SELECT var_name,var_type,var_value,descr,var_subs FROM payrparm WHERE prd_code = ? ";   // and var_name = ?";
				
			sql =	"SELECT * FROM HOLIDAY WHERE HOL_TBLNO=?"; 
			pstmt =  conn.prepareStatement(sql);
			pstmt.setString(1,hol_tblno);
			//pstmt.setString(2,varName);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				
				
				holDate = rs.getString("hol_date");
				holType = rs.getString("hol_type");
				descr = rs.getString("descr");
				noDays =  rs.getInt("no_days");
				
				
				
				//sql1 = "SELECT count(*) FROM payrparm WHERE prd_code = ? and var_name = ? ";
				
				sql1 ="SELECT count(*) FROM HOLIDAY WHERE HOL_TBLNO = ? and hol_date=? and hol_type=?";
				
				pstmt1 =  conn.prepareStatement(sql1);
				pstmt1.setString(1, newhol_tblno);
				
				
				//pstmt1.setTimestamp(2,holDate);
			    pstmt1.setString(2, getDD_MM_YYYY_format(holDate));
				pstmt1.setString(3,holType);
				
				
				rs1 = pstmt1.executeQuery();
				if(rs1.next())
				{
					cnt =  rs1.getInt(1);

				}
				rs1.close();
				rs1 = null;
				pstmt1.close();
				pstmt1 = null;
				System.out.println("@@@@@:::cnt:["+cnt+"]");
				if(cnt == 0)
				{
					sqlInser = "INSERT INTO HOLIDAY  ( hol_tblno, hol_date, hol_type, descr, no_days, chg_date, chg_user, chg_term) "
						       +" VALUES (?,?,?, ?,?,?, ?,?)  ";
					pstmtInser	= conn.prepareStatement(sqlInser);
					pstmtInser.setString(1,newhol_tblno);
					//pstmtInser.setString(2,holDate);
					pstmtInser.setTimestamp(2,getTimestamp(holDate));
					
					pstmtInser.setString(3,holType);
					pstmtInser.setString(4,descr);
					pstmtInser.setInt(5,noDays);
					pstmtInser.setTimestamp(6, currDate );  ///currAppdate
					pstmtInser.setString(7,chgUser);
					pstmtInser.setString(8,chgTerm);
					
					
					updCnt = pstmtInser.executeUpdate();
					pstmtInser.close();
					pstmtInser=null;
					if(updCnt > 0 )
					{
						conn.commit();
						System.out.println("@@@@@ Record inserted for table no["+newhol_tblno +"]:::::::: @@@@@");
					}
				}
				else
				{
					//System.out.println("@@@@@ :::::Record already exist for PeriodCode["+newPeriodCode +"]::varName["+ varName +"]  :::::: @@@@@");
					//errString = itmDBAccessLocal.getErrorString("","VMDUPREC1","");
				}

			}
			if(rs != null)
			{
				rs.close();
				rs = null;
			}
			if(pstmt != null)
			{
				pstmt.close();
				pstmt = null;
			}
			}		
			//valueXmlString.append("</Root>\r\n");
		} // end of try 
		catch(Exception e)
		{
			try
			{
				conn.rollback();
			}
			catch(Exception ex)
			{
				throw new ITMException(ex);

			}
			throw new ITMException(e);

		}
		finally
		{
			try
			{
				conn.close();
				conn = null;
			}
			catch(Exception e)
			{
				throw new ITMException(e);
			}
		}
		return errString ;
	}
	
	public String getDD_MM_YYYY_format(String str) throws Exception{
		
		  DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		  Date date;
		  String dd_mm_yyyy="";
	
			date = (Date)formatter.parse(str);
			System.out.println("DDMMYYYY="+date);
			SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");   
			dd_mm_yyyy=format.format(date);
			
	       //String orcleFormat= oracleFormatDate(dd_mm_yyyy); 
	       System.out.println("orcleFormat dd-mm-yyyy=="+dd_mm_yyyy);
	     return dd_mm_yyyy;
	}
	
public Timestamp getTimestamp(String str) throws Exception{
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
		java.util.Date date = sdf.parse(str);
		java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
		//System.out.println("====="+timestamp);
		return timestamp;
		
	}


}


