Skip to content

Instantly share code, notes, and snippets.

@NahianAhmed
Last active July 21, 2022 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NahianAhmed/b1da4441155f38ebd50a155a82fb50ba to your computer and use it in GitHub Desktop.
Save NahianAhmed/b1da4441155f38ebd50a155a82fb50ba to your computer and use it in GitHub Desktop.
Export CSV or TSV files as Zip using Java
@Log4j2
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TagDataExporter {
@Nonnull private final DataExportImportSupport dataExportImportSupport;
@Nonnull private final AccountFileSupport accountFileSupport;
@Nonnull private final IdeaService ideaService;
private static final String TSV_DELIMITER = "\t";
private static final String FILE_NAME_INFIX = "tags";
private static final int MAX_PAGE_SIZE = 500;
private final String[] EXPORT_COLUMNS = new String[]{
"community_id",
"idea_id",
"tag",
};
public File export(Community community) throws Exception {
File fullyQualifiedFile = new File(accountFileSupport.getAccountRootFolder(community.getAccountId()), getFilename(community));
try (PrintStream tsv = dataExportImportSupport.createNlpDataExportStream(fullyQualifiedFile, EXPORT_COLUMNS)) {
Pageable pageable = PageRequest.of(0, MAX_PAGE_SIZE);
Page<Long> ideaIds;
do {
ideaIds = ideaService.findIdeaIds(community, pageable);
ideaIds.getContent().forEach(ideaId -> exportIdeaTags(community, ideaId, tsv));
pageable = ideaIds.nextPageable();
} while (ideaIds.hasNext());
}
return fullyQualifiedFile;
}
private void exportIdeaTags(Community community, Long ideaId, PrintStream tsv) {
ideaService.findIdeaTags(community, ideaId).forEach(ideaTag -> {
try {
StringJoiner row = new StringJoiner(TSV_DELIMITER)
.add(String.valueOf(ideaTag.getCommunityId()))
.add(String.valueOf(ideaTag.getIdeaId()))
.add(ideaTag.getTag());
tsv.println(row);
} catch (Exception exception) {
log.warn("Failed to export Idea tags of Id :: {} for community :: {}", ideaTag.getIdeaId(), community.getShortUrl());
}
});
}
private String getFilename(Community community) {
return community.getId()
+ "-" + FILE_NAME_INFIX
+ ".tsv";
}
}
@Component
@Log4j2
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class NlpDataExporter {
@Nonnull private final AccountFileSupport accountFileSupport;
@Nonnull private final TagDataExporter tagDataExporter;
public File export(Community community) throws Exception {
List<File> exportedFiles = new ArrayList<>();
exportedFiles.add(tagDataExporter.export(community));
// add more here
return createZipFile(community, exportedFiles);
}
private File createZipFile(Community community, List<File> exportedFiles) throws IOException {
File exportFolder = new File(accountFileSupport.getAccountRootFolder(community.getAccountId()), community.getDataSourceKey().getId()
+ DASH + community.getId() + DASH + community.getName());
exportedFiles.forEach(file -> {
try {
FileUtils.moveFileToDirectory(file, exportFolder, true);
} catch (IOException e) {
log.warn("Failed to move file into zip folder");
}
});
File zipFile = new File(accountFileSupport.getAccountRootFolder(community.getAccountId()), exportFolder.getName() + ".zip");
ZipUtils.zipFolder(exportFolder.toPath(), zipFile.toPath());
FileUtils.deleteDirectory(exportFolder);
return zipFile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment