<%
/*
 *	Name		:	EgModifyMeasuresJson.jsp
 *	Purpose		:	To modify test measures in IC
 */
%>

<%@ include file = "../../EgTrackerAJSON.jsp"%>
<%@ page language = "java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import = "egurkha.TestBean"%>
<%@ page import = "net.sf.json.JSONObject"%>
<%@ page import = "net.sf.json.JSONArray"%>
<%@ page import = "egurkha.*"%>
<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json;charset=UTF-8");
%>

<%!
	private String [] processes = {	"UNALTERED",
					"PERCENT_INCREASE",
					"PERCENT_DECREASE",
					"PERCENT_CHANGE",
					"RATIO",
					"RATE",
					"DIFFERENCE"
					};
//	private boolean isFirst = true;
//	private Hashtable factorsMapping = new Hashtable();
%>

<%
	JSONObject rootJSON = new JSONObject();
	rootJSON.put("success","true");
	JSONObject hiddens = new JSONObject();
	JSONObject obj = new JSONObject();
	boolean isDouble=EgDbInfo.doubleByteEnabled;
//	if(isFirst)
//	{
//		if(factors != null && factors.length > 0)
//		{
//			for (int i=0;i<factors.length;i++)
//			{
//				String var = (String) factors[i];
//				String val = iniUI.getValue("CONVERSION_FACTORS", var);
//				factorsMapping.put(val, var);
//			}
//			isFirst = false;
//		}
//	}

	IniFile iniUI = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_ui.ini");
	String factors [] = iniUI.getVariables("CONVERSION_FACTORS");

	IniFile iniFile=new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_udtests.ini");
	TestBean testBean = new TestBean(true);
	EgAggregateInfo aggregateInfo = new EgAggregateInfo("manager");
	String test = request.getParameter("test");
	String testType = testBean.getTestType(test);
	String mbean = request.getParameter("mbeanVal");
	String mbeanDisplayVal = request.getParameter("mbeanDispVal");
	
	String mname = request.getParameter("measure");
	if(mname == null || mname.length() == 0)
		mname = "";

	//session.setAttribute("factorsMapping", factorsMapping);

	String unitValue = request.getParameter("unit");
	unitValue=EgUIUtils.decodeSecurityChar(unitValue);
	
	if(mbean != null && mbean.length() > 0 && !mbean.equals(""))
		session.setAttribute("selectedMbean", mbean);
	if(mbeanDisplayVal != null && mbeanDisplayVal.length() > 0 && !mbeanDisplayVal.equals(""))
		session.setAttribute("mbeanDisplayVal", mbeanDisplayVal);
	else
		mbeanDisplayVal = (String) session.getAttribute("mbeanDispVal");

	String index = request.getParameter("index");
	String org_measure = request.getParameter("org_measure");
	org_measure=EgUIUtils.decodeSecurityChar(org_measure);
	boolean isConfigTest = EgInstallInfo.isConfigTest(test);
	String alarm_msg = request.getParameter("alarmdescription");
	//String domainName = request.getParameter("domain");
	String org_process = "";
	String org_factor = "";
	String org_counter = "";
	String org_oid = "";
	String process = "";
	String counter = "";
	String oid = "";
	String  fileName=request.getParameter("fileName");
	if((fileName==null || fileName.equals("")) && (mname!=null && mname.length()!=0))
		 fileName=iniFile.getValue("TEST_MIBFILE_MAPPING",test+"#"+mname+":ObjectOID");
	String tmpObj1 = testBean.getObject(test);
	String domainName = tmpObj1;

	//skm
	String testname = null;
	String aggregate_measure = null;
	String fname = null;
	List listOfMeasures = null;
	String[] listOfUnits = null;
	String[] tmpAggregateMeasures = null;
	String[] tmpFunctionNames = null;
	String[] function_names = null;
	String factor = null;
	String mfactor = "1";
