Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add duplicate currencies. #236

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading