<%@ include file= "../../EgTrackerAJSON.jsp"%>
<%@ page errorPage="../../EgProbs.jsp"%>
<%@ page import = "com.eg.EgCrypt" %>
<%@ page import = "com.eg.EgMemInfo" %>
<%@ page import = "com.eg.EgName"%>
<%@ page import = "com.eg.MappingNameSorter"%>
<%@ page import = "com.eg.EgInstallInfo"%>
<%@ page import="com.egurkha.ini.IniUI"%>
<%@ page import = "com.egurkha.util.StringUtils" %>
<%@ page import = "com.eg.UpdateAndTransmitMonitorInfo"%>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "com.egurkha.admin.EgAssetManagementModel"%>
<%@ page import = "java.util.ArrayList"%>
<%@ page import = "java.util.Hashtable"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
%>
<%
try
{
	String userLanguage = _user.getLanguage();
	MappingNameSorter sorter = new MappingNameSorter(userLanguage);
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile infraIni = new IniFile(s_iniFile_dir + "eg_infrastructure.ini");
	IniFile servicesIni = new IniFile(s_iniFile_dir + "eg_services.ini");

	EgAssetManagementModel egAssetMgmtModel = new EgAssetManagementModel();
	String comp_type = request.getParameter("serverType");
	comp_type = StringUtils.decodeSecurityChar(comp_type);
	String compNick = request.getParameter("serverName");
	compNick = StringUtils.decodeSecurityChar(compNick);
	String comp_name=compNick;
	if(compNick.indexOf(":NULL") > -1)
	comp_name=compNick.substring(0,compNick.indexOf(":NULL"));
	String asset_name = request.getParameter("AssetName");
	asset_name = StringUtils.decodeSecurityChar(asset_name);
	String asset_type = request.getParameter("AssetType");
	asset_type = StringUtils.decodeSecurityChar(asset_type);
	String asset_location = request.getParameter("AssetLocation");
	asset_location = StringUtils.decodeSecurityChar(asset_location);
	String asset_macAddr = request.getParameter("MacAddr");
	asset_macAddr = StringUtils.decodeSecurityChar(asset_macAddr);
	String asset_manufacturer = request.getParameter("Manufacturer");
	asset_manufacturer = StringUtils.decodeSecurityChar(asset_manufacturer);
	String asset_suppotGroup = request.getParameter("SupprtGroups");
	asset_suppotGroup = StringUtils.decodeSecurityChar(asset_suppotGroup);
	String asset_BusinessImpct = request.getParameter("BusinessImpact");
	asset_BusinessImpct = StringUtils.decodeSecurityChar(asset_BusinessImpct);
	String asset_description = request.getParameter("AssetDescription");
	asset_description = StringUtils.decodeSecurityChar(asset_description);
	String asset_ipAddess = request.getParameter("IpAddr");
	asset_ipAddess = StringUtils.decodeSecurityChar(asset_ipAddess);
	String asset_state = request.getParameter("AssetState");
	asset_state = StringUtils.decodeSecurityChar(asset_state);
	String asset_OS = request.getParameter("OperatingSys");
	asset_OS = StringUtils.decodeSecurityChar(asset_OS);
	String asset_owner = request.getParameter("OwnerForAsset");
	asset_owner = StringUtils.decodeSecurityChar(asset_owner);
	String asset_businessService = request.getParameter("BusinessServc");
	asset_businessService = StringUtils.decodeSecurityChar(asset_businessService);
	String asset_phNo = request.getParameter("AssetPhNo");
	asset_phNo = StringUtils.decodeSecurityChar(asset_phNo);
	String asset_emailAddr = request.getParameter("AssetEmailAddr");
	asset_emailAddr = StringUtils.decodeSecurityChar(asset_emailAddr);
	String asset_licnsExpDate = request.getParameter("LiceDate");
	asset_licnsExpDate = StringUtils.decodeSecurityChar(asset_licnsExpDate);
	String asset_ASMExpDate = request.getParameter("ASMDate");
	asset_ASMExpDate = StringUtils.decodeSecurityChar(asset_ASMExpDate);
	String asset_serialNum = request.getParameter("AssetSerialNum");
	asset_serialNum = StringUtils.decodeSecurityChar(asset_serialNum);
	String asset_licnsSerialNum = request.getParameter("AssetLicenseSerialNum");
	asset_licnsSerialNum = StringUtils.decodeSecurityChar(asset_licnsSerialNum);
	String osVersion = request.getParameter("osVersion");
	osVersion = StringUtils.decodeSecurityChar(osVersion);
	String memory = request.getParameter("memory");
	memory = StringUtils.decodeSecurityChar(memory);
	String diskSpace = request.getParameter("diskSpace");
	diskSpace = StringUtils.decodeSecurityChar(diskSpace);
	String cpuSpeed = request.getParameter("cpuSpeed");
	cpuSpeed = StringUtils.decodeSecurityChar(cpuSpeed);
	String cpuCoreCount = request.getParameter("cpuCoreCount");
	cpuCoreCount = StringUtils.decodeSecurityChar(cpuCoreCount);
	String uuid = request.getParameter("uuid");
	uuid = StringUtils.decodeSecurityChar(uuid);
	String osServicePack = request.getParameter("osServicePack");
	osServicePack = StringUtils.decodeSecurityChar(osServicePack);
	String product = request.getParameter("product");
	product = StringUtils.decodeSecurityChar(product);
	String vendor = request.getParameter("vendor");
	vendor = StringUtils.decodeSecurityChar(vendor);
	String cost = request.getParameter("cost");
	cost = StringUtils.decodeSecurityChar(cost);
	String licenseKey = request.getParameter("licenseKey");
	licenseKey = StringUtils.decodeSecurityChar(licenseKey);
	String installationDate = request.getParameter("installationDate");
	installationDate = StringUtils.decodeSecurityChar(installationDate);
	String licenseType = request.getParameter("licenseType");
	licenseType = StringUtils.decodeSecurityChar(licenseType);
	String version = request.getParameter("version");
	version = StringUtils.decodeSecurityChar(version);
	String domain = request.getParameter("domain");
	domain = StringUtils.decodeSecurityChar(domain);
	String installedMachine = request.getParameter("installedMachine");
	installedMachine = StringUtils.decodeSecurityChar(installedMachine);
	String installationPath = request.getParameter("installationPath");
	installationPath = StringUtils.decodeSecurityChar(installationPath);
	String warranty = request.getParameter("warranty");
	warranty = StringUtils.decodeSecurityChar(warranty);
	String acquisitionDate = request.getParameter("acquisitionDate");
	acquisitionDate = StringUtils.decodeSecurityChar(acquisitionDate);
	String bandwidth = request.getParameter("bandwidth");
	bandwidth = StringUtils.decodeSecurityChar(bandwidth);
	String networkRange = request.getParameter("networkRange");
	networkRange = StringUtils.decodeSecurityChar(networkRange);
	String batteryStatus = request.getParameter("batteryStatus");
	batteryStatus = StringUtils.decodeSecurityChar(batteryStatus);
	String batteryVoltage = request.getParameter("batteryVoltage");
	batteryVoltage = StringUtils.decodeSecurityChar(batteryVoltage);
	String batteryCurrent = request.getParameter("batteryCurrent");
	batteryCurrent = StringUtils.decodeSecurityChar(batteryCurrent);
	String firmware = request.getParameter("firmware");
	firmware = StringUtils.decodeSecurityChar(firmware);
	String firmwareVersion = request.getParameter("firmwareVersion");
	firmwareVersion = StringUtils.decodeSecurityChar(firmwareVersion);
	String subnetMask = request.getParameter("subnetMask");
	subnetMask = StringUtils.decodeSecurityChar(subnetMask);
	String lastAuditDate = request.getParameter("lastAuditDate");
	lastAuditDate = StringUtils.decodeSecurityChar(lastAuditDate);
	String account = request.getParameter("account");
	account = StringUtils.decodeSecurityChar(account);
	String region = request.getParameter("region");
	region = StringUtils.decodeSecurityChar(region);
	String availabilityZone = request.getParameter("availabilityZone");
	availabilityZone = StringUtils.decodeSecurityChar(availabilityZone);
	String isApplyForAllServer = request.getParameter("applyAll");
	isApplyForAllServer = StringUtils.decodeSecurityChar(isApplyForAllServer);
	String customColNameAndVaues = request.getParameter("customColVaues");
	customColNameAndVaues = StringUtils.decodeSecurityChar(customColNameAndVaues);
	String[] applyForAllServerType = request.getParameterValues("applyForAllServerType");
	ArrayList defalutField_forAsset = new ArrayList();
	ArrayList trgtHostList = new ArrayList();
	Hashtable customFields = new Hashtable();
	Hashtable rsTable = new Hashtable();
	String update_msg=null;

	Boolean isSaved=new Boolean(false);
	if (StringUtils.isNull(comp_type))
		comp_type = "";
	if (StringUtils.isNull(comp_name))
		comp_name = "";
	if (StringUtils.isNull(asset_name))
		asset_name = "NONE";
	if (StringUtils.isNull(asset_type))
		asset_type = "NONE";
	if (StringUtils.isNull(asset_location))
		asset_location = "NONE";
	if (StringUtils.isNull(asset_macAddr))
		asset_macAddr = "NONE";
	if (StringUtils.isNull(asset_manufacturer))
		asset_manufacturer = "NONE";
	if (StringUtils.isNull(asset_suppotGroup))
		asset_suppotGroup = "NONE";
	if (StringUtils.isNull(asset_BusinessImpct))
		asset_BusinessImpct = "NONE";
	if (StringUtils.isNull(asset_description))
		asset_description = "NONE";
	if (StringUtils.isNull(asset_ipAddess))
		asset_ipAddess = "NONE";
	if (StringUtils.isNull(asset_state))
		asset_state = "NONE";
	if (StringUtils.isNull(asset_OS))
		asset_OS = "NONE";
	if (StringUtils.isNull(asset_owner))
		asset_owner = "NONE";
	if (StringUtils.isNull(asset_businessService))
		asset_businessService = "NONE";
	if (StringUtils.isNull(asset_phNo))
		asset_phNo = "NONE";
	if (StringUtils.isNull(asset_emailAddr))
		asset_emailAddr = "NONE";
	if (StringUtils.isNull(asset_licnsExpDate))
		asset_licnsExpDate = "NONE";
	if (StringUtils.isNull(asset_ASMExpDate))
		asset_ASMExpDate = "NONE";
	if (StringUtils.isNull(asset_serialNum))
		asset_serialNum = "NONE";
	if (StringUtils.isNull(asset_licnsSerialNum))
		asset_licnsSerialNum = "NONE";

	if (StringUtils.isNull(osVersion))
		osVersion = "NONE";
	if (StringUtils.isNull(memory))
		memory = "NONE";
	if (StringUtils.isNull(diskSpace))
		diskSpace = "NONE";
	if (StringUtils.isNull(cpuSpeed))
		cpuSpeed = "NONE";
	if (StringUtils.isNull(cpuCoreCount))
		cpuCoreCount = "NONE";
	if (StringUtils.isNull(uuid))
		uuid = "NONE";
	if (StringUtils.isNull(osServicePack))
		osServicePack = "NONE";
	if (StringUtils.isNull(product))
		product = "NONE";
	if (StringUtils.isNull(vendor))
		vendor = "NONE";
	if (StringUtils.isNull(cost))
		cost = "NONE";
	if (StringUtils.isNull(licenseKey))
		licenseKey = "NONE";
	if (StringUtils.isNull(installationDate))
		installationDate = "NONE";
	if (StringUtils.isNull(licenseType))
		licenseType = "NONE";
	if (StringUtils.isNull(version))
		version = "NONE";
	if (StringUtils.isNull(domain))
		domain = "NONE";
	if (StringUtils.isNull(installedMachine))
		installedMachine = "NONE";
	if (StringUtils.isNull(installationPath))
		installationPath = "NONE";
	if (StringUtils.isNull(warranty))
		warranty = "NONE";
	if (StringUtils.isNull(acquisitionDate))
		acquisitionDate = "NONE";
	if (StringUtils.isNull(bandwidth))
		bandwidth = "NONE";
	if (StringUtils.isNull(networkRange))
		networkRange = "NONE";
	if (StringUtils.isNull(batteryStatus))
		batteryStatus = "NONE";
	if (StringUtils.isNull(batteryVoltage))
		batteryVoltage = "NONE";
	if (StringUtils.isNull(batteryCurrent))
		batteryCurrent = "NONE";
	if (StringUtils.isNull(firmware))
		firmware = "NONE";
	if (StringUtils.isNull(firmwareVersion))
		firmwareVersion = "NONE";
	if (StringUtils.isNull(subnetMask))
		subnetMask = "NONE";
	if (StringUtils.isNull(lastAuditDate))
		lastAuditDate = "NONE";
	if (StringUtils.isNull(account))
		account = "NONE";
	if (StringUtils.isNull(region))
		region = "NONE";
	if (StringUtils.isNull(availabilityZone))
		availabilityZone = "NONE";

	if ((!StringUtils.isNull(isApplyForAllServer) && isApplyForAllServer.equalsIgnoreCase("on")) && (applyForAllServerType != null && applyForAllServerType.length > 0))
	{
		//trgtHostList = new ArrayList(Arrays.asList(applyForAllServerType));
		String applyServerTypeStr = (String)applyForAllServerType[0];
		StringTokenizer stk = new StringTokenizer(applyServerTypeStr,",");
	
		while(stk.hasMoreTokens())
		{
			String trgHostStr = stk.nextToken();
			String serverTypeStr = trgHostStr.substring(0,trgHostStr.indexOf(":"));
			ArrayList serverTypeList = new ArrayList();
			serverTypeList.add(serverTypeStr);
			serverTypeList = sorter.getSortedCompTypeList(serverTypeList);
			EgName nameObj = (EgName)serverTypeList.get(0);
			String actualNameForCompType = nameObj.getActualName();
			String displyNameForCompType = nameObj.getLocalName();
			
			String[] infra_Subjects = infraIni.getSubjects();
			String server_Category="";
			for (int a = 0;a<infra_Subjects.length;a++ )
			{
				server_Category = infra_Subjects[a];
				String[] all_Variables = infraIni.getVariables(server_Category);
				if(all_Variables==null || all_Variables.length==0)
					continue;
				boolean has_Var = false;
				for(int b=0;b<all_Variables.length;b++)
				{
					String variables = all_Variables[b];
					if(variables.equalsIgnoreCase(actualNameForCompType)){
						has_Var = true;
						break;
					}
				}
				//if(!hasVar) continue;
				if(has_Var) break;
			}
			String[] asset_TypeList = servicesIni.getVariables("ASSET_TYPE_CATEGORY MAPPING");
			String selected_AssetType = null;
			for(int ij=0;ij<asset_TypeList.length; ij++)
			{
				boolean type_Found=false;
				String as_type =asset_TypeList[ij];
				String var=servicesIni.getValue("ASSET_TYPE_CATEGORY MAPPING",as_type);
				String category_Arr[]=var.split(",");
				for(int iii=0; iii<category_Arr.length;iii++)
				{
					String arr_Element =  category_Arr[iii];
					if(arr_Element.equalsIgnoreCase(server_Category)){
						selected_AssetType = as_type;
						type_Found=true;
						break;
					}
				}	
				if(type_Found) break;
			}
			
			if(selected_AssetType == null)
				selected_AssetType = "Application";

			if(trgHostStr.indexOf(":NULL") > -1)
			{
				trgHostStr=trgHostStr.substring(0,trgHostStr.indexOf(":NULL"));
			}
			trgtHostList.add(trgHostStr+"!^!"+selected_AssetType);
		}
	}
	if (!StringUtils.isNull(comp_type) && !StringUtils.isNull(comp_name))
		trgtHostList.add(comp_type+":"+comp_name);
	defalutField_forAsset.add(comp_type);
	defalutField_forAsset.add(comp_name);
	defalutField_forAsset.add(asset_name);
	defalutField_forAsset.add(asset_type);
	defalutField_forAsset.add(asset_location);
	defalutField_forAsset.add(asset_macAddr);
	defalutField_forAsset.add(asset_manufacturer);
	defalutField_forAsset.add(asset_suppotGroup);
	defalutField_forAsset.add(asset_BusinessImpct);
	defalutField_forAsset.add(asset_description);
	defalutField_forAsset.add(asset_ipAddess);
	defalutField_forAsset.add(asset_state);
	defalutField_forAsset.add(asset_OS);
	defalutField_forAsset.add(asset_owner);
	defalutField_forAsset.add(asset_businessService);
	defalutField_forAsset.add(asset_phNo);
	defalutField_forAsset.add(asset_emailAddr);
	defalutField_forAsset.add(asset_licnsExpDate);
	defalutField_forAsset.add(asset_ASMExpDate);
	defalutField_forAsset.add(asset_serialNum);
	defalutField_forAsset.add(asset_licnsSerialNum);

	defalutField_forAsset.add(osVersion);
	defalutField_forAsset.add(memory);
	defalutField_forAsset.add(diskSpace);
	defalutField_forAsset.add(cpuSpeed);
	defalutField_forAsset.add(cpuCoreCount);
	defalutField_forAsset.add(uuid);
	defalutField_forAsset.add(osServicePack);
	defalutField_forAsset.add(product);
	defalutField_forAsset.add(vendor);
	defalutField_forAsset.add(cost);
	defalutField_forAsset.add(licenseKey);
	defalutField_forAsset.add(installationDate);
	defalutField_forAsset.add(licenseType);
	defalutField_forAsset.add(version);
	defalutField_forAsset.add(domain);
	defalutField_forAsset.add(installedMachine);
	defalutField_forAsset.add(installationPath);
	defalutField_forAsset.add(warranty);
	defalutField_forAsset.add(acquisitionDate);
	defalutField_forAsset.add(bandwidth);
	defalutField_forAsset.add(networkRange);
	defalutField_forAsset.add(batteryStatus);
	defalutField_forAsset.add(batteryVoltage);
	defalutField_forAsset.add(batteryCurrent);
	defalutField_forAsset.add(firmware);
	defalutField_forAsset.add(firmwareVersion);
	defalutField_forAsset.add(subnetMask);
	defalutField_forAsset.add(lastAuditDate);
	defalutField_forAsset.add(account);
	defalutField_forAsset.add(region);
	defalutField_forAsset.add(availabilityZone);

	if (customColNameAndVaues != null && customColNameAndVaues.length() > 0 && customColNameAndVaues.indexOf("#~#") > -1)
	{
		ArrayList colNamesAndValList = StringUtils.myTokenizer(customColNameAndVaues,"#~#");
		if ( colNamesAndValList != null && !colNamesAndValList.isEmpty() )
		{
			for ( int i = 0 ; i < colNamesAndValList.size() ; i++ )
			{
				String customNameAndVal = (String)colNamesAndValList.get(i);
				if ( customNameAndVal != null && customNameAndVal.indexOf("~@~") > -1 )
				{
					String customColName = customNameAndVal.substring(0,customNameAndVal.indexOf("~@~"));
					String customColValue = customNameAndVal.substring(customNameAndVal.indexOf("~@~")+3);
					if ( !StringUtils.isNull(customColValue))
					{
						customColName = customColName.toLowerCase();
						customColName=customColName.replace(" ","_");
						customFields.put(customColName,customColValue);

					}
				}
				else
					continue;

			}
		}
	}
	else if ( customColNameAndVaues != null && customColNameAndVaues.indexOf("~@~") > -1 )
	{
		String customColName = customColNameAndVaues.substring(0,customColNameAndVaues.indexOf("~@~"));
		String customColValue = customColNameAndVaues.substring(customColNameAndVaues.indexOf("~@~")+3);
		if ( !StringUtils.isNull(customColValue))
		{
			customColName = customColName.toLowerCase();
			customColName=customColName.replace(" ","_");
			customFields.put(customColName,customColValue);
			
		}
	}
	/************** Cluster Support STARTS ***************/

	if ( EgInstallInfo.isClusterSupported )
	{
		UpdateAndTransmitMonitorInfo updateInfo = new UpdateAndTransmitMonitorInfo();
		updateInfo.updateAssetConfiguration(defalutField_forAsset,trgtHostList,customFields,"updateassetconfiguration");
		rsTable=egAssetMgmtModel.updateAssetConfiguration(defalutField_forAsset,trgtHostList,customFields);

	}
	else
	{
		rsTable=egAssetMgmtModel.updateAssetConfiguration(defalutField_forAsset,trgtHostList,customFields);
	}

	/************** Cluster Support ENDS ***************/

	Vector sess_vec = new Vector();
	sess_vec.addElement(_mapping.getTextDescription("admin.mail.Define Asset settings"));
	sess_vec.addElement("../../../admin/EgAssetInfo.jsp?serverType="+comp_type+"&serverName="+compNick);
	sess_vec.addElement(_mapping.getTextDescription("admin.mail.Define Asset setting")+".");
	
	// Asset configuration saved or modified starts
	if(rsTable!=null&& rsTable.size() > 0)
	{
		Boolean isAssetInfoUpdated=(Boolean)rsTable.get("isUpdated");
		isSaved=new Boolean(((Boolean)rsTable.get("isSaved")));
		if(isAssetInfoUpdated)
		{
			update_msg=_mapping.getTextDescription("admin.mail.Asset information has been modified successfully");
		}
		else
		{
			update_msg=_mapping.getTextDescription("admin.mail.Asset information has been defined successfully");
		}
	}
	//End
	request.setAttribute("update_msg",update_msg);
	request.setAttribute("vec_Result",sess_vec);
	request.setAttribute("DB_Result",isSaved);
	session.setAttribute("vec_Results",sess_vec);
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}


%>
<!--Forwarding the page EgAssetInfo.jsp after the asset configuration saved or modified-->
<jsp:forward page="../../../admin/EgAssetInfo.jsp" />