]> git.ktnx.net Git - mobile-ledger-staging.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java
a step towards API-independent JSON parsing of accounts
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / json / AccountListParser.java
index 4dd2e375ae7ac79e9c66c31feb7afc6b6e978e07..053b1e9074c6561359daf0b4d235cdd91e5775cf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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 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() {
+        if (!iterator.hasNext())
+            return null;
+
+        ParsedLedgerAccount next = iterator.next();
+
+        if (next.getAname()
+                .equalsIgnoreCase("root"))
+            return nextAccount();
+
+        debug("accounts", String.format("Got account '%s' [v1.15]", next.getAname()));
+        return next;
     }
-    public ParsedLedgerAccount nextAccount() throws IOException {
-        return iter.hasNext() ? iter.next() : null;
+    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;
     }
+
 }