package ibase.webitm.ejb.fin.adv;

import ibase.system.config.ConnDriver;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SupplierInetPortalAprv 
{
	public String confirm(String tranId, String empCodeAprv, String xmlDataAll, String processId, String keyFlag) throws RemoteException, ITMException {
		System.out.println("confirm workflow invocation");
		System.out.println("Parameters ::: tranId["+tranId+"],empCodeAprv["+empCodeAprv+"],xmlDataAll["+xmlDataAll+"],processId["+processId+"],keyFlag["+keyFlag+"]");
		String retString = "";
		Document dom = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "";
		ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr = 0, childNodeListLength = 0, cnt = 0;
		String childNodeName = null;
		String supp_type="",group_code="",addr1="",addr2="",addr3="",supp_code="",
		city="",state_code="",pin="",count_code="",sh_name="",tele1="",tax_reg="",
		chq_name="",pan_no="",excise_ref="",ser_tax_reg="",full_name="",supp_name="";
		try 
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			conn.setAutoCommit(false);
			empCodeAprv = empCodeAprv == null ? "" : empCodeAprv.trim();
			
			System.out.println("start");
			if (xmlDataAll != null && xmlDataAll.trim().length() != 0) 
			{
				dom = genericUtility.parseString(xmlDataAll);
				System.out.println("dom:["+genericUtility.serializeDom(dom)+"]");
				
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				childNodeListLength = childNodeList.getLength();

				for (ctr = 0; ctr < childNodeListLength; ctr++) {
					childNode = childNodeList.item(ctr);
				    childNodeName = childNode.getNodeName();
				    System.out.println("childNodeName:" + childNodeName);
				    supp_code = genericUtility.getColumnValue("supp_code", dom);
				    System.out.println("supp_code="+supp_code);
					
				    if (childNodeName.equalsIgnoreCase("supp_code"))
					{
						supp_code = genericUtility.getColumnValue("supp_code", dom);
						supp_code = supp_code == null ? "" : supp_code.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("supp_type")) 
					{
						supp_type = genericUtility.getColumnValue("supp_type", dom);
						supp_type = supp_type == null ? "" : supp_type.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("group_code")) 
					{
						group_code = genericUtility.getColumnValue("group_code", dom);
						group_code = group_code == null ? "" : group_code.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("addr1")) 
					{
						addr1 = genericUtility.getColumnValue("addr1", dom);
						addr1 = addr1 == null ? "" : addr1.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("addr2")) 
					{
						addr2 = genericUtility.getColumnValue("addr2", dom);
						addr2 = addr2 == null ? "" : addr2.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("addr3")) 
					{
						addr3 = genericUtility.getColumnValue("addr3", dom);
						addr3 = addr3 == null ? "" : addr3.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("city")) 
					{
						city = genericUtility.getColumnValue("city", dom);
						city = city == null ? "" : city.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("state_code")) 
					{
						state_code = genericUtility.getColumnValue("state_code", dom);
						state_code = state_code == null ? "" : state_code.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("pin")) 
					{
						pin = genericUtility.getColumnValue("pin", dom);
						pin = pin == null ? "" : pin.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("count_code")) 
					{
						count_code = genericUtility.getColumnValue("count_code", dom);
						count_code = count_code == null ? "" : count_code.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("sh_name")) 
					{
						sh_name = genericUtility.getColumnValue("sh_name", dom);
						sh_name = sh_name == null ? "" : sh_name.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("tele1")) 
					{
						tele1 = genericUtility.getColumnValue("tele1", dom);
						tele1 = tele1 == null ? "" : tele1.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("tax_reg")) 
					{
						tax_reg = genericUtility.getColumnValue("tax_reg", dom);
						tax_reg = tax_reg == null ? "" : tax_reg.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("chq_name")) 
					{
						chq_name = genericUtility.getColumnValue("chq_name", dom);
						chq_name = chq_name == null ? "" : chq_name.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("pan_no")) 
					{
						pan_no = genericUtility.getColumnValue("pan_no", dom);
						pan_no = pan_no == null ? "" : pan_no.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("excise_ref")) 
					{
						excise_ref = genericUtility.getColumnValue("excise_ref", dom);
						excise_ref = excise_ref == null ? "" : excise_ref.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("ser_tax_reg")) 
					{
						ser_tax_reg = genericUtility.getColumnValue("ser_tax_reg", dom);
						ser_tax_reg = ser_tax_reg == null ? "" : ser_tax_reg.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("full_name")) 
					{
						full_name = genericUtility.getColumnValue("full_name", dom);
						full_name = full_name == null ? "" : full_name.trim();
						
					}
					else if (childNodeName.equalsIgnoreCase("supp_name")) 
					{
						supp_name = genericUtility.getColumnValue("supp_name", dom);
						supp_name = supp_name == null ? "" : supp_name.trim();
						
					}
				}
			
			        
			    sql = "UPDATE SUPPLIER SET SUPP_TYPE=?,GROUP_CODE=?,ADDR1=?,ADDR2=?,ADDR3=?,CITY=?,STATE_CODE=?,PIN=?," +
			    		"COUNT_CODE=?,SH_NAME=?,TELE1=?,TAX_REG=?,CHQ_NAME=?,PAN_NO=?,EXCISE_REF=?,SER_TAX_REG=?,FULL_NAME=?,SUPP_NAME=?"+
					  "WHERE SUPP_CODE=?";
				
				pstmt = conn.prepareStatement(sql);
				
				pstmt.setString(1, supp_type);
				pstmt.setString(2, group_code);
				pstmt.setString(3, addr1);
				pstmt.setString(4, addr2);
				pstmt.setString(5, addr3);
				pstmt.setString(6, city);
				pstmt.setString(7, state_code);
				pstmt.setString(8, pin);
				pstmt.setString(9, count_code);
				pstmt.setString(10, sh_name);
				pstmt.setString(11, tele1);
				pstmt.setString(12, tax_reg);
				pstmt.setString(13, chq_name);
				pstmt.setString(14, pan_no);
				pstmt.setString(15, excise_ref);
				pstmt.setString(16, ser_tax_reg);
				pstmt.setString(17, full_name);
				pstmt.setString(18, supp_name);
				pstmt.setString(19, tranId);
				cnt += pstmt.executeUpdate();
				
				pstmt.close();
				pstmt = null;
				System.out.println("Saved Successfully:"+cnt);
				if(cnt > 0) {
					conn.commit();
					retString = "Y";
				} else
				{
					conn.rollback();
					retString = "N";
				}
			}
		} 
		catch (Exception e) 
		{
			try 
			{
				conn.rollback();
			} 
			catch (SQLException e1) 
			{
				e1.printStackTrace();
			}
			e.printStackTrace();
			System.out.println("Exeption occured");

			throw new ITMException(e);
		} 
		finally
		{
			if (conn != null)
			{
				try 
				{
					conn.commit();
					conn.close();
					conn = null;
				} 
				catch (SQLException e) 
				{
					e.printStackTrace();
					System.out.println("Exeption in Finally");
				}
			}
			System.gc();
		}

		return retString;
	}
}

