package ibase.webitm.ejb.mfg;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.dis.InvAllocTrace;
import ibase.webitm.ejb.dis.StockAllocationPrc;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReqViewMrpBean {
	public List preSaveForm(String xmlString1 )throws ITMException
	{
		Document dom = null;
		List finallist= new ArrayList();
		System.out.println("@@@@@@@@@---------ReqViewMrpBean EJB called...");
		try
		{
			System.out.println("@@@@@@@xmlString1["+xmlString1+"]@@@@@@@");
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom = GenericUtility.getInstance().parseString(xmlString1);
				finallist = executepreSaveForm(dom);
			}			
		}
		catch(Exception e)
		{
			System.out.println("Exception :ReqViewMrpBean ::" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("jsp finallist:"+finallist);
		return finallist;
	}

	private List executepreSaveForm(Document dom )throws ITMException
	{
		PreparedStatement pstmt = null;
		String sql = "", itemCode = "", effDate = "", itemSer = "";
		int updCnt = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		NodeList hdrDom = null;
		

		String saleOrder="",lineNo="", siteCode="", lotNo="", locCode="", lotSl="", tranSer="", runningSupplyStr="", qtyReqdStr="",allocQtyStr="";
		double runningSupply=0,qtyReqd=0;
		String tranId="",refId="",refLineNo="",unit="",errString="";
		double quantity=0,qtyAllocated=0,totAllocQty=0,allocQty=0;
		int cnt=0;
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver(); 
		//String supplyStr="",availableSupplyStr="";
		
		List rowlist= null;		
		List finallist= new ArrayList();
		List titlelist= null;		
		List title= new ArrayList();
		
		String suppSour="",demand="",supply="",dueDate="",planSupply="",runningDemand="",runningPlanSupply="",independentDemand="";
		String bomCode="",orderOpt="",batchQtyType="",minOrderQty="",integralQty="",maxQty="",minQty="",reorderQty="",yieldPerc="";
		String minPlanPerc="",batchQty="",percRate="",purLeadTime="",qcLeadTime="",mfgLeadTime="",description="",stockQty="",othSupply="";
		String pendingPo="",pendingDo="",pendingIndent="",pendingDr="",quarntineStk="";
		
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			
			NodeList hdrDommList = dom.getElementsByTagName("Detail3");
			System.out.println("len===["+hdrDommList.getLength()+"]");

			for (int dtlCtr = 0; dtlCtr < hdrDommList.getLength(); dtlCtr++)
			{
				Node detailListNode = hdrDommList.item(dtlCtr);
				NodeList detail3List= detailListNode.getChildNodes(); 
				System.out.println("@@@@@@@@@node name[" + detailListNode.getNodeName()+"]");

				if("Detail3".equalsIgnoreCase(detailListNode.getNodeName()))
				{
					System.out.println("@@@@inside detail3----------------");
					for (int cntr = 0; cntr < detail3List.getLength(); cntr++) 
					{
						Node	detail3Node = detail3List.item(cntr); 
						NodeList detail3NodeList = detail3Node.getChildNodes(); 
						
						if("site_code".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							siteCode =  detail3List.item(cntr).getFirstChild().getNodeValue();
							System.out.println("siteCode["+siteCode+"]");
						}
						if("item_code".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							itemCode = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("supp_sour".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							suppSour =  detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("demand".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							demand = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("supply".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							supply =  detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("plan_supply".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							planSupply =  detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("running_demand".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							runningDemand = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("running_supply".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							runningSupplyStr =  detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("running_plan_supply".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							runningPlanSupply = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("unit".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							unit = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("reorder_qty".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							reorderQty = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("yield_perc".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							yieldPerc = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("batch_qty".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							batchQty = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("pur_lead_time".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							purLeadTime = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("qc_lead_time".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							qcLeadTime = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("mfg_lead_time".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							mfgLeadTime = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("description".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							description = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("pending_po".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							pendingPo = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("pending_indent".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							pendingIndent = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						if("quarntine_stk".equalsIgnoreCase( detail3Node.getNodeName()))
						{
							quarntineStk = detail3List.item(cntr).getFirstChild().getNodeValue();
						}
						

					}
					
				}		
				titlelist= new ArrayList();
				titlelist.add(siteCode);
				
				rowlist= new ArrayList();
				rowlist.add(itemCode);
				rowlist.add(description);
				rowlist.add(unit);
				rowlist.add(suppSour);
				rowlist.add(demand);
				rowlist.add(supply);
				rowlist.add(quarntineStk);
				rowlist.add(planSupply);
				rowlist.add(runningPlanSupply);
				rowlist.add(runningDemand);
				rowlist.add(runningSupplyStr);
				rowlist.add(pendingIndent);
				rowlist.add(pendingPo);
				rowlist.add(reorderQty);
				rowlist.add(purLeadTime);
				rowlist.add(mfgLeadTime);
				rowlist.add(qcLeadTime);
				rowlist.add(batchQty);
				rowlist.add(yieldPerc);
				finallist.add(rowlist);
			}//end for
			finallist.add(titlelist);
			
		}//try end
		catch (SQLException sqx)
		{
			System.out.println("The SQLException occurs in ReqViewMrpBean :"+sqx);
			sqx.printStackTrace();
			throw new ITMException(sqx);
		}
		catch(Exception e)
		{
			System.out.println("The SQLException occurs in ReqViewMrpBean  :"+e);			
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally 
		{
			try
			{
				
				if (errString == null || errString.trim().length() == 0)
				{
					System.out.println("@@@@@@commit errString :::"+ errString );
					conn.commit();
					//return errString;
				}
				
				if (pstmt!= null)
				{
					pstmt = null;
				}
				if( conn != null )
				{
					conn.close();
					conn=null;
				}
			}
			catch(Exception e)
			{
				System.out.println("The SQLException occurs in ReqViewMrpBean  :"+e);			
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return finallist;
		
	}




}
