<%@ include file = "../../EcTrackerJson.jsp" %>
<%@ page errorPage="EcProbs.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import ="egurkha.config.EcProcessData"%>
<%@ page import ="egurkha.config.EcUtils"%>
<%@ page import = "java.net.URLEncoder" %>
<%@ page import = "egurkha.config.EcProcessResult" %>
<%@ page import = "egurkha.config.EcPrintData" %>
<%@ page import = "com.egurkha.pdfmaker.*"%>
<%@ page import = "com.eg.EgMappingInfo"%>
<%@ page import="com.egurkha.User" %>
<%@ page import="com.egurkha.UserRegistry" %>
<%@ page import="com.egurkha.ini.IniUI"%>
<%@ page import ="com.eg.EgURLEncoder"%>
<%@ page import = "egurkha.config.EcFusionChartBean" %>
<%@ page import = "egurkha.config.ConfigDbSetting" %>
<%@ page import = "com.eg.EgConfigConstants"%>
<%@ page import = "com.eg.EgConfigTestsInfo"%>
<%@ page import = "com.eg.EgConfigInfo"%>
<%@ page import = "java.text.*"%>
<%@ page import = "com.egurkha.EgTimeZoneConverter"%>
<%@ page import = "com.egurkha.util.DateUtil"%>
<%@ page import = "com.eg.EgInstallInfo" %>
<%@ page import = "net.sf.json.*"%>
<%@ page import = "com.eg.EgGroupInfo"%>
<%@ page import = "com.eg.EgTopoInfo"%>
<%@ page import = "com.eg.EgExternalInfo"%>
<%@ page import ="com.eg.EgURLDecoder"%>
<%@ page import="egurkha.config.EcProcessForm" %>
<%@ page import="egurkha.EgUIUtils" %>
<%@ page import="egurkha.config.EcSoftwareHotfixHandler" %>
<%@ page import = "com.eg.EgThresholdInfo"%>
<%@ page import="java.net.URLDecoder" %>

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

<%!
	String[] arrTopN = {"5","10","20","25","30","40","50","75","100"};
	String[] criterias = {"Zone","Service","Segment","Component"};
	
	public static ArrayList breakString(String givenString,int screenLen)
	{
		ArrayList aListResultant = new ArrayList();
		int strLen = givenString.length();
		int resLen = strLen/screenLen;
		int idx = 0;
		int k = 0;
		if (givenString.indexOf(" ")>-1)
		{
			for (int i=0;i<resLen;i++)
			{
				String res = givenString.substring(idx,idx+screenLen);
				int s = res.length();
				if (res.charAt(s-1)==' ')
				{
					res = res.trim();
					aListResultant.add(res);
					idx = idx+screenLen;
				}
				else
				{
					k = res.lastIndexOf(" ");
					if (k==0 || k == -1)
					{
						aListResultant.add(res.substring(0));
						idx = idx+screenLen;
					}
					else
					{
						aListResultant.add(res.substring(0,k));
						String temp = res.substring(k);
						int m = temp.length();
						idx = idx+screenLen;
						idx = idx-m;
					}
				}
				if (i == resLen-1)
				{
					int lastLen = strLen-idx;
					if (lastLen > screenLen)
					{
						resLen = resLen+1;
					}
				}
			}
			String ss = givenString.substring(idx).trim();
			aListResultant.add(ss);
		}
		else
		{
			for (int i=0;i<resLen ;i++)
			{
				String res = givenString.substring(idx,idx+screenLen);
				aListResultant.add(res);
				idx = idx+screenLen;
			}
			aListResultant.add(givenString.substring(idx));
		}
		return aListResultant;
	}


	String selectedTextInComo = "";
	public JSONArray fillSelectBox(Vector v, Vector vd, int i_displength,String s_default,String[] s_sel_text)
	{
		JSONArray selectBoxArr = new JSONArray();
		JSONObject tmpObj = new JSONObject();
		Vector v_sel = null;
		try
		{
			if((v == null || v.size() == 0) || (vd ==null || vd.size() == 0) || vd.size() != v.size())
			{
				selectedTextInComo = "-1";
				tmpObj.put("valueField","-1");
				tmpObj.put("displayField","Not Applicable!");
				selectBoxArr.add(tmpObj);
				return selectBoxArr;
			}

			
			StringBuffer sb;
			if(s_default == null || s_default.length() == 0 )
			{
				tmpObj = new JSONObject();
			}
			else if(s_sel_text == null || s_sel_text.length == 0)
			{
				selectedTextInComo = "-1";
				tmpObj.put("valueField","-1");
				tmpObj.put("displayField",s_default);
				selectBoxArr.add(tmpObj);
			}
			else
			{
				tmpObj.put("valueField","-1");
				tmpObj.put("displayField",s_default);
				selectBoxArr.add(tmpObj);
			}

			if(!(s_sel_text == null || s_sel_text.length ==0))
			{
				v_sel = new Vector(s_sel_text.length);
				for(int i=0;i<s_sel_text.length;i++)
				{
					v_sel.addElement(s_sel_text[i]);
				}
			}

			if(i_displength > 0)
			{
				for(int i=0;i<v.size();i++)
				{
					tmpObj = new JSONObject();
					if(!(v_sel == null) && v_sel.contains(v.elementAt(i)))
					{
						selectedTextInComo = (String)v.elementAt(i);
						tmpObj.put("valueField",v.elementAt(i));
						tmpObj.put("displayField",((String)vd.elementAt(i)).substring(0,i_displength));
						selectBoxArr.add(tmpObj);
					}
					else
					{
						tmpObj.put("valueField",v.elementAt(i));
						tmpObj.put("displayField",((String)vd.elementAt(i)).substring(0,i_displength));
						selectBoxArr.add(tmpObj);
					}
				}
			}
			else
			{
				for(int i=0;i<v.size();i++)
				{
					tmpObj = new JSONObject();
					if(!(v_sel == null) && v_sel.contains(v.elementAt(i)))
					{
						selectedTextInComo = (String)v.elementAt(i);
						tmpObj.put("valueField",v.elementAt(i));
						tmpObj.put("displayField",vd.elementAt(i));
						selectBoxArr.add(tmpObj);
					}
					else
					{
						tmpObj.put("valueField",v.elementAt(i));
						tmpObj.put("displayField",vd.elementAt(i));
						selectBoxArr.add(tmpObj);
					}
				}
			}
			return selectBoxArr;
		}
		catch(Exception ex)
		{
			selectedTextInComo = "-1";
			tmpObj = new JSONObject();
			tmpObj.put("valueField","-1");
			tmpObj.put("displayField","Not Applicable!");
			selectBoxArr.add(tmpObj);
			return selectBoxArr;
		}
	}

	public JSONArray fillSelectBox(Vector v,String module,String s_default,String s_sel_text)
	{
		Vector vd=new Vector();
		if(v==null || v.size()==0)
			return fillSelectBox(v,vd,-1,s_default,new String[]{s_sel_text});
		else
		{
			for(int n=0;n<v.size();n++)
			{
				String displayName="";
				String name=(String)v.get(n);
				if(name.indexOf(',')>-1)
					name=name.replace(',',' ');

				if(name.indexOf("Microsoft")>-1)
					displayName=name.substring(name.indexOf(' ')+1);
				else
					displayName=name;
				vd.addElement(displayName);
			}
			return fillSelectBox(v,vd,-1,s_default,new String[]{s_sel_text});
		}
	}

	private String prepareQueryString(Map params)
	{
		if (params == null || params.size() == 0) return "";
		String queryStr = "";
		Iterator itr = params.keySet().iterator();
		while (itr.hasNext())
		{
			String key = (String)itr.next();
			if (key.trim().length() == 0) continue;
			Object value= params.get(key);

			if (value instanceof String[])
			{
				String val[] =(String[])value;
				for(int i=0;i<val.length;i++)
				{
					if (key.equals("softwareName") || key.equals("details"))
					{
						queryStr += key + "=" + URLEncoder.encode(EgUIUtils.encodeSecurityChar(val[i])) + "&";
					}
					else
					{
						queryStr += key + "=" + val[i] + "&";
					}
				}
			}
			else
			{
				if (value == null) value = "";
				if (key.equals("softwareName") || key.equals("details"))
				{
					queryStr += key + "=" + URLEncoder.encode(EgUIUtils.encodeSecurityChar((String)value)) + "&";
				}
				else
				{
					queryStr += key + "=" + (String)value + "&";
				}
			}
		}
		if (queryStr.length() > 0) queryStr = queryStr.substring(0,queryStr.length()-1);
		return queryStr;
	}
	
	private int getTestmsrNoForInstldDscrptr(String testName)
	{
		int msrNoForInstldDscrptr = 0;
		if(testName != null)
		{
			EgThresholdInfo egThresholdInfo = new EgThresholdInfo("manager");
			String[] measures = egThresholdInfo.getMeasurementsForTest(testName);
			if(measures != null && measures.length >0)
			{
				msrNoForInstldDscrptr = measures.length-1;
			}
		}
		return msrNoForInstldDscrptr;
	}
