Skip to content

Commit

Permalink
Migration fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarnagiris committed Oct 18, 2014
1 parent 038e384 commit 3a6ed5d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static <T> T checkNotNull(T object, String message) throws NullPointerExc
}

public static <T> T checkNull(T object, String message) throws IllegalStateException {
if (object == null) {
if (object != null) {
throw new IllegalStateException(message);
}
return object;
Expand Down
2 changes: 1 addition & 1 deletion financius/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
defaultConfig {
applicationId 'com.code44.finance'
targetSdkVersion 21
minSdkVersion 14
minSdkVersion 21
versionCode 46
versionName '0.10.0'
buildConfigField "boolean", "USE_LOCAL_SERVER", "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.code44.finance.data.model.SyncState;
import com.code44.finance.data.model.Tag;
import com.code44.finance.data.model.Transaction;
import com.code44.finance.data.providers.TransactionsProvider;

import java.util.UUID;

Expand Down Expand Up @@ -50,16 +51,12 @@ public static void upgradeV19(SQLiteDatabase db) {
db.execSQL("drop table " + tempCategoriesTable);
db.execSQL("drop table " + tempTransactionsTable);

// TODO Recalculate accounts balance.
TransactionsProvider.updateAllAccountsBalances(db);

db.setTransactionSuccessful();
} finally {
db.endTransaction();
}

// Currencies
String tableName = "currencies";
db.execSQL("alter table " + tableName + " rename to ");
}

private static void v19EnsureIds(SQLiteDatabase db, String tableName) {
Expand Down Expand Up @@ -175,7 +172,7 @@ private static String v19MigrateCategories(SQLiteDatabase db) {
} else {
tag.setId(cursor.getString(0));
tag.setTitle(cursor.getString(1));
db.insert(Tables.Tags.TABLE_NAME, null, category.asValues());
db.insert(Tables.Tags.TABLE_NAME, null, tag.asValues());
}
} while (cursor.moveToNext());
}
Expand Down Expand Up @@ -236,7 +233,7 @@ private static String v19MigrateTransactions(SQLiteDatabase db, String tempAccou
if (level == 1) {
category.setId(cursor.getString(10));
} else {
category.setTitle(cursor.getString(13));
category.setId(cursor.getString(13));
values.clear();
values.put(Tables.TransactionTags.TRANSACTION_ID.getName(), transaction.getId());
values.put(Tables.TransactionTags.TAG_ID.getName(), cursor.getString(10));
Expand Down Expand Up @@ -266,7 +263,9 @@ private static String v19MigrateTransactions(SQLiteDatabase db, String tempAccou
break;
}

db.insert(Tables.Transactions.TABLE_NAME, null, transaction.asValues());
final ContentValues transactionValues = transaction.asValues();
transactionValues.remove(Tables.Tags.ID.getName());
db.insert(Tables.Transactions.TABLE_NAME, null, transactionValues);
} while (cursor.moveToNext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

Expand Down Expand Up @@ -36,6 +37,48 @@ public static Uri uriTransaction(String transactionServerId) {
return uriModel(TransactionsProvider.class, Tables.Transactions.TABLE_NAME, transactionServerId);
}

public static void updateAccountBalance(SQLiteDatabase database, String accountId) {
final Cursor cursor = Query.create()
.projection("sum( case" +
" when " + Tables.Transactions.ACCOUNT_FROM_ID + "=? then -" + Tables.Transactions.AMOUNT + "" +
" when " + Tables.Transactions.TYPE + "=? then " + Tables.Transactions.AMOUNT + "*" + Tables.Transactions.EXCHANGE_RATE +
" else " + Tables.Transactions.AMOUNT + " end)")
.args(accountId, TransactionType.Transfer.asString())
.selection(Tables.Transactions.MODEL_STATE + "=?", ModelState.Normal.asString())
.selection(" and " + Tables.Transactions.STATE + "=?", TransactionState.Confirmed.asString())
.selection(" and (" + Tables.Transactions.ACCOUNT_FROM_ID + "=? or " + Tables.Transactions.ACCOUNT_TO_ID + "=?)", accountId, accountId)
.from(database, Tables.Transactions.TABLE_NAME)
.execute();

long balance = 0;
if (cursor.moveToFirst()) {
balance = cursor.getLong(0);
}
IOUtils.closeQuietly(cursor);

final ContentValues values = new ContentValues();
values.put(Tables.Accounts.BALANCE.getName(), balance);
DataStore.update()
.values(values)
.withSelection(Tables.Accounts.ID + "=?", accountId)
.into(database, Tables.Accounts.TABLE_NAME);
}

public static void updateAllAccountsBalances(SQLiteDatabase database) {
final Cursor cursor = Query.create()
.projection(Tables.Accounts.ID.getName())
.selection(Tables.Accounts.MODEL_STATE + "=?", String.valueOf(ModelState.Normal.asInt()))
.from(database, Tables.Accounts.TABLE_NAME)
.execute();
if (cursor.moveToFirst()) {
final int iId = cursor.getColumnIndex(Tables.Accounts.ID.getName());
do {
updateAccountBalance(database, cursor.getString(iId));
} while (cursor.moveToNext());
}
IOUtils.closeQuietly(cursor);
}

@Override protected String getModelTable() {
return Tables.Transactions.TABLE_NAME;
}
Expand Down Expand Up @@ -111,11 +154,11 @@ public static Uri uriTransaction(String transactionServerId) {
final boolean isAccountToInDb = !StringUtils.isEmpty(accountToId);

if (isAccountFromInDb) {
updateAccountBalance(accountFromId);
updateAccountBalance(getDatabase(), accountFromId);
}

if (isAccountToInDb) {
updateAccountBalance(accountToId);
updateAccountBalance(getDatabase(), accountToId);
}
}

Expand All @@ -125,7 +168,7 @@ public static Uri uriTransaction(String transactionServerId) {

@Override protected void onAfterDeleteItems(Uri uri, String selection, String[] selectionArgs, ModelState modelState, Map<String, Object> extras) {
super.onAfterDeleteItems(uri, selection, selectionArgs, modelState, extras);
updateAllAccountsBalances();
updateAllAccountsBalances(getDatabase());
}

@Override protected void onBeforeBulkInsertIteration(Uri uri, ContentValues values, Map<String, Object> extras) {
Expand All @@ -135,55 +178,13 @@ public static Uri uriTransaction(String transactionServerId) {

@Override protected void onAfterBulkInsertItems(Uri uri, ContentValues[] valuesArray, Map<String, Object> extras) {
super.onAfterBulkInsertItems(uri, valuesArray, extras);
updateAllAccountsBalances();
updateAllAccountsBalances(getDatabase());
}

@Override protected Uri[] getOtherUrisToNotify() {
return new Uri[]{AccountsProvider.uriAccounts()};
}

private void updateAccountBalance(String accountId) {
final Cursor cursor = Query.create()
.projection("sum( case" +
" when " + Tables.Transactions.ACCOUNT_FROM_ID + "=? then -" + Tables.Transactions.AMOUNT + "" +
" when " + Tables.Transactions.TYPE + "=? then " + Tables.Transactions.AMOUNT + "*" + Tables.Transactions.EXCHANGE_RATE +
" else " + Tables.Transactions.AMOUNT + " end)")
.args(accountId, TransactionType.Transfer.asString())
.selection(Tables.Transactions.MODEL_STATE + "=?", ModelState.Normal.asString())
.selection(" and " + Tables.Transactions.STATE + "=?", TransactionState.Confirmed.asString())
.selection(" and (" + Tables.Transactions.ACCOUNT_FROM_ID + "=? or " + Tables.Transactions.ACCOUNT_TO_ID + "=?)", accountId, accountId)
.from(getDatabase(), Tables.Transactions.TABLE_NAME)
.execute();

long balance = 0;
if (cursor.moveToFirst()) {
balance = cursor.getLong(0);
}
IOUtils.closeQuietly(cursor);

final ContentValues values = new ContentValues();
values.put(Tables.Accounts.BALANCE.getName(), balance);
DataStore.update()
.values(values)
.withSelection(Tables.Accounts.ID + "=?", accountId)
.into(getDatabase(), Tables.Accounts.TABLE_NAME);
}

private void updateAllAccountsBalances() {
final Cursor cursor = Query.create()
.projection(Tables.Accounts.ID.getName())
.selection(Tables.Accounts.MODEL_STATE + "=?", String.valueOf(ModelState.Normal.asInt()))
.from(getDatabase(), Tables.Accounts.TABLE_NAME)
.execute();
if (cursor.moveToFirst()) {
final int iId = cursor.getColumnIndex(Tables.Accounts.ID.getName());
do {
updateAccountBalance(cursor.getString(iId));
} while (cursor.moveToNext());
}
IOUtils.closeQuietly(cursor);
}

private void updateTransactionTags(ContentValues values) {
// Remove current tags
final String transactionId = values.getAsString(Tables.Transactions.ID.getName());
Expand Down
15 changes: 15 additions & 0 deletions financius/src/release/java/com/code44/finance/Modules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.code44.finance;

import com.code44.finance.modules.AppModule;

final class Modules {
private Modules() {
// No instances.
}

static Object[] list(App app) {
return new Object[]{
new AppModule(app)
};
}
}

0 comments on commit 3a6ed5d

Please sign in to comment.