<%
/*********************************************************************************************************
*	Name			:	EgVmwareDesktopData.jsp
*	Version			:	eG 4.0
*	Purpose			:	To show the Desktop and Server infos of the Vmware_vdi_server and Vmware_esx_server
*	Creation Date	:	20/06/2007
*	Author			:	Alagu sundaram
						20/12/2011  Sri Devi G User - VM Changes
*	Modifyed ExtJs	:	Saravanan.A

********************************************************************************************************/
%>

<%@ taglib prefix = "egui" uri = "../WEB-INF/eg-taglib.tld"%>
<%@ include file = "EgServerDashboardTracker.jsp"%>
<%@ page import = "com.eg.EgStateManager"%>
<%@ page import = "com.eg.EgExternalInfo"%>
<%@ page import = "com.eg.EgDbAdapterTypeM"%>
<%@ page import = "java.util.*" %>
<%@ page import = "java.io.*"	%>
<%@ page import = "com.eg.EgDiscInfo"%>
<%@ page import = "com.egurkha.ini.IniUI"%>
<%@ page import = "com.egurkha.monitor.alarms.*" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.*"%>
<%@ page import = "egurkha.*"%>
<%@ page import = "com.eg.EgAlarmTable" %>
<%@ page import = "com.egurkha.*" %>
<%@ page import = "com.eg.EgTablesInfo"%>
<%@ page import = "com.egurkha.monitor.*" %>
<%@ page import = "com.eg.EgMappingInfo" %>
<%@ page import = "com.eg.EgURLEncoder"%>
<%@ page import = "com.egurkha.monitor.UserViewData"%>
<%@ page import = "com.egurkha.monitor.UserViewResult"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="net.sf.json.*"%>

