Skip to content

Commit

Permalink
Don't add duplicate currencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jan 6, 2025
1 parent 6bd4870 commit bb90d13
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/src/main/java/org/gnucash/android/importer/GncXmlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,9 @@ public void endElement(String uri, String localName, String qualifiedName) throw
+ "' currency code not found in the database for account " + mAccount.getUID());
}
String currencyId = commodity.getCurrencyCode();
if (mCurrencyCount.containsKey(currencyId)) {
mCurrencyCount.put(currencyId, mCurrencyCount.get(currencyId) + 1);
} else {
mCurrencyCount.put(currencyId, 1);
}
Integer currencyCount = mCurrencyCount.get(currencyId);
if (currencyCount == null) currencyCount = 0;
mCurrencyCount.put(currencyId, currencyCount + 1);
}
break;
case TAG_ACCT_PARENT:
Expand Down Expand Up @@ -907,7 +905,7 @@ public void endDocument() throws SAXException {
String currencyCode = split.getAccountUID();
Account imbAccount = mapImbalanceAccount.get(currencyCode);
if (imbAccount == null) {
imbAccount = new Account(imbalancePrefix + currencyCode, mCommoditiesDbAdapter.getCommodity(currencyCode));
imbAccount = new Account(imbalancePrefix + currencyCode, getCommodity(Commodity.COMMODITY_CURRENCY, currencyCode));
imbAccount.setParentUID(mRootAccount.getUID());
imbAccount.setAccountType(AccountType.BANK);
mapImbalanceAccount.put(currencyCode, imbAccount);
Expand All @@ -916,7 +914,7 @@ public void endDocument() throws SAXException {
split.setAccountUID(imbAccount.getUID());
}

java.util.Stack<Account> stack = new Stack<>();
Stack<Account> stack = new Stack<>();
for (Account account : mAccountList) {
if (mapFullName.get(account.getUID()) != null) {
continue;
Expand Down Expand Up @@ -1169,8 +1167,11 @@ private Commodity getCommodity(@Nullable String space, @Nullable String id) {
commoditiesById = mCommodities.get(Commodity.COMMODITY_CURRENCY);
}
}
if (commoditiesById == null) return null;
return commoditiesById.get(id);
if (commoditiesById != null) {
Commodity commodity = commoditiesById.get(id);
if (commodity != null) return commodity;
}
return mCommoditiesDbAdapter.getCommodity(id);
}

@Nullable
Expand All @@ -1182,6 +1183,9 @@ private Commodity putCommodity(@NonNull Commodity commodity) {
if (TextUtils.isEmpty(id)) return null;
if (TEMPLATE.equals(id)) return null;

// Already a database record?
if (commodity.id != 0L) return null;

Map<String, Commodity> commoditiesById = mCommodities.get(space);
if (commoditiesById == null) {
commoditiesById = new HashMap<>();
Expand Down

0 comments on commit bb90d13

Please sign in to comment.