Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
fix accounting CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetancollaud committed Sep 11, 2015
1 parent 38ff744 commit d6a5a3b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class HistoryEntry implements Comparable<HistoryEntry> {
private final int id;
@CsvField(headerName = "type")
private final HistoryEntryType type;
@CsvField(headerName = "comment")
private final String comment;
@CsvField(headerName = "date")
private final Date date;
@CsvField(headerName = "amount")
private final double amount;
@CsvField(headerName = "detail")
private final String detail;
@CsvField(headerName = "comment")
private final String comment;
@CsvField(headerName = "user")
private final HistoryEntryUser user;

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/net/collaud/fablab/manager/export/CsvExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
@Slf4j
public class CsvExporter<T> {

public static final String FIELD_SEPARATOR = ";";
public static final String FIELD_SEPARATOR = ",";
public static final String FIELD_SEPARATOR_REPLACE = ";";
public static final String LINE_SEPARATOR = "\n";
public static final String ESCAPE_CHAR = "\"";

Expand All @@ -32,12 +33,16 @@ public CsvExporter(Class<T> type) {
.filter(f -> f.getAnnotation(CsvField.class) != null)
.forEach(f -> fields.put(f, f.getAnnotation(CsvField.class)));
}

protected String postHeader(String header){
return header;
}

public CsvExporter writeHeader() {
return writeLine(fields.values().stream()
return writeLine(postHeader(fields.values().stream()
.map(f -> f.headerName())
.reduce(reduce())
.orElse(""));
.orElse("")));
}

public CsvExporter writeRow(T obj) {
Expand All @@ -47,7 +52,7 @@ public CsvExporter writeRow(T obj) {
.orElse(""));
}

private BinaryOperator<String> reduce() {
protected BinaryOperator<String> reduce() {
return (l, r) ->l + FIELD_SEPARATOR + r ;
}

Expand All @@ -60,19 +65,23 @@ public String getFileName() {
return classAnnotation.fileName();
}

private String getFieldValue(T obj, Field f) {
protected String getFieldValue(T obj, Field f) {
try {
f.setAccessible(true);
return Optional.ofNullable(f.get(obj))
.map(v -> v.toString())
.map(v-> v.replace(FIELD_SEPARATOR, FIELD_SEPARATOR_REPLACE))
.map(v -> v.replace("\r\n", "\t"))
.map(v -> v.replace("\n", "\t"))
.map(v -> v.replace("\r", "\t"))
.orElse("");
} catch (IllegalAccessException ex) {
log.error("Cannot access field " + f.getName(), ex);
return "";
}
}

private CsvExporter writeLine(String l) {
protected CsvExporter writeLine(String l) {
builder.append(l);
builder.append(LINE_SEPARATOR);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.collaud.fablab.manager.export.custom;

import net.collaud.fablab.manager.export.*;
import java.lang.reflect.Field;
import lombok.extern.slf4j.Slf4j;
import net.collaud.fablab.manager.data.virtual.HistoryEntry;

/**
*
* @author Gaetan Collaud
* @param <T>
*/
@Slf4j
public class CsvExporterAccounting extends CsvExporter<HistoryEntry>{

public static final String FIELD_TO_OVERRIDE = "amount";

public CsvExporterAccounting() {
super(HistoryEntry.class);
}

@Override
public String postHeader(String header){
return header.replace(FIELD_TO_OVERRIDE, "credit"+FIELD_SEPARATOR+"debit");
}

@Override
protected String getFieldValue(HistoryEntry obj, Field f) {
String parsed = super.getFieldValue(obj, f);
if(f.getAnnotation(CsvField.class).headerName().equals(FIELD_TO_OVERRIDE)){
Double v = obj.getAmount();
if(v<0){
return FIELD_SEPARATOR+Double.toString(-v);
}else{
return parsed+FIELD_SEPARATOR;
}
}
return parsed;
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.collaud.fablab.manager.annotation.JavascriptAPIConstant;
import net.collaud.fablab.manager.data.virtual.HistoryEntry;
import net.collaud.fablab.manager.export.CsvExporter;
import net.collaud.fablab.manager.export.custom.CsvExporterAccounting;
import net.collaud.fablab.manager.rest.v1.criteria.PeriodSearchCriteria;
import net.collaud.fablab.manager.rest.v1.model.BaseModel;
import net.collaud.fablab.manager.rest.v1.model.DataModel;
Expand Down Expand Up @@ -46,7 +47,7 @@ public String export(@RequestParam("dateFrom") Long dateFrom,@RequestParam("date
if (list.isEmpty()) {
return "";
}
CsvExporter exporter = new CsvExporter<>(list.get(0).getClass());
CsvExporter exporter = new CsvExporterAccounting();
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s.csv\"",exporter.getFileName()));
exporter.writeHeader();
list.forEach(l -> exporter.writeRow(l));
Expand Down

0 comments on commit d6a5a3b

Please sign in to comment.