|
23 | 23 | import android.database.sqlite.SQLiteStatement; |
24 | 24 | import android.net.Uri; |
25 | 25 | import android.support.annotation.NonNull; |
| 26 | +import android.util.Log; |
26 | 27 |
|
27 | 28 | import org.gnucash.android.R; |
28 | 29 | import org.gnucash.android.app.GnuCashApplication; |
| 30 | +import org.gnucash.android.db.DatabaseHelper; |
29 | 31 | import org.gnucash.android.db.DatabaseSchema.BookEntry; |
30 | 32 | import org.gnucash.android.model.Book; |
31 | 33 | import org.gnucash.android.ui.settings.PreferenceActivity; |
@@ -190,11 +192,70 @@ public NoActiveBookFoundException(String message) { |
190 | 192 | } |
191 | 193 | } |
192 | 194 |
|
193 | | - /** Sets the first book in the database as active. */ |
| 195 | + /** Tries to fix the books database. */ |
194 | 196 | public void fixBooksDatabase() { |
| 197 | + Log.w(LOG_TAG, "Looking for books to set as active..."); |
| 198 | + if (getRecordsCount() <= 0) { |
| 199 | + Log.w(LOG_TAG, "No books found in the database. Recovering books records..."); |
| 200 | + recoverBookRecords(); |
| 201 | + } |
| 202 | + setFirstBookAsActive(); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Restores the records in the book database. |
| 207 | + * |
| 208 | + * Does so by looking for database files from books. |
| 209 | + */ |
| 210 | + private void recoverBookRecords() { |
| 211 | + for (String dbName : getBookDatabases()) { |
| 212 | + Book book = new Book(getRootAccountUID(dbName)); |
| 213 | + book.setUID(dbName); |
| 214 | + book.setDisplayName(generateDefaultBookName()); |
| 215 | + addRecord(book); |
| 216 | + Log.w(LOG_TAG, "Recovered book record: " + book.getUID()); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Returns the root account UID from the database with name dbName. |
| 222 | + */ |
| 223 | + private String getRootAccountUID(String dbName) { |
| 224 | + Context context = GnuCashApplication.getAppContext(); |
| 225 | + DatabaseHelper databaseHelper = new DatabaseHelper(context, dbName); |
| 226 | + SQLiteDatabase db = databaseHelper.getReadableDatabase(); |
| 227 | + AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(db, |
| 228 | + new TransactionsDbAdapter(db, new SplitsDbAdapter(db))); |
| 229 | + String uid = accountsDbAdapter.getOrCreateGnuCashRootAccountUID(); |
| 230 | + db.close(); |
| 231 | + return uid; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Sets the first book in the database as active. |
| 236 | + */ |
| 237 | + private void setFirstBookAsActive() { |
195 | 238 | Book firstBook = getAllRecords().get(0); |
196 | 239 | firstBook.setActive(true); |
197 | | - BooksDbAdapter.getInstance().addRecord(firstBook); |
| 240 | + addRecord(firstBook); |
| 241 | + Log.w(LOG_TAG, "Book " + firstBook.getUID() + " set as active."); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Returns a list of database names corresponding to book databases. |
| 246 | + */ |
| 247 | + private List<String> getBookDatabases() { |
| 248 | + List<String> bookDatabases = new ArrayList<>(); |
| 249 | + for (String database : GnuCashApplication.getAppContext().databaseList()) { |
| 250 | + if (isBookDatabase(database)) { |
| 251 | + bookDatabases.add(database); |
| 252 | + } |
| 253 | + } |
| 254 | + return bookDatabases; |
| 255 | + } |
| 256 | + |
| 257 | + private boolean isBookDatabase(String databaseName) { |
| 258 | + return databaseName.matches("[a-z0-9]{32}"); // UID regex |
198 | 259 | } |
199 | 260 |
|
200 | 261 | public @NonNull List<String> getAllBookUIDs(){ |
|
0 commit comments