<%@ include file="../../EgTrackerAJSON.jsp"%>
<%@ page import ="java.util.ArrayList"%>
<%@ page import="com.egurkha.*"%>
<%@ page import="egurkha.EgUIUtils"%>
<%@ page import = "java.text.SimpleDateFormat"%>
<%@ page import = "java.text.DateFormat"%>
<%@ page import = "com.egurkha.ini.IniUI"%>
<%@ page import = "com.egurkha.util.DateUtil"%>
<%@ page import = "com.egurkha.util.EgSorter"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import = "net.sf.json.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="port" scope="page" class="egurkha.ServiceBean"/>

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json");
	JSONObject rootObj = new JSONObject();
	JSONObject tmpObj = new JSONObject();
	JSONObject viewasObj = new JSONObject();
	JSONObject searchObj = new JSONObject();
	JSONObject timelineObj = new JSONObject();
	JSONObject columnDataObj = new JSONObject();
	JSONArray gridArray = new JSONArray();
	JSONArray viewasArr = new JSONArray();
	JSONArray searchArr = new JSONArray();
	JSONArray timelineArr = new JSONArray();
	
	String selectedNode = request.getParameter("selectedNode");
	String from=request.getParameter("from");
	String choosenCompType=request.getParameter("choosenCompType");
	String searchVal =  request.getParameter("nickSearch");
	String searchType =  request.getParameter("searchType");
	String fromDateSearch =  request.getParameter("fromdateSearch");
	fromDateSearch = EgUIUtils.decodeSecurityChar(fromDateSearch);
	String todateSearch =  request.getParameter("todateSearch");
	todateSearch = EgUIUtils.decodeSecurityChar(todateSearch);
	String timelineVal =  request.getParameter("timelineVal");
	String value =  request.getParameter("value");
	ArrayList dateArrList = new ArrayList();
	searchVal=(searchVal==null)?"":searchVal;
	searchVal = EgUIUtils.decodeSecurityChar(searchVal);

	EgDiscInfo discInfo = new EgDiscInfo("manager");
	Hashtable allNickIpMap = discInfo.getNickIpMap(true,"");

	if (selectedNode == null || selectedNode.equals("null") || selectedNode.length() == 0)
	{
		selectedNode = "node1";
	}
	IniFile egAutoDelete = new IniFile(EgConfigInfo.egMgrConfigDir + "eg_autodelete.ini");
	String[] firstDiscoveryDetails = egAutoDelete.getVariables("Components_Firsttime_Discovery_Details");
	String[] reDiscoveryDetails = egAutoDelete.getVariables("Components_Discovery_Details");

	//Pdf
	String tmp=(String)request.getQueryString();
	String mngrIProc = new IniFile(new EgConfigInfo("manager").getDbFile()).getValue("DB_PROPERTIES","ManagerUrl");
	String htmlFileName =mngrIProc+"/final/admin/EgDiscoveredComponentPdf.jsp;jsessionid="+session.getId()+"?"+tmp+"&print=pdfpage";
	session.setAttribute("PDF_URL",htmlFileName);
	session.setAttribute("REPORT_NAME","Discovered_Components_Report");
	session.setAttribute("REPORT_LAYOUT","Portrait");
	session.setAttribute("PAGE_WIDTH_PDF","1100");
	
	ArrayList firstDiscoverdCompArrList = new ArrayList();
	ArrayList reDiscoveryArr = new ArrayList();
	for(int j=0; j<reDiscoveryDetails.length; j++)
	{
		String reDiscDetail = reDiscoveryDetails[j];
		reDiscoveryArr.add(reDiscDetail);
	}
	IniUI iniUI = IniUI.getInstance();
	String userLanguage = iniUI.getDefaultLanguage();
	if (_user != null)
	{
		userLanguage = _user.getLanguage();
	}
	String userDateFormat = _user.getUserDateFormat();
	String userTimeFormat = _user.getUserTimeZone();
	if(userTimeFormat!=null && (userTimeFormat.equalsIgnoreCase("default") || com.egurkha.util.StringUtils.isNull(userTimeFormat)))
	{
		EgDateInfo egDate = new EgDateInfo();
		TimeZone tmz = egDate.getRealTimeZone();
		userTimeFormat = tmz.getID();
	}
	String userDateTimeFormat = userDateFormat+" HH:mm:ss";
	TimeZone timeZone = TimeZone.getTimeZone(userTimeFormat);
	MappingNameSorter sorter = new MappingNameSorter(userLanguage);
	int totalDiscCount = 0;
	viewasObj.put("displayField","All");
	viewasObj.put("valueField","All");
	viewasArr.add(viewasObj);
	ArrayList firstDiscArr = new ArrayList(Arrays.asList(firstDiscoveryDetails));
	ArrayList sortedComponents = sorter.getSortedCompTypesWithNick(firstDiscArr);
	ArrayList totalComponents = new ArrayList();
	ArrayList choosenCompArr = new ArrayList();
	ArrayList serachCompList = new ArrayList();
	ArrayList totalGridArr = new ArrayList();
	String csvDuration = "";
	
	for(int h=0; h<sortedComponents.size(); h++)
	{
		String startdate="";
		String date="";
		String firstdiscDetail = (String)sortedComponents.get(h);
		String firstDiscTime = egAutoDelete.getValue("Components_Firsttime_Discovery_Details",firstdiscDetail);
		totalGridArr.add(firstDiscTime);
		//  startdate = getDateValue(firstDiscTime,userDateFormat,userTimeFormat); 
		if(reDiscoveryArr.contains(firstdiscDetail))
		{
			String reDiscTime = egAutoDelete.getValue("Components_Discovery_Details",firstdiscDetail);
			totalGridArr.add(reDiscTime);
			//date = getDateValue(reDiscTime,userDateFormat,userTimeFormat); 
		}
		else
		{
			totalGridArr.add(firstDiscTime);
		//	date=startdate;
		}
		StringTokenizer strTok = new StringTokenizer(firstdiscDetail,":");
		//while(strTok.hasMoreTokens())
		if(strTok.countTokens() > 2)
		{
			String SID = "-";
			String compType = strTok.nextToken();
			String compTypeDisplay = _mapping.getComponentTypeDescription(compType);
			String nick = strTok.nextToken();
			String portVal = strTok.nextToken();
			String tempPortVal = portVal;
			totalGridArr.add(compTypeDisplay);
			totalGridArr.add(nick);
			if (strTok.hasMoreTokens())
			{
				SID = strTok.nextToken();
				tempPortVal+="^"+SID;
			}
			totalGridArr.add(tempPortVal);
			String discFrom = "";
			String discFromHostIp="";
			String discByNick = "";
			String AgentUser = "";
			String discFromDetail = egAutoDelete.getValue("Components_Discovered_From_Details",firstdiscDetail);
			if(discFromDetail!=null && discFromDetail.length() > 0)
			{
				int ind =discFromDetail.indexOf("#:#");
				discFrom = discFromDetail.substring(0,ind);
				totalGridArr.add(discFrom);  //Agent or Manager
				discFromDetail= discFromDetail.substring(ind+3);
				int secind = discFromDetail.indexOf("#:#");
				discFromHostIp = discFromDetail.substring(0,secind);
				totalGridArr.add(discFromHostIp);
				discFromDetail= discFromDetail.substring(secind+3);
				int thridx = discFromDetail.lastIndexOf("#:#");
				discByNick = discFromDetail.substring(0,thridx);
				discByNick=discByNick.replace(",","#:#");
				totalGridArr.add(discByNick);
				AgentUser = discFromDetail.substring(thridx+3);
				totalGridArr.add(AgentUser);
			}
			else
			{
				totalGridArr.add("-");  //Agent or Manager
				totalGridArr.add("-");
				totalGridArr.add("-");
				totalGridArr.add("-");
			}

			String compNameWithoutType = (compType.equalsIgnoreCase("oracle_server")) ? nick+":"+portVal+":"+SID : nick+":"+portVal;
			boolean isExistInDiscoverIni = discInfo.isCompAlreadyExist(compType,compNameWithoutType); //isComponentExist
			if(isExistInDiscoverIni)
			{
			//	String components = compTypeDisplay+"#"+nick+"#"+portVal+"#"+SID+"#"+startdate+"#"+date;
			//	totalComponents.add(components);
				if(!firstDiscoverdCompArrList.contains(compType))
				{
					firstDiscoverdCompArrList.add(compType);
				}
			}
		}
	}