//	String[] factors = {"1","MP"};
	List lst = aggregateInfo.getMeasurePeriod();
	String[] tmpFactors = null;
	String factor1 = null;
	String restdatafld="";
	String oldRESTDataField="";
	String restParentNodeDataField="";
	//skm

	Hashtable MBeanDetails = (Hashtable) session.getAttribute("MBeanDetails");
	Hashtable MBeans = new Hashtable();
	Hashtable attributeTable = new Hashtable();
	ArrayList attributes = new ArrayList();
	List mlist = new ArrayList();
	Hashtable beanName = new Hashtable();
	String selectedMbean = mbean;

	if(testType != null)
	{
		factor = request.getParameter("factor");
		if(factor == null || factor.length() == 0 || factor.equals("null") || factor.equals("none"))
			factor = "1";

		org_factor = request.getParameter("org_factor");
			if(org_factor == null || org_factor.length() == 0)
				org_factor = "";

		if(!testType.equalsIgnoreCase("Custom"))
		{
			org_process = request.getParameter("org_process");
			if(org_process == null || org_process.length() == 0)
				org_process = "";

			process = request.getParameter("process");
			if(process == null || process.length() == 0)
				process = "";

		}
		if(testType.equalsIgnoreCase("Perfmon") || testType.equalsIgnoreCase("Jmx"))
		{
			org_counter = request.getParameter("org_counter");
			if(org_counter == null || org_counter.length() == 0)
				org_counter = "";
			else
			{
				org_counter = EgUIUtils.decodeSecurityChar(org_counter);
				org_counter = org_counter.replace('*',' ');
				org_counter = org_counter.replace('|','%');
				org_counter = org_counter.trim();
			}

			counter = request.getParameter("counterVal");
			String mbeanSampleVal = request.getParameter("mbeanVal");

			if(counter == null || counter.length() == 0)
				counter = "";
			else
			{
				counter = EgUIUtils.decodeSecurityChar(counter);
				counter = counter.replace('*',' ');
				counter = counter.replace('|','%');
				counter = counter.trim();
			}
			mbean = request.getParameter("mbean");
			if(mbean == null || mbean.length() == 0)
			{
				mbean = "";
			}
		}
		if(testType.equalsIgnoreCase("Snmp"))
		{
			org_oid = request.getParameter("org_oid");
			if(org_oid == null || org_oid.length() == 0)
				org_oid = "";

			oid = request.getParameter("oid");
			if(oid == null || oid.length() == 0)
				oid = "";
		}		
		if(testType.equalsIgnoreCase("REST"))
		{
			oldRESTDataField = request.getParameter("oldRESTDataColumn");
			if(oldRESTDataField == null || oldRESTDataField.length() == 0)
				oldRESTDataField = "";

			restdatafld = request.getParameter("restdatafld");
			if(restdatafld == null || restdatafld.length() == 0)
				restdatafld = "";

			restParentNodeDataField= request.getParameter("dataFieldParentNode");
			if(restParentNodeDataField == null || restParentNodeDataField.length() == 0)
				restParentNodeDataField = "";
		}	
	}

	String from = request.getParameter("from");
	String callPage = from + "?test=" + test + "&testtype=" + testType;

	ArrayList units = testBean.getAllUnits();
	Collections.sort(units);

	boolean otherUnit = false;
	if(units != null && units.size() > 0)
	{
	//	if(!units.contains(unitValue))
	//			otherUnit = true;

		if(unitValue.equals("|"))
		{
			unitValue = unitValue.replace('|','%');
			if(org_measure.indexOf("|") > -1)
				org_measure = org_measure.replace('|','%'); 
		}
		else
		{
			if(org_measure.indexOf("|") > -1)
				org_measure = org_measure.replace('|','%'); 
		}

		if(!units.contains(unitValue))
			otherUnit = true;
	}
	if((MBeanDetails != null && MBeanDetails.size() > 0) && domainName != null)
	{
		MBeans = (Hashtable) MBeanDetails.get(domainName);
		String tmpObj = testBean.getObject(test);
		if(!tmpObj.equals(domainName))
			testBean.setObject(test, domainName);
		MBeanDetails = null;
	}
	if(MBeans != null && MBeans.size() > 0)
	{
		Set keys = MBeans.keySet();
		Iterator itr = keys.iterator();
		while(itr.hasNext())
		{
			StringBuffer sb = new StringBuffer();
			String mBean = (String) itr.next();
			StringTokenizer st = new StringTokenizer(mBean, ",");
			while(st.hasMoreTokens())
			{
				String str = st.nextToken();
				if(str != null && str.indexOf("=") > -1)
				{
					sb.append(str.substring(str.indexOf("=")+1));
					if(st.hasMoreTokens())
						sb.append(":");
				}
			}
			mlist.add(sb.toString());
			beanName.put(sb.toString(), mBean);
		}
	}
	if(selectedMbean != null && selectedMbean.length() > 0 && !selectedMbean.equals("null") && !selectedMbean.equals("-1"))
	{
		attributeTable = (Hashtable) MBeans.get(selectedMbean);
		if(attributeTable!=null && attributeTable.size() > 0)
		{
			Set attributeSet = attributeTable.keySet();
			Iterator itr = attributeSet.iterator();
			while(itr.hasNext())
			{
				String attribute = (String) itr.next();
				String value = (String) attributeTable.get(attribute);
				if(value != null)
				{
					if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false"))
					{
						attributes.add(attribute);
					}
					else
					{
						try
						{
							double dd = Double.parseDouble(value);
						}
						catch(Exception e)
						{
							continue;
						}
						attributes.add(attribute);
					}
				}
			}
		}
	}

	Collections.sort(mlist);

	String size1 = "";
	String size2 = "";
	String size3 = "";
	String size4 = "";
	String size5 = "";

	String dbsize = request.getParameter("dbsize");
	dbsize=EgUIUtils.decodeSecurityChar(dbsize);
	if(dbsize != null && dbsize.length() != 0)
	{
		if(dbsize.equals("Number(7,4)"))
			size1 = "selected";
		else if(dbsize.equals("Number(20,4)"))
			size2 = "selected";
		else if(dbsize.equals("Number(16)"))
			size3 = "selected";
	}

	String originalFactor = "";
	String [] factorSel = new String[factors.length];
	for(int i=0; i<factorSel.length; i++)
	{
		if(iniUI.getValue("CONVERSION_FACTORS",(String)factors[i]).equals(factor))
		{
			factorSel[i] = "selected";
			originalFactor = (String) factor;
		}
		else
			factorSel[i] = "";
	}
	boolean isFactorEnabled = false;
	if(process == null)
	{
		isFactorEnabled = true;
	}
	else if(process.equals("UNALTERED") || process.equals("DIFFERENCE"))
	{
		isFactorEnabled = true;
	}
	
	String originalProcess = "";
	String [] procSel = new String[processes.length];
	for(int i=0; i<procSel.length; i++)
	{
		if(processes[i].equals(process))
		{
			procSel[i] = "selected";
			originalProcess = processes[i];
		}
		else
			procSel[i] = "";
	}

	Hashtable procVals = testBean.getProcesses(test);
	Hashtable factVals = testBean.getFactors(test);
	
	hiddens.put("fileName",fileName);
	hiddens.put("configtestflag",(isConfigTest?"true":""));

	rootJSON.put("test",test);
	rootJSON.put("testType",testType);
	rootJSON.put("measureName",mname);
	rootJSON.put("index",index);

	String reloaded = request.getParameter("name");
	if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
	{
		hiddens.put("$_s_measure","Measure name$"+request.getParameter("originalMeasure"));
	}
	else
	{
		hiddens.put("$_s_measure","Measure name$"+mname);	
	}

	if(!isConfigTest)
	{
		String temp = "";
		if(size1.equalsIgnoreCase("selected"))
			temp = "Number(7,4)";
		if(size2.equalsIgnoreCase("selected"))
			temp = "Number(20,4)";
		if(size3.equalsIgnoreCase("selected"))
			temp = "Number(16)";

		JSONArray dbCols = new JSONArray();
		String num = _mapping.getTextDescription("admin.ic.Number");
		String[] dbColsDisp = new String[]{num+"(7,4)",num+"(20,4)",num+"(16)"};
		String[] dbColsVal = new String[]{"Number(7,4)","Number(20,4)","Number(16)"};
		for (int i=0; i<dbColsDisp.length; i++)
		{
			obj = new JSONObject();
			obj.put("displayField",dbColsDisp[i]);
			obj.put("valueField",dbColsVal[i]);
			dbCols.add(obj);
		}
		rootJSON.put("dbColSizeList",dbCols);
		rootJSON.put("dbColSizeSelected",dbsize);

		if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
		{
			hiddens.put("$_s_dbsize","Database column size$"+request.getParameter("originalDB"));
		}
		else
		{
			hiddens.put("$_s_dbsize","Database column size$"+EgUIUtils.encodeSecurityChar(temp));
		}
	}


	if(!isConfigTest)
	{
		String temp = "";
		JSONArray unitsArr = new JSONArray();
		obj = new JSONObject();
		obj.put("displayField",_mapping.getTextDescription("admin.ic.Please select a unit"));
		obj.put("valueField","-1");
		unitsArr.add(obj);
		rootJSON.put("unitSelected","-1");
		String selected = "";
		if(units != null && units.size() > 0)
		{
			for(int i=0; i<units.size(); i++)
			{
				String un = (String) units.get(i);
			//	if(un.equals("%"))
			//		continue;
				if(un.equals(unitValue))
				{
					temp = un;
					rootJSON.put("unitSelected",un);
				}
				obj = new JSONObject();
				obj.put("displayField",un);
				obj.put("valueField",un);
				unitsArr.add(obj);
			}
		}
		if(otherUnit)
		{
			rootJSON.put("unitSelected","other");
		}
		obj = new JSONObject();
		obj.put("displayField",_mapping.getTextDescription("admin.ic.Other"));
		obj.put("valueField","other");
		unitsArr.add(obj);
		rootJSON.put("unitsList",unitsArr);

		if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
		{
			hiddens.put("$_s_un","Unit$"+request.getParameter("originalUnit"));
		}
		else
		{
			hiddens.put("$_s_un","Unit$"+unitValue);
		}
		if(otherUnit)
		{
			rootJSON.put("showOtherTextBox",true);
			if(unitValue.equalsIgnoreCase("other"))
				unitValue = "";
			rootJSON.put("otherUnitValue",unitValue);
			hiddens.put("$_s_otherunit","Other$"+unitValue);
		}
	}	

	if(testType.equalsIgnoreCase("Custom"))
	{
		rootJSON.put("hideConvFactor",false);
		JSONArray convFacts = new JSONArray();
		String tmpFact = "none";
		for(int i=0; i<factors.length; i++)
		{
			String fact = iniUI.getValue("CONVERSION_FACTORS", factors[i].toString());
			String tmpSel = factorSel[i];
			if(tmpSel.equals("selected"))
			{
				tmpFact = fact;
				rootJSON.put("convFactorSelected",fact);
			}
			obj = new JSONObject();
			obj.put("valueField",fact);
			obj.put("displayField",factors[i].toString());
			convFacts.add(obj);
		}
		rootJSON.put("convFactorList",convFacts);
		//hiddens.put("s_factor",tmpFact);
		hiddens.put("$_cFactor","'Conversion Factor$"+request.getParameter("originalFactor"));
	}
	else
	{
		rootJSON.put("hideConvFactor",true);
	}
	if((alarm_msg != null) && !(alarm_msg.equalsIgnoreCase("null")))
	{
		rootJSON.put("hideAlarmDispStr",false);
		rootJSON.put("alarmDispStrValue",alarm_msg);
	
		if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
		{
			hiddens.put("$_alarmdescription","Alarm display string$"+request.getParameter("originalAlarm"));
		}
		else
		{
			hiddens.put("$_alarmdescription","Alarm display string$"+alarm_msg);
		}
	}
	else
	{
		rootJSON.put("alarmDispStrValue","");
	}
	if(testType != null && !testType.equalsIgnoreCase("Custom"))
	{
		JSONArray processArr = new JSONArray();
		rootJSON.put("selectedProcessMethod",processes[0]);
		String temp = "";
		for(int i=0; i<processes.length; i++)
		{
			obj = new JSONObject();
			String tmpProc = processes[i];
			String tmpSel = procSel[i];
			obj.put("displayField",tmpProc);
			obj.put("valueField",tmpProc);

			if(tmpSel.equalsIgnoreCase("selected"))
			{
				rootJSON.put("selectedProcessMethod",tmpProc);
				temp = tmpProc;
			}
			processArr.add(obj);
		}
		rootJSON.put("processMethodList",processArr);

		//if(isFactorEnabled)
		{		
			JSONArray convFacts = new JSONArray();
			String tmpFact = "none";
			for(int i=0; i<factors.length; i++)
			{
				String fact = iniUI.getValue("CONVERSION_FACTORS", factors[i].toString());
				String tmpSel = factorSel[i];
				if(tmpSel.equals("selected"))
				{
					tmpFact = fact;
					rootJSON.put("convFactorSelected",fact);
				}
				obj = new JSONObject();
				obj.put("valueField",fact);
				obj.put("displayField",factors[i].toString());
				convFacts.add(obj);
			}
			rootJSON.put("convFactorList",convFacts);
			//hiddens.put("s_factor",tmpFact);
		}
		if (isFactorEnabled)
		{
			rootJSON.put("hideConvFactor",false);
			hiddens.put("$_cFactor","Conversion Factor$"+request.getParameter("originalFactor"));
		}
		if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
		{
			hiddens.put("$_s_process","Process method$"+request.getParameter("originalProcess"));
		}
		else
		{
			hiddens.put("$_s_process","Process method$"+temp);
		}

		if(testType != null)
		{
			rootJSON.put("showCounterName",false);
			rootJSON.put("showAttrributeName",false);
			rootJSON.put("showObjectOID",false);
			
			if(testType.equalsIgnoreCase("Perfmon"))
			{
				rootJSON.put("showCounterName",true);
				rootJSON.put("counterValue",counter);
			}
		    else if(testType.equalsIgnoreCase("Jmx"))
			{
				rootJSON.put("showAttrributeName",true);
				rootJSON.put("attributeValue",counter);
			}
			else if(testType.equalsIgnoreCase("Snmp"))
			{
				rootJSON.put("showObjectOID",true);
				rootJSON.put("oidValue",oid);
			}
			else if(testType.equalsIgnoreCase("REST"))
			{
				rootJSON.put("showRESTDataField",true);
				rootJSON.put("restDataFld",restdatafld);
				rootJSON.put("restParentNodeDataField",EgUIUtils.encodeSecurityChar(restParentNodeDataField));
			}
		}

		if(testType.equalsIgnoreCase("REST"))
		{
			if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
			{
				String tmpRestFld = request.getParameter("oldRESTDataField");
				if(tmpRestFld != null && tmpRestFld.indexOf(":") !=-1)
					tmpRestFld = tmpRestFld.substring(tmpRestFld.indexOf(":")+1);
				hiddens.put("$_s_restdatafld","REST Data Column$"+EgUIUtils.encodeSecurityChar(tmpRestFld));
			}		
			else
			{
				String tmpRestFld = restdatafld;
				if(tmpRestFld != null && tmpRestFld.indexOf(":") !=-1)
					tmpRestFld = tmpRestFld.substring(tmpRestFld.indexOf(":")+1);

				hiddens.put("$_s_restdatafld","REST Data Column$"+tmpRestFld);
			}
			//hiddens.put("restParentNode","");
		}
			
		
		if(testType.equalsIgnoreCase("Snmp"))
		{
			if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
			{
				String tempOid = request.getParameter("org_oid");
				if(tempOid != null && tempOid.indexOf(":") !=-1)
					tempOid = tempOid.substring(tempOid.indexOf(":")+1);
				hiddens.put("$_s_oid","Object ID$"+tempOid);
			}		
			else
			{
				String tempOid = oid;
				if(tempOid != null && tempOid.indexOf(":") !=-1)
					tempOid = tempOid.substring(tempOid.indexOf(":")+1);
				hiddens.put("$_s_oid","Object ID$"+tempOid);
			}
		}	

		if(testType.equalsIgnoreCase("Perfmon") || testType.equalsIgnoreCase("Jmx"))
		{
			if(reloaded != null && reloaded.equalsIgnoreCase("jamesthomas"))
			{
				String tempCounter = request.getParameter("org_counter");
				if(tempCounter != null && tempCounter.indexOf(":") !=-1)
					tempCounter = tempCounter.substring(tempCounter.indexOf(":")+1);
				hiddens.put("$_s_counter","Counter name$"+tempCounter);
			}
			else
			{
				String tempCounter = counter;
				if(tempCounter != null && tempCounter.indexOf(":") !=-1)
					tempCounter = tempCounter.substring(tempCounter.indexOf(":")+1);
				hiddens.put("$_s_counter","Counter name$"+tempCounter);
			}
		}	
	}
	rootJSON.put("mBeansListShow",false);
	boolean doesMbeanExist = testBean.doesMbeanExist(test,mname);
	rootJSON.put("doesMbeanExist",doesMbeanExist);
	Hashtable mbeanMeasure = testBean.getMbean(test);
	Enumeration enumVal = mbeanMeasure.keys();
	while(enumVal.hasMoreElements())
	{
		String mKey = (String) enumVal.nextElement();
		String mValue = (String) mbeanMeasure.get(mKey);
		if(mKey.equalsIgnoreCase(mname))
		{
			rootJSON.put("previousMBeanSelected",mValue);
		}
	}
	
	if(mlist != null && mlist.size() > 0)
	{
		rootJSON.put("mBeansListShow",true);
		JSONArray mBeansArr = new JSONArray();
		obj = new JSONObject();
		obj.put("displayField",_mapping.getTextDescription("admin.ic.Please select an MBean"));
		obj.put("valueField","-1");
		mBeansArr.add(obj);
		rootJSON.put("mBeanSelected","-1");
		String tmpSelect = "";
		for(int i=0; i<mlist.size(); i++)
		{
			String mBean = (String) mlist.get(i);
			String mapStr = (String) beanName.get(mBean);
			
			if(mapStr.equals(selectedMbean))
			{
				rootJSON.put("mBeanSelected",mapStr);
			}
			obj = new JSONObject();
			obj.put("displayField",mBean);
			obj.put("valueField",mapStr);
			mBeansArr.add(obj);
		}
		rootJSON.put("mBeansList",mBeansArr);
	}
	
	if(attributes != null && attributes.size() > 0)
	{
		rootJSON.put("attributesInputType","combo");
		JSONArray attrArr = new JSONArray();
		obj = new JSONObject();
		obj.put("displayField",_mapping.getTextDescription("admin.ic.Please select an attribute"));
		obj.put("valueField","-1");
		attrArr.add(obj);
		rootJSON.put("attributeSelected","-1");

		for(int i=0; i<attributes.size(); i++)
		{
			String attr = (String) attributes.get(i);
			obj = new JSONObject();
			obj.put("displayField",attr);
			obj.put("valueField",attr);
			if(counter.equals(attr))
			{
				rootJSON.put("attributeSelected",counter);
			}
			attrArr.add(obj);
		}
		rootJSON.put("attributesList",attrArr);
		//hiddens.put("counter","");
	}
	else
	{
		rootJSON.put("attributesInputType","text");
		rootJSON.put("attributeValue",counter);
	}
	hiddens.put("test",test);
	hiddens.put("testtype",testType);
	hiddens.put("org_measure",org_measure);
	hiddens.put("org_process",org_process);
	hiddens.put("org_factor",org_factor);
	hiddens.put("org_counter",org_counter);
	hiddens.put("org_oid",org_oid);
	hiddens.put("oldRESTDataColumn",oldRESTDataField);
	hiddens.put("index",index);
	hiddens.put("mbean",mbean);
	hiddens.put("mbeanDispVal",mbeanDisplayVal);
	hiddens.put("from",from);
	hiddens.put("callPage",callPage);

	rootJSON.put("hiddens",hiddens);
	out.println(rootJSON);

%>



