class TaggingServiceImpl implements TaggingService, Constants {

    @Override
    public ResponseWithCount getUnTaggedAssetsByApplication(Request request) throws ServiceException {
        String assetGroup = request.getAg();
        String searchText = request.getSearchtext();
        int from = request.getFrom();
        int size = request.getSize();

        List<Map<String, Object>> masterDetailList = new ArrayList<>();
        LinkedHashMap<String, Object> app;
        JsonArray buckets;
        try {
            buckets = repository.getUntaggedIssuesByapplicationFromES(assetGroup, mandatoryTags, searchText, from,
                    size);
        } catch (DataException e) {
            throw new ServiceException(e);
        }
        JsonObject tagsObj = null;
        JsonObject bucketsTags = null;
        JsonObject tagsJson = null;

        JsonElement tagsDocId = null;
        String tags = null;
        String tagName = null;
        int total = 0;
        for (int i = 0; i < buckets.size(); i++) {
            total = buckets.size();
            app = new LinkedHashMap<>();
            app.put("application", buckets.get(i).getAsJsonObject().get("key").getAsString());
            tagsObj = (JsonObject) buckets.get(i).getAsJsonObject().get("tags");
            bucketsTags = (JsonObject) tagsObj.get("buckets");
            Iterator<String> it = bucketsTags.keySet().iterator();

            while (it.hasNext()) {
                tags = it.next();
                tagsJson = (JsonObject) bucketsTags.get(tags);
                tagsDocId = tagsJson.get("doc_count");
                StringBuilder sb = new StringBuilder(tags);
                sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
                tagName = sb.toString();
                app.put(tagName + "Untagged", tagsDocId.getAsLong());
            }
            masterDetailList.add(app);
        }

        if (masterDetailList.isEmpty()) {
            throw new ServiceException(NO_DATA_FOUND);
        }

        if (from >= masterDetailList.size()) {
            throw new ServiceException("From exceeds the size of list");
        }

        int endIndex = 0;

        if ((from + size) > masterDetailList.size()) {
            endIndex = masterDetailList.size();
        } else {
            endIndex = from + size;
        }
        List<Map<String, Object>> subDetailList;

        if (from == 0 && size == 0) {
            subDetailList = masterDetailList;
        } else {
            subDetailList = masterDetailList.subList(from, endIndex);
        }
        if (buckets.size() > 0) {
            return new ResponseWithCount(subDetailList, total);
        } else {
            throw new ServiceException("No records found!!");
        }
    }

}