private void generateCSVFileFromSearchResult()

in repository/src/main/java/org/apache/atlas/repository/store/graph/v2/tasks/searchdownload/SearchResultDownloadTask.java [159:266]


    private void generateCSVFileFromSearchResult(AtlasSearchResult searchResult, Map<String, String> attributeLabelMap, AtlasSearchResult.AtlasQueryType queryType) throws IOException {
        List<AtlasEntityHeader>                 allEntityHeaders      = searchResult.getEntities();
        AtlasSearchResult.AttributeSearchResult attributeSearchResult = searchResult.getAttributes();
        String                                  fileName              = (String) getTaskDef().getParameters().get(CSV_FILE_NAME_KEY);

        if ((queryType == BASIC && CollectionUtils.isEmpty(allEntityHeaders)) || (queryType == DSL && (CollectionUtils.isEmpty(allEntityHeaders) && attributeSearchResult == null))) {
            LOG.info("No result found. Not generating csv file: {}", fileName);

            return;
        }

        File dir = new File(DOWNLOAD_DIR_PATH, RequestContext.getCurrentUser());

        if (!dir.exists()) {
            dir.mkdirs();
        }

        File csvFile = new File(dir, fileName);

        try (FileWriter fileWriter = new FileWriter(csvFile); CSVWriter csvWriter = new CSVWriter(fileWriter)) {
            String[] defaultHeaders = new String[] {"Type name", "Name", "Classifications", "Terms"};
            String[] attributeHeaders;
            int      attrSize;

            if (attributeLabelMap == null) {
                attributeLabelMap = new HashMap<>();
            }

            attributeLabelMap.put("Owner", "owner");
            attributeLabelMap.put("Description", "description");

            Collection<String> attributeHeaderLabels = attributeLabelMap.keySet();

            if (queryType == DSL && (CollectionUtils.isEmpty(allEntityHeaders) && attributeSearchResult != null)) {
                attributeHeaderLabels = attributeSearchResult.getName();
                defaultHeaders        = new String[0];
            }

            attrSize         = (attributeHeaderLabels == null) ? 0 : attributeHeaderLabels.size();
            attributeHeaders = new String[attrSize];

            if (attributeHeaderLabels != null) {
                attributeHeaders = attributeHeaderLabels.toArray(attributeHeaders);
            }

            int      headerSize = attrSize + defaultHeaders.length;
            String[] headers    = new String[headerSize];

            System.arraycopy(defaultHeaders, 0, headers, 0, defaultHeaders.length);

            if (ArrayUtils.isNotEmpty(attributeHeaders)) {
                System.arraycopy(attributeHeaders, 0, headers, defaultHeaders.length, attrSize);
            }

            csvWriter.writeNext(headers);

            String[] entityRecords = new String[headerSize];

            if (CollectionUtils.isNotEmpty(allEntityHeaders)) {
                for (AtlasEntityHeader entityHeader : allEntityHeaders) {
                    entityRecords[0] = entityHeader.getTypeName();
                    entityRecords[1] = entityHeader.getDisplayText() != null ? entityHeader.getDisplayText() : entityHeader.getGuid();
                    entityRecords[2] = String.join(",", entityHeader.getClassificationNames());
                    entityRecords[3] = String.join(",", entityHeader.getMeaningNames());

                    if (MapUtils.isNotEmpty(entityHeader.getAttributes())) {
                        for (int i = defaultHeaders.length; i < headerSize; i++) {
                            Object attrValue = entityHeader.getAttribute(attributeLabelMap.get(headers[i]));

                            if (attrValue instanceof AtlasObjectId) {
                                entityRecords[i] = String.valueOf(((AtlasObjectId) attrValue).getUniqueAttributes().get("qualifiedName"));
                            } else if (attrValue instanceof List) {
                                if (CollectionUtils.isNotEmpty((List<?>) attrValue)) {
                                    List<String> valueList = new ArrayList<>();

                                    for (Object attrVal : (List<?>) attrValue) {
                                        if (attrVal instanceof AtlasObjectId) {
                                            String value = String.valueOf(((AtlasObjectId) attrVal).getUniqueAttributes().get("qualifiedName"));

                                            valueList.add(value);
                                        } else {
                                            valueList.add(String.valueOf(attrVal));
                                        }
                                    }

                                    entityRecords[i] = String.join(",", valueList);
                                }
                            } else {
                                entityRecords[i] = attrValue == null ? EMPTY_STRING : String.valueOf(attrValue);
                            }
                        }
                    }

                    csvWriter.writeNext(entityRecords);
                }
            }

            if (queryType == DSL && attributeSearchResult != null) {
                for (List<Object> resultSet : attributeSearchResult.getValues()) {
                    for (int i = defaultHeaders.length; i < headerSize; i++) {
                        entityRecords[i] = resultSet.get(i) == null ? EMPTY_STRING : String.valueOf(resultSet.get(i));
                    }

                    csvWriter.writeNext(entityRecords);
                }
            }
        }
    }