class VulnerabilityService implements Constants {

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getAllVulnerabilitiesDetailsByAssetGroup(String assetGroup,
			Map<String, String> filter,String applicationFilter,String environmentFilter, String searchText, int from, int size) throws Exception {
		List<String> vulnTargetTypes = getVulnTargetTypes(assetGroup);
		boolean flag = false;
		if (vulnTargetTypes.size() > 1) {
			flag = true;
		}
		List<Map<String, Object>> vulnerabilitiesDetails = new ArrayList<>();
		List<Map<String, Object>> vulnerabilitiesDetailsFinal = new ArrayList<>();
	/*	Map<String, Object> mustFilter = new HashMap<>();
		Map<String, Object> mustTermsFilter = new HashMap<>();
		mustFilter.put("latest", "true");
		for (Map.Entry<String, String> entry : filter.entrySet()) {
			List<String> severities = Arrays.asList(entry.getValue().split(","));
			mustTermsFilter.put(entry.getKey(), severities);
		}
		String targetType = "vulninfo";
		List<String> occurrenceFieldList = Arrays.asList(vulnOccurrenceFields.split(","));*/
		List<String> occurrenceFieldList = Arrays.asList(vulnOccurrenceFields.split(","));
		for (String parentType : vulnTargetTypes) {
		String targetType = "vulninfo";
		Map<String, Object> mustFilterMap = new LinkedHashMap<>();
		mustFilterMap.put("latest", "true");
		Map<String, Object> parentBool = new HashMap<>();
		List<Map<String, Object>> mustList = new ArrayList<>();
		Map<String, Object> matchMap = new HashMap<>();
		Map<String, String> match = new HashMap<>();
		Map<String, Object> mustTermsFilter = new HashMap<>();
		for (Map.Entry<String, String> entry : filter.entrySet()) {
			List<String> severities = Arrays.asList(entry.getValue().split(","));
			mustTermsFilter.put(entry.getKey(), severities);
		}
		// mustFilterMap.putAll(mustTermsFilter);
		match.put(Constants.LATEST, Constants.TRUE);
		matchMap.put(Constants.MATCH, match);
		mustList.add(matchMap);

		if (applicationFilter != null) {
			Map<String, String> applicationFilterMap = new HashMap<String, String>();
			Map<String, Object> applicationFilterMap1 = new HashMap<String, Object>();
			applicationFilterMap.put("tags.Application.keyword", applicationFilter);
			applicationFilterMap1.put("match", applicationFilterMap);
			mustList.add(applicationFilterMap1);
		}

		if (environmentFilter != null) {
			Map<String, String> environmentFilterMap = new HashMap<String, String>();
			Map<String, Object> environmentFilterMap1 = new HashMap<String, Object>();
			environmentFilterMap.put("tags.Environment.keyword", environmentFilter);
			environmentFilterMap1.put("match", environmentFilterMap);
			mustList.add(environmentFilterMap1);
		}

		parentBool.put("must", mustList);
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("bool", parentBool);
		Map<String, Object> parentEntryMap = new LinkedHashMap<>();
		parentEntryMap.put("parent_type", parentType);
		parentEntryMap.put("query", queryMap);
		mustFilterMap.put("has_parent", parentEntryMap);
		
		try {

			vulnerabilitiesDetails = vulnerabilityRepository.getAllVulnerabilitiesByAssetGroup(assetGroup, targetType, mustFilterMap,occurrenceFieldList, from, size,
					mustTermsFilter);
			vulnerabilitiesDetailsFinal.addAll(vulnerabilitiesDetails);
		} catch (Exception e) {
			logger.error("Error in getAllVulnerabilitiesDetailsByAssetGroup ", e);
			throw e;
		}
		}
		List<String> listDistinctResourceId = getResourceId(vulnerabilitiesDetailsFinal);
		List<Map<String, Object>> resourceIdDetails = getResourcedetailsByResourceId(assetGroup, listDistinctResourceId,
				flag);

		List<Map<String, Object>> cartesianResultList = getCartesianProduct(vulnerabilitiesDetailsFinal, resourceIdDetails);
		cartesianResultList = (List<Map<String, Object>>) filterMatchingCollectionElements(cartesianResultList,
				searchText, true);
		for (Map<String, Object> map : cartesianResultList) {
			List<String> underscoreKeys = new ArrayList<String>();
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if (entry.getKey().startsWith("_")) {
					underscoreKeys.add(entry.getKey());
				}
			}
			underscoreKeys.forEach(underscoreElement -> {
				String underscoreRemovedKey = underscoreElement.substring(1, underscoreElement.length());
				map.put(underscoreRemovedKey, map.get(underscoreElement));
				map.remove(underscoreElement);
			});
		}

		return cartesianResultList;
	}

}