/*
 * Created on Nov 28, 2006
 *
 */
package ibase.webitm.ejb.fin;

/**
 * @author Jiten
 */
 
import java.rmi.RemoteException;
import java.text.*;
import java.sql.*;
import org.w3c.dom.*;

import javax.ejb.*;

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

@Stateless // added for ejb3
public class MiscPayAuto extends ValidatorEJB implements MiscPayAutoLocal,MiscPayAutoRemote // SessionBean
{
	/*public void ejbCreate() throws RemoteException,CreateException{
		
	}
	public void ejbRemove(){
		
	}
	public void setSessionContext(SessionContext sess){
		
	}	*/
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException 
	{
		String resultString = "";
		Document dom = null,dom1 = null,dom2 = null;
		try{
			if(xmlString != null){
				dom = GenericUtility.getInstance().parseString(xmlString);
			}
			if(xmlString1 != null){
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
			}
			if(xmlString2 != null){
				dom2 = GenericUtility.getInstance().parseString(xmlString2);
			}
			resultString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}catch(ITMException e){
			System.out.println("ITMException "+e);
			e.printStackTrace();
			throw e;
		}
		catch(Exception e){
			System.out.println("Exception in itemchange "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}		
		return resultString;
	} 
	public String itemChanged(Document dom,Document dom1,Document dom2,String objContext, String currentColumn, String editFlag, String xtraParams) throws ITMException 
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "";		
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><Header><editFlag>");
		java.util.Date currDate = new java.util.Date(System.currentTimeMillis());
		String dateStr = "";
		String loginSite = "";
		try{
			conn = getConnection();
			SimpleDateFormat sdf = new SimpleDateFormat(GenericUtility.getInstance().getApplDateFormat());
			dateStr = sdf.format(currDate);
			loginSite = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams,"loginSite");
			valueXmlString.append(editFlag).append("</editFlag></Header>\r\n");
			valueXmlString.append("<Detail1>\r\n");
			if(currentColumn.equalsIgnoreCase("itm_default")){
				valueXmlString.append("<date__pay>").append(dateStr).append("</date__pay>\r\n");
				valueXmlString.append("<date__fr>").append(dateStr).append("</date__fr>\r\n");
				valueXmlString.append("<date__to>").append(dateStr).append("</date__to>\r\n");
				valueXmlString.append("<site_code__fr>").append(loginSite).append("</site_code__fr>\r\n");
				String bankCode = "",bankName = "";
				sql = "select bank_code from site where site_code = '"+loginSite+"'";
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if(rs.next()){
					bankCode = rs.getString("bank_code");
					if(bankCode == null)
						bankCode = "";
				}
				stmt.close();
				rs.close();
				sql = "select bank_name from bank where bank_code = '"+bankCode+"'";
				System.out.println("SQL :: "+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if(rs.next()){
					bankName = rs.getString("bank_name");
					if(bankName == null)
						bankName = "";
				}
				valueXmlString.append("<bank_name>").append(bankName).append("</bank_name>\r\n");
			}
			/*else if(currentColumn.equalsIgnoreCase("bank_code")){
				String bankCode = "",bankName = "";
				bankCode = GenericUtility.getInstance().getColumnValue("bank_code",dom1);
				sql = "select bank_name from bank where bank_code = '"+bankCode+"'";
				System.out.println("SQL :: "+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if(rs.next()){
					bankName = rs.getString("bank_name");
					if(bankName == null)
						bankName = "";
				}
				valueXmlString.append("<bank_name>").append(bankName).append("</bank_name>\r\n");
				bankCode = null;
				bankName = null;				
			}
			else if(currentColumn.equalsIgnoreCase("bank_code__add")){
				String bankCodeAdd = "",bankNameAdd = "";
				bankCodeAdd = GenericUtility.getInstance().getColumnValue("bank_code__add",dom1);
				sql = "select bank_name from bank where bank_code = '"+bankCodeAdd+"'";
				System.out.println("SQL :: "+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if(rs.next()){
					bankNameAdd = rs.getString("bank_name");
					if(bankNameAdd == null)
						bankNameAdd = "";
				}
				valueXmlString.append("<bank_name__add>").append(bankNameAdd).append("</bank_name__add>\r\n");
				bankCodeAdd = null;
				bankNameAdd = null;
			}*/
			valueXmlString.append("</Detail1>");
		}catch(ITMException e){
			throw e;
		}
		catch(Exception e){
			throw new ITMException(e);
		}
		finally{
			try{
				if(rs != null){
					rs.close();
					rs = null;
				}
				if(stmt != null){
					stmt.close();
					stmt = null;
				}
				if(conn != null){
					conn.close();
					conn = null;
				}
			}catch(Exception e){}
		}
		
		return valueXmlString.toString();
	}
	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null,dom1 = null,dom2 = null;
		String returnString = "";
		try{
		    System.out.println("xmlString :"+xmlString+"\nxmlString1 : "+xmlString1+"\nxmlString2 : "+xmlString2);
			if(xmlString != null){
				dom = GenericUtility.getInstance().parseString(xmlString);
			}
			if(xmlString1 != null){
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
			}
			if(xmlString2 != null){
				dom2 = GenericUtility.getInstance().parseString(xmlString2);
			}
			returnString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}catch(ITMException e){
			System.out.println("ITMException "+e);
			e.printStackTrace();
			throw e;
		}
		catch(Exception e){
			System.out.println("Exception in Validation : "+e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		return returnString;
	}	
	public String wfValData(Document dom,Document dom1,Document dom2,String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
	    Connection conn = null;
	    Statement stmt = null;
	    ResultSet rs = null;
	    String sql = "";
		Node detail = null;
		NodeList childNodes = null;
		Node currNode = null;
		String nodeName = "";
		String columnValue = "";
		int detailLength = 0;
		String errCode = "",errString = "",userId = "";
		ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();
		SimpleDateFormat sdf = null;
		try{
		    conn = getConnection();
			sdf = new SimpleDateFormat(GenericUtility.getInstance().getApplDateFormat());
			userId = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams,"termId");
			detail = dom.getElementsByTagName("Detail2").item(0);
			childNodes = detail.getChildNodes();
			detailLength = childNodes.getLength();
			for(int i = 0;i < detailLength;i++){
				currNode = childNodes.item(i);
				nodeName = currNode.getNodeName(); 
				if(nodeName.equalsIgnoreCase("date__pay")){
					java.util.Date tranDate = null;
					columnValue = GenericUtility.getInstance().getColumnValue("date__pay",dom);					
					if(columnValue == null){
						errString = itmDBAccess.getErrorString("date__pay","VEDATE",userId);
						break;
					}
					else{
						String siteCode = "";	
						if(columnValue.indexOf("-") != -1){
						    columnValue = columnValue.replace('-','/');
				        }
						tranDate = sdf.parse(columnValue.substring(0,8));
						siteCode = GenericUtility.getInstance().getColumnValue("site_code__fr",dom);
						errCode = nfCheckPeriod("FIN",tranDate , siteCode);
						if(errCode != null && errCode.trim().length() > 0){
							errString = itmDBAccess.getErrorString("date__pay",errCode,userId);
							break;
						}
					}
				}
				else if(nodeName.equalsIgnoreCase("date__fr") || nodeName.equalsIgnoreCase("date__to")){
					java.util.Date fromUtilDate = null, toUtilDate = null,payUtilDate = null;
					
					String frDate = "",todate = "", payDate = "";
					frDate = GenericUtility.getInstance().getColumnValue("date__fr",dom);
					todate = GenericUtility.getInstance().getColumnValue("date__to",dom);
					payDate =  GenericUtility.getInstance().getColumnValue("date__pay",dom);
					if(frDate == null || todate == null){
					    errString = itmDBAccess.getErrorString(nodeName,"VEDATE",userId);
						break;
					}
					if(frDate!= null && frDate.indexOf("-") != -1){
			            frDate = frDate.replace('-','/');
			        }
			        if(todate!= null && todate.indexOf("-") != -1){
			            todate = todate.replace('-','/');
			        }
			        if(payDate!= null && payDate.indexOf("-") != -1){
			            payDate = payDate.replace('-','/');
			        }
			        
					fromUtilDate = sdf.parse(frDate.substring(0,8));
					toUtilDate = sdf.parse(todate.substring(0,8));
					
					if (fromUtilDate.after(toUtilDate)){
					    errString = itmDBAccess.getErrorString(nodeName,"VFRTODATE",userId);
						break;
					}
					payUtilDate = sdf.parse(payDate.substring(0,8));
					if(payUtilDate.before(toUtilDate)){
					    errString = itmDBAccess.getErrorString(nodeName,"VXPAYDATE",userId);
						break;					    	
					}
				}				
				else if(nodeName.equalsIgnoreCase("site_code__fr")){
				    int cnt = 0;
				    columnValue = GenericUtility.getInstance().getColumnValue("site_code__fr",dom);
				    sql = "select count(*) from site where site_code = '"+columnValue+"'";
				    stmt = conn.createStatement();
				    rs = stmt.executeQuery(sql);
				    if(rs.next()){
				        cnt = rs.getInt(1);
				    }
				    if(cnt == 0){
				        errString = itmDBAccess.getErrorString("site_code__fr","VMSITE1",userId);
						break;
				    }
				    else{
				        String payDate = "",siteCode = ""; 
				        payDate = GenericUtility.getInstance().getColumnValue("date__pay",dom);
				        if(payDate!= null && payDate.indexOf("-") != -1){
				            payDate = payDate.replace('-','/');
				        }
				        java.util.Date tranDate = sdf.parse(payDate.substring(0,8));
				        siteCode = GenericUtility.getInstance().getColumnValue("site_code__fr",dom);
				        errCode = nfCheckPeriod("FIN",tranDate,siteCode);
				        if(errCode != null && errCode.trim().length() > 0){
				            errString = itmDBAccess.getErrorString("site_code__fr",errCode,userId);
							break;
				        }
				    }
				}
				else if(nodeName.equalsIgnoreCase("pay_mode")){
				    String payMode = "";
				    payMode = GenericUtility.getInstance().getColumnValue("pay_mode",dom);
				    if(payMode == null || payMode.trim().length() == 0){
				        errString = itmDBAccess.getErrorString("pay_mode","VTPAYMODE1",userId);
						break;
				    }
				}
				/*else if(nodeName.equalsIgnoreCase("bank_code") || nodeName.equalsIgnoreCase("bank_code__add")){
				    String siteCode = "", bankCode = "";
				    siteCode = GenericUtility.getInstance().getColumnValue("site_code__fr",dom);
				    bankCode = GenericUtility.getInstance().getColumnValue(nodeName.toLowerCase(),dom);
				    errCode = isBankCode(siteCode,bankCode,"FIN");
				    if(errCode != null && errCode.trim().length() > 0){
				        errString = itmDBAccess.getErrorString(nodeName.toLowerCase(),errCode,userId);
						break;
				    }
				}*/				
			}
			
		}catch(ITMException e){
			throw e;
		}
		catch(Exception e){
			throw new ITMException(e);
		}
		return errString;
	}
}

