]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java
add support for hledger-web 1.23 json schema
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / json / AccountListParser.java
index 4dd2e375ae7ac79e9c66c31feb7afc6b6e978e07..baeeb2eab58528cc6f722639a6b43299411c97c9 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 net.ktnx.mobileledger.async.RetrieveTransactionsTask;
+import net.ktnx.mobileledger.model.LedgerAccount;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashMap;
 
-public class AccountListParser {
-
-    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;
+    public static AccountListParser forApiVersion(API version, InputStream input)
+            throws IOException {
+        switch (version) {
+            case v1_14:
+                return new net.ktnx.mobileledger.json.v1_14.AccountListParser(input);
+            case v1_15:
+                return new net.ktnx.mobileledger.json.v1_15.AccountListParser(input);
+            case v1_19_1:
+                return new net.ktnx.mobileledger.json.v1_19_1.AccountListParser(input);
+            case v1_23:
+                return new net.ktnx.mobileledger.json.v1_23.AccountListParser(input);
+            default:
+                throw new RuntimeException("Unsupported version " + version.toString());
+        }
 
-        iter = reader.readValues(input);
     }
-    public ParsedLedgerAccount nextAccount() throws IOException {
-        return iter.hasNext() ? iter.next() : null;
+    public abstract API getApiVersion();
+    public LedgerAccount nextAccount(RetrieveTransactionsTask task,
+                                     HashMap<String, LedgerAccount> map) {
+        if (!iterator.hasNext())
+            return null;
+
+        LedgerAccount next = iterator.next()
+                                     .toLedgerAccount(task, map);
+
+        if (next.getName()
+                .equalsIgnoreCase("root"))
+            return nextAccount(task, map);
+
+        debug("accounts", String.format("Got account '%s' [%s]", next.getName(),
+                getApiVersion().getDescription()));
+        return next;
     }
+
 }