package ibase.webitm.ejb.sys;

import javax.ejb.Stateless;



import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.*;
import java.text.SimpleDateFormat;
import org.w3c.dom.*;

@javax.ejb.Stateless
public class MessagesLevel extends ValidatorEJB implements MessagesLevelLocal, MessagesLevelRemote 
{
	public String wfValData() throws RemoteException, ITMException 
	{
		return "";
	}

	public String wfValData(String xmlString, String xmlString1, String xmlString2, String objContext, String editFlag, String xtraParams) 
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String errString = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try {
			System.out.println("xmlString:::" + xmlString);
			if (xmlString != null && xmlString.trim().length() != 0)
			{
				dom = genericUtility.parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length() != 0) 
			{
				dom1 = genericUtility.parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length() != 0) 
			{
				dom2 = genericUtility.parseString(xmlString2);
			}
			errString = wfValData(dom, dom1, dom2, objContext, editFlag, xtraParams);
		} catch (Exception e) 
		{
			System.out.println("Exception : Cpp : wfValData(String xmlString) : ==>\n" + e.getMessage());
		}

		System.out.println("return from WsfaGradeTest wfValData");
		return (errString);
	}
	
	
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException, ITMException 
	{
		System.out.println("wfValData.....called..");
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr = 0, currentFormNo = 0, childNodeListLength = 0, cnt = 0;
		String loginSite = "", userId = "";
		String childNodeName = null;
		String childNodeValue = null;
		String errString = "";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		SimpleDateFormat simpleDateFormat = null;
		java.util.Date chgDate = null;
		java.util.Date toDate = null;
		try
		{
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");

			userId = getValueFromXTRA_PARAMS(xtraParams, "loginCode");
			loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
			GenericUtility genericUtility = GenericUtility.getInstance();

			simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());

			if (objContext != null && objContext.trim().length() > 0) 
			{
				currentFormNo = Integer.parseInt(objContext);
			}

			switch (currentFormNo)
			{ 
			
			    case 1:
				parentNodeList = dom.getElementsByTagName("Detail" + currentFormNo);
				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("idchildNodeName.editFlag." + childNodeName + ".." + editFlag);
					if (childNode.getFirstChild() != null) 
					{
						childNodeValue = childNode.getFirstChild().getNodeValue();
					}

					if (childNodeName.equalsIgnoreCase("msg_no")) 
					{
					
						if (childNode.getFirstChild() == null) 
						{
							errString = getErrorString("msg_no", "VMMSGNO", userId);
							break;
						} 
						
						else
						{
							if (!editFlag.equalsIgnoreCase("Ee")) 
							{
								int seriesCount = 0;
						String id = genericUtility.getColumnValue("msg_no", dom);
								String countSerSql = "SELECT COUNT(1) AS COUNT FROM messages WHERE msg_no = ?";
								PreparedStatement pstmtSeriesCnt = conn.prepareStatement(countSerSql);
								pstmtSeriesCnt.setString(1, id);
								ResultSet rsCnt = pstmtSeriesCnt.executeQuery();

								System.out.println("countSerSql" + countSerSql);
								if (rsCnt.next()) 
								{
									seriesCount = rsCnt.getInt("COUNT");
								}
								if (seriesCount == 0) 
								{
									errString = getErrorString("id", "VMMSGNULL", userId);
									break;
								}
							}
						}
					}

					
				
				}
				break;
			
			}// END SWITCH
		}// END TRY
		catch (Exception e) 
		{
			System.out.println("Exception ::" + e);
			e.printStackTrace();
			errString = e.getMessage();
		} 
		finally
		{
			try
			{
				if (conn != null)
				{
					conn.close();
				}
				conn = null;
			}
			catch (Exception d) 
			{
				d.printStackTrace();
			}
		}

		System.out.println("ErrString ::" + errString);
		return errString;
}// END OF VALIDATION

	@Override
	public String itemChanged(String xmlString, String xmlString1, String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException, ITMException 
	 {
		Document dom1 = null;
		Document dom = null;
		Document dom2 = null;
		String valueXmlString = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try 
		{
			if (xmlString != null && xmlString.trim().length() != 0)
			{
				dom = genericUtility.parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length() != 0)
			{
				dom1 = genericUtility.parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length() != 0) 
			{
				dom2 = genericUtility.parseString(xmlString2);
			}
			  valueXmlString = itemChanged(dom, dom1, dom2, objContext, currentColumn, editFlag, xtraParams);
		}
		catch (Exception e) 
		{
			System.out.println("Exception :WsfaGradeTest:itemChanged(String,String,String,String,String,String):" + e.getMessage() + ":");
			valueXmlString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("returning from WsfaGradeTest itemChanged");
		return (valueXmlString);
	 }

	@Override
	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException, ITMException 
	{
		StringBuffer valueXmlString = new StringBuffer();

		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		String sql = "";
		int currentFormNo = 0;

		GenericUtility genericUtility;
		SimpleDateFormat sdf;
		Timestamp timestamp;
		String  msg_no = "", msg_descr = "";

		try 
		{
			genericUtility = GenericUtility.getInstance();
			sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			timestamp = new Timestamp(System.currentTimeMillis());
			// siteCode = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");

			ConnDriver conndriver = new ConnDriver();
			conn = conndriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			conndriver = null;
			if (objContext != null && objContext.trim().length() > 0)
				currentFormNo = Integer.parseInt(objContext);

			currentColumn = currentColumn == null ? "" : currentColumn.trim();
			System.out.println("currentColumn : " + currentColumn);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			System.out.println("current form no: " + currentFormNo);
			System.out.println("dom:::::::::: : " + genericUtility.serializeDom(dom));
			System.out.println("dom11111111111111:::::::::: : " + genericUtility.serializeDom(dom1));
			System.out.println("dom222222222222222:::::::::: : " + genericUtility.serializeDom(dom2));

			switch (currentFormNo) 
			{
			case 1:
				valueXmlString.append("<Detail1>");
				if ("itm_default".equalsIgnoreCase(currentColumn))
				{
					System.out.println("itm_default : ");
					System.out.println("Content of xtraParams ..> " + xtraParams);
					//tranDate = (sdf.format(timestamp).toString()).trim();
				} 
				else if (currentColumn.trim().equalsIgnoreCase("msg_no")) 
				{
					msg_no = genericUtility.getColumnValue("msg_no", dom);
					msg_no = msg_no == null ? "" : msg_no.trim();
					sql = "select msg_descr from messages  where msg_no=?";
					pStmt = conn.prepareStatement(sql);
					pStmt.setString(1, msg_no);
					rs = pStmt.executeQuery();
					if (rs.next())
					{
						msg_descr = rs.getString(1);

					}
					rs.close();
					rs = null;
					pStmt.close();
					pStmt = null;

					msg_descr = msg_descr == null ? "" : msg_descr.trim();

					valueXmlString.append("<msg_descr>").append(msg_descr).append("</msg_descr>\r\n");
				} 
				valueXmlString.append("</Detail1 >");
				break;
			}
			valueXmlString.append("</Root>\r\n");
		}   
		catch (Exception e)
		{
			System.out.println("Exception :WsfaGradeTest :itemChange•••••••(Document,String):" + e.getMessage() + ":");
			valueXmlString.delete(0, valueXmlString.length());
			
			e.printStackTrace();
		}
		System.out.println("\n***** ValueXmlString :" + valueXmlString + ":*******");
		return valueXmlString.toString();
	}

	
}
			