implemented download of account data as CSV
Build Docker Image / Docker-Build (push) Successful in 2m4s
Build Docker Image / Clean-Registry (push) Successful in 6s

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-08-16 23:08:51 +02:00
parent 695c1e769f
commit 94f11ea38f
5 changed files with 78 additions and 19 deletions
@@ -2,18 +2,18 @@
package de.srsoftware.umbrella.core.api;
import static de.srsoftware.umbrella.core.Util.mapValues;
import static java.nio.charset.StandardCharsets.UTF_8;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.constants.Field;
import de.srsoftware.umbrella.core.model.Account;
import de.srsoftware.umbrella.core.model.Transaction;
import de.srsoftware.umbrella.core.model.Translatable;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public interface AccountingService {
public record AccountData(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) implements Mappable {
record AccountData(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) implements Mappable {
public static AccountData of(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) {
return new AccountData(account, transactions, userMap);
}
@@ -26,6 +26,16 @@ public interface AccountingService {
Field.USER_LIST,mapValues(userMap)
);
}
public byte[] toCsv(Map<Long, UmbrellaUser> users) {
var keys = List.of(Field.DATE, Field.SOURCE, Field.AMOUNT, Field.DESTINATION, Field.PURPOSE,Field.TAGS);
var sb = new StringBuilder();
keys.stream().map(Translatable::t).forEach(field -> sb.append(field).append(";"));
transactions.stream().sorted(Comparator.comparing(Transaction::id))
.map(transaction -> transaction.csvLine(users, keys,";"))
.forEach(line -> sb.append("\n").append(line));
return sb.toString().getBytes(UTF_8);
}
}
AccountData loadAccount(long accountId);
@@ -9,9 +9,8 @@ import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
public class Transaction extends UmbrellaObject {
private final long accountId;
@@ -25,11 +24,11 @@ public class Transaction extends UmbrellaObject {
public Transaction(long id, long accountId, LocalDateTime date, IdOrString source, IdOrString destination, double amount, String purpose, Set<String> tags){
super(id);
this.accountId = accountId;
this.date = date;
this.source = source;
this.destination = destination;
this.amount = amount;
this.date = date;
this.destination = destination;
this.purpose = purpose;
this.source = source;
this.tags = tags == null ? new HashSet<>() : tags;
}
@@ -52,6 +51,10 @@ public class Transaction extends UmbrellaObject {
return this;
}
public String csvLine(Map<Long, UmbrellaUser> users, List<String> keys, String delimiter) {
return keys.stream().map(this::get).map(o -> resolveUsers(users,o)).map(o -> quote(o, delimiter)).collect(Collectors.joining(delimiter));
}
public LocalDateTime date(){
return date;
}
@@ -76,6 +79,19 @@ public class Transaction extends UmbrellaObject {
return this;
}
public Object get(String property){
return switch (property){
case Field.AMOUNT -> amount;
case Field.DATE -> date.toLocalDate();
case Field.DESTINATION -> destination;
case Field.ID -> id();
case Field.PURPOSE -> purpose;
case Field.SOURCE -> source;
case Field.TAGS -> tags.stream().sorted().collect(Collectors.joining(", "));
case null, default -> null;
};
}
public boolean isDirty(){
return !dirtyFields.isEmpty();
}
@@ -102,6 +118,18 @@ public class Transaction extends UmbrellaObject {
return this;
}
private static String quote(Object o, String delimiter){
var str = o == null ? "" : o instanceof Map<?,?> m ? m.get("value").toString() : o.toString();
if (str.contains(delimiter)) return '"'+str.replace("\"","\"\"")+'"';
return str;
}
private Object resolveUsers(Map<Long, UmbrellaUser> users, Object o) {
if (!(o instanceof IdOrString ios)) return o;
if (ios.isId()) return users.get(ios.id()) instanceof UmbrellaUser user ? user.name() : "unknown";
return ios;
}
public IdOrString source(){
return source;
}