/*
- * 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 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<ParsedLedgerAccount> iter;
+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);
+ 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<String, LedgerAccount> 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;
}
+
}