//	if(searchType!=null && searchType.equalsIgnoreCase("Last discovered time"))
	if(totalGridArr.size() >0 )
	{
		int colNumberVal = 9;
		EgSorter egsort = new EgSorter(totalGridArr,colNumberVal);
		List sortedlist = egsort.sort(1,"long","DESC"); //1 - sort according to the First Discovery Time
		totalComponents.clear();
		int sortedListSize=sortedlist.size();
		for(int b=0; b<sortedListSize; b=b+9)
		{
			String start = (String)sortedlist.get(b);
			String end = (String)sortedlist.get(b+1);
			String comp = (String)sortedlist.get(b+2);
			String nick = (String)sortedlist.get(b+3);
			String portSort = (String)sortedlist.get(b+4);
			String discoverBy = (String)sortedlist.get(b+5);
			String discoverByIp = (String)sortedlist.get(b+6);
			String discoverybynick = (String)sortedlist.get(b+7);
			discoverybynick=discoverybynick.replace("#:#",", ");
			String discoverybyUser = (String)sortedlist.get(b+8);
			String sid = "-";
			int portSortIndex=portSort.indexOf("^");
			//if (_mapping.getReverseComponentTypeDescription(comp).toLowerCase().equalsIgnoreCase("oracle_server"))
			if(portSortIndex!=-1)
			{
				sid=portSort.substring(portSortIndex+1);
				portSort=portSort.substring(0,portSortIndex);
			}
			String firstDate = getDateValue(start,userDateFormat,userTimeFormat); 
			String lastDate = getDateValue(end,userDateFormat,userTimeFormat); 
			String totComp = comp+"#::#"+nick+"#::#"+portSort+"#::#"+sid+"#::#"+firstDate+"#::#"+lastDate+"#::#"+discoverBy+"#::#"+discoverByIp+"#::#"+discoverybynick+"#::#"+discoverybyUser;
			totalComponents.add(totComp);
		}
	}
	if(from!= null && from.equalsIgnoreCase("combo"))
	{
		String nick="";
		String portVal="";
		String sid = "";
		String components="";
		String startdate="";
		String endDateTime="";
		String disBy = "";
		String disByIp = "";
		String disByNick = "";
		String disByuser = "";
		choosenCompType = _mapping.getComponentTypeDescription(choosenCompType);
		for(int l=0; l<totalComponents.size(); l++)
		{
			String comp = (String)totalComponents.get(l);
			ArrayList tokList = StringUtils.egTokenizer(comp,"#::#");
			Iterator tok = tokList.iterator();
			while(tok.hasNext())
			{
				String compType =(String) tok.next();
				//choosenCompType = _mapping.getComponentTypeDescription(choosenCompType);
				if(choosenCompType.equalsIgnoreCase(compType))
				{
					nick =(String) tok.next();
					portVal =(String) tok.next();
					sid =(String) tok.next();
					startdate =(String) tok.next();
					endDateTime =(String) tok.next();
					disBy =(String) tok.next();
					disByIp =(String) tok.next();
					disByNick =(String) tok.next();
					disByuser =(String) tok.next();
					components = compType+"#::#"+nick+"#::#"+portVal+"#::#"+sid+"#::#"+startdate+"#::#"+endDateTime+"#::#"+disBy+"#::#"+disByIp+"#::#"+disByNick+"#::#"+disByuser;
					choosenCompArr.add(components);
				}
				else if(choosenCompType.equalsIgnoreCase("All"))
				{
					nick = (String) tok.next();
					portVal =(String) tok.next();
					sid =(String) tok.next();
					startdate =(String) tok.next();
					endDateTime =(String) tok.next();
					disBy =(String) tok.next();
					disByIp =(String) tok.next();
					disByNick =(String) tok.next();
					disByuser =(String) tok.next();
					components = compType+"#::#"+nick+"#::#"+portVal+"#::#"+sid+"#::#"+startdate+"#::#"+endDateTime+"#::#"+disBy+"#::#"+disByIp+"#::#"+disByNick+"#::#"+disByuser;
					choosenCompArr.add(components);
				}
				else
				{
					nick =(String) tok.next();
					portVal =(String) tok.next();
					sid =(String) tok.next();
					startdate =(String) tok.next();
					endDateTime =(String) tok.next();
					disBy =(String) tok.next();
					disByIp =(String) tok.next();
					disByNick =(String) tok.next();
					disByuser =(String) tok.next();
				}
			}
		}
		totalComponents.clear();
		totalComponents.addAll(choosenCompArr);

		if(value!=null && value.equalsIgnoreCase("searchbox") && (searchVal!=null && !searchVal.equals("") || (fromDateSearch != null && !fromDateSearch.equals("null") && todateSearch != null && !todateSearch.equals("null"))))
		{
			boolean isSearchResult =(searchVal!=null);
			ArrayList searchList = new ArrayList();
			if (isSearchResult)
			{
				StringTokenizer st = new StringTokenizer(searchVal);
				while (st.hasMoreTokens())
				{
					String thisToken = st.nextToken().trim();
					if (thisToken.equals(""))
						continue;
					searchList.add(thisToken);
				}
			}
			for(int k=0; k<totalComponents.size(); k++)
			{
				String comp = (String)totalComponents.get(k);
				ArrayList tokList = StringUtils.egTokenizer(comp,"#::#");
				Iterator tok = tokList.iterator();
				while(tok.hasNext())
				{
					String compType =(String) tok.next();
					nick =(String) tok.next();
					Enumeration nickEnum = allNickIpMap.keys();
					String ipVal = "-";
					while(nickEnum.hasMoreElements())
					{
						String nickKey=(String) nickEnum.nextElement();
						if(nick.equalsIgnoreCase(nickKey))
						{
							ipVal =(String)allNickIpMap.get(nickKey);
						}
					}
					portVal =(String) tok.next();
					sid =(String) tok.next();
					startdate =(String) tok.next();
					endDateTime =(String) tok.next();
					disBy =(String) tok.next();
					disByIp =(String) tok.next();
					disByNick =(String) tok.next();
					disByuser =(String) tok.next();
					boolean exist = false;
					if(searchType!=null && searchType.equalsIgnoreCase("Nick name"))
					{
						exist = doesExist(nick,searchList);
					}
					else if(searchType!=null && searchType.equalsIgnoreCase("HOST IP/NAME"))
					{
						exist = doesExist(ipVal,searchList);
					}
					else if(searchType!=null && searchType.equalsIgnoreCase("Component type"))
					{
						exist = doesExist(compType,searchList);
					}
					else if(searchType!=null && searchType.equalsIgnoreCase("Last discovered time"))
					{
						if(endDateTime != "-" && endDateTime!=null)
						{
							StringTokenizer dateTok = new StringTokenizer(endDateTime," ");
							while(dateTok.hasMoreTokens())
							{
								String dateOnly = dateTok.nextToken();
								String time = dateTok.nextToken();
								exist = doesDateExist(endDateTime,fromDateSearch,todateSearch,timeZone,userDateTimeFormat,timelineVal);
							}
						}
					}
					if(exist)
					{
						components = compType+"#::#"+nick+"#::#"+portVal+"#::#"+sid+"#::#"+startdate+"#::#"+endDateTime+"#::#"+disBy+"#::#"+disByIp+"#::#"+disByNick+"#::#"+disByuser;
						serachCompList.add(components);
					}
				}
			}
			totalComponents.clear();
			totalComponents.addAll(serachCompList);
		}
	}
	int totalCompSize = 0;//totalComponents.size();
