import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.err.HTTPException;
import net.ktnx.mobileledger.json.v1_15.AccountListParser;
-import net.ktnx.mobileledger.json.v1_15.ParsedBalance;
-import net.ktnx.mobileledger.json.v1_15.ParsedLedgerAccount;
import net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction;
import net.ktnx.mobileledger.json.v1_15.TransactionListParser;
import net.ktnx.mobileledger.model.Data;
private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
// %3A is '='
- private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
- private Pattern reAccountValue = Pattern.compile(
+ private final Pattern reAccountName =
+ Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
+ private final Pattern reAccountValue = Pattern.compile(
"<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
- private MainModel mainModel;
- private MobileLedgerProfile profile;
- private List<LedgerAccount> prevAccounts;
+ private final MainModel mainModel;
+ private final MobileLedgerProfile profile;
+ private final List<LedgerAccount> prevAccounts;
private int expectedPostingsCount = -1;
public RetrieveTransactionsTask(@NonNull MainModel mainModel,
@NonNull MobileLedgerProfile profile,
return null;
}
}
+ public MobileLedgerProfile getProfile() {
+ return profile;
+ }
@Override
protected void onProgressUpdate(Progress... values) {
super.onProgressUpdate(values);
throwIfCancelled();
}
}
- private @NonNull
- LedgerAccount ensureAccountExists(String accountName, HashMap<String, LedgerAccount> map,
- ArrayList<LedgerAccount> createdAccounts) {
+ @NonNull
+ public LedgerAccount ensureAccountExists(String accountName, HashMap<String, LedgerAccount> map,
+ ArrayList<LedgerAccount> createdAccounts) {
LedgerAccount acc = map.get(accountName);
if (acc != null)
createdAccounts.add(acc);
return acc;
}
+ public void addNumberOfPostings(int number) {
+ expectedPostingsCount += number;
+ }
private List<LedgerAccount> retrieveAccountList() throws IOException, HTTPException {
HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
http.setAllowUserInteraction(false);
while (true) {
throwIfCancelled();
- ParsedLedgerAccount parsedAccount = parser.nextAccount();
- if (parsedAccount == null) {
+ LedgerAccount acc = parser.nextLedgerAccount(this, map);
+ if (acc == null)
break;
- }
- expectedPostingsCount += 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 = ensureAccountExists(parentName, map, createdParents);
- parent.setHasSubAccounts(true);
- }
- acc = new LedgerAccount(profile, accName, parent);
list.add(acc);
- map.put(accName, acc);
-
- String lastCurrency = null;
- float lastCurrencyAmount = 0;
- for (ParsedBalance b : parsedAccount.getAibalance()) {
- 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);
}
throwIfCancelled();
}
Data.backgroundTaskFinished();
}
}
- private void throwIfCancelled() {
+ public void throwIfCancelled() {
if (isCancelled())
throw new OperationCanceledException(null);
}