Skip to content

Commit

Permalink
34816 Allow column name customization on export
Browse files Browse the repository at this point in the history
Added implementation of header aliases for export
  • Loading branch information
cgendreau committed Sep 26, 2024
1 parent 07f6fe9 commit 641ac06
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
@RelatedEntity(DataExport.class)
@CustomFieldAdapter(adapters = {
FieldsAdapter.DataExportQueryFieldAdapter.class,
FieldsAdapter.DataExportColumnsFieldAdapter.class
FieldsAdapter.DataExportColumnsFieldAdapter.class,
FieldsAdapter.DataExportColumnAliasesFieldAdapter.class
})
@Getter
@Setter
Expand Down Expand Up @@ -49,4 +50,7 @@ public class DataExportDto {
@IgnoreDinaMapping(reason = "handled by DataExportColumnsFieldAdapter")
private List<String> columns;

@IgnoreDinaMapping(reason = "handled by DataExportColumnsFieldAdapter")
private List<String> columnAliases;

}
35 changes: 35 additions & 0 deletions src/main/java/ca/gc/aafc/dina/export/api/dto/FieldsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,41 @@ public Supplier<String[]> entitySupplyMethod(DataExport entityRef) {
public Supplier<List<String>> dtoSupplyMethod(DataExportDto dtoRef) {
return dtoRef::getColumns;
}
}

@NoArgsConstructor
public static final class DataExportColumnAliasesFieldAdapter
implements DinaFieldAdapter<DataExportDto, DataExport, List<String>, String[]> {

@Override
public List<String> toDTO(String[] columnAliases) {
return columnAliases == null ? null : Arrays.asList(columnAliases);
}

@Override
public String[] toEntity(List<String> columnAliases) {
return columnAliases == null ? null : columnAliases.toArray(String[]::new);
}

@Override
public Consumer<String[]> entityApplyMethod(DataExport entityRef) {
return entityRef::setColumnAliases;
}

@Override
public Consumer<List<String>> dtoApplyMethod(DataExportDto dtoRef) {
return dtoRef::setColumnAliases;
}

@Override
public Supplier<String[]> entitySupplyMethod(DataExport entityRef) {
return entityRef::getColumnAliases;
}

@Override
public Supplier<List<String>> dtoSupplyMethod(DataExportDto dtoRef) {
return dtoRef::getColumnAliases;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public enum ExportType { TABULAR_DATA, OBJECT_ARCHIVE }
@Column
private String[] columns;

@Type(type = "string-array")
@Column
private String[] columnAliases;

@Enumerated(EnumType.STRING)
@NotNull
@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ public CompletableFuture<UUID> export(DataExport dinaExport) throws IOException
try {
//Create the directory
ensureDirectoryExists(exportPath.getParent());

List<String> headerAliases = dinaExport.getColumnAliases() != null ?
Arrays.asList(dinaExport.getColumnAliases()) : null;

// csv output
try (Writer w = new FileWriter(exportPath.toFile(),
StandardCharsets.UTF_8);
try (Writer w = new FileWriter(exportPath.toFile(), StandardCharsets.UTF_8);
CsvOutput<JsonNode> output =
CsvOutput.create(Arrays.asList(dinaExport.getColumns()), new TypeReference<>() {
}, w)) {
CsvOutput.create(Arrays.asList(dinaExport.getColumns()), headerAliases,
new TypeReference<>() {
}, w)) {
export(dinaExport.getSource(), objectMapper.writeValueAsString(dinaExport.getQuery()),
output);
}
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/ca/gc/aafc/dina/export/api/output/CsvOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.Writer;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.SequenceWriter;
Expand Down Expand Up @@ -38,6 +40,45 @@ public static <T> CsvOutput<T> create(List<String> headers, TypeReference<T> typ
.with(csvSchema).writeValues(writer));
}

/**
* Same as {@link #create(List, TypeReference, Writer)} but using aliases for headers.
* @param headers should match the properties available in the type.
* @param headersAliases aliases for headers. Matched by the order in the list.
* @param typeRef the type
* @param writer won't be closed. Responsibility of the caller.
* @return
*/
public static <T> CsvOutput<T> create(List<String> headers, List<String> headersAliases,
TypeReference<T> typeRef, Writer writer) throws IOException {

if (CollectionUtils.isEmpty(headersAliases)) {
return create(headers, typeRef, writer);
}

if (headers == null || headers.size() != headersAliases.size()) {
throw new IllegalArgumentException(
"headersAliases should match headers size and not be null");
}

// Write all the header aliases first
CsvSchema.Builder builder = CsvSchema.builder()
.addColumns(headersAliases, CsvSchema.ColumnType.STRING);
CsvSchema csvSchema = builder.build().withHeader();
CsvMapper csvMapper = new CsvMapper();
SequenceWriter ow = csvMapper.writer().with(csvSchema).writeValues(writer);
ow.write(null);
ow.flush();

//Use the real headers but configure the builder to not write them (since we have the aliases)
builder = CsvSchema.builder()
.addColumns(headers, CsvSchema.ColumnType.STRING);
csvSchema = builder.build().withoutHeader();
csvMapper = new CsvMapper();
csvMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
return new CsvOutput<>(csvMapper.writerFor(typeRef)
.with(csvSchema).writeValues(writer));
}

private CsvOutput(SequenceWriter sw) {
this.sw = sw;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<include file="db/changelog/migrations/4-Add_export_type_to_export_table.xml"/>
<include file="db/changelog/migrations/5-Add_name_to_export_table.xml"/>
<include file="db/changelog/migrations/6-Add_type_variables_to_report_template_table.xml"/>
<include file="db/changelog/migrations/7-Add_column_aliases_to_data_export_table.xml"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.1" encoding="UTF-8" standalone="no" ?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://local.xsd/dbchangelog-4.4.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">

<changeSet id="7-Add_column_aliases_to_data_export_table" context="schema-change" author="cgendreau">
<addColumn tableName="data_export">
<column name="column_aliases" type="text[]"/>
</addColumn>
</changeSet>
</databaseChangeLog>

0 comments on commit 641ac06

Please sign in to comment.