//	totalDiscCount = totalCompSize;
	String firstDiscoveredDate = "";
	String lastDisoveredDate = "";
	
	if(searchType!=null && searchType.equalsIgnoreCase("Last discovered time") && timelineVal != null && !timelineVal.equalsIgnoreCase("Any"))
	{
		String timeLines[] = setStartAndEndDateTime(timelineVal,userDateFormat);
		firstDiscoveredDate = timeLines[0];
		lastDisoveredDate = timeLines[1];
		csvDuration =  "Timeline"+","+timelineVal+","+"From : "+firstDiscoveredDate+","+" To : "+lastDisoveredDate;
	}
	else if(searchType!=null && searchType.equalsIgnoreCase("Last discovered time") && fromDateSearch != null && !fromDateSearch.equals("null") && todateSearch != null && !todateSearch.equals("null"))
	{
		String fromVal = DateUtil.changeDateFormat(fromDateSearch.trim(),"MM/dd/yyyy", userDateFormat);
		String toVal = DateUtil.changeDateFormat(todateSearch.trim(),"MM/dd/yyyy", userDateFormat);
		csvDuration = "Timeline"+","+timelineVal+","+"From : "+fromVal+","+" To : "+toVal;
	}
	Date currDate = new Date();
	SimpleDateFormat smdf = new SimpleDateFormat(userDateTimeFormat);
	String currDateStr = smdf.format(currDate); 
	String reportGenCsv = "Report generated on"+","+currDateStr;
	//String totDisc = Integer.toString(totalCompSize);
	//String csvTotal = "";
	//if(choosenCompType!= null && choosenCompType.equalsIgnoreCase("All") && (searchVal==null || searchVal.equals("")))
	//{
	//	csvTotal = "Total discovered components"+","+totDisc;
	//}
	//else
	//{
	//	csvTotal = "Discovered components"+","+totDisc;
	//}
	
	String csvHeading = "DISCOVERED COMPONENTS REPORT";
	String csvHeading1 = "Component type,Host IP/Name,Nick name,Port,SID/Service name,First discovered time,Last discovered time,Discovered By,Discovered From";
	ArrayList csvDataEntry = new ArrayList();
	List csvDataList = new ArrayList();
	csvDataList.add(csvHeading);
	csvDataList.add("");
	csvDataList.add(reportGenCsv);
	if(csvDuration != null && timelineVal != null)
	{
		csvDataList.add(csvDuration);
	}
	//csvDataList.add(csvTotal);
	//csvDataList.add("");
	//csvDataList.add(csvHeading1);
	for(int k=0; k<totalComponents.size(); k++)
	{
		String compwithnick = (String)totalComponents.get(k);
		String compTypeVal = "";
		String ipVal = "-";
		String nickVal = "";
		String portValue = "";
		String sidVal = "";
		String startDate = "";
		String endDate = "";
		String disby= "";
		String disbyip = "";
		String disByNick= "";
		String disByuser= "";
	//	StringTokenizer tok = new StringTokenizer(compwithnick,"\\#::#");
		ArrayList tokList = StringUtils.egTokenizer(compwithnick,"#::#");
		Iterator tok = tokList.iterator();
		while(tok.hasNext())
		{
			compTypeVal =(String) tok.next();
			String compValue = _mapping.getReverseComponentTypeDescription(compTypeVal);
			nickVal =(String) tok.next();
			Enumeration nickEnum = allNickIpMap.keys();
			while(nickEnum.hasMoreElements())
			{
				String nickKey=(String) nickEnum.nextElement();
				if(nickVal.equalsIgnoreCase(nickKey))
				{
					ipVal =(String)allNickIpMap.get(nickKey);
				}
			}
			portValue = (String) tok.next();
			sidVal =(String) tok.next();
			startDate =(String) tok.next();
			endDate =(String) tok.next();
			disby =(String) tok.next();
			disbyip =(String) tok.next();
			disByNick =(String) tok.next();
			disByuser = (String)tok.next();
			String compNameWithType = (compValue.equalsIgnoreCase("oracle_server")) ? compValue+":"+nickVal+":"+portValue+":"+sidVal : compValue+":"+nickVal+":"+portValue;
			boolean isNewOrUnmanageComp = getUnmanageAndNewComponents(compValue,compNameWithType);
			if(ipVal != null && ipVal != "-")
			{
				tmpObj.put("iconhide",isNewOrUnmanageComp);
				tmpObj.put("componenetType",compTypeVal);
				tmpObj.put("ip",ipVal);
				tmpObj.put("Nick Name",nickVal);

				if(portValue == "NULL" || portValue.equalsIgnoreCase("null") || portValue == "" || portValue.equals(""))
				{
					portValue = "-";
				}
				tmpObj.put("port",portValue);
				tmpObj.put("sid",sidVal);
				tmpObj.put("start_time",startDate);
				tmpObj.put("end_time",endDate);
				tmpObj.put("hiddenfield",compNameWithType);
				tmpObj.put("disby",disby);
				tmpObj.put("disByNick",disByNick);
				tmpObj.put("disbyip",disbyip);
				gridArray.add(tmpObj);
				startDate = "\""+startDate+"\"";
				endDate = "\""+endDate+"\"";
				String csvData = compTypeVal+","+ipVal+","+nickVal+","+portValue+","+sidVal+","+startDate+","+endDate+","+disby+","+disByNick;
				csvDataEntry.add(csvData);
				totalCompSize++;
				//csvDataList.add(csvData);
			}
		}
	}
	for(int s=0; s<firstDiscoverdCompArrList.size(); s++)
	{
		String compTypeValue = (String)firstDiscoverdCompArrList.get(s);
		String compTypeDisplayValue = _mapping.getComponentTypeDescription(compTypeValue);
		viewasObj.put("valueField",compTypeValue);
		viewasObj.put("displayField",compTypeDisplayValue);
		viewasArr.add(viewasObj);
	}
	String[] time = new String[11];
	time=new String[]{"Any","1 hour","6 hours","12 hours","24 hours", "2 days", "4 days","1 week","4 weeks","1 month"};
	for (int a=0; a < time.length; a++)
	{
		timelineObj = new JSONObject();
		timelineObj.put("valueField",time[a]);
		timelineObj.put("displayField",_mapping.getTextDescription("admin.discovery."+time[a]));
		timelineArr.add(timelineObj);
	}

	//	String[] columnData = new String[6];
	//	String[] actualcolumnData = new String[6];
	//	columnData=new String[]{"componenetType","ip","Nick Name","port","sid","start_time","end_time","hiddenfield","iconhide"};
	//	actualcolumnData=new String[]{"COMPONENT TYPE","HOST IP/NAME","NICK NAME","PORT","SID/Service name","FIRST DISCOVERED TIME","LAST DISCOVERED TIME","hiddenfield","iconhide"};
	//	for (int g=0; g < columnData.length; g++)
	//	{
	//		columnDataObj.put(columnData[g],actualcolumnData[g]);
	//	}

		String[] columnData = new String[7];
		String[] actualcolumnData = new String[7];
		columnData=new String[]{"componenetType","ip","Nick Name","port","sid","start_time","end_time","hiddenfield","disByNick","disby",
		"iconhide"};
		actualcolumnData=new String[]{"COMPONENT TYPE","HOST IP/NAME","NICK NAME","PORT","SID/Service name","FIRST DISCOVERED TIME","LAST DISCOVERED TIME","hiddenfield","DISCOVERED FROM HOST","DISCOVERED BY","iconhide"};
		for (int g=0; g < columnData.length; g++)
		{
			columnDataObj.put(columnData[g],actualcolumnData[g]);
		}
	
	rootObj.put("columns",columnDataObj);
	ArrayList searchByCombo = new ArrayList();
	searchByCombo.add("Component type");
	searchByCombo.add("Host IP/Name");
	searchByCombo.add("Nick name");
	searchByCombo.add("Last discovered time");
	if(choosenCompType!= null && !choosenCompType.equalsIgnoreCase("All"))
	{
		searchByCombo.remove("Component type");
	}
	for (int h=0; h < searchByCombo.size(); h++)
	{
		String searchByVal = (String)searchByCombo.get(h);
		searchObj = new JSONObject();
		searchObj.put("valueField",searchByVal);
		searchObj.put("displayField",_mapping.getTextDescription("admin.discovery."+searchByVal));
		searchArr.add(searchObj);
	}
	//int totalCompSize = ;//totalComponents.size();
	totalDiscCount = totalCompSize;
	String totDisc = Integer.toString(totalCompSize);
	String csvTotal = "";
	if(choosenCompType!= null && choosenCompType.equalsIgnoreCase("All") && (searchVal==null || searchVal.equals("")))
	{
		csvTotal = "Total discovered components"+","+totDisc;
	}
	else
	{
		csvTotal = "Discovered components"+","+totDisc;
	}
	csvDataList.add(csvTotal);
	csvDataList.add("");
	csvDataList.add(csvHeading1);
	Iterator it = csvDataEntry.iterator();
	while(it.hasNext())
	{
		String csvData = (String)it.next();
		csvDataList.add(csvData);
	}
	rootObj.put("viewas",viewasArr);
	rootObj.put("searchBy",searchArr);
	rootObj.put("showcomponentNameWise",gridArray);
	rootObj.put("totalCount",totalDiscCount);
	rootObj.put("timeLineValues",timelineArr);
	session.setAttribute("list",csvDataList);
	session.setAttribute("totalDiscCount",totDisc);
	session.setAttribute("from",from);
	session.setAttribute("value",value);
	session.setAttribute("fromdateSearch",fromDateSearch);
	session.setAttribute("todateSearch",todateSearch);
	session.setAttribute("timelineVal",timelineVal);
	session.setAttribute("searchType",searchType);
	session.setAttribute("searchVal",searchVal);
	session.setAttribute("choosenCompType",choosenCompType);
	session.setAttribute("adminLastAccessedURL","/final/admin/EgDiscoveryPane.jsp?selectedNode="+selectedNode);
	out.println(rootObj.toString());