%>

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
%>

<%
	
	String queryString = prepareQueryString(request.getParameterMap());
	JSONObject rootJson = new JSONObject();
	JSONObject hiddenFields = new JSONObject();
	JSONObject chartData = new JSONObject();
	//JSONArray resultantArray = new JSONArray();

	rootJson.put("success","true");

	UserRegistry registry = UserRegistry.getInstance();
	User user = registry.getUser(session.getId());
	IniUI iniUI = IniUI.getInstance();

	String userLanguage = iniUI.getDefaultLanguage();
	if (user != null)
	{
		userLanguage = user.getLanguage();
	}
	EgMappingInfo mappingInfo = new EgMappingInfo(userLanguage);

	String userName = (String)session.getAttribute("sess_auserid");

	EcProcessResult processResult=(EcProcessResult)request.getAttribute("Result");
	EcProcessData processData=(EcProcessData)request.getAttribute("dataResult");

	EcUtils ecUtils = new EcUtils();
	EcPrintData printData=new EcPrintData();

	String uiFile = "eg_ui.ini";
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile iniFile = new IniFile(s_iniFile_dir + uiFile);
	String currentDateFormat = iniFile.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	
	String userDateFormat = user.getUserDateFormat();
	if (userDateFormat != null && userDateFormat.equalsIgnoreCase("default"))
	{
		userDateFormat = currentDateFormat;
	}

	// If user date format is set, assign it to current date format.
	if (userDateFormat != null)
	{
		currentDateFormat = userDateFormat;
	}

	SimpleDateFormat formatter = new SimpleDateFormat(currentDateFormat+" "+"HH:mm:ss");
	Date currentDateTime = new Date();
	String dateString = formatter.format(currentDateTime);

	/* Added by Prakash.K Starts */
	EgTimeZoneConverter timeZoneConverter = new EgTimeZoneConverter(dateString,user);
	dateString = timeZoneConverter.getDateTime();
	/* Added by Prakash.K Ends */

	EgConfigTestsInfo configTestsInfo = new EgConfigTestsInfo("manager");
	Map osMap = configTestsInfo.getOSMapping();
	Map dashboardSetting = (Hashtable)ConfigDbSetting.getDbSettingData(user.getLoginID(),EgConfigConstants.USER_COMMON_SETTING);
	String barColor = configTestsInfo.getServerDisplayValue("MISC_ARGS","StackedBarChartColors");

	Vector vecUserOptions = (Vector)session.getAttribute("vecUserOptions");
	String ec_userOption = (String)session.getAttribute("ec_userOption");
	String ec_userOptionPrevious = request.getParameter("ec_userOptionPrevious");
	String configSkin = user.getConfigSkin();
	String submitOrNot = (String) request.getAttribute("isSubmit");
	String viewCount=request.getParameter("viewCount");
	String softwareName= EgUIUtils.decodeSecurityChar(request.getParameter("softwareName"));
	String osOnly=request.getParameter("osOnly");
	String strBackRequest = request.getParameter("back_request");
	if (strBackRequest != null && strBackRequest.equalsIgnoreCase("ScheduleConfig"))
	{
		String[] softwareNameArr = (String[]) request.getAttribute("software");
		softwareName = softwareNameArr[0];
		String[] osOnlyArr = (String[]) request.getAttribute("operatingSystem");
		osOnly = osOnlyArr[0];
		String[] viewCountArr = (String[]) request.getAttribute("count");
		if (viewCountArr != null)
		{
			viewCount = viewCountArr[0];
		}
		else
		{
			viewCount = "All";
		}
		String[] statusCountArr = (String[]) request.getAttribute("statusChoice");
		String statusCount = statusCountArr[0];
	}

	//String view = (String) request.getParameter("ec_criteria");
	//String option = ec_userOption;

	String ec_criteria = (String)session.getAttribute("ec_criteria");
	String ec_criteriaPrevious = request.getParameter("ec_criteriaPrevious");
	String request_from = request.getParameter("request_from");


	Map scheduleDetails = (Map)request.getAttribute("ScheduleDetails");
	if (scheduleDetails != null && request_from != null && request_from.equalsIgnoreCase("saveSchedule"))
	{
		vecUserOptions = (Vector) scheduleDetails.get("vecUserOptions");
		softwareName = (String)scheduleDetails.get("softwareName");
		osOnly = (String)scheduleDetails.get("osName");
		viewCount = (String)scheduleDetails.get("countChoice");
		ec_userOption = (String)scheduleDetails.get("ec_userOption");
		ec_userOptionPrevious = (String)scheduleDetails.get("ec_userOptionPrevious");
	}
	Map finalData=new TreeMap();
	Map countData=new TreeMap();
	ArrayList softwareCount=new ArrayList();

	if (scheduleDetails != null && request_from != null && request_from.equalsIgnoreCase("saveSchedule"))
	{
		ec_criteria = (String)scheduleDetails.get("ec_criteria");
		ec_criteriaPrevious = (String)scheduleDetails.get("ec_criteriaPrevious");
	}
	
	Vector vecCriterias = new Vector();
	for (int g=0;g<criterias.length;g++)
	{
		String criteria = criterias[g];
		vecCriterias.add(criteria);
	}
	Vector osList=processResult.getOsList();
	if(osList!=null && osList.size()>0)
	{
		osList.remove("Unknown");
	}

	String testName=processResult.getTestName();
	String osName=processResult.getOsName();

	//JSONObject osObj = new JSONObject();
	//JSONArray osArr = new JSONArray();
	//rootJson.put("selectedOsName",osList);
	//if(!osList.isEmpty())
	//{
		//for (int i=0; i<osList.size(); i++)
		//{
			//String value = (String)osList.get(i);
			//osObj.put("displayField",_mapping.getTextDescription("configmgmt.inventory.availability."+value));
			//osObj.put("valueField",value);
			//osArr.add(osObj);
		//}
	//}
	//else
	//{
		//osObj.put("displayField",_mapping.getTextDescription("configmgmt.inventory.availability.Not Applicable"));
		//osObj.put("valueField","-1");
		//osArr.add(osObj);
	//}

	//rootJson.put("osList",osArr);
	rootJson.put("osList",fillSelectBox(osList,"config","",osName));
	rootJson.put("selectedOS",selectedTextInComo);

	int barCount=processResult.getBarCount();
	if(testName!=null && osName!=null)
	{
		finalData=(TreeMap)processResult.getStatusData();
		countData=(TreeMap)processResult.getStatusCount();
		Iterator citerate=countData.keySet().iterator();
		while(citerate.hasNext())
		{
			Integer count=(Integer)citerate.next();
			softwareCount.add(count);
		}
	}
	Vector softwareNameList=processResult.getInfoNameList();

	boolean goBack = false;
	boolean requestFromSoftware = false;
	String scheduleBack = (String) request.getAttribute("schedule");
	boolean isDisplay = true;
	if (request_from != null && !request_from.equalsIgnoreCase("HomePage") && !"null".equalsIgnoreCase(request_from))
	{
		goBack = true;
		requestFromSoftware = true;
	}
	if (scheduleBack != null && scheduleBack.equalsIgnoreCase("scheduleBack") || request_from != null && request_from.equalsIgnoreCase("SoftwareAvailability"))
	{
		requestFromSoftware = false;
	}
	if (request_from != null && request_from.equalsIgnoreCase("saveSchedule"))
	{
		goBack = false;
		requestFromSoftware = false;
		submitOrNot = "false";
		isDisplay = false;
	}
	if (submitOrNot == null || "null".equals(submitOrNot))
	{
		submitOrNot = "true";
		isDisplay = true;
	}

	String id = request.getParameter("id");
	String mngrIProc = new IniFile(new EgConfigInfo("manager").getDbFile()).getValue("DB_PROPERTIES","ManagerUrl");


	hiddenFields.put("ec_rtype","SoftwareAvailability");
	hiddenFields.put("ec_trgttype","Configuration_Report");
	hiddenFields.put("isSubmit","true");
	hiddenFields.put("statusChoice",testName);
	hiddenFields.put("ec_criteriaPrevious",ec_criteria);
	hiddenFields.put("ec_userOptionPrevious",ec_userOption);
	hiddenFields.put("ec_option1",requestFromSoftware);


	JSONObject backOperation = new JSONObject();
	if(requestFromSoftware)
	{
	}

	if (goBack)
	{
		backOperation.put("historyBack",new Boolean(true));
	}

	else if (request_from != null && request_from.equalsIgnoreCase("saveSchedule"))
	{
		backOperation.put("schedule",new Boolean(true));
	}

	rootJson.put("backOperation",backOperation);
	
	if(requestFromSoftware)
	{
	}


	JSONObject viewByObj = new JSONObject();
	JSONArray viewByArr = new JSONArray();

	if (!requestFromSoftware)
	{
		if (vecCriterias != null && !vecCriterias.isEmpty())
		{
			for(int i=0; i<vecCriterias.size(); i++)
			{
				String selected = "";
				String strCriteria = (String)vecCriterias.get(i);
				if(ec_criteria != null && strCriteria.equals(ec_criteria))
				{
					rootJson.put("selectedEcOpt",ec_criteria);
				}
				viewByObj.put("displayField",_mapping.getTextDescription("configmgmt.common."+strCriteria));
				viewByObj.put("valueField",strCriteria);
				viewByArr.add(viewByObj);
			}
		}
		else
		{
			rootJson.put("selectedEcOpt","-1");
			viewByObj.put("displayField",_mapping.getTextDescription("configmgmt.common.Not Applicable"));
			viewByObj.put("valueField","-1");
			viewByArr.add(viewByObj);
		}
		rootJson.put("viewByList",viewByArr);
		
		if (ec_criteria != null && !ec_criteria.equalsIgnoreCase(criterias[3]) && !ec_criteria.equals("-1"))
		{
			if (ec_criteria.equalsIgnoreCase(criterias[0]))
			{
				rootJson.put("specViewByTitle",_mapping.getTextDescription("configmgmt.common."+criterias[0]));
			}
			else if (ec_criteria.equalsIgnoreCase(criterias[1]))
			{
				rootJson.put("specViewByTitle",_mapping.getTextDescription("configmgmt.common."+criterias[1]));
			}
			else
			{
				rootJson.put("specViewByTitle",_mapping.getTextDescription("configmgmt.common."+criterias[2]));
			}

			JSONObject specviewByObj = new JSONObject();
			JSONArray specviewByArr = new JSONArray();

			if (vecUserOptions != null && !vecUserOptions.isEmpty())
			{
				for(int i=0; i<vecUserOptions.size(); i++)
				{
					String selected = "";
					String strUserOption = (String)vecUserOptions.get(i);
					if(ec_userOption != null && strUserOption.equals(ec_userOption))
					{
						rootJson.put("selectedUserOption",ec_userOption);
					}
					specviewByObj.put("displayField",strUserOption);
					specviewByObj.put("valueField",strUserOption);
					specviewByArr.add(specviewByObj);
				}
			}
			else
			{
				rootJson.put("selectedUserOption","-1");
				specviewByObj.put("displayField",_mapping.getTextDescription("configmgmt.inventory.availability.Not Applicable"));
				specviewByObj.put("valueField","-1");
				specviewByArr.add(specviewByObj);
			}
			rootJson.put("specViewByList",specviewByArr);
		}
		else
		{
			
		}

		JSONObject softwareObj = new JSONObject();
		JSONArray softwareArr = new JSONArray();
		if (softwareNameList != null && !softwareNameList.isEmpty())
		{
			for(int i=0; i<softwareNameList.size(); i++)
			{
				String selected = "";
				String strSoftwareName = (String)softwareNameList.get(i);
				if(softwareName != null && softwareName.equals(strSoftwareName))
				{
					rootJson.put("selectedSoftwareName",softwareName);
				}
				if(strSoftwareName.equalsIgnoreCase("All"))
				{
					rootJson.put("selectedSoftwareName",softwareName);
					softwareObj.put("displayField",strSoftwareName);
					softwareObj.put("valueField",strSoftwareName);
					softwareArr.add(softwareObj);
				}
				else
				{
					softwareObj.put("displayField",strSoftwareName);
					softwareObj.put("valueField",strSoftwareName);
					softwareArr.add(softwareObj);
				}
			}
		}
		else
		{
			rootJson.put("selectedSoftwareName","-1");
			softwareObj.put("displayField",_mapping.getTextDescription("configmgmt.inventory.availability.Not Applicable"));
			softwareObj.put("valueField","-1");
			softwareArr.add(softwareObj);
		}
		rootJson.put("softwareList",softwareArr);


		
		JSONObject showTopNResObj = new JSONObject();
		JSONArray showTopNResArr = new JSONArray();

		showTopNResObj.put("displayField",_mapping.getTextDescription("configmgmt.inventory.availability.All"));
		showTopNResObj.put("valueField","All");
		showTopNResArr.add(showTopNResObj);

		for (int i=0;i<arrTopN.length;i++)
		{
			String strTopN = arrTopN[i];

			String selected = "";
			if (strTopN.equals(viewCount))
			{
				rootJson.put("selectedShowTopNRes",strTopN);
			}

			showTopNResObj.put("displayField",_mapping.getTextDescription("configmgmt.inventory.availability.Top")+"-"+strTopN);
			showTopNResObj.put("valueField",strTopN);
			showTopNResArr.add(showTopNResObj);
		}
		rootJson.put("showList",showTopNResArr);

		if (request_from != null && request_from.equalsIgnoreCase("saveSchedule"))
		{
		}
		else
		{
		}
	}

	if(osName!=null && osName.length()>0 && osOnly==null && !osName.equals("null") && submitOrNot.equals("true"))
	{
		rootJson.put("containerTitle",mappingInfo.getTextDescription("configmgmt.inventory.availability.Software availability/unavailability in ").toUpperCase()+" "+osName +" "+mappingInfo.getTextDescription("configmgmt.inventory.availability.Systems").toUpperCase());
	}
	//pdf work
	ArrayList aListPdfData = new ArrayList();
	EgPdfData pdfData = new EgPdfData();
	EgTable egTable = new EgTable();
	EgReportAttributes reportAttributes = new EgReportAttributes();
	if(finalData != null && finalData.size() > 0 && submitOrNot.equals("true"))
	{
		rootJson.put("hasConfigDetails",new Boolean(true));

		// //pdf work
		// ArrayList aListPdfData = new ArrayList();
		// EgPdfData pdfData = new EgPdfData();
		// EgTable egTable = new EgTable();
		// EgReportAttributes reportAttributes = new EgReportAttributes();
		String pdfTitle = "";
		if (ec_criteria != null && !ec_criteria.equalsIgnoreCase(criterias[3]) && !ec_criteria.equals("-1"))
		{
			pdfTitle = mappingInfo.getTextDescription("configmgmt.inventory.availability.INVENTORY REPORT BY  ") + " " + mappingInfo.getTextDescription("configmgmt.inventory.availability. SOFTWARE AVAILABILITY/UNAVAILABILITY") + " IN" + " '" + ec_userOption + "' "  +  ec_criteria + "  " + mappingInfo.getTextDescription("configmgmt.configuration.as on") + "  " + dateString;
		}
		else
		{
			pdfTitle = mappingInfo.getTextDescription("configmgmt.inventory.availability.INVENTORY REPORT BY ") + " " + mappingInfo.getTextDescription("configmgmt.inventory.availability. SOFTWARE AVAILABILITY/UNAVAILABILITY") + "  " + mappingInfo.getTextDescription("configmgmt.configuration.as on") + "  " + dateString;
		}
		reportAttributes.setMainTitle(pdfTitle);
		String reportSubTitle = "";
		if (softwareName != null)
		{
			if (softwareName.equalsIgnoreCase("All"))
			{
				reportSubTitle = mappingInfo.getTextDescription("configmgmt.inventory.availability.SOFTWARE AVAILABILITY/UNAVAILABILITY") + " IN " + osName +" " + mappingInfo.getTextDescription("configmgmt.inventory.availability.SYSTEMS");
			}
			else
			{
				reportSubTitle = mappingInfo.getTextDescription("configmgmt.inventory.availability.AVAILABILITY/UNAVAILABILITY OF ") + " " + softwareName+" SOFTWARE IN " + osName + mappingInfo.getTextDescription("configmgmt.inventory.availability. SYSTEMS");
			}
		}

		reportAttributes.setSubTitle(reportSubTitle);

		reportAttributes.setReportName("SoftwareAvailability");
		reportAttributes.setModule("CONFIG");
		reportAttributes.setPortrait(true);
		pdfData.setAttributes(reportAttributes);


		if(softwareName!=null)
		{
			String barLabels="";
			String barLabelsForPrint="";
			String sampleValues="";
			String barColors = "";

			String barTitle=mappingInfo.getTextDescription("configmgmt.inventory.availability.Top")+" - "+barCount+"  "+mappingInfo.getTextDescription("configmgmt.inventory.availability.Software Availability in")+" "+ osName;

			String rangeAxisLabel="No of systems";
			rangeAxisLabel = mappingInfo.getTextDescription("configmgmt.inventory.availability."+rangeAxisLabel);

			int countVal=0;
			int maxCount=0;
			boolean flag=false;
			String barHeight = "260";
			String yAxisMaxValue  = " ";
			ArrayList aListCount = new ArrayList();

			for(int n=0;n<softwareCount.size();n++)
			{
				Integer count=(Integer)softwareCount.get(n);
				if (!aListCount.contains(count))
				{
					aListCount.add(count);
				}

				List infoName=(ArrayList)countData.get(count);

				//new code
				if(!("All".equalsIgnoreCase(softwareName)))
				{
					List alist=new ArrayList();
					alist.add("+"+softwareName);
					if(infoName!=null && infoName.size()==1)
					{
						String tempSoftware=(String)infoName.get(0);
						if(!tempSoftware.equals("+"+softwareName))
							continue;
					}
					else if(!infoName.retainAll(alist))
						continue;
						
					barHeight = "200";
				}
				//end !!!!

				//Collections.sort(infoName,String.CASE_INSENSITIVE_ORDER);
				int size=count.intValue();
				//maxCount=Math.max(maxCount,size);
				for(int n1=0;((infoName!=null && infoName.size()>0) && n1<infoName.size());n1++)
				{
					String info=(String)infoName.get(n1);

					//code change for maxCount
					if(n1 == 0)
					{
						if(!("All".equalsIgnoreCase(softwareName)))
						{
							barTitle = mappingInfo.getTextDescription("configmgmt.inventory.availability.Software Availability in")+" " + osName;
						}
						String associatedNickName=(String)finalData.get(info);
						int availCount = 0;
						int unAvailCount = 0;
						ArrayList nickList1=processData.getInfoNickNames(osName,associatedNickName,true);
						ArrayList nickList2=processData.getInfoNickNames(osName,associatedNickName,false);

						if(nickList1 != null) availCount = nickList1.size();
						if(nickList2 != null) unAvailCount = nickList2.size();
						maxCount = availCount + unAvailCount;
					}
					//End

					if(countVal==0)
					{
						String infoTmp = info.substring(1);
						String infoTmp_A = infoTmp;
						//infoTmp = EgURLEncoder.encode(infoTmp,"UTF-8");
						barLabels = infoTmp;
						sampleValues=""+count.toString();
						barColors = barColor;

						if (infoTmp_A != null && infoTmp_A.length() > 50)
						{
							ArrayList aListStrValues = breakString(infoTmp_A,50);
							infoTmp_A = "";
							for (int r=0;r<aListStrValues.size();r++)
							{
								String str_value = (String)aListStrValues.get(r);
								infoTmp_A += str_value + "\\n";
							}
						}
						barLabelsForPrint = infoTmp_A;
					}
					else
					{
						String infoTmp = info.substring(1);
						String infoTmp_A = infoTmp;
						//infoTmp = EgURLEncoder.encode(infoTmp,"UTF-8");
						barLabels=barLabels+","+infoTmp;
						sampleValues=sampleValues+","+count.toString();
						barColors = barColors + "," + barColor;

						if (infoTmp_A != null && infoTmp_A.length() > 50)
						{
							ArrayList aListStrValues = breakString(infoTmp_A,50);
							infoTmp_A = "";
							for (int r=0;r<aListStrValues.size();r++)
							{
								String str_value = (String)aListStrValues.get(r);
								infoTmp_A += str_value + "\\n";
							}
						}
						barLabelsForPrint = barLabelsForPrint + "," + infoTmp_A;
					}
					countVal++;
					if(countVal==barCount)
					{
						flag=true;
						break;
					}
				}
				if(flag)
					break;
			}

			boolean flagEqualsOne = true;
			for (int i=1; i<aListCount.size(); i++)
			{
				Integer count_Value = (Integer)aListCount.get(i);
				if (count_Value.intValue() > 1)
				{
					flagEqualsOne = false;
					break;
				}
			}
			if (flagEqualsOne)
			{
				yAxisMaxValue = "5";
			}
			else
			{
				yAxisMaxValue = "0";
			}

			//Fusion Bar Chart Data starts--Rekha
			HashMap mapChartAttributes = new HashMap();//map for chart attributes
			mapChartAttributes.put("bgcolor","FFFFFF");
			mapChartAttributes.put("yAxisName","Number of systems");
			mapChartAttributes.put("showToolTip","1");
			mapChartAttributes.put("showValues","1");
			mapChartAttributes.put("showBorder","0");
			mapChartAttributes.put("showShadow","0");
			mapChartAttributes.put("alpha","0");
			mapChartAttributes.put("canvasBgColor","F2F2F2");
			mapChartAttributes.put("canvasBgAlpha","60");
			mapChartAttributes.put("canvasBorderColor","000000");
			mapChartAttributes.put("canvasBorderThickness","1");
			mapChartAttributes.put("canvasBorderAlpha","30");
			mapChartAttributes.put("yAxisMaxValue",yAxisMaxValue);
			mapChartAttributes.put("maxBarHeight","40");

			HashMap mapChartData = new HashMap();//map for chart data
			mapChartData.put("barLabels",barLabels);
			mapChartData.put("barValues",sampleValues);
			mapChartData.put("barColors",barColors);

			HashMap mapProperties = new HashMap();//map for misc properties
			mapProperties.put("showColors","true");
			mapProperties.put("enableLinks","false");

			EcFusionChartBean chartbean = new EcFusionChartBean();
			String xmlString = chartbean.getXMLDataForStackedBarChart(mapChartAttributes,mapChartData,mapProperties);
			//Fusion Bar Chart Data ends
			chartData.put("xmlString",xmlString);

			printData.setChartTitle(barTitle);
			printData.setSampleAxisLabel(rangeAxisLabel);
			printData.setSampleValue(sampleValues);
			printData.setBarLabel(barLabelsForPrint);
			printData.setMaxRange(String.valueOf(maxCount));

			chartData.put("chartTitle",barTitle);
			chartData.put("xAxisLabel",chartbean.getXAxisDataLabel());
			chartData.put("yAxisLabel",(String)chartbean.getYAxisLabel());
			chartData.put("chartHeight",chartbean.getBarChartHeight());
			chartData.put("chartWidth",chartbean.getBarChartWidth());

			JSONArray dataSet = new JSONArray();
			JSONObject s1=new JSONObject();
			s1.put("name","");
			s1.put("data",chartbean.getBarDataSet());
			s1.put("showInLegend", new Boolean(false));
			dataSet.add(s1);

			chartData.put("DataSet",dataSet);
			chartData.put("title",barTitle);
			chartData.put("tickInterval",1);
			chartData.put("chartType","bar");
			//chartData.put("inside", new Boolean(true));
			chartData.put("legend", new Boolean(false));
			rootJson.put("chartData",chartData);

			// pdf work
			EgTableAttributes egTableAttributes = new EgTableAttributes();
			egTableAttributes.setImageData(true);

			ArrayList aListData = new ArrayList();
			aListData.add(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/final/servlet/com.eg.EcChartController?view=GRAPH&pageName=AVAILABILITY&chartType=bar&height="+URLEncoder.encode(barHeight)+"&width=650&chartTitle="+EgURLEncoder.encode(barTitle,"UTF-8")+"&sampleValues="+URLEncoder.encode(sampleValues)+"&barLabels="+EgURLEncoder.encode(barLabels,"UTF-8")+"&rangeAxisLabel="+EgURLEncoder.encode(rangeAxisLabel,"UTF-8")+"&range="+maxCount+"&userName="+EgURLEncoder.encode(userName,"UTF-8"));

			ArrayList userColumnNames = new ArrayList();
			userColumnNames.add("Image");

			EgTableModel egTableModel = new EgTableModel(aListData,userColumnNames);

			egTable = new EgTable();
			egTable.setModel(egTableModel);
			egTable.setAttributes(egTableAttributes);

			aListPdfData.add(egTable);
		}

		//Start to assign print data
		printData.setOsName(osName);
		printData.setTestName(testName);
		//printData.setMapData(finalData);
		printData.setShowCount(viewCount);
		printData.setAvailableName(softwareName);
		printData.setListData(softwareCount);
		printData.setMapData(finalData);
		printData.setMapCountData(countData);

		ArrayList colorsAList = new ArrayList();
		StringTokenizer barColorTkns = new StringTokenizer(barColor,",");
		while(barColorTkns.hasMoreTokens())
		{
			colorsAList.add(barColorTkns.nextToken());
		}
		String chartColorTemp = (String)colorsAList.get(0);
		String chartColorForPrint = chartColorTemp.substring(1,chartColorTemp.length());
		printData.setSampleColor(chartColorForPrint);

		session.setAttribute("SOFTWARE_AVAILABILITY",printData);
		session.setAttribute("processDataObject",processData);
		//End to assign print data

		// /*int selCount=0; 
		// boolean skip=false;
		// for(int n=0;n<softwareCount.size();n++)
		// {
			// Integer count=(Integer)softwareCount.get(n);
			// List infoName=(ArrayList)countData.get(count);
			
			// if(!("All".equalsIgnoreCase(softwareName)))
			// {
				// List alist=new ArrayList();
				// alist.add("+"+softwareName);
				// if(infoName!=null && infoName.size()==1)
				// {
					// String tempSoftware=(String)infoName.get(0);
					// if(!tempSoftware.equals("+"+softwareName))
						// continue;
				// }
				// else if(!infoName.retainAll(alist))
					// continue;
			// }

			// //Collections.sort(infoName,String.CASE_INSENSITIVE_ORDER);

			// JSONObject infoObj = new JSONObject();

			// for(int n1=0;((infoName!=null && infoName.size()>0) && n1<infoName.size());n1++)
			// {
				// //pdf work
				// ArrayList aListData = new ArrayList();
				// ArrayList aListColumnNames = new ArrayList();
				// ArrayList userColumnNames = new ArrayList();

				// aListColumnNames.add("C1");
				// aListColumnNames.add("C2");

				// userColumnNames.add("");

				// EgTableAttributes egTableAttributes = new EgTableAttributes();
				// egTableAttributes.setStartX(0);
				// egTableAttributes.setDataGrouping(true);
				// egTableAttributes.setDataGroupingField(0);
				// egTableAttributes.setColumnNames(userColumnNames);

				// String info=(String)infoName.get(n1);
				// String associatedNickName=(String)finalData.get(info);
				// if(viewCount!=null && !viewCount.equalsIgnoreCase("All"))
				// {
					// int showCount=Integer.parseInt(viewCount);
					// if(showCount==selCount)
					// {
						// skip=true;
						// break;
					// }
					// selCount++;
				// }
				// //egTableAttributes.setTitle(info.substring(1));
				// userColumnNames.add(info.substring(1));

				// infoObj.put("software",info.substring(1));

				// ArrayList nickList=processData.getInfoNickNames(osName,associatedNickName,true);
				// String AvailableSys = "";
				// String unAvailableSys = "";
				// String reqSys = "";
				// if(nickList!=null && nickList.size()>0)
				// {
					// //pdf work
					// String temp = null;
					// if(nickList.size() > 1)
						// temp = "Systems";
					// else temp = "System";
					// aListData.add(mappingInfo.getTextDescription("configmgmt.inventory.availability.Available in")+" "+nickList.size()+" "+mappingInfo.getTextDescription("configmgmt.inventory.availability."+temp));

					// String availableStr = mappingInfo.getTextDescription("configmgmt.inventory.availability.Available in")+" "+nickList.size()+" "+mappingInfo.getTextDescription("configmgmt.inventory.availability."+temp);

					// //infoObj.put("Availability",availableStr);

					// String systemNamePdf = null;
					// String tempSystemName = null;
					// for(int s=0;s<nickList.size();s++)
					// {
						// String systemName=(String)nickList.get(s);
						// systemName=systemName.trim();

						// //pdf work
						// if(systemNamePdf == null){
							// systemNamePdf = systemName;
						// }else{
							// systemNamePdf = systemNamePdf + ", " + systemName;
						// }

						// if(tempSystemName == null){
							// tempSystemName = systemName;
						// }else{
							// tempSystemName = tempSystemName + "#$#" + systemName;
						// }

						// if(nickList!=null && (s!=nickList.size()-1))
						// {
							// //rootJson.put("systemName",systemName);
						// }
						// else
						// {
							// //rootJson.put("systemName",systemName);
						// }
					// }
					// //pdf work
					// aListData.add(systemNamePdf);
					// AvailableSys = availableStr + "#$#" +tempSystemName;
					// //infoObj.put("AvailableSys",AvailableSys);
				// }

				// nickList=processData.getInfoNickNames(osName,associatedNickName,false);
				// if(nickList!=null && nickList.size()>0)
				// {
					// //pdf work
					// String temp = null;
					// if(nickList.size() > 1)
						// temp = "Systems";
					// else temp = "System";
					// aListData.add(mappingInfo.getTextDescription("configmgmt.inventory.availability.Unavailable in")+" "+nickList.size()+" "+mappingInfo.getTextDescription("configmgmt.inventory.availability."+temp));

					// String unAvailableStr = mappingInfo.getTextDescription("configmgmt.inventory.availability.Unavailable in")+" "+nickList.size()+" "+mappingInfo.getTextDescription("configmgmt.inventory.availability."+temp);

					// //infoObj.put("Unavailability",unAvailableStr);
					// String systemNamePdf = null;
					// String tempSystemName = null;
					// for(int s=0;s<nickList.size();s++)
					// {
						// String systemName=(String)nickList.get(s);
						// systemName=systemName.trim();

						// //pdf work
						// if(systemNamePdf == null){
							// systemNamePdf = systemName;
						// }else{
							// systemNamePdf = systemNamePdf + ", " + systemName;
						// }

						// if(tempSystemName == null){
							// tempSystemName = systemName;
						// }else{
							// tempSystemName = tempSystemName + "#$#" + systemName;
						// }

						// if(nickList!=null && (s!=nickList.size()-1))
						// {
							// rootJson.put("systemName",systemName);
						// }
						// else
						// {
							// rootJson.put("systemName",systemName);
						// }
					// }
					// //pdf work
					// aListData.add(systemNamePdf);
					// unAvailableSys = unAvailableStr + "#$#" +tempSystemName;
					// //infoObj.put("unAvailableSys",unAvailableSys);
				// }

				// if(unAvailableSys != "" && unAvailableSys != null)
					// reqSys = AvailableSys + "|" + unAvailableSys;
				// else
					// reqSys = AvailableSys;
				// infoObj.put("system",reqSys);
				// resultantArray.add(infoObj);

				// //pdf work
				// EgTableModel egTableModel = new EgTableModel(aListData,aListColumnNames);
				// egTable = new EgTable();
				// egTable.setModel(egTableModel);
				// egTable.setAttributes(egTableAttributes);

				// aListPdfData.add(egTable);
			// }

			// if(skip)
				// break;
		// }
		// //pdf work
		// pdfData.setData(aListPdfData);
		// session.setAttribute("pdfData",pdfData);*/
	}
	
	else if(osName != null && !osName.equals("null") && !osName.equals("-1") && submitOrNot.equals("true"))
	{  // while finalData Loop
		rootJson.put("hasConfigDetails",new Boolean(false));
		rootJson.put("errorText",mappingInfo.getTextDescription("configmgmt.inventory.availability.Software not available in systems of the selected OS"));
	}

	if(!("All".equalsIgnoreCase(softwareName)))
	{

	}
	
	StringBuffer printQS = new StringBuffer();
	printQS.append("printType=SOFTWARE_AVAILABILITY");
	printQS.append("&printPageTitle=INVENTORY_REPORT_BY_SOFTWARE_AVAILABILITY/UNAVAILABILITY");
	printQS.append("&Page=OS");
	printQS.append("&printData=yes");
	printQS.append("&userName="+EgURLEncoder.encode(userName,"UTF-8"));
	printQS.append("&user_language="+userLanguage);
	printQS.append("&ec_criteria="+ec_criteria);
	printQS.append("&ec_userOption="+((ec_userOption != null)?EgURLEncoder.encode(ec_userOption,"UTF-8"):""));

	String s_qryString = "../servlet/com.eg.EcRepoLiteC?"+printQS.toString()+"&ec_forPDF=No";
	rootJson.put("printQueryStr",s_qryString);

	session.setAttribute("PDF_URL",mngrIProc+"/final/servlet/com.eg.EcRepoLiteC;jsessionid="+session.getId()+"?"+printQS.toString()+"&ec_forPDF=Yes");
	session.setAttribute("REPORT_NAME","InventoryReportBySoftware");
	session.setAttribute("REPORT_LAYOUT","Portrait");
	session.setAttribute("PAGE_WIDTH_PDF","1100");
	session.setAttribute("PAGE_WIDTH_MAIL_IMAGE","1300");
	session.setAttribute("PAGE_HEIGHT_MAIL_IMAGE","768");
	
	//New  changes starts
	if(testName != null && "SoftwareList_cf".equalsIgnoreCase(testName))
	{
		int msrNoForInstldDscrptr = getTestmsrNoForInstldDscrptr(testName);
		int ttlCmpntsCount = 0;
		
		EcSoftwareHotfixHandler ecSoftwareHotfixHandler = new EcSoftwareHotfixHandler();
	
		List allAssctdMngdCmpTypCmpnts = (ArrayList)session.getAttribute("associatedComponents");
		Map avlblCmpntNmType =ecSoftwareHotfixHandler.extractCmpntNmType(allAssctdMngdCmpTypCmpnts);
		
		//Total components Details
		Map trgtHostOsNm = ecSoftwareHotfixHandler.getAllTrgtHostsWithOsNm(avlblCmpntNmType);
		Map selectedOsNameWithTrgtHost = new HashMap();
		
		if(osName != null && "All".equalsIgnoreCase(osName))
		{
			ttlCmpntsCount = trgtHostOsNm.size();
		}
		else
		{
			Iterator itr = trgtHostOsNm.keySet().iterator();
			int count =0;
			while (itr.hasNext()) 
			{
				String   curKey = (String) itr.next();
				String curVal = (String)trgtHostOsNm.get(curKey);
				String tempOsName =(String)osMap.get(curVal);
				
				if(tempOsName != null && osName.equalsIgnoreCase(tempOsName))
				{
					count++;
				}
                else if( (tempOsName == null) && (curVal != null) && (osName != null) && (osName.trim().equalsIgnoreCase(curVal.trim())) )
				{
				    count++;	
				}
			}
			ttlCmpntsCount = count;
		}
		
		LinkedHashMap availableSftwrNameCount = (LinkedHashMap)ecSoftwareHotfixHandler.getAvailableComponentsInfoCount( avlblCmpntNmType, trgtHostOsNm, "Software" ,msrNoForInstldDscrptr, osName, softwareName,viewCount);
		session.setAttribute("availableSftwrNameCount",availableSftwrNameCount);
		session.setAttribute("ttlCmpntsCount",ttlCmpntsCount);
		if(availableSftwrNameCount != null && availableSftwrNameCount.size() >0)
		{
			Iterator iterator = availableSftwrNameCount.keySet().iterator();
			JSONArray sftwrAvlblUnvlblCountArr = new JSONArray();
			
			while(iterator.hasNext())
			{ 
				JSONObject tempJson = new JSONObject();
				String currentSftwrName = (String)iterator.next();
				int availableCount = ((Integer)availableSftwrNameCount.get(currentSftwrName)).intValue();
				int unAvailableCount = ttlCmpntsCount- availableCount;
				tempJson.put("SoftwareName", currentSftwrName);
				tempJson.put("availableCount", availableCount);
				tempJson.put("unavailableCount", unAvailableCount);
				sftwrAvlblUnvlblCountArr.add(tempJson);
				
				//pdfData
				ArrayList aListData = new ArrayList();
				ArrayList aListColumnNames = new ArrayList();
				ArrayList userColumnNames = new ArrayList();

				aListColumnNames.add("C1");
				aListColumnNames.add("C2");
				userColumnNames.add("");
				
				EgTableAttributes egTableAttributes = new EgTableAttributes();
				egTableAttributes.setStartX(0);
				egTableAttributes.setDataGrouping(true);
				egTableAttributes.setDataGroupingField(0);
				egTableAttributes.setColumnNames(userColumnNames);
				userColumnNames.add(currentSftwrName);
				aListData.add(mappingInfo.getTextDescription("configmgmt.inventory.availability.Number of components having the softwares")+": "+availableCount);
				aListData.add(mappingInfo.getTextDescription("configmgmt.inventory.availability.Number of components not having the softwares")+": "+unAvailableCount);
				EgTableModel egTableModel = new EgTableModel(aListData,aListColumnNames);
				egTable = new EgTable();
				egTable.setModel(egTableModel);
				egTable.setAttributes(egTableAttributes);
				aListPdfData.add(egTable);
			}
			rootJson.put("sftwrAvlblUnvlblCount",sftwrAvlblUnvlblCountArr);
			
			//pdfData
			pdfData.setData(aListPdfData);
			session.setAttribute("pdfData",pdfData);
		}
	}
	//New changes end
	rootJson.put("CSRF_NONCE",request.getParameter("com.eg.CSRF_NONCE"));
	if(queryString !=null)
	{
		session.setAttribute("configLastAccessedURL","/final/config/EcSoftwareAvailabilityIndex.jsp?"+queryString);
	}
	else
	{
		session.setAttribute("configLastAccessedURL","/final/config/EcSoftwareAvailabilityIndex.jsp?cmd=Availability&view=software&viewCount=All&softwareName=All&testName="+testName);
	}

	rootJson.put("hiddenFields",hiddenFields);
	//rootJson.put("resultantArray",resultantArray);
	out.println(rootJson.toString());

%>



