

/********************************************************
	Title     : WOTrackStatusIC(M14GSUN005)
	Date      : 06-10-2014
	Developer : mahendra Jadhav
	
********************************************************/

package ibase.webitm.ejb.sys;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ValidatorEJB;
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.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.ejb.Stateless;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@Stateless  
public class WOTrackStatusIC extends ValidatorEJB implements WOTrackStatusICLocal,WOTrackStatusICRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	
	public String wfValData(String xmlString, String xmlString1, String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = "";
		System.out.println("wfValdata() called for facility");
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		try
		{
			if (xmlString != null && xmlString.trim().length() > 0 )
			{
				dom = parseString(xmlString);
				
			}
			if (xmlString1 != null && xmlString1.trim().length() > 0 )
			{
				dom1 = parseString(xmlString1);
				
			}
			if (xmlString2 != null && xmlString2.trim().length() > 0 )
			{
				dom2 = parseString(xmlString2);
				
			}
			errString = wfValData(dom, dom1, dom2, objContext, editFlag, xtraParams);
		}
		catch(Exception e)
		{
			throw new ITMException(e);
		}
		return(errString);
	}
	
	
	
	
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String childNodeName = null;
		String errString = "";
		String errCode = "";
		String userId = "";
		String sql = "";
		String errorType = "";
		String refSer ="",refId="",actCode="",roleCode="",trackLevel="",tranId="";
		int lineNo=0;
		int cnt = 0;
		int ctr=0;
		int childNodeListLength;
		NodeList parentNodeList = null;
		NodeList childNodeList = null;	
		Node parentNode = null;
		Node childNode = null;
		ArrayList<String> errList = new ArrayList<String>();
		ArrayList<String> errFields = new ArrayList<String>();
		Connection conn = null;
		PreparedStatement pstmt = null ;
		ResultSet rs = null;
		ConnDriver connDriver = new ConnDriver();
		StringBuffer errStringXml = new StringBuffer("<?xml version = \"1.0\"?> \r\n <Root> <Errors>");
    
		int currentFormNo =0,recCnt=0,ct=0;
		
		String facilityCode="",facilityDescr="",keyFlag="",stanCode="" ;
		
		double exchRate=0;

		try
		{
			
			System.out.println("FacilityIC.java ::wfvaldata called !!!!!!");
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			switch(currentFormNo)
			{
			case 1 :
				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);
					if(childNodeName.equalsIgnoreCase("facility_code"))
					{   
						tranId = checkNull(genericUtility.getColumnValue("tran_id", dom));
						System.out.println("tranId !!!! "+tranId);

						sql = "select key_flag from transetup where tran_window='w_wo_track_status'";
						
						pstmt = conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							keyFlag = rs.getString("key_flag");									
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						
						if("M".equalsIgnoreCase(keyFlag) && "A".equalsIgnoreCase(editFlag))
						{
							if(tranId == null || tranId.trim().length() == 0  )
							{
								errCode = "VMEMTTRAN";//Tran id should not be null for manually transetup
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
								errString = getErrorString("tran id should not be blank", errCode, userId);
								System.out.println("Error due to tran id blank!!");
							}
							else
							{
								sql = " select count (*) from wo_track_status where tran_id = ?   ";
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1, facilityCode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									ct = rs.getInt(1);	
								}
								pstmt.close();
								pstmt = null;
								rs.close();
								rs = null;
								
								if(ct > 0)
								{
									errCode = "VMINVTRAN";//Invalid tran id!!!
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
									errString = getErrorString("Invalid facility code", errCode, userId);
									System.out.println("Error due to tran id already exist in master!!");
								}

							 }

						}//end of case transetup


					}
					
					if(childNodeName.equalsIgnoreCase("line_no"))
					{    
						
						if(genericUtility.getColumnValue("line_no", dom) == null)
						{
							errCode = "VMEMTLINO";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("line no can not be null");
						}
						
					}
					
					if(childNodeName.equalsIgnoreCase("ref_ser"))
					{    
						refSer = genericUtility.getColumnValue("ref_ser", dom);
						System.out.println("refSer ::"+refSer);
						if(genericUtility.getColumnValue("ref_ser", dom) == null)
						{
							errCode = "VMEMTRFSR";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("ref ser can not be null");
						}
						
					}
					
					if(childNodeName.equalsIgnoreCase("ref_id"))
					{    
						refId = genericUtility.getColumnValue("ref_ser", dom);
						System.out.println("refId ::"+refId);
						if(genericUtility.getColumnValue("ref_id", dom) == null)
						{
							errCode = "VMEMTRFID";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("ref id can not be null");
						}
						
					}
					
					if(childNodeName.equalsIgnoreCase("ref_line_no"))
					{    
						System.out.println("ref_line_no :"+genericUtility.getColumnValue("ref_line_no", dom));
						if(genericUtility.getColumnValue("ref_line_no", dom) == null)
						{
							errCode = "VMEMTRFLN";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("ref line no can not be null");
						}
						
					}
					
					if(childNodeName.equalsIgnoreCase("seq_no"))
					{    
						System.out.println("seq_no :"+genericUtility.getColumnValue("seq_no", dom));
						if(genericUtility.getColumnValue("seq_no", dom) == null)
						{
							errCode = "VMEMTSQNO";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("seq no can not be null");
						}
						
					}
					
					if(childNodeName.equalsIgnoreCase("act_code"))
					{    
						actCode = genericUtility.getColumnValue("act_code", dom);
						System.out.println("actCode ::"+actCode);
						if(genericUtility.getColumnValue("act_code", dom) == null)
						{
							errCode = "VMEMTACNO";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("act code can not be null");
						}
						
					}
					
					
					if(childNodeName.equalsIgnoreCase("role_code"))
					{    
						roleCode = genericUtility.getColumnValue("role_code", dom);
						System.out.println("roleCode ::"+roleCode);
						if(genericUtility.getColumnValue("role_code", dom) == null)
						{
							errCode = "VMEMTRLCD";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("role code can not be null");
						}
						
					}
					
					if(childNodeName.equalsIgnoreCase("track_level"))
					{    
						trackLevel = genericUtility.getColumnValue("track_level", dom);
						System.out.println("roleCode ::"+trackLevel);
						if(genericUtility.getColumnValue("track_level", dom) == null)
						{
							errCode = "VMEMTRLVL";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("track level can not be null");
						}
						
					}
					
					
					if(childNodeName.equalsIgnoreCase("days_required"))
					{    
						System.out.println("days_required :"+genericUtility.getColumnValue("days_required", dom));
						if(genericUtility.getColumnValue("days_required", dom) == null)
						{
							errCode = "VMEMTDRQ";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("days required can not be null");
						}
						
					}
					
					
					if(childNodeName.equalsIgnoreCase("plan_end_date"))
					{    
						System.out.println("plan_end_date :"+genericUtility.getColumnValue("plan_end_date", dom));
						if(genericUtility.getColumnValue("plan_end_date", dom) == null)
						{
							errCode = "VMEMTPLDT";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
							System.out.println("plan end date can not be null");
						}
						
					}
					
						
					
					
				} // end for
				break;  // case 1 end

			}//end of switch 

			int errListSize = errList.size();
			cnt = 0;
			String errFldName = null;
			if(errList != null && errListSize > 0)
			{
				for(cnt = 0; cnt < errListSize; cnt ++)
				{
					errCode = errList.get(cnt);
					errFldName = errFields.get(cnt);
					System.out.println("errCode .........." + errCode);
					errString = getErrorString(errFldName, errCode, userId);
					errorType = errorType(conn , errCode);
					if(errString.length() > 0)
					{
						String bifurErrString = errString.substring(errString.indexOf("<Errors>") + 8, errString.indexOf("<trace>"));
						bifurErrString = bifurErrString + errString.substring(errString.indexOf("</trace>") + 8, errString.indexOf("</Errors>"));
						errStringXml.append(bifurErrString);
						errString = "";
					}
					if(errorType.equalsIgnoreCase("E"))
					{
						break;
					}
				}
				errList.clear();
				errList = null;
				errFields.clear();
				errFields = null;
				errStringXml.append("</Errors> </Root> \r\n");
			}
			else
			{
				errStringXml = new StringBuffer("");
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			errString = e.getMessage();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(conn != null)
				{
					if(rs != null) 
					{
						rs.close();
						rs = null;
					}
					if(pstmt != null) 
					{
						pstmt.close();
						pstmt = null;
					}
					conn.close();
				}
				conn = null;
			} 
			catch(Exception d)
			{
				d.printStackTrace();
				throw new ITMException(d);
			}
		}
		errString = errStringXml.toString();
		return errString;
	}
	
	

	
	private String checkNull(String input) 
	{
		if(input == null)
		{
			input = "";
		}
		return input;
	}

	private String errorType(Connection conn , String errorCode)
	{
		String msgType = "";
		PreparedStatement pstmt = null ; 
		ResultSet rs = null;
		try
		{			
			String  sql = "SELECT MSG_TYPE FROM MESSAGES WHERE MSG_NO = ?";
			pstmt = conn.prepareStatement(sql);			
			pstmt.setString(1,errorCode);			
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				msgType = rs.getString("MSG_TYPE");
			}			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}		
		finally
		{
			try
			{
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}		
		return msgType;
	}
	
	
	private boolean isExist(Connection conn, String tableName, String columnName, String value) throws ITMException, RemoteException
	{
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		boolean status = false;
		try
		{
			sql = "SELECT count(*) from " + tableName + " where " + columnName + "  = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, value);
			rs = pstmt.executeQuery();

			if (rs.next())
			{
				if (rs.getBoolean(1))
				{
					status = true;
				}
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
		} catch (Exception e)
		{
			System.out.println("Exception in isExist ");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from isExist ");
		return status;
	}
	
	

 
}	