%>
<%!
public boolean doesExist(String compType,List searchStr)
{
	Iterator i = searchStr.iterator();
	while(i.hasNext())
	{
		String nextStr = (String) i.next();
		if (compType.indexOf(nextStr)!=-1||(compType.toUpperCase().indexOf(nextStr.toUpperCase()))!=-1)
		{
			return true;
		}
	}
	return false;
}
public boolean doesDateExist(String compDate,String fromDate,String toDate,TimeZone timeZone,String userDateFormat,String timelineVal)
{
	String stFormat=null;
	String endFormat=null;
	IniFile iniFile = new IniFile(EgInstallInfo.getInstallDir()+ "/manager/config/eg_ui.ini");
	String currentDateFormat = iniFile.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	if(DateUtil.isDate(fromDate.trim(), "MM/dd/yyyy"))
		stFormat = "MM/dd/yyyy";
	else
		stFormat = currentDateFormat;

	if(DateUtil.isDate(toDate.trim(), "MM/dd/yyyy"))
		endFormat = "MM/dd/yyyy";
	else
		endFormat = currentDateFormat;

	String fromDateStr = DateUtil.changeDateFormat(fromDate.trim(),stFormat, "E MMM dd yyyy HH:mm:ss");
	String toDateStr = DateUtil.changeDateFormat(toDate.trim(),endFormat, "E MMM dd yyyy 23:59:59");
	String currDateStr = DateUtil.changeDateFormat(compDate,userDateFormat,"E MMM dd yyyy HH:mm:ss");
	Date currDateVal = new Date(currDateStr);
	Date fromDateVal = new Date(fromDateStr);
	Date toDateVal = new Date(toDateStr);

	long firstdateVal = currDateVal.getTime();
	Date currDate = new Date(firstdateVal);

	Calendar cal = Calendar.getInstance();
	cal.setTime(currDate);
	cal.setTimeZone(timeZone);
	Date date = new Date();
	if(timelineVal != null && timelineVal.startsWith("1 hour"))
	{
		long val12 = System.currentTimeMillis();
		Date minusHourDate = new Date(System.currentTimeMillis() - 3600 * 1000);
		if(cal.getTime().after(minusHourDate) || cal.getTime().equals(minusHourDate))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("6 hours"))
	{
		Date minusSixHourDate = new Date(System.currentTimeMillis() - 6 * 3600 * 1000);
		if(cal.getTime().after(minusSixHourDate) || cal.getTime().equals(minusSixHourDate))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("12 hours"))
	{
		Date minusTwelveHourDate = new Date(System.currentTimeMillis() - 12 * 3600 * 1000);
		if(cal.getTime().after(minusTwelveHourDate) || cal.getTime().equals(minusTwelveHourDate))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("24 hours"))
	{
		Date minusTwetyFourHour = new Date(System.currentTimeMillis() - 24 * 3600 * 1000);
		if(cal.getTime().after(minusTwetyFourHour) || cal.getTime().equals(minusTwetyFourHour))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("2 days"))
	{
		Date minusTwodays = new Date(System.currentTimeMillis() - 2 * 24 * 3600 * 1000);
		if(cal.getTime().after(minusTwodays) || cal.getTime().equals(minusTwodays))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("4 days"))
	{
		Date minusFourDays = new Date(System.currentTimeMillis() - 4 * 24 * 3600 * 1000);
		if(cal.getTime().after(minusFourDays) || cal.getTime().equals(minusFourDays))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("1 week"))
	{
		Date minusOneWeek = new Date(System.currentTimeMillis() - 7 * 24 * 3600 * 1000);
		if(cal.getTime().after(minusOneWeek) || cal.getTime().equals(minusOneWeek))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("4 weeks"))
	{
		Calendar calDate = Calendar.getInstance();
		calDate.add(Calendar.DATE, -28);
		Date minusfourWeek = calDate.getTime();
		if(cal.getTime().after(minusfourWeek) || cal.getTime().equals(minusfourWeek))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("1 month"))
	{
		Calendar calMonth = Calendar.getInstance();
		calMonth.add(Calendar.MONTH, -1);
		Date minusOneMonth = calMonth.getTime();
		if(cal.getTime().after(minusOneMonth) || cal.getTime().equals(minusOneMonth))
		{
			return true;
		}
	}
	else if(timelineVal != null && timelineVal.startsWith("Any"))
	{
		if(cal.getTime().after(fromDateVal) && cal.getTime().before(toDateVal) || cal.getTime().equals(fromDateVal) || cal.getTime().equals(toDateVal)) {
			return true;
		}
	}
	return false;
}
public String getDateValue(String dateValue,String userDateFormat,String userTimeFormat)
{
	Calendar calendar = new GregorianCalendar();
	DateFormat df= new SimpleDateFormat(userDateFormat);
	TimeZone timeZone = TimeZone.getTimeZone(userTimeFormat);
	calendar.setTimeZone(timeZone);
	long firstdateVal = Long.valueOf(dateValue);
	Date dd = new Date(firstdateVal);
	calendar.setTime(dd);
	String start_mgr_time = calendar.get(calendar.HOUR_OF_DAY)+":"+calendar.get(calendar.MINUTE)+":"+calendar.get(calendar.SECOND);
	String startdate = df.format(dd);
	startdate = startdate+" "+start_mgr_time;
	return startdate;
}

