/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
package net.ktnx.mobileledger.json;
-import android.util.Log;
-
import com.fasterxml.jackson.databind.MappingIterator;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.ObjectReader;
-import java.io.IOException;
-import java.io.InputStream;
+import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
+import net.ktnx.mobileledger.model.LedgerAccount;
-public class AccountListParser {
+import java.util.ArrayList;
+import java.util.HashMap;
- private final MappingIterator<ParsedLedgerAccount> iter;
+import static net.ktnx.mobileledger.utils.Logger.debug;
- public AccountListParser(InputStream input) throws IOException {
- ObjectMapper mapper = new ObjectMapper();
- ObjectReader reader = mapper.readerFor(ParsedLedgerAccount.class);
+abstract public class AccountListParser {
+ protected MappingIterator<net.ktnx.mobileledger.json.ParsedLedgerAccount> iterator;
- iter = reader.readValues(input);
- }
- public ParsedLedgerAccount nextAccount() throws IOException {
- if (!iter.hasNext()) return null;
+ public ParsedLedgerAccount nextAccount() {
+ if (!iterator.hasNext())
+ return null;
- ParsedLedgerAccount next = iter.next();
+ ParsedLedgerAccount next = iterator.next();
- if (next.getAname().equalsIgnoreCase("root")) return nextAccount();
+ if (next.getAname()
+ .equalsIgnoreCase("root"))
+ return nextAccount();
- Log.d("accounts", String.format("Got account '%s'", next.getAname()));
+ debug("accounts", String.format("Got account '%s' [v1.15]", next.getAname()));
return next;
}
+ public LedgerAccount nextLedgerAccount(RetrieveTransactionsTask task,
+ HashMap<String, LedgerAccount> map) {
+ ParsedLedgerAccount parsedAccount = nextAccount();
+ if (parsedAccount == null)
+ return null;
+
+ task.addNumberOfPostings(parsedAccount.getAnumpostings());
+ final String accName = parsedAccount.getAname();
+ LedgerAccount acc = map.get(accName);
+ if (acc != null)
+ throw new RuntimeException(
+ String.format("Account '%s' already present", acc.getName()));
+ String parentName = LedgerAccount.extractParentName(accName);
+ ArrayList<LedgerAccount> createdParents = new ArrayList<>();
+ LedgerAccount parent;
+ if (parentName == null) {
+ parent = null;
+ }
+ else {
+ parent = task.ensureAccountExists(parentName, map, createdParents);
+ parent.setHasSubAccounts(true);
+ }
+ acc = new LedgerAccount(task.getProfile(), accName, parent);
+ map.put(accName, acc);
+
+ String lastCurrency = null;
+ float lastCurrencyAmount = 0;
+ for (ParsedBalance b : parsedAccount.getAibalance()) {
+ task.throwIfCancelled();
+ final String currency = b.getAcommodity();
+ final float amount = b.getAquantity()
+ .asFloat();
+ if (currency.equals(lastCurrency)) {
+ lastCurrencyAmount += amount;
+ }
+ else {
+ if (lastCurrency != null) {
+ acc.addAmount(lastCurrencyAmount, lastCurrency);
+ }
+ lastCurrency = currency;
+ lastCurrencyAmount = amount;
+ }
+ }
+ if (lastCurrency != null) {
+ acc.addAmount(lastCurrencyAmount, lastCurrency);
+ }
+ for (LedgerAccount p : createdParents)
+ acc.propagateAmountsTo(p);
+
+ return acc;
+ }
+
}