package ibase.webitm.ejb.sys;

import javax.ejb.Stateless;



import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.*;
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;
		String chgUser="";
		String sql="";
		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("profile_id")) 
				{
						String profileId = genericUtility.getColumnValue("profile_id", dom);
						profileId = profileId == null ? "" : profileId.trim();
						if (profileId.length() == 0) {
							errString = getErrorString("profile_id", "VMPFINO", userId);
							break;
						}  
						else
						{
							sql = null;
							sql = "select count(1) count from messages_level where profile_id = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, profileId);
							rs = pstmt.executeQuery();

							if (rs.next()) {
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if (cnt == 0) {
								errString = getErrorString("profile_id", "VMPFIV", userId);
								break;
							}
						}
						profileId = null;
					}
					
					else if (childNodeName.equalsIgnoreCase("msg_no")) {
						String msgNo = genericUtility.getColumnValue("msg_no", dom);
						msgNo = msgNo == null ? "" : msgNo.trim();
						if (msgNo.length() == 0) 
						{
							errString = getErrorString("msg_no", "VMMSGNULL", userId);
							break;
						}  
						else
						{
						//	if (!editFlag.equalsIgnoreCase("E")) 
							//{
							sql = null;
							sql = "select count(1) count from messages where msg_no = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, msgNo);
							rs = pstmt.executeQuery();

							if (rs.next()) {
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if (cnt == 0) {
								errString = getErrorString("msg_no", "VMMSGNO", userId);
								break;
							}
				//	}
						}
						msgNo = null;
					}
				

					else if (childNodeName.equalsIgnoreCase("win_name")) {
						String winName = genericUtility.getColumnValue("win_name", dom);
						winName = winName == null ? "" : winName.trim();
						if (winName.length() == 0) {
							errString = getErrorString("win_name", "VMWNMNO", userId);
							break;
						}  
						else
						{
							sql = null;
							sql = "select count(1) count from messages_level where win_name = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, winName);
							rs = pstmt.executeQuery();

							if (rs.next()) {
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if (cnt == 0) {
								errString = getErrorString("win_name", "VMWINNMIV", userId);
								break;
							}
							else
							{
								int a = 0;
								String id1 = genericUtility.getColumnValue("profile_id", dom);
								String id2 = genericUtility.getColumnValue("msg_no", dom);
								String id3 = genericUtility.getColumnValue("win_name", dom);
								 sql = "SELECT COUNT(1) AS COUNT FROM messages_level WHERE profile_id = ? AND msg_no= ? AND win_name= ? ";
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,id1);
								pstmt.setString(2,id2);
								pstmt.setString(3,id3);
								rs = pstmt.executeQuery();

								System.out.println("Sql" + sql);
								if (rs.next()) {
									a = rs.getInt("COUNT");
								}
								if (a > 0) 
								{
						
									errString = getErrorString("", "VMALEXTDB", chgUser);						
									break;
								}
							
							}
						}
						winName = null;
					}
				
				}
				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();
	}

	
}
			