+ acc = new LedgerAccount(accountName, parentAccount);
+ createdAccounts.add(acc);
+ return acc;
+ }
+ public void addNumberOfPostings(int number) {
+ expectedPostingsCount += number;
+ }
+ private List<LedgerAccount> retrieveAccountList()
+ throws IOException, HTTPException, ApiNotSupportedException {
+ final API apiVersion = API.valueOf(profile.getApiVersion());
+ if (apiVersion.equals(API.auto)) {
+ return retrieveAccountListAnyVersion();
+ }
+ else if (apiVersion.equals(API.html)) {
+ Logger.debug("json",
+ "Declining using JSON API for /accounts with configured legacy API version");
+ return null;
+ }
+ else {
+ return retrieveAccountListForVersion(apiVersion);
+ }
+ }
+ private List<LedgerAccount> retrieveAccountListAnyVersion()
+ throws ApiNotSupportedException, IOException, HTTPException {
+ for (API ver : API.allVersions) {
+ try {
+ return retrieveAccountListForVersion(ver);
+ }
+ catch (JsonParseException | RuntimeJsonMappingException e) {
+ Logger.debug("json",
+ String.format(Locale.US, "Error during account list retrieval using API %s",
+ ver.getDescription()), e);
+ }
+
+ }
+
+ throw new ApiNotSupportedException();
+ }
+ private List<LedgerAccount> retrieveAccountListForVersion(API version)
+ throws IOException, HTTPException {
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
+ http.setAllowUserInteraction(false);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ return null;
+ default:
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+ }
+ publishProgress(Progress.indeterminate());
+ ArrayList<LedgerAccount> list = new ArrayList<>();
+ HashMap<String, LedgerAccount> map = new HashMap<>();
+ throwIfCancelled();
+ try (InputStream resp = http.getInputStream()) {
+ throwIfCancelled();
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+
+ AccountListParser parser = AccountListParser.forApiVersion(version, resp);
+ expectedPostingsCount = 0;
+
+ while (true) {
+ throwIfCancelled();
+ LedgerAccount acc = parser.nextAccount(this, map);
+ if (acc == null)
+ break;
+ list.add(acc);
+ }
+ throwIfCancelled();
+ }
+
+ return list;
+ }
+ private List<LedgerTransaction> retrieveTransactionList()
+ throws ParseException, HTTPException, IOException, ApiNotSupportedException {
+ final API apiVersion = API.valueOf(profile.getApiVersion());
+ if (apiVersion.equals(API.auto)) {
+ return retrieveTransactionListAnyVersion();
+ }
+ else if (apiVersion.equals(API.html)) {
+ Logger.debug("json",
+ "Declining using JSON API for /accounts with configured legacy API version");
+ return null;
+ }
+ else {
+ return retrieveTransactionListForVersion(apiVersion);
+ }
+
+ }
+ private List<LedgerTransaction> retrieveTransactionListAnyVersion()
+ throws ApiNotSupportedException {
+ for (API ver : API.allVersions) {
+ try {
+ return retrieveTransactionListForVersion(ver);
+ }
+ catch (Exception e) {
+ Logger.debug("json",
+ String.format(Locale.US, "Error during account list retrieval using API %s",
+ ver.getDescription()));
+ }
+
+ }
+
+ throw new ApiNotSupportedException();
+ }
+ private List<LedgerTransaction> retrieveTransactionListForVersion(API apiVersion)
+ throws IOException, ParseException, HTTPException {
+ Progress progress = new Progress();
+ progress.setTotal(expectedPostingsCount);
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ return null;
+ default:
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+ }
+ ArrayList<LedgerTransaction> trList = new ArrayList<>();
+ try (InputStream resp = http.getInputStream()) {
+ throwIfCancelled();
+
+ TransactionListParser parser = TransactionListParser.forApiVersion(apiVersion, resp);
+
+ int processedPostings = 0;
+
+ while (true) {
+ throwIfCancelled();
+ LedgerTransaction transaction = parser.nextTransaction();
+ throwIfCancelled();
+ if (transaction == null)
+ break;
+
+ trList.add(transaction);
+
+ progress.setProgress(processedPostings += transaction.getAccounts()
+ .size());
+// Logger.debug("trParser",
+// String.format(Locale.US, "Parsed transaction %d - %s", transaction
+// .getId(),
+// transaction.getDescription()));
+// for (LedgerTransactionAccount acc : transaction.getAccounts()) {
+// Logger.debug("trParser",
+// String.format(Locale.US, " %s", acc.getAccountName()));
+// }
+ publishProgress(progress);