Skip to content

Commit

Permalink
Merge pull request #18 from WietseV/issues/#15-empty-csv-export
Browse files Browse the repository at this point in the history
Issue#15 Exporting a csv sometimes gives completely empty file
  • Loading branch information
bdhoine authored Feb 8, 2024
2 parents d65da4c + 734be24 commit 756dbaa
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,40 @@ private void writeCsvHeader(PrintWriter writer, RequestParameter delimiter, List
}

private void writeCsvRows(PrintWriter writer, RequestParameter delimiter, List<Resource> languageResources) {
Resource firstLanguageResource = languageResources.get(0);

Iterator<Resource> labelChildren = firstLanguageResource.listChildren();
while (labelChildren.hasNext()) {
Resource labelResource = labelChildren.next();
StringBuilder csvRow = buildCsvRow(labelResource, delimiter, languageResources);
writer.println(csvRow);
LOG.debug("CSV row: " + csvRow);
List<String> labels = new ArrayList<>();
Resource currentLanguageResource = null;
if (!languageResources.isEmpty()) {
for (int i = 0; i < languageResources.size(); i++) {
currentLanguageResource = languageResources.get(i);
Iterator<Resource> labelChildren = currentLanguageResource.listChildren();
while (labelChildren.hasNext()) {
Resource labelResource = labelChildren.next();
if (!labels.contains(labelResource.getName())){
StringBuilder csvRow = buildCsvRow(labelResource, delimiter, languageResources);
writer.println(csvRow);
labels.add(labelResource.getName());
LOG.debug("CSV row: " + csvRow);
}
}
}
}
}

private StringBuilder buildCsvRow(Resource labelResource, RequestParameter delimiter, List<Resource> languageResources) {
StringBuilder csvRow = new StringBuilder(labelResource.getValueMap().get(SLING_KEY, String.class));
StringBuilder csvRow;
if (labelResource.getValueMap().containsKey(SLING_KEY)) {
csvRow = new StringBuilder(labelResource.getValueMap().get(SLING_KEY, String.class));
} else {
csvRow = new StringBuilder(labelResource.getName());
}

for (Resource languageResource : languageResources) {
Resource correspondingLabelResource = languageResource.getChild(labelResource.getName());
csvRow.append(delimiter);
String translation = correspondingLabelResource.getValueMap().get(SLING_MESSAGE, String.class);
String translation = " ";
if (correspondingLabelResource != null) {
translation = correspondingLabelResource.getValueMap().get(SLING_MESSAGE, String.class);
}
if (translation == null) {
translation = " "; // This should be a space because appending an empty string will delete the whole string
}
Expand Down

0 comments on commit 756dbaa

Please sign in to comment.