
package ibase.webitm.ejb.fin;
import ibase.scheduler.utility.interfaces.Schedule;
import java.rmi.RemoteException;
import java.util.*;
import java.util.Date;
import java.sql.*;
import java.io.*;

import org.omg.CORBA.ORB;
import org.w3c.dom.*;



import java.util.Properties;

import javax.swing.text.NumberFormatter;
import javax.xml.parsers.*;
import javax.ejb.*;
import javax.naming.InitialContext;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.ejb.fin.GenerateReceiptPrc;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.TransIDGenerator;

import ibase.utility.BaseException;
import ibase.utility.CommonConstants;
import ibase.utility.UserInfoBean;
import ibase.ejb.*;
import ibase.system.config.*;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.math.*;
import java.net.InetAddress;
import ibase.webitm.ejb.sys.UtilMethods;


public class MiscDrcrRcpSchedule implements Schedule
{
	File filePtr = null;

	static
	{
		File mkd = new File("../log");
		if(!mkd.exists())
		{
			mkd.mkdirs();
			System.out.println("Directory Built................ "+mkd);
		}

	}
	//GenericUtility genericUtility = GenericUtility.getInstance();
	//ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	//ConnDriver connDriver = new ConnDriver();
	//CommonConstants commonConstants = new CommonConstants();
	String chgUser = null;
	String chgTerm = null;
	static long count_records=0;
	boolean isError = false;
	//Change by Rohan on 2/7/12
	String loginSiteCode = "";

	
	public static void main(String args[])
	{
		//PhysicalCountSchedule pcs=new PhysicalCountSchedule();
		//pcs.insertSchedule();
		System.out.println("Main Method Calling******************************************************88");
	}
	
	public String schedulePriority( String wrkflwPriority )throws Exception
	{
		return "";
	}
	public String schedule(HashMap map)throws Exception
	{
		return "";
	}
	public String schedule(String scheduleParamXML)throws Exception
	{
		String tranSer = "",tranType = "",xtraParams = "";
		
		ibase.utility.UserInfoBean userInfo = null;
		try
		{
			//Chnaged by Rohan on 7/2/12.start
			//insertSchedule();
			System.out.println("************ ["+scheduleParamXML+"]");
			userInfo = new ibase.utility.UserInfoBean( scheduleParamXML );
			loginSiteCode = userInfo.getSiteCode();
			System.out.println("Site code======**************====> "+loginSiteCode);
			//added for misc drcr 
			GenericUtility genericUtility=GenericUtility.getInstance();
			System.out.println("scheduleParamXML-----"+scheduleParamXML);
			Document dom=genericUtility.parseString(scheduleParamXML);
			
			NodeList paramList = dom.getElementsByTagName( "SCHEDULE" );
            Node currDetail = null ;            
            int noOfParam = paramList.getLength();            
            			NodeList parentNodeList = null,childNodeList = null;
						Node parentNode = null,childNode = null;
						String DisparmSites = "";
						int childNodeListLength = 0;
						String childNodeName = null;

						parentNodeList = dom.getElementsByTagName("ACTUALPARAMETERS");

						parentNode = parentNodeList.item(0);
						childNodeList = parentNode.getChildNodes();
						childNodeListLength = childNodeList.getLength();
						for(int ctr = 0; ctr < childNodeListLength; ctr++)
						{
							childNode = childNodeList.item(ctr);
							childNodeName = childNode.getNodeName();
							
							if(childNodeName!=null && !"#text".equalsIgnoreCase(childNodeName))
							{
								
								if(ctr==0)
								{
									tranSer=childNode.getFirstChild().getNodeValue();
								}
								if(ctr==1)
								{
									tranType=childNode.getFirstChild().getNodeValue();
								}
							}
						}
						System.out.println("tranSer----"+tranSer);
						System.out.println("tranType----"+tranType);


						
			//tranSer=genericUtility.getColumnValue("tran_ser",dom);
			//tranType=genericUtility.getColumnValue("tran_type",dom);
			xtraParams = "loginCode="+userInfo.getEmpCode()+"~~termId="+userInfo.getRemoteHost()+"~~loginSiteCode="+loginSiteCode;
			insertSchedule(tranSer,tranType,userInfo,xtraParams);
			
			
		}
		catch(Exception e)
		{
			throw new Exception(e);
		}
		return "";
	}
	
	//String loginSiteCode,userInfo.getEmpCode(),
	public void insertSchedule(String tranSer,String tranType,UserInfoBean userInfo,String xtraParams)
	{
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		PreparedStatement pstmtInsert = null;
		ResultSet rs = null;
		Statement stmt = null;
		ConnDriver connDriver = new ConnDriver();
		String errString = "",tranId = "",queryString = "",customError = "";
		java.sql.Date currDate = null;
		String currAppdate= "";

		try
		{
			
			GenericUtility genericUtility=GenericUtility.getInstance();
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			//get tran id from misc_drcr_rcp
			MiscDrCrRcpConf mrdrcp= new MiscDrCrRcpConf();
			
			currDate = new java.sql.Date(System.currentTimeMillis()) ;
			currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate);
			currAppdate = currAppdate.replace("/", "-");		
			filePtr = new File("../log/MiscDRCR_Confirmation Fail"+currAppdate+".log");
			System.out.print("filePtr@@@@@@@@@@@@@@"+filePtr);
			
			queryString="select tran_id from misc_drcr_rcp where tran_type=? and tran_ser=?  and confirmed = 'N'";
			pstmt=conn.prepareStatement(queryString);
			pstmt.setString(1,tranType);
			pstmt.setString(2,tranSer);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				tranId=rs.getString("tran_id");
				if(tranId!=null && tranId.length()>0)
				{
					MiscDrCrRcpConf miscdr = new MiscDrCrRcpConf();
					System.out.println("calling confirm methode from MiscCreditConf--->>");
					//removed connection according to meshode signature 
					errString = miscdr.confirm(tranId,xtraParams," ",conn);//,conn
					System.out.println("errString --->>>>"+errString.toString());
				}
				if(errString != null || errString.length()>0)
				{
					if(errString.indexOf("CONFSUCCES") > -1)
					{
						System.out.println("Committing..............");
						conn.commit();
						//conn.close();
						//conn=null;
					}
					else
					{
						System.out.println("Genrate custom Error due to unconfirm transection-->>");
						//customError+="Transction "+tranId+" Not confirmed \n " + errString;
						customError+= tranId+"\n";
						System.out.println("customError:::::---->>>>"+customError);
						conn.rollback();
					}
			}
			}
			writeLog(filePtr,"Tran Series "+tranSer+" Tran Type "+tranType+"\n"+customError,true);
			rs.close();
			rs=null;
			pstmt.close();
			pstmt=null;
			
			//end of geting tran_id 
			
		
		}
		catch(Exception e)
		{
			isError = true;
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.out.println("Exception is "+e.getMessage());
			System.out.println("Exception is "+e);
			e.printStackTrace();
		}
		finally
		{
			
			try
			{
				if(conn != null)
				{
					if(pstmt!=null)
					{
						pstmt.close();
						pstmt=null;
					}
					if(pstmtInsert!=null)
					{
						pstmtInsert.close();
						pstmtInsert=null;
					}
					if(rs!=null)
					{
						rs.close();
						rs=null;
					}
				
					//conn.commit();
					conn.close();
				}
			}
			catch(Exception e)
			{
				isError = true;
				System.out.println("Exception is "+e.getMessage());
			}
			
		}
		
	}
	private static void writeLog(File f,String ex,boolean flag)
	{
		try
		{
			PrintWriter pw = new PrintWriter((new FileOutputStream(f,flag)),flag);
			pw.println(ex);
			pw.close();
		}
		catch(Exception exWm){exWm.printStackTrace();}
	}
	
}