Skip to content

Commit

Permalink
Add separator as parameter (#1903)
Browse files Browse the repository at this point in the history
Default separator is comma ','.
  • Loading branch information
dr0i committed Sep 28, 2023
1 parent 1f3ab0a commit 823019e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
21 changes: 19 additions & 2 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ private static Result searchResult(String q, String location, int from,
"attachment; filename=organisations.csv");
return ok(csvExport(format, orgs)).as("text/csv; charset=utf-8");
});
results.put("tsv", () -> {
String queryResultString =
searchQueryResult(q, location, from, size, aggregations);
String orgs = Json.parse(queryResultString).get("member").toString();
response().setHeader("Content-Disposition",
"attachment; filename=organisations.tsv");
return ok(csvExport(format, orgs, CsvExport.TAB_SEPARATOR)).as("text/tsv; charset=utf-8");
});
Supplier<Result> json = () -> {
String queryResultString =
searchQueryResult(q, location, from, size, aggregations);
Expand Down Expand Up @@ -490,11 +498,20 @@ private static Optional<JsonNode> getOptional(JsonNode json, String field) {
return Optional.ofNullable(json.get(field));
}

private static String csvExport(String format, String orgs) {
private static String csvExport(String format, String orgs, String separator) {
String[] formatConfig = format.split(FORMAT_CONFIG_SEP); // e.g. csv:name,id
String fields = formatConfig.length > 1 && !formatConfig[1].isEmpty()
? formatConfig[1] : defaultFields();
return new CsvExport(orgs).of(fields);
if (separator == null) {
return new CsvExport(orgs).of(fields);
}
else {
return new CsvExport(orgs).of(fields, separator);
}
}

private static String csvExport(String format, String orgs) {
return csvExport(format, orgs, CsvExport.DEFAULT_SEPARATOR);
}

private static String defaultFields() {
Expand Down
21 changes: 16 additions & 5 deletions app/transformation/CsvExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
import play.libs.Json;

/**
* Export organisations JSON data as CSV.
* Export organisations JSON data as CSV. Allows defining an other
* separator than comma.
*
* @author Fabian Steeg (fsteeg)
*/
public class CsvExport {

private final JsonNode organisations;
public final static String DEFAULT_SEPARATOR = ",";
public final static String TAB_SEPARATOR = "\t";

/**
* @param json The organisations JSON data to export
Expand All @@ -35,10 +38,19 @@ public CsvExport(String json) {
* @return The data for the given fields in CSV format
*/
public String of(String fields) {
return of(fields, DEFAULT_SEPARATOR);
}

/**
* @param fields The JSON fields to include in the export
* @param separator The separator to separate entries in the CSV
* @return The data for the given fields in [C*]SV format
*/
public String of(final String fields, final String separator) {
StringBuilder csv = new StringBuilder(fields + "\n");
for (Iterator<JsonNode> iter = organisations.elements(); iter.hasNext();) {
for (Iterator<JsonNode> iter = organisations.elements(); iter.hasNext(); ) {
JsonNode org = iter.next();
csv.append(Arrays.asList(fields.split(",")).stream().map(field -> {
csv.append(Arrays.asList(fields.split(separator)).stream().map(field -> {
try {
Object value = JsonPath.read(Configuration.defaultConfiguration()
.jsonProvider().parse(org.toString()), "$." + field);
Expand All @@ -50,9 +62,8 @@ public String of(String fields) {
// https://www.w3.org/TR/2015/REC-tabular-data-model-20151217/#empty-and-quoted-cells
return "";
}
}).collect(Collectors.joining(","))).append("\n");
}).collect(Collectors.joining(separator))).append("\n");
}
return csv.toString();
}

}
14 changes: 11 additions & 3 deletions test/transformation/CsvExportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@

@SuppressWarnings("javadoc")
public class CsvExportTest {
@Test
public void testFlatFieldsDefaultSeparator() {
testFlatFields(CsvExport.DEFAULT_SEPARATOR);
}

@Test
public void testFlatFields() {
public void testFlatFieldsTabulatorSeparator() {
testFlatFields(CsvExport.TAB_SEPARATOR);
}

private void testFlatFields(final String sep) {
ObjectNode node1 = Json.newObject();
node1.put("field1", "org1-value1");
node1.put("field2", "org1-value2");
Expand All @@ -28,11 +36,11 @@ public void testFlatFields() {
node2.put("field3", "org2-value3");
List<ObjectNode> orgs = Arrays.asList(node1, node2);
CsvExport export = new CsvExport(Json.stringify(Json.toJson(orgs)));
String expected = String.format("%s,%s\n%s,%s\n%s,%s\n", //
String expected = String.format("%s" + sep + "%s\n%s" + sep + "%s\n%s" + sep + "%s\n", //
"field1", "field3", //
"\"org1-value1\"", "\"org1-value3\"", //
"\"org2-value1\"", "\"org2-value3\"");
assertThat(export.of("field1,field3")).isEqualTo(expected);
assertThat(export.of("field1" + sep + "field3", sep)).isEqualTo(expected);
}

@Test
Expand Down

0 comments on commit 823019e

Please sign in to comment.