Skip to content

Commit

Permalink
retry import default accounts when error occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Dec 26, 2024
1 parent 27c9acb commit da41830
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
15 changes: 10 additions & 5 deletions app/src/main/java/org/gnucash/android/app/GnuCashApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.gnucash.android.app;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class GnuCashApplication extends Application {
*/
public static final String FILE_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider";

@SuppressLint("StaticFieldLeak")
private static Context context;
@Nullable
private static AccountsDbAdapter mAccountsDbAdapter;
Expand Down Expand Up @@ -134,21 +136,24 @@ public void onTerminate() {
*
* @param context the context.
*/
public static void initializeDatabaseAdapters(Context context) {
public static void initializeDatabaseAdapters(@NonNull Context context) {
BookDbHelper bookDbHelper = new BookDbHelper(context);
mBooksDbAdapter = new BooksDbAdapter(bookDbHelper.getWritableDatabase());
SQLiteDatabase db = bookDbHelper.getWritableDatabase();
mBooksDbAdapter = new BooksDbAdapter(db);

if (mDbHelper != null) { //close if open
mDbHelper.getReadableDatabase().close();
}

String bookUID = null;
String bookUID;
try {
bookUID = mBooksDbAdapter.getActiveBookUID();
} catch (BooksDbAdapter.NoActiveBookFoundException e) {
mBooksDbAdapter.fixBooksDatabase();
bookUID = mBooksDbAdapter.fixBooksDatabase();
}
if (TextUtils.isEmpty(bookUID)) {
bookUID = bookDbHelper.insertBlankBook(db).getUID();
}
if (TextUtils.isEmpty(bookUID)) return;
mDbHelper = new DatabaseHelper(context, bookUID);
SQLiteDatabase mainDb;
try {
Expand Down
31 changes: 27 additions & 4 deletions app/src/main/java/org/gnucash/android/db/BookDbHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.DatabaseSchema.BookEntry;
import org.gnucash.android.db.adapter.AccountsDbAdapter;
Expand Down Expand Up @@ -55,19 +57,28 @@ public class BookDbHelper extends SQLiteOpenHelper {
+ BookEntry.COLUMN_MODIFIED_AT + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "
+ ");" + DatabaseHelper.createUpdatedAtTrigger(BookEntry.TABLE_NAME);

public BookDbHelper(Context context) {
@NonNull
private final Context context;

public BookDbHelper(@NonNull Context context) {
super(context, DatabaseSchema.BOOK_DATABASE_NAME, null, DatabaseSchema.BOOK_DATABASE_VERSION);
this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(BOOKS_TABLE_CREATE);

insertBlankBook(db);
}

@NonNull
public Book insertBlankBook(@NonNull SQLiteDatabase db) {
Book book = new Book();
DatabaseHelper helper = new DatabaseHelper(GnuCashApplication.getAppContext(), book.getUID());
DatabaseHelper helper = new DatabaseHelper(context, book.getUID());
SQLiteDatabase mainDb = helper.getWritableDatabase(); //actually create the db
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mainDb,
new TransactionsDbAdapter(mainDb, new SplitsDbAdapter(mainDb)));
new TransactionsDbAdapter(mainDb, new SplitsDbAdapter(mainDb)));

String rootAccountUID = accountsDbAdapter.getOrCreateGnuCashRootAccountUID();
try {
Expand All @@ -78,6 +89,7 @@ public void onCreate(SQLiteDatabase db) {
book.setRootAccountUID(rootAccountUID);
book.setActive(true);
insertBook(db, book);
return book;
}

/**
Expand All @@ -87,7 +99,18 @@ public void onCreate(SQLiteDatabase db) {
* @return SQLiteDatabase of the book
*/
public static SQLiteDatabase getDatabase(String bookUID) {
DatabaseHelper dbHelper = new DatabaseHelper(GnuCashApplication.getAppContext(), bookUID);
return getDatabase(GnuCashApplication.getAppContext(), bookUID);
}

/**
* Returns the database for the book
*
* @param context The application context.
* @param bookUID GUID of the book
* @return SQLiteDatabase of the book
*/
public static SQLiteDatabase getDatabase(@NonNull Context context, String bookUID) {
DatabaseHelper dbHelper = new DatabaseHelper(context, bookUID);
return dbHelper.getWritableDatabase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import org.gnucash.android.R;
Expand Down Expand Up @@ -203,14 +204,19 @@ public NoActiveBookFoundException(String message) {

/**
* Tries to fix the books database.
* @return the active book UID.
*/
public void fixBooksDatabase() {
@Nullable
public String fixBooksDatabase() {
Timber.v("Looking for books to set as active...");
if (getRecordsCount() <= 0) {
Timber.w("No books found in the database. Recovering books records...");
recoverBookRecords();
if (getRecordsCount() <= 0) {
return null;
}
}
setFirstBookAsActive();
return setFirstBookAsActive();
}

/**
Expand Down Expand Up @@ -244,17 +250,21 @@ private String getRootAccountUID(String dbName) {

/**
* Sets the first book in the database as active.
*
* @return the book UID.
*/
private void setFirstBookAsActive() {
@Nullable
private String setFirstBookAsActive() {
List<Book> books = getAllRecords();
if (books.isEmpty()) {
Timber.w("No books.");
return;
return null;
}
Book firstBook = books.get(0);
firstBook.setActive(true);
addRecord(firstBook);
Timber.i("Book " + firstBook.getUID() + " set as active.");
return firstBook.getUID();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import org.gnucash.android.ui.util.TaskDelegate;
import org.gnucash.android.ui.wizard.FirstRunWizardActivity;
import org.gnucash.android.util.BackupManager;
import org.gnucash.android.util.ContentExtKt;

import timber.log.Timber;

Expand Down Expand Up @@ -352,25 +351,26 @@ public void setCurrentTab() {
* <p>Also handles displaying the What's New dialog</p>
*/
private void init() {
PreferenceManager.setDefaultValues(this, GnuCashApplication.getActiveBookUID(),
final Context context = this;
PreferenceManager.setDefaultValues(context, GnuCashApplication.getActiveBookUID(),
Context.MODE_PRIVATE, R.xml.fragment_transaction_preferences, true);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean firstRun = prefs.getBoolean(getString(R.string.key_first_run), true);

if (firstRun) {
//default to using double entry and save the preference explicitly
prefs.edit().putBoolean(getString(R.string.key_use_double_entry), true).apply();

startActivity(new Intent(GnuCashApplication.getAppContext(), FirstRunWizardActivity.class));
startActivity(new Intent(context, FirstRunWizardActivity.class));
finish();
return;
}

if (hasNewFeatures()) {
showWhatsNewDialog(this);
showWhatsNewDialog(context);
}
ScheduledActionService.schedulePeriodic(this);
ScheduledActionService.schedulePeriodic(context);
}

@Override
Expand Down

0 comments on commit da41830

Please sign in to comment.