X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fjson%2FAccountListParser.java;h=872cac79c7828f6dd06a637431aaa7f36a2dbfab;hb=bb789332571609eeb1bef6e39b7ad359227d1045;hp=2551c28d9024365ebfb5ee0f30613bcac74327a5;hpb=8440406eafce2900954cfc280343aa1a5daa7d29;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java index 2551c28d..872cac79 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java @@ -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 @@ -17,33 +17,49 @@ 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 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 { +import static net.ktnx.mobileledger.utils.Logger.debug; - private final MappingIterator iter; +abstract public class AccountListParser { + protected MappingIterator 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); + default: + throw new RuntimeException("Unsupported version " + version.toString()); + } - public AccountListParser(InputStream input) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - ObjectReader reader = mapper.readerFor(ParsedLedgerAccount.class); - - iter = reader.readValues(input); } - public ParsedLedgerAccount nextAccount() throws IOException { - if (!iter.hasNext()) return null; + public abstract API getApiVersion(); + public LedgerAccount nextAccount(RetrieveTransactionsTask task, + HashMap map) { + if (!iterator.hasNext()) + return null; - ParsedLedgerAccount next = iter.next(); + LedgerAccount next = iterator.next() + .toLedgerAccount(task, map); - if (next.getAname().equalsIgnoreCase("root")) return nextAccount(); + if (next.getName() + .equalsIgnoreCase("root")) + return nextAccount(task, map); - Log.d("accounts", String.format("Got account '%s'", next.getAname())); + debug("accounts", String.format("Got account '%s' [%s]", next.getName(), + getApiVersion().getDescription())); return next; } + }