<%

	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader ("Expires", -1);
	response.setContentType("application/json");

	UserRegistry registry = UserRegistry.getInstance();
	User user = registry.getUser(session.getId());
	String thisLanguage = "en_US";
	if (user!=null)
	{
		String userLang = user.getLanguage();
		if (userLang!=null && !userLang.equalsIgnoreCase("NULL"))
		{
			thisLanguage=userLang;
		}
	}
	EgMappingInfo mappingInfo = new EgMappingInfo(thisLanguage);

	int minServers = EgConstants.MIN_SERVERS_FOR_SEARCH;
	String source = request.getParameter("from");
	String selectedUserView = request.getParameter("userName");//used in userView
	String stateName = request.getParameter("stateName");
	String check = request.getParameter("check");
	String infoNameSearch =request.getParameter("infoNameSearch");
	infoNameSearch=com.egurkha.util.StringUtils.decodeSecurityChar(infoNameSearch);
	infoNameSearch=(infoNameSearch==null || infoNameSearch.length()==0)?"":infoNameSearch;
	//String monitor_Skin = request.getParameter("monitor_Skin");
	ArrayList aList_infowithstate=new ArrayList();
	Iterator itr = null;
	String test_name = "";
	String layer_name = "";
	String comptype = "";
	String compName = "";
	String comp_name = "";
	String display = "";
	String serverName = "";
	Hashtable ht_infosearch = new Hashtable();
	Hashtable vmsWithTypesAndStates = new Hashtable();
	Hashtable vmsWithTypesAndLayer = new Hashtable();
	ArrayList poweredOn = new ArrayList();
	boolean isfromSuper = false;

	ArrayList compList=new ArrayList();

	//Fix to change icon based on OS type
	HashMap vmDetails = new HashMap();
	List desktopOSList = new ArrayList();
	com.eg.IniFile externalIni =  new com.eg.IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_external.ini");
	String desktopOs = externalIni.getValue("DeskTOP_OS","OS");
	if(desktopOs == null || desktopOs.trim().length() == 0){
		desktopOs = "Windows 10,Windows 8,Windows 7,Windows Vista,Windows XP,Windows ME,Windows 2000,Windows XP Professional,Windows NT";
	}
	desktopOSList = com.egurkha.util.StringUtils.egTokenizer(desktopOs, ",",false,false,true);//param 3-->includeEmpty,4-->unique,5-->trim
	//end
	
	if(source == null || source.equals("null"))
	{
		test_name=request.getParameter("Layer");
		layer_name=request.getParameter("Layer");
		compName=request.getParameter("comp");
		comptype=request.getParameter("comptype");
		StringTokenizer sttok=new StringTokenizer(compName,":");
		display=sttok.nextToken();
		String host="";
		String port="";
		comp_name=compName+":"+comptype;
		serverName=request.getParameter("servername");
		if (serverName==null)
			serverName="";
		isfromSuper = session.getAttribute("fromSuper") != null && session.getAttribute("fromSuper").toString().equalsIgnoreCase("yes");

		ArrayList al_serverName=new ArrayList();
		if (serverName!=null && serverName.length()>0)
		{
			StringTokenizer st=new StringTokenizer(serverName,"*");
			while (st.hasMoreTokens())
			{
				al_serverName.add(st.nextToken());
			}
			serverName=serverName.replace('*',' ');
		}
		StringTokenizer str=new StringTokenizer(compName,":");
		int tokCount = str.countTokens();
		if(tokCount > 0)
		{

			host=str.nextToken();
			port=str.nextToken();
		}
		else
		{
			host=str.nextToken();
		}
		VMDesktopBean vm=new VMDesktopBean(comptype,compName,layer_name);

		ht_infosearch=vm.getInfoForTest();
		itr=ht_infosearch.keySet().iterator();

		while(itr.hasNext())
		{
			String key=(String)itr.next();
			String state=(String)ht_infosearch.get(key);
			if (al_serverName.size()>0)
			{
				for (int i=0;i<al_serverName.size();i++)
				{
					String searchInfo=(String)al_serverName.get(i);
					if (key.toUpperCase().indexOf(searchInfo.toUpperCase())>-1)
					{
						if(infoNameSearch!=null && infoNameSearch.length()>0)
						{
							if (key.toUpperCase().indexOf(infoNameSearch.toUpperCase())>-1)
							{
								aList_infowithstate.add(new EgObject(key,State.getState(state)));
							}
						}
						else
						{
							aList_infowithstate.add(new EgObject(key,State.getState(state)));
						}
						
					}
				}
			}
			else
			{
				if(infoNameSearch!=null && infoNameSearch.length()>0)
				{
					if (key.toUpperCase().indexOf(infoNameSearch.toUpperCase())>-1)
					{
						aList_infowithstate.add(new EgObject(key,State.getState(state)));
					}
				}
				else
				{
					aList_infowithstate.add(new EgObject(key,State.getState(state)));
				}
			}
		}
	}
	else
	{
		serverName=request.getParameter("servername");
		if (serverName==null)
			serverName="";
		ArrayList al_serverName=new ArrayList();
		if (serverName!=null && serverName.length()>0)
		{
			//It comes with a space.
			StringTokenizer st=new StringTokenizer(serverName," ");
			while (st.hasMoreTokens())
			{
				al_serverName.add(st.nextToken());
			}
			serverName=serverName.replace('*',' ');
		}

		if(source.equals("MainDashboard") || source.equals("userView"))
		{
			if(source.equals("MainDashboard"))
			{
				DashboardData d = null;
				DashboardResult r = null;
				if(check!=null && check.equals("uservms"))
				{
					d = (DashboardData) request.getAttribute("data");
					r = (DashboardResult) request.getAttribute("result");
					if(r==null)
					{
						d = (DashboardData) session.getAttribute("dashboardData");
						r = (DashboardResult) session.getAttribute("dashboardResult");
					}
				}
				else
				{
					d = (DashboardData) session.getAttribute("dashboardData");
					r = (DashboardResult) session.getAttribute("dashboardResult");
				}
				if(r!=null)
				{
					vmsWithTypesAndStates = (Hashtable) r.vmsWithTypesAndStates;
					vmsWithTypesAndLayer = (Hashtable) r.vmsWithTypesAndLayer;
				}
			}
			if(source.equals("userView"))
			{
				UserViewData d = (UserViewData) session.getAttribute("data");
				UserViewResult r = (UserViewResult) session.getAttribute("result");
				if (r==null) {
					d = (UserViewData) session.getAttribute("userViewData");
					r = (UserViewResult) session.getAttribute("userViewResult");
				}
				vmsWithTypesAndStates = (Hashtable) r.vmsWithTypesAndStates;
				vmsWithTypesAndLayer = (Hashtable) r.vmsWithTypesAndLayer;
			}
			
			Hashtable validInfo = new Hashtable();
			
			if(vmsWithTypesAndStates!=null && vmsWithTypesAndStates.size()>0)
			{
				ArrayList alreadyCalculated = new ArrayList();
				for(Enumeration e = vmsWithTypesAndStates.keys();e.hasMoreElements();)
				{
					String key = (String)e.nextElement();
					String info = key;
					State state = (State)vmsWithTypesAndStates.get(key);
					String host = "";
					if(vmsWithTypesAndLayer!=null && vmsWithTypesAndLayer.size() > 0)
					{
						host = (String)vmsWithTypesAndLayer.get(info);
						if(host!=null)
						{
							try
							{
								StringTokenizer st_temp = new StringTokenizer(host,"@");
								if(st_temp!=null)
								{
									comptype = st_temp.nextToken();
									compName= st_temp.nextToken();
									layer_name = st_temp.nextToken();
									if(!compList.contains(comptype+":"+compName))
										compList.add(comptype+":"+compName);
								}
							}
							catch(Exception ee)
							{
								
							}
						}
						else
							continue;
					}
					if(stateName!=null && stateName.equals("all"))
					{
						if (al_serverName.size()>0)
						{
							for (int i=0;i<al_serverName.size();i++)
							{
								String searchInfo=(String)al_serverName.get(i);
								if (info.toUpperCase().indexOf(searchInfo.toUpperCase())>-1)
								{
									if(infoNameSearch!=null && infoNameSearch.length()>0)
									{
										if (info.toUpperCase().indexOf(infoNameSearch.toUpperCase())>-1)
										{
											aList_infowithstate.add(new EgObject(info,state));
										}
									}
									else
									{
										aList_infowithstate.add(new EgObject(info,state));
									}
								}
							}
						}
						else
						{
							if(infoNameSearch!=null && infoNameSearch.length()>0)
							{
								if (info.toUpperCase().indexOf(infoNameSearch.toUpperCase())>-1)
								{
									aList_infowithstate.add(new EgObject(info,state));
								}
							}
							else
							{
								aList_infowithstate.add(new EgObject(info,state));
							}
						}
					}
					else if(stateName!=null && stateName.equals(state.getName()))
					{
						if (al_serverName.size()>0)
						{
							for (int i=0;i<al_serverName.size();i++)
							{
								String searchInfo=(String)al_serverName.get(i);
								if (info.toUpperCase().indexOf(searchInfo.toUpperCase())>-1)
								{
									if(infoNameSearch!=null && infoNameSearch.length()>0)
									{
										if (info.toUpperCase().indexOf(infoNameSearch.toUpperCase())>-1)
										{
											aList_infowithstate.add(new EgObject(info,state));
										}
									}
									else
									{
										aList_infowithstate.add(new EgObject(info,state));
									}
								}
							}
						}
						else
						{
							if(infoNameSearch!=null && infoNameSearch.length()>0)
							{
								if (info.toUpperCase().indexOf(infoNameSearch.toUpperCase())>-1)
								{
									aList_infowithstate.add(new EgObject(info,state));
								}
							}
							else
							{
								aList_infowithstate.add(new EgObject(info,state));
							}
						}
					}

					if(!alreadyCalculated.contains(host))
					{
						VMDesktopBean vm=new VMDesktopBean(comptype,compName,layer_name,"DashboardModel",false);
						validInfo=vm.getInfoForTest();
						if(validInfo!=null && validInfo.size()>0)
						{
							Enumeration keys=validInfo.keys();
							while(keys.hasMoreElements())
							{
								key = (String)keys.nextElement();
								if(poweredOn.size() ==0)
								{
									poweredOn.add(key.toLowerCase());
								}
								else
								{
									if(!poweredOn.contains(key.toLowerCase()))
									{
										poweredOn.add(key.toLowerCase());
									}
								}
							}
						}
					}

					alreadyCalculated.add(host);
				}
			}
		}
	}

	Collections.sort(aList_infowithstate,new NameComparatorAsc());
	Collections.sort(aList_infowithstate,new StateComparatorDes());
	itr=aList_infowithstate.iterator();

	String title=(layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))? "Desktops" : "Servers";

	if(source!=null && (source.equals("MainDashboard") || source.equals("userView")) && (vmsWithTypesAndStates!=null && vmsWithTypesAndStates.size()>0))
	{
		title=(layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))? "Desktops" : "VMs";
	}

	String uiFile_Config = "eg_dashboardConfig.ini";
	String iniFileDir_Config = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile egUI_Config = new IniFile(iniFileDir_Config + uiFile_Config);
	String needLiveGraphLink="";
	if(comptype != null && comptype.trim().length() > 0)
		needLiveGraphLink=egUI_Config.getValue("VMDash_Supported_Types",comptype);

	String title1 ="";// "monitor.title.View for";
	//title1 =mappingInfo.getTextDescription(title1);
	//title1 ="View for";
	title1 ="Inside View of ";

	//String thisTitle="header.jsp?title="+title1+" "+title+"&CompName="+display+"&servername="+serverName;
	String thisTitle=title1+" "+title;
	if(!display.equals("") && display.trim().length()>0)
		thisTitle=title1+" "+title+" on "+display;
	
	if(user.isOnlyVMAssigned() || user.isVMAssigned())
		thisTitle = mappingInfo.getComponentTypeDescription("Virtual_Machine");

	JSONObject json = new JSONObject();
	JSONObject screenLabels = new JSONObject();
	JSONArray virtualMachinesJsonList = new JSONArray();

	screenLabels.put("Title",thisTitle);

	json.put("screenLabels",screenLabels);

	String layerModelLink=response.encodeURL("/final/servlet/com.eg.EgDashboardController?")+"from=list&site=NULL&qctr=1&host="+EgURLEncoder.encode(compName,"UTF-8")+"&comptype="+comptype+"&layer="+layer_name+"&test=null&faketype=&tab=LayerModel&toDashBoardLayer=true&fromPage=vmServer&monitor_Skin="+monitor_Skin;

	String virtualLGraphLink=response.encodeURL("/final/monitor/livegraph/EgVmwareLiveGraphHome.jsp?")+"comptype="+comptype+"&er_component="+EgURLEncoder.encode(compName,"UTF-8");
	
	if(aList_infowithstate!=null && aList_infowithstate.size()>0 && !isfromSuper)
	{
		if(source == null)
		{
			JSONObject otherLinks = new JSONObject();

			otherLinks.put("COMPONENT_LAYERS",mappingInfo.getTextDescription("monitor.vmware.COMPONENT LAYERS"));
			otherLinks.put("COMPONENT_LAYERS_URL",layerModelLink);
			
			if(!EgInstallInfo.isSuperManager)
			{
				otherLinks.put("LIVE_GRAPH",mappingInfo.getTextDescription("monitor.vmware.LIVE GRAPH"));
				otherLinks.put("LIVE_GRAPH_URL",virtualLGraphLink);
			}
			otherLinks.put("USER_NAME_SEARCH",mappingInfo.getTextDescription("monitor.vmware.Search"));
			otherLinks.put("SEARCH_VALUE",infoNameSearch);
			
			json.put("otherLinks",otherLinks);
		}
		else if( source.equalsIgnoreCase("MainDashboard"))
		{
			JSONObject otherLinks = new JSONObject();
			otherLinks.put("SEARCH_VALUE",infoNameSearch);
			otherLinks.put("USER_NAME_SEARCH",mappingInfo.getTextDescription("monitor.vmware.Search"));
			json.put("otherLinks",otherLinks);
		}
	}
	else
	{
		if(source == null)
		{
			JSONObject otherLinks = new JSONObject();

			otherLinks.put("COMPONENT_LAYERS",mappingInfo.getTextDescription("monitor.vmware.COMPONENT LAYERS"));
			otherLinks.put("COMPONENT_LAYERS_URL",layerModelLink);

			if(!EgInstallInfo.isSuperManager)
			{
				if(needLiveGraphLink!=null && needLiveGraphLink.trim().length()>0 && needLiveGraphLink.equalsIgnoreCase("Yes"))
				{
					otherLinks.put("LIVE_GRAPH",mappingInfo.getTextDescription("monitor.vmware.LIVE GRAPH"));
					otherLinks.put("LIVE_GRAPH_URL",virtualLGraphLink);
				}
			}
			
			otherLinks.put("USER_NAME_SEARCH",mappingInfo.getTextDescription("monitor.vmware.Search"));
			otherLinks.put("SEARCH_VALUE",infoNameSearch);
			
			json.put("otherLinks",otherLinks);
		}
		else if( source.equalsIgnoreCase("MainDashboard"))
		{
			JSONObject otherLinks = new JSONObject();
			otherLinks.put("SEARCH_VALUE",infoNameSearch);
			otherLinks.put("USER_NAME_SEARCH",mappingInfo.getTextDescription("monitor.vmware.Search"));
			json.put("otherLinks",otherLinks);
		}
	}

	if(((ht_infosearch!=null && ht_infosearch.size()>minServers) && (aList_infowithstate!=null && aList_infowithstate.size() >minServers)) ||(((source!=null && (source.equals("MainDashboard")|| source.equals("userView")) && (aList_infowithstate!=null && aList_infowithstate.size() >minServers)))))
	{
		JSONObject searchJson = new JSONObject();

		if(source!=null && (source.equals("MainDashboard") || source.equals("userView")))
		{
			//out.println("VMs");
			searchJson.put("Search_Display","VMs");
		}
		else
		{
			if(layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))
			{
				//out.println("Desktops");
				searchJson.put("Search_Display",mappingInfo.getTextDescription("monitor.vmware.Desktops"));
			}
			else
			{
				//out.println("Servers");
				searchJson.put("Search_Display",mappingInfo.getTextDescription("monitor.vmware.Servers"));
			}
		}

		searchJson.put("Search_Server",serverName);
		searchJson.put("Search_Str",mappingInfo.getTextDescription("monitor.vmware.You can search using any character of your component.")+" "+mappingInfo.getTextDescription("monitor.vmware.Multiple search strings should be separated using a blank space."));

		json.put("searchJson",searchJson);
	}

	VMDesktopBean vmBean = new VMDesktopBean(request);
	if(compName != null && compName.trim().length() > 0 && comptype != null && comptype.trim().length() > 0)
		vmDetails = vmBean.getOSDetails(comptype+":"+compName);
	
	if(compList.size() > 0)
	{
		HashMap tempVmDetails = new HashMap();
		compList.remove(comptype+":"+compName);
		for(int p=0;p<compList.size();p++)
		{
			tempVmDetails = vmBean.getOSDetails((String)compList.get(p));
			vmDetails.putAll(tempVmDetails);
		}
	}
	
	Hashtable currentUserVMs = new Hashtable();
	List vmTypes = user.getOnlyVMTypes();
	for(int k=0;k<vmTypes.size();k++){
		currentUserVMs.putAll(user.getAllUserDesktopVms((String)vmTypes.get(k),new Hashtable()));
		currentUserVMs.remove("allInfos");
	}

	int size = aList_infowithstate.size();
	if(size > 0)
	{
		while(itr.hasNext()) 
		{
			JSONObject virtualMachinesJson = new JSONObject();
			EgObject ob=(EgObject) itr.next();
			String info= ob.getName();//(String) itr.next();
			String State=((State)ob.getState()).getName();//(String)ht_infowithstate.get(info);
			boolean isDesktopOS = getOSVersionForVM(vmDetails,desktopOSList,info);
			String tempInfo=info;
			if(currentUserVMs != null){
				if(tempInfo.indexOf("_on_") == -1 && currentUserVMs.containsKey(tempInfo)){
					ArrayList users = (ArrayList)currentUserVMs.get(tempInfo);
					for(int l=0;l<users.size();l++){
						String usr = (String)users.get(l);
						String temp_usr = usr.substring(usr.indexOf("_on_")+4);
						if(temp_usr.equals(tempInfo)){
							tempInfo = usr;
							break;
						}
					}
				}
				else{
					String desktopName = (tempInfo.indexOf("_on_") > 0)?tempInfo.substring(tempInfo.indexOf("_on_")+4):tempInfo;
					if(currentUserVMs!=null && currentUserVMs.size()>0 && !currentUserVMs.containsKey(desktopName))
						tempInfo = desktopName;
				}
			}

			String info_for_url=tempInfo;
			if(info_for_url!=null && info_for_url.trim().length()>0)
			{
				info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"+","|plus|");
				info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"&","|amp|");
				info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"%","|pct|");
				info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"#","|hash|");
				info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"\\","\\\\");
			}
			if(source!=null && (source.equals("MainDashboard") || source.equals("userView")))
			{
				if(vmsWithTypesAndLayer!=null && vmsWithTypesAndLayer.size() >0)
				{
					String temp = (String)vmsWithTypesAndLayer.get(info);

					if(temp!=null)
					{
						try
						{
							StringTokenizer st_temp = new StringTokenizer(temp,"@");
							if(st_temp!=null)
							{
								comptype = st_temp.nextToken();
								compName= st_temp.nextToken();
								layer_name = st_temp.nextToken();
								comp_name=compName+":"+comptype;
							}
						}
						catch(Exception ee)
						{	
						}
					}
				}
			}
			/* Just have the link displayed only for powered on VM's */

			//virtualMachinesJson.put("VmStateImg","/final/monitor/eg_images/eg_layout/VM_"+State+"big.gif");
			//virtualMachinesJson.put("VmStateImg","/final/images/component_icons/server_"+State.toLowerCase()+".png");
			virtualMachinesJson.put("VmStateImg",State.toLowerCase());
			String en_info="";
			if(info!= null && info.trim().length() > 0)
				en_info=com.egurkha.util.StringUtils.encodeSecurityChar(info);
			if (layer_name.equalsIgnoreCase("VIRTUAL_DESKTOP"))
			{
				String iconName = "";
				String measures_iconName = "";
				if(info!= null && info.indexOf("_on_") > 0){
					if(isDesktopOS){
						iconName = "USER_DESKTOP_WINDOWS";
						measures_iconName = "iconUserDesktop_";
					}
					else{
						iconName = "USER_DESKTOP";
						measures_iconName = "iconUserDesktop_";
					}
				}
				else if(isDesktopOS){
					iconName = "desktop_default";
					measures_iconName = "iconDesktop16_";
				}
				else{
					iconName = "server";
					measures_iconName = "iconServer16_";
				}

				if(source == null)
				{
					virtualMachinesJson.put("VmURL",response.encodeURL("/final/monitor/egInsideviewDashboard.jsp?")+"getFor=measure&comp="+EgURLEncoder.encode(compName,"UTF-8")+"&comptype="+comptype+"&Layer="+layer_name+"&info="+EgURLEncoder.encode(info_for_url,"UTF-8")+"&compName="+EgURLEncoder.encode(comp_name,"UTF-8")+"&measuresIcon="+measures_iconName+"&oldVmName="+EgURLEncoder.encode(en_info,"UTF-8"));

					virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/"+iconName+".png");
				}
				else if(source!=null && (source.equals("MainDashboard")|| source.equals("userView")))
				{
					if(poweredOn!=null && (poweredOn.contains(info.toLowerCase()) || poweredOn.contains(tempInfo.toLowerCase())))
					{
						if(source.equals("userView"))
						{
							virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/"+iconName+".png");
						}
						else
						{
							virtualMachinesJson.put("VmURL",response.encodeURL("/final/monitor/egInsideviewDashboard.jsp?")+"getFor=measure&comp="+EgURLEncoder.encode(compName,"UTF-8")+"&comptype="+comptype+"&Layer="+layer_name+"&info="+EgURLEncoder.encode(info_for_url,"UTF-8")+"&compName="+EgURLEncoder.encode(comp_name,"UTF-8")+"&source="+source+"&isVM=true&userName="+selectedUserView+"&stateName="+stateName+"&measuresIcon="+measures_iconName+"&check="+check+"&from="+source+"&oldVmName="+EgURLEncoder.encode(en_info,"UTF-8"));

							virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/"+iconName+".png");
						}
					}
					else
					{
						virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/"+iconName+".png");
					}
				}
			}
			else
			{
				if(source == null)
				{
					virtualMachinesJson.put("VmURL",response.encodeURL("/final/monitor/egInsideviewDashboard.jsp?")+"getFor=measure&comp="+EgURLEncoder.encode(compName,"UTF-8")+"&comptype="+comptype+"&Layer="+layer_name+"&info="+EgURLEncoder.encode(info_for_url,"UTF-8")+"&compName="+EgURLEncoder.encode(comp_name,"UTF-8")+"&oldVmName="+EgURLEncoder.encode(en_info,"UTF-8"));

					virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/VM_SERVER.png");
				}
				else if(source!=null && (source.equals("MainDashboard") || source.equals("userView")))
				{
					if(info_for_url!=null && info_for_url.trim().length() >0)
					{
						info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"#","|hash|");
						info_for_url=com.egurkha.util.StringUtils.replace(info_for_url,"&","|amp|");
					}

					if(source.equals("userView"))
					{
						virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/VM_SERVER.png");
					}
					else
					{
						virtualMachinesJson.put("VmURL",response.encodeURL("/final/monitor/egInsideviewDashboard.jsp?")+"getFor=measure&comp="+EgURLEncoder.encode(compName,"UTF-8")+"&comptype="+comptype+"&Layer="+layer_name+"&info="+EgURLEncoder.encode(info_for_url,"UTF-8")+"&compName="+EgURLEncoder.encode(comp_name,"UTF-8")+"&source="+source+"&isVM=true&servername="+serverName+"&userName="+selectedUserView+"&stateName="+stateName+"&check="+check+"&from="+source+"&oldVmName="+EgURLEncoder.encode(en_info,"UTF-8"));

						virtualMachinesJson.put("VmImg","/final/images/"+monitor_Skin+"/component_icons/VM_SERVER.png");
					}
				}
			}

			String info_to_display=tempInfo;
			if(info!=null && info.trim().length()>28)
			{
				info_to_display=info.substring(0,28);
				String tempInfoStr=info.substring(28);
				while(tempInfoStr.trim().length()>28)
				{
					if(tempInfoStr.trim().length()>4)
						info_to_display=info_to_display+"<br>"+tempInfoStr.substring(0,28);
					else
						info_to_display=info_to_display+""+tempInfoStr;
					tempInfoStr=tempInfoStr.substring(28);
				}
				if(tempInfoStr.trim().length()>4)
					info_to_display=info_to_display+"<br>"+tempInfoStr;
				else
					info_to_display=info_to_display+""+tempInfoStr;
			}
			//out.println(info_to_display);
			virtualMachinesJson.put("VmDispName",info_to_display);
			virtualMachinesJson.put("VmDispNameTooltip",info);

			virtualMachinesJsonList.add(virtualMachinesJson);
		}//End of for (Iterator...)
	}
	else
	{
		JSONObject virtualMachinesJson = new JSONObject();
		virtualMachinesJson.put("NoDataImg","/final/images/"+monitor_Skin+"/icon_warning.png");
		if (serverName==null || serverName.length()==0)
		{
			//out.println((layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))? "Desktops" : "Servers");
			String desk_server ="";
			if(layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))
				desk_server = mappingInfo.getTextDescription("monitor.vmware.Desktops");
			else desk_server = mappingInfo.getTextDescription("monitor.vmware.Servers");

			if(infoNameSearch!=null && infoNameSearch.length()>0)
			{
				virtualMachinesJson.put("NoDataText",mappingInfo.getTextDescription("monitor.vmware.Currently there are no Virtual")+" "+desk_server +" "+ mappingInfo.getTextDescription("monitor.vmware.available for this host search"));
			}
			else
			{
				virtualMachinesJson.put("NoDataText",mappingInfo.getTextDescription("monitor.vmware.Currently there are no Virtual")+" "+desk_server +" "+ mappingInfo.getTextDescription("monitor.vmware.available for this host"));
			}
		}
		else
		{
			//out.println("No Virtual "+((layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))? "Desktops" : "Servers")+" are available for your current search");
			String desk_server ="";
			if(layer_name == null || layer_name.equals("VIRTUAL_DESKTOP"))
				desk_server = mappingInfo.getTextDescription("monitor.vmware.Desktops");
			else desk_server = mappingInfo.getTextDescription("monitor.vmware.Servers");

			if(infoNameSearch!=null && infoNameSearch.length()>0)
			{
				virtualMachinesJson.put("NoDataText",mappingInfo.getTextDescription("monitor.vmware.Currently there are no Virtual")+" "+desk_server +" "+ mappingInfo.getTextDescription("monitor.vmware.available for this host search"));
			}
			else
			{
				virtualMachinesJson.put("NoDataText",mappingInfo.getTextDescription("monitor.vmware.No Virtual ")+desk_server+" "+mappingInfo.getTextDescription("monitor.vmware.are available for your current search"));
			}
		}
		virtualMachinesJsonList.add(virtualMachinesJson);
	}

	json.put("vmwareDesktop",virtualMachinesJsonList);

	response.getWriter().write(json.toString());

	if(request.getQueryString()!=null)
	{
		if(source!=null && !source.equals("null")&& (source.equals("MainDashboard")|| source.equals("userView")))
		{
			if(serverName!=null && serverName.length() >0)
			{
				session.setAttribute("monitorLastSession","/final/servlet/com.eg.EgMonitorController?monCmd=dashboard&action=uservms&frompage=insideBack&servername="+serverName+"&"+request.getQueryString());
			}
			else
				session.setAttribute("monitorLastSession","/final/servlet/com.eg.EgMonitorController?monCmd=dashboard&action=uservms&"+request.getQueryString());
		}
		else
		{
			session.setAttribute("monitorLastSession","/final/monitor/EgVmwareDesktop.jsp?"+request.getQueryString());
		}
	}
	else 
	{
		session.setAttribute("monitorLastSession","/final/monitor/EgVmwareDesktop.jsp");
	}
	
%>

<%!
	public boolean getOSVersionForVM(HashMap vmDetails,List desktopOSList,String vmName)
	{
		if(vmDetails == null || vmDetails.size() == 0)
			return false;

		if(vmName != null && vmName.indexOf("_on_") > 0)
			vmName = vmName.substring(vmName.indexOf("_on_")+4);

		String osName  = (String)vmDetails.get(vmName);
		if(osName != null && osName.trim().length() > 0){
			if(desktopOSList != null && desktopOSList.contains(osName))
				return true;
			else if(desktopOSList != null && desktopOSList.size() > 0){
				for(int l=0;l<desktopOSList.size();l++){
					String os_name = (String)desktopOSList.get(l);
					if(os_name != null && osName.indexOf(os_name) >= 0){
						//System.out.println("osName.indexOf(os_name)--------->"+osName.indexOf(os_name));
						return true;
					}
				}
			}
		}
		return false;
	}

%>
