Skip to content

Commit

Permalink
Improved: If error occurs when parsing template transactions, they wi…
Browse files Browse the repository at this point in the history
…ll be ignored.

(Scheduled transactions will also be ignored)

Fixed: crash when restoring backups with no SD card present
Fixed: deleted template transactions instead of non-template transactions after export
Fixed: crash due to computation of imbalance with multi-currency transactions
  • Loading branch information
codinguser committed May 4, 2015
1 parent 8f42d8c commit ba16ca3
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'crashlytics'
def versionMajor = 1
def versionMinor = 6
def versionPatch = 0
def versionBuild = 1
def versionBuild = 2

def buildTime() {
def df = new SimpleDateFormat("yyyyMMdd_HHmm'UTC'")
Expand Down Expand Up @@ -89,7 +89,7 @@ android {

beta {
resValue "string", "app_name", "GnuCash - beta"
versionName "${versionMajor}.${versionMinor}.${versionPatch}-beta${versionBuild}}"
versionName "${versionMajor}.${versionMinor}.${versionPatch}-beta${versionBuild}"
resValue "string", "app_version_name", "${versionName}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public int getNumCurrencies(String transactionUID) {
* @return Number of records deleted
*/
public int deleteAllNonTemplateTransactions(){
String where = TransactionEntry.COLUMN_TEMPLATE + "!=0";
String where = TransactionEntry.COLUMN_TEMPLATE + "=0";
return mDb.delete(mTableName, where, null);
}

Expand Down
36 changes: 27 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 @@ -180,6 +180,13 @@ public class GncXmlHandler extends DefaultHandler {
boolean mIsLastRun = false;
boolean mIsRecurrenceStart = false;

/**
* Flag to determine if to ignore template transactions.
* If this flag is set, both template transactions and scheduled actions will be ignored.
* This flag is mostly set when an error occurs in parsing the template transaction amount.
*/
boolean mIgnoreTemplateTransactions = false;

/**
* Multiplier for the recurrence period type. e.g. period type of week and multiplier of 2 means bi-weekly
*/
Expand Down Expand Up @@ -428,7 +435,8 @@ public void endElement(String uri, String localName, String qualifiedName) throw
Log.e(LOG_TAG, msg + "\n" + e.getMessage());
Crashlytics.log(msg);
Crashlytics.logException(e);
throw new SAXException(msg, e); //if we fail to parse the split amount, terminate import - data integrity compromised
mIgnoreTemplateTransactions = true;
//throw new SAXException(msg, e); //if we fail to parse the split amount, terminate import - data integrity compromised
} finally {
mInCreditFormulaSlot = false;
}
Expand All @@ -443,7 +451,8 @@ public void endElement(String uri, String localName, String qualifiedName) throw
Log.e(LOG_TAG, msg + "\n" + e.getMessage());
Crashlytics.log(msg);
Crashlytics.logException(e);
throw new SAXException(msg, e); //if we fail to parse the split amount, terminate import - data integrity compromised
mIgnoreTemplateTransactions = true;
//throw new SAXException(msg, e); //if we fail to parse the split amount, terminate import - data integrity compromised
} finally {
mInDebitFormulaSlot = false;
}
Expand Down Expand Up @@ -524,10 +533,12 @@ public void endElement(String uri, String localName, String qualifiedName) throw
if (imbSplit != null) {
mAutoBalanceSplits.add(imbSplit);
}
mTransactionList.add(mTransaction);
if (mInTemplates){
mTemplateTransactions.add(mTransaction);
} else {
mTransactionList.add(mTransaction);
}

if (mRecurrencePeriod > 0) { //if we find an old format recurrence period, parse it
mTransaction.setTemplate(true);
ScheduledAction scheduledAction = ScheduledAction.parseScheduledAction(mTransaction, mRecurrencePeriod);
Expand Down Expand Up @@ -606,8 +617,10 @@ public void endElement(String uri, String localName, String qualifiedName) throw
break;
case GncXmlHelper.TAG_SCHEDULED_ACTION:
mScheduledActionsList.add(mScheduledAction);
int count = generateMissedScheduledTransactions(mScheduledAction);
Log.i(LOG_TAG, String.format("Generated %d transactions from scheduled action", count));
if (!mIgnoreTemplateTransactions) {
int count = generateMissedScheduledTransactions(mScheduledAction);
Log.i(LOG_TAG, String.format("Generated %d transactions from scheduled action", count));
}
mRecurrenceMultiplier = 1; //reset it, even though it will be parsed from XML each time
break;
}
Expand Down Expand Up @@ -711,10 +724,15 @@ public void endDocument() throws SAXException {
mAccountsDbAdapter.deleteAllRecords();
long nAccounts = mAccountsDbAdapter.bulkAddAccounts(mAccountList);
Log.d("Handler:", String.format("%d accounts inserted", nAccounts));
//We need to add scheduled actions first because there is a foreign key constraint on transactions
//which are generated from scheduled actions (we do auto-create some transactions during import)
int nSchedActions = mScheduledActionsDbAdapter.bulkAddScheduledActions(mScheduledActionsList);
Log.d("Handler:", String.format("%d scheduled actions inserted", nSchedActions));
//this flag is set when we have issues with parsing the template transaction amount
if (!mIgnoreTemplateTransactions) {
//We need to add scheduled actions first because there is a foreign key constraint on transactions
//which are generated from scheduled actions (we do auto-create some transactions during import)
int nSchedActions = mScheduledActionsDbAdapter.bulkAddScheduledActions(mScheduledActionsList);
Log.d("Handler:", String.format("%d scheduled actions inserted", nSchedActions));
long nTempTransactions = mTransactionsDbAdapter.bulkAddTransactions(mTemplateTransactions);
Log.d("Handler:", String.format("%d template transactions inserted", nTempTransactions));
}
long nTransactions = mTransactionsDbAdapter.bulkAddTransactions(mTransactionList);
Log.d("Handler:", String.format("%d transactions inserted", nTransactions));
long endTime = System.nanoTime();
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/org/gnucash/android/model/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,16 @@ public Money getBalance(String accountUID){

/**
* Computes the imbalance amount for the given transaction.
* In double entry, all transactions should resolve to zero. However a user may not enter all such values which
* means there is an extra amount which is unresolved.
* @return Money imbalance of the transaction
* In double entry, all transactions should resolve to zero. But imbalance occurs when there are unresolved splits.
* <p>If it is a multi-currency transaction, an imbalance of zero will be returned</p>
* @return Money imbalance of the transaction or zero if it is a multi-currency transaction
*/
public Money getImbalance(){
Money imbalance = Money.createZeroInstance(mCurrencyCode);
for (Split split : mSplitList) {
//TODO: Handle this better when multi-currency support is introduced
if (!split.getAmount().getCurrency().getCurrencyCode().equals(mCurrencyCode))
return Money.createZeroInstance(mCurrencyCode); //abort
Money amount = split.getAmount().absolute();
if (split.getType() == TransactionType.DEBIT)
imbalance = imbalance.subtract(amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import org.gnucash.android.R;
Expand All @@ -43,10 +44,10 @@
* @author Yongxin Wang <fefe.wyx@gmail.com>
*/
@TargetApi(11)
public class DeleteAllTransacationsConfirmationDialog extends DialogFragment {
public class DeleteAllTransactionsConfirmationDialog extends DialogFragment {

public static DeleteAllTransacationsConfirmationDialog newInstance() {
DeleteAllTransacationsConfirmationDialog frag = new DeleteAllTransacationsConfirmationDialog();
public static DeleteAllTransactionsConfirmationDialog newInstance() {
DeleteAllTransactionsConfirmationDialog frag = new DeleteAllTransactionsConfirmationDialog();
return frag;
}

Expand All @@ -62,13 +63,14 @@ public void onClick(DialogInterface dialog, int whichButton) {

Context context = getActivity();
AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance();
List<Transaction> openingBalances = new ArrayList<Transaction>();
List<Transaction> openingBalances = new ArrayList<>();
boolean preserveOpeningBalances = GnuCashApplication.shouldSaveOpeningBalances(false);
if (preserveOpeningBalances) {
openingBalances = accountsDbAdapter.getAllOpeningBalanceTransactions();
}
TransactionsDbAdapter transactionsDbAdapter = TransactionsDbAdapter.getInstance();
transactionsDbAdapter.deleteAllNonTemplateTransactions();
int count = transactionsDbAdapter.deleteAllNonTemplateTransactions();
Log.i("DeleteDialog", String.format("Deleted %d transactions successfully", count));

if (preserveOpeningBalances) {
transactionsDbAdapter.bulkAddTransactions(openingBalances);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,12 @@ public void importAccounts() {
public void restoreBackup() {
Log.i("Settings", "Opening GnuCash XML backups for restore");
File[] backupFiles = new File(Exporter.BACKUP_FOLDER_PATH).listFiles();
if (backupFiles == null){
Toast.makeText(this, R.string.toast_backup_folder_not_found, Toast.LENGTH_LONG).show();
new File(Exporter.BACKUP_FOLDER_PATH).mkdirs();
return;
}

Arrays.sort(backupFiles);
List<File> backupFilesList = Arrays.asList(backupFiles);
Collections.reverse(backupFilesList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
* Deletes all transactions in the system
*/
public void deleteAllTransactions(){
DeleteAllTransacationsConfirmationDialog deleteTransactionsConfirmationDialog =
DeleteAllTransacationsConfirmationDialog.newInstance();
DeleteAllTransactionsConfirmationDialog deleteTransactionsConfirmationDialog =
DeleteAllTransactionsConfirmationDialog.newInstance();
deleteTransactionsConfirmationDialog.show(getFragmentManager(), "transaction_settings");
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,5 @@
<string name="msg_enable_crashlytics">Enable to send information about malfunctions to the developers for improvement (recommended).
No user-identifiable information will be collected as part of this process!</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es-rMX/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,5 @@
<string name="msg_enable_crashlytics">Activar para enviar informaci&#243;n de errores a los desarolladores (recomendado).
Este proceso solo recoge informaci&#243;n que no permite identificar al usuario</string>
<string name="label_export_format">Formato de exportaci&#243;n</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-hu/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-nb/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,5 @@
No user-identifiable information will be collected as part of this process!
</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,5 @@
<string name="msg_enable_crashlytics">Enable to send information about malfunctions to the developers for improvement (recommended).
No user-identifiable information will be collected as part of this process!</string>
<string name="label_export_format">Export Format</string>
<string name="toast_backup_folder_not_found">Backup folder cannot be found. Make sure the SD Card is mounted!</string>
</resources>

0 comments on commit ba16ca3

Please sign in to comment.