public boolean getUnmanageAndNewComponents(String compType,String compVal)
{
	String disIniFilePath = EgInstallInfo.getInstallDir() + "/manager/config/eg_discover.ini";
	IniFile disIniFile = new IniFile(disIniFilePath);
	String newstr = "#New|";
	String unmanagestr = "#Unmanage|";
	String newValue = disIniFile.getValue(compType, newstr+compVal);
	if(newValue != null && newValue.length() > 0)	// Check for new component
	{
		return true;
	}
	else
	{
		String unmanageValue = disIniFile.getValue(compType,unmanagestr+compVal);
		if(unmanageValue != null && unmanageValue.length() > 0)
		{
			return true;
		}
	}
	return false;
}

public String[] setStartAndEndDateTime(String selectedTimeLine,String userDateTimeFormat)
{
	int blankSpaceIndex = selectedTimeLine.indexOf(" ");
	long value = Long.parseLong(selectedTimeLine.substring(0, blankSpaceIndex));
	String unit = selectedTimeLine.substring(blankSpaceIndex+1).toLowerCase();
	SimpleDateFormat sdf = new SimpleDateFormat(userDateTimeFormat);
	String startAndEndDates[] = {"",""};

	if(unit.startsWith("h"))
	{
		value *= 3600;
	}
	else if(unit.startsWith("d"))
	{
		value *= (24 * 3600);
	}
	else if(unit.startsWith("w"))
	{
		value *= (7 * 24 * 3600);
	}
	else if(unit.startsWith("mo"))
	{
		value *= (30 * 24 * 3600);
	}
	Date start = new Date();
	String endDateTime = sdf.format(start);
	start.setTime(start.getTime() - (value * 1000));
	String startDateTime = sdf.format(start);
	startAndEndDates [0] = startDateTime;
	startAndEndDates [1] = endDateTime;
	return startAndEndDates;
}

