class CertificateRepository implements Constants {

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getCertificatesDetails(String assetGroup,
            String searchText, Map<String, String> filter) throws DataException {

        List<Map<String, Object>> certificateDetails = new ArrayList<>();
        try {
            Map<String, Object> mustFilter = new HashMap<>();
            mustFilter.put("latest", true);
            if (null != filter && filter.size() > 0) {
                filter.forEach((key, value) -> mustFilter.put(key, value));
            }

            StringBuilder urlToQueryBuffer = new StringBuilder(esUrl)
                    .append("/").append(assetGroup).append(CERT_SEARCH)
                    .append("?scroll=").append(esPageScrollTTL);

            String urlToQuery = urlToQueryBuffer.toString();
            String urlToScroll = new StringBuilder(esUrl).append("/")
                    .append("_search").append("/scroll").toString();

            StringBuilder requestBody = new StringBuilder(
                    "{\"size\":").append(TEN_THOUSAND).append(",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}");
            if (filter.containsKey("tags.Application.keyword")) {
                requestBody
                        .append(",{\"match\":{\"tags.Application.keyword\":\"");
                requestBody.append(filter.get("tags.Application.keyword"));
                requestBody.append("\"}}");
            }
            if (filter.containsKey("tags.Environment.keyword")) {
                requestBody
                        .append(",{\"match\":{\"tags.Environment.keyword\":\"");
                requestBody.append(filter.get("tags.Environment.keyword"));
                requestBody.append("\"}}");
            }
            if (filter.containsKey(EXP_IN)) {
                if (filter.get(EXP_IN).equals(THIRTY)) {
                    requestBody
                            .append(",{\"has_child\":{\"type\":\"issue_cert\",\"query\":{\"bool\":{\"must\":[{\"term\":{\"issueStatus.keyword\":\"open\"}},{\"term\":{\"type.keyword\":\"issue\"}},{\"match\":{\"ruleId.keyword\":\"");
                    requestBody.append(SSL_CERT_30_DAYS_EXP_RULE);
                    requestBody.append("\"}}]}}}}");
                } else if (filter.get(EXP_IN).equals(FOURTYFIVE)) {
                    requestBody
                            .append(",{\"has_child\":{\"type\":\"issue_cert\",\"query\":{\"bool\":{\"must\":[{\"term\":{\"issueStatus.keyword\":\"open\"}},{\"term\":{\"type.keyword\":\"issue\"}},{\"match\":{\"ruleId.keyword\":\"");
                    requestBody.append(SSL_CERT_45_DAYS_EXP_RULE);
                    requestBody.append("\"}}]}}}}");
                }
                mustFilter.remove(EXP_IN);
            }
            requestBody.append("]}}}");

            List<Map<String, Object>> certificatesDetailsFromES = new ArrayList<>();
            Long totalDocs = getCertificatesDetailsCount(assetGroup, mustFilter);
            String request = requestBody.toString();
            String scroolId = null;
            String responseDetails;
            for (int index = 0; index <= (totalDocs / esPageSize); index++) {
          try{
                    if (!Strings.isNullOrEmpty(scroolId)) {
                        request = elasticSearchRepository.buildScrollRequest(
                                scroolId, esPageScrollTTL);
                        urlToQuery = urlToScroll;
                    }
                    responseDetails = PacHttpUtils.doHttpPost(urlToQuery,
                            request);
                    scroolId = elasticSearchRepository
                            .processResponseAndSendTheScrollBack(
                                    responseDetails, certificatesDetailsFromES);
                } catch (Exception e) {
                    LOGGER.error("Error in getCerticatesDetails from ES", e);
                    throw new DataException(e);
                }

            }

            certificatesDetailsFromES = (List<Map<String, Object>>) CommonUtils
                    .filterMatchingCollectionElements(
                            certificatesDetailsFromES, searchText, true);
            certificatesDetailsFromES.sort(Comparator
                    .comparing(m -> LocalDateTime.parse(
                            (String) m.get(VALID_TO),
                            DateTimeFormatter.ofPattern("M/d/yyyy H:m"))
                            .toLocalDate()));

            for (Map<String, Object> _certificatesDetailsFromES : certificatesDetailsFromES) {
                Map<String, Object> certificate = new LinkedHashMap<>();
                certificate.put("name",
                        _certificatesDetailsFromES.get("commonname"));
                if (StringUtils.isNotEmpty(_certificatesDetailsFromES.get(
                        VALID_TO).toString())) {
                    certificate.put(EXP_IN,
                            calculateExpiryDuration(_certificatesDetailsFromES
                                    .get(VALID_TO).toString()) + " days");
                } else {
                    certificate.put(EXP_IN, 0);
                }
                certificate.put("type",
                        _certificatesDetailsFromES.get("certType"));
                certificate.put("application",
                        _certificatesDetailsFromES.get("tags.Application"));
                certificate.put("environment",
                        _certificatesDetailsFromES.get("tags.Environment"));
                certificate.put("owner",
                        _certificatesDetailsFromES.get("tags.Owner"));
                certificate.put("issuer",
                        _certificatesDetailsFromES.get("issuerdn"));
                certificate.put("validUntil",
                        _certificatesDetailsFromES.get(VALID_TO));
                certificate.put("validfrom",
                        _certificatesDetailsFromES.get("validfrom"));
                certificate.put("status",
                        _certificatesDetailsFromES.get("status"));
                certificateDetails.add(certificate);
            }
        } catch (DataException e) {
            LOGGER.error("Error in getCerticatesDetails", e);
        }
        return certificateDetails;
    }

}