|
22 | 22 | import android.content.ContentValues; |
23 | 23 | import android.content.Context; |
24 | 24 | import android.database.Cursor; |
| 25 | +import android.database.SQLException; |
25 | 26 | import android.database.sqlite.SQLiteDatabase; |
26 | 27 | import android.database.sqlite.SQLiteQueryBuilder; |
27 | 28 | import android.database.sqlite.SQLiteStatement; |
| 29 | +import android.text.TextUtils; |
28 | 30 | import android.util.Log; |
29 | 31 |
|
30 | 32 | import org.gnucash.android.model.*; |
@@ -86,16 +88,40 @@ public long addTransaction(Transaction transaction){ |
86 | 88 | contentValues.put(TransactionEntry.COLUMN_RECURRENCE_PERIOD, transaction.getRecurrencePeriod()); |
87 | 89 |
|
88 | 90 | Log.d(TAG, "Replacing transaction in db"); |
89 | | - long rowId = mDb.replace(TransactionEntry.TABLE_NAME, null, contentValues); |
| 91 | + long rowId = -1; |
| 92 | + mDb.beginTransaction(); |
| 93 | + try { |
| 94 | + rowId = mDb.replaceOrThrow(TransactionEntry.TABLE_NAME, null, contentValues); |
90 | 95 |
|
91 | | - if (rowId > 0){ |
92 | 96 | Log.d(TAG, "Adding splits for transaction"); |
| 97 | + ArrayList<String> splitUIDs = new ArrayList<String>(transaction.getSplits().size()); |
93 | 98 | for (Split split : transaction.getSplits()) { |
94 | | - mSplitsDbAdapter.addSplit(split); |
| 99 | + contentValues.clear(); |
| 100 | + contentValues.put(SplitEntry.COLUMN_UID, split.getUID()); |
| 101 | + contentValues.put(SplitEntry.COLUMN_AMOUNT, split.getAmount().absolute().toPlainString()); |
| 102 | + contentValues.put(SplitEntry.COLUMN_TYPE, split.getType().name()); |
| 103 | + contentValues.put(SplitEntry.COLUMN_MEMO, split.getMemo()); |
| 104 | + contentValues.put(SplitEntry.COLUMN_ACCOUNT_UID, split.getAccountUID()); |
| 105 | + contentValues.put(SplitEntry.COLUMN_TRANSACTION_UID, split.getTransactionUID()); |
| 106 | + splitUIDs.add(split.getUID()); |
| 107 | + |
| 108 | + Log.d(TAG, "Replace transaction split in db"); |
| 109 | + mDb.replaceOrThrow(SplitEntry.TABLE_NAME, null, contentValues); |
95 | 110 | } |
96 | 111 | Log.d(TAG, transaction.getSplits().size() + " splits added"); |
| 112 | + |
| 113 | + long deleted = mDb.delete(SplitEntry.TABLE_NAME, |
| 114 | + SplitEntry.COLUMN_TRANSACTION_UID + " = ? AND " |
| 115 | + + SplitEntry.COLUMN_UID + " NOT IN ('" + TextUtils.join("' , '", splitUIDs) + "')", |
| 116 | + new String[]{transaction.getUID()}); |
| 117 | + Log.d(TAG, deleted + " splits deleted"); |
| 118 | + mDb.setTransactionSuccessful(); |
| 119 | + } catch (SQLException sqle) { |
| 120 | + sqle.printStackTrace(); |
| 121 | + } finally { |
| 122 | + mDb.endTransaction(); |
97 | 123 | } |
98 | | - return rowId; |
| 124 | + return rowId; |
99 | 125 | } |
100 | 126 |
|
101 | 127 | /** |
|
0 commit comments