public boolean isComponentExist(String compType, String compName)
{
	String comp[] = getAllServersOfType(compType,true);
	boolean result = false;
	boolean isOracle = false;
	isOracle = compType.equals("Oracle_server");
	if (comp != null && comp.length > 0)
	{
		for (int i=0; i < comp.length; i++)
		{
			String var = comp[i];
			if (var.equals(compType+":"+compName))
			{
				result = true;
				break;
			}
			else if(var.equals("#New|"+compType+":"+compName))
			{
				result = true;
				break;
			}
			else if(var.equals("#Unmanage|"+compType+":"+compName))
			{
				result = true;
				break;
			}
			else if (isOracle)
			{
				if (var.startsWith(compType+":"+compName+":"))
				{
					result = true;
					break;
				}
			}
		}
	}
	else
	{
		result = false;
	}

	return result;
}
public String[] getAllServersOfType (String type, boolean b1)
{
	String disIniFilePath = EgInstallInfo.getInstallDir() + "/manager/config/eg_discover.ini";
	IniFile disIniFile = new IniFile(disIniFilePath);
	String[] vars = disIniFile.getVariables(type);
	ArrayList a = new ArrayList();
	if (vars == null)
	{
		return null;
	}
	for (int i=0; i<vars.length; i++) 
	{
		if (vars[i].startsWith("Format:"))
		{
			continue;
		}
		a.add(vars[i]);
	}
	int c = a.size();
	String res[] = new String[c];
	res = (String [])a.toArray(res);
	for (int i=0; i<c; i++) 
	{
		res[i] = (String) a.get(i);
	}
	a = null;
	return res;